Skip to content

Commit a8b3928

Browse files
authored
Respect process-level umask when creating temporary staging files. (#3)
tempfile automatically creates files/directories with 0o*00 permissions, which usually means that the gobbler's service account can't read them. chmod'ing can help but will override FACLs which is not desirable, e.g., the gobbler service account can't delete non-empty directories. The best solution is to respect the process-level umask when creating tempfiles. So we use the tempfile.mk*temp() functions to suggest a name, and then we actually create the file/dir via regular Python functions.
1 parent 4f7c3b4 commit a8b3928

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/pygobbler/_utils.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ def dump_request(staging: str, url: str, action: str, payload: Optional[Dict]) -
2222
else:
2323
as_str = json.dumps(payload, indent=4)
2424

25+
# Doing this little shuffle to get the right permissions. tempfile loves to
26+
# create 0o600 directories that the gobbler service account can't actually
27+
# read, so we just delete it and create it again under the more permissive
28+
# umask. Unfortunately we can't use chmod as this screws up FACLs.
2529
prefix = "request-" + action + "-"
2630
fd, holding_name = tempfile.mkstemp(dir=staging, prefix=prefix)
27-
with os.fdopen(fd, "w") as handle:
31+
os.close(fd)
32+
os.remove(holding_name)
33+
with open(holding_name, "w") as handle:
2834
handle.write(as_str)
2935

3036
res = requests.post(url + "/new/" + os.path.basename(holding_name))
+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import os
2+
import tempfile
3+
4+
15
def allocate_upload_directory(staging: str) -> str:
26
"""
3-
Allocate a subdirectory in the staging directory to prepare files for
4-
upload via :py:func:`~.upload_directory`.
7+
Allocate a subdirectory in the staging directory to prepare files for upload via :py:func:`~.upload_directory`.
58
69
Args:
710
staging:
@@ -10,5 +13,11 @@ def allocate_upload_directory(staging: str) -> str:
1013
Returns:
1114
Path to a new subdirectory for staging uploads.
1215
"""
13-
import tempfile
14-
return tempfile.mkdtemp(dir=staging)
16+
trial = tempfile.mkdtemp(dir=staging)
17+
# Doing this little shuffle to get the right permissions. tempfile loves to
18+
# create 0o700 directories that the gobbler service account can't actually
19+
# read, so we just delete it and create it again under the more permissive
20+
# umask. Unfortunately we can't use chmod as this screws up FACLs.
21+
os.rmdir(trial)
22+
os.mkdir(trial)
23+
return trial

0 commit comments

Comments
 (0)