Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Taught util.misc.mkdir() to set umask to allow group-w for makedirs c… #4000

Merged
merged 1 commit into from Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/pulp/common/bundle.py
@@ -1,6 +1,7 @@
import os
import re
from logging import getLogger
from pulp.plugins.util import misc

from M2Crypto import X509

Expand Down Expand Up @@ -199,8 +200,7 @@ def mkdir(self):
Ensure I{root} directory exists.
"""
path = os.path.dirname(self.path)
if not os.path.exists(path):
os.makedirs(path)
misc.mkdir(path)

def cn(self):
"""
Expand Down
5 changes: 3 additions & 2 deletions common/pulp/common/lock.py
Expand Up @@ -7,6 +7,8 @@

from threading import RLock

from pulp.plugins.util import misc


class LockFailed(Exception):
pass
Expand Down Expand Up @@ -92,8 +94,7 @@ def setpid(self, pid=os.getpid()):

def __mkdir(self, path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
misc.mkdir(dir)


class Lock:
Expand Down
12 changes: 4 additions & 8 deletions server/pulp/plugins/file/distributor.py
Expand Up @@ -9,6 +9,7 @@
from pulp.common.plugins.distributor_constants import MANIFEST_FILENAME
from pulp.common.plugins.progress import ProgressReport
from pulp.plugins.distributor import Distributor
from pulp.plugins.util import misc
from pulp.server.managers.repo import _common as common_utils
from pulp.server.util import copytree
from pulp.server.db.model.criteria import UnitAssociationCriteria
Expand Down Expand Up @@ -86,7 +87,7 @@ def publish_repo(self, repo, publish_conduit, config):
# Set up an empty build_dir
working_dir = common_utils.get_working_directory()
build_dir = os.path.join(working_dir, BUILD_DIRNAME)
os.makedirs(build_dir)
misc.mkdir(build_dir)

self.initialize_metadata(build_dir)

Expand Down Expand Up @@ -148,7 +149,7 @@ def publish_repo_fast_forward(self, repo, publish_conduit, config):
build_dir = os.path.join(working_dir, BUILD_DIRNAME)

self._rmtree_if_exists(build_dir)
os.makedirs(build_dir)
misc.mkdir(build_dir)

self.initialize_metadata(build_dir)
unit_checksum_set = set()
Expand Down Expand Up @@ -361,12 +362,7 @@ def _symlink_unit(self, build_dir, unit, target_paths):
# so now we should recreate it.
dir_path = os.path.dirname(symlink_filename)
# make sure any required subdirectory exists
if not os.path.exists(dir_path):
try:
os.makedirs(dir_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
misc.mkdir(dir_path)

os.symlink(unit.storage_path, symlink_filename)

Expand Down
3 changes: 2 additions & 1 deletion server/pulp/plugins/file/model_distributor.py
Expand Up @@ -9,6 +9,7 @@
from pulp.common.plugins.distributor_constants import MANIFEST_FILENAME
from pulp.plugins.distributor import Distributor
from pulp.plugins.file.distributor import FilePublishProgressReport
from pulp.plugins.util import misc
from pulp.server.controllers import repository as repo_controller
from pulp.server.managers.repo import _common as common_utils

Expand Down Expand Up @@ -79,7 +80,7 @@ def publish_repo(self, repo, publish_conduit, config):
# Set up an empty build_dir
working_dir = common_utils.get_working_directory()
build_dir = os.path.join(working_dir, BUILD_DIRNAME)
os.makedirs(build_dir)
misc.mkdir(build_dir)

self.initialize_metadata(build_dir)

Expand Down
4 changes: 2 additions & 2 deletions server/pulp/plugins/migration/standard_storage_path.py
Expand Up @@ -28,7 +28,7 @@ def migrate(*args, **kwargs):
from hashlib import sha256
from itertools import chain

from pulp.plugins.util.misc import mkdir
from pulp.plugins.util import misc
from pulp.server.config import config


Expand Down Expand Up @@ -307,7 +307,7 @@ def migrate(self, unit_id, path, new_path):
:type new_path: str
"""
if os.path.exists(path):
mkdir(os.path.dirname(new_path))
misc.mkdir(os.path.dirname(new_path))
shutil.move(path, new_path)
self.collection.update_one(
filter={
Expand Down
4 changes: 2 additions & 2 deletions server/pulp/plugins/rsync/publish.py
Expand Up @@ -9,6 +9,7 @@
import mongoengine

from pulp.common import dateutils
from pulp.plugins.util import misc
from pulp.plugins.util.publish_step import PublishStep
from pulp.server.config import config as pulp_config
from pulp.server.exceptions import PulpCodedException
Expand Down Expand Up @@ -236,8 +237,7 @@ def rsync(self):
"""
if not self.file_list and not self.delete:
return (True, _("Nothing to sync"))
if not os.path.exists(self.src_directory):
os.makedirs(self.src_directory)
misc.mkdir(self.src_directory)

output = ""
list_of_files = os.path.join(self.get_working_dir(), str(uuid.uuid4()))
Expand Down
4 changes: 2 additions & 2 deletions server/pulp/plugins/util/metadata_writer.py
Expand Up @@ -12,7 +12,7 @@
from pulp.common import error_codes
from pulp.server.exceptions import PulpCodedValidationException, PulpCodedException
from pulp.server.util import CHECKSUM_FUNCTIONS

from pulp.plugins.util import misc
_LOG = logging.getLogger(__name__)
BUFFER_SIZE = 1024

Expand Down Expand Up @@ -124,7 +124,7 @@ def _open_metadata_file_handle(self):
parent_dir = os.path.dirname(self.metadata_file_path)

if not os.path.exists(parent_dir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to check for presence of the path here, remove the if condition

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The os.access() check in the elif is only needed if we're not creating the path in the mkdir() call. I tried a couple of iterations here, and decided that while this one is a little redundant around the mkdir(), it seemed more clear in terms of understanding the code. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh right, i did not notice the elif statement, i am cool with leaving if

os.makedirs(parent_dir, mode=0770)
misc.mkdir(parent_dir, mode=0770)

elif not os.access(parent_dir, os.R_OK | os.W_OK | os.X_OK):
msg = _('Insufficient permissions to write metadata file in directory [%(d)s]')
Expand Down
7 changes: 5 additions & 2 deletions server/pulp/plugins/util/misc.py
Expand Up @@ -40,16 +40,20 @@ def mkdir(*args, **kwargs):
"""
Create the specified directory.
Tolerant of race conditions.
Sets umask to 002.

:param args: path[, mode] that goes to os.makedirs
:param kwargs: path
:return:
"""
mask = os.umask(0002)
try:
os.makedirs(*args, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The umask is apparently never restored if this raises an exception. Should the original os.umask be above the "try", and the umask reset in a new "finally" block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...I am an idiot. I had exactly that at some point, and apparently lost it somewhere along the way. Thank you for the great catch, that would have been very embarrassing to allow out into the wild!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohanpm @ipanova fixed mkdir() - more eyes please, when you have a moment free

except OSError, e:
if e.errno != errno.EEXIST:
raise
finally:
os.umask(mask)


def get_parent_directory(path):
Expand Down Expand Up @@ -96,9 +100,8 @@ def create_symlink(source_path, link_path, directory_permissions=0770):
link_path = link_path[:-1]

link_parent_dir = os.path.dirname(link_path)

if not os.path.exists(link_parent_dir):
os.makedirs(link_parent_dir, mode=directory_permissions)
mkdir(link_parent_dir, mode=directory_permissions)
elif os.path.lexists(link_path):
if os.path.islink(link_path):
link_target = os.readlink(link_path)
Expand Down
3 changes: 1 addition & 2 deletions server/pulp/plugins/util/publish_step.py
Expand Up @@ -1360,8 +1360,7 @@ def make_link_unit(self, unit, filename, working_dir, remote_repo_path, remote_r

"""
extra_src_path = ['.relative'] + published_unit_path
if not os.path.exists(os.path.join(working_dir, *extra_src_path)):
os.makedirs(os.path.join(working_dir, *extra_src_path))
misc.mkdir(os.path.join(working_dir, *extra_src_path))

origin_path = self.get_origin_rel_path(unit)

Expand Down
8 changes: 3 additions & 5 deletions server/pulp/server/async/tasks.py
Expand Up @@ -20,6 +20,8 @@

from pulp.common.constants import RESOURCE_MANAGER_WORKER_NAME, SCHEDULER_WORKER_NAME
from pulp.common import constants, dateutils, tags
from pulp.plugins.util import misc

from pulp.server.async.celery_instance import celery, RESOURCE_MANAGER_QUEUE, \
DEDICATED_QUEUE_EXCHANGE
from pulp.server.exceptions import PulpException, MissingResource, \
Expand Down Expand Up @@ -780,11 +782,7 @@ def _handle_cProfile(self, task_id):
if config.getboolean('profiling', 'enabled') is True:
self.pr.disable()
profile_directory = config.get('profiling', 'directory')
try:
os.makedirs(profile_directory, 0755)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
misc.mkdir(profile_directory, mode=0755)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0775

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is important and i understand correctly how umask works - if having mode 0755 the result will be:
usr grp others
-rwx r-x r-x is represented in octal as 0755
-000 000 010 umask of 002 removes any permission where there is a 1
-rwx r-x r-x is the result achieved requesting 0755 with umask of 002...and we are screwed :)

if having mode 0775 the result will be:
usr grp others
-rwx rwx r-x is represented in octal as 0775
-000 000 010 umask of 002 removes any permission where there is a 1
-rwx rwx r-x is the result achieved requesting 0775 with umask of 002

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few things like certificate-directories and this profiling-directory alone - they're not 'content', and are more sensitive to permissions. Unless we're migrating profiling-information, this set of perms shouldn't affect migration, and I felt it better to leave the permissions as they had been specifically set.

(Your understanding of umask is correct - it turns off permission-bits where the umask has a 1. So 0755, with the mkdirs() umask of 0002, leaves 0755)

self.pr.dump_stats("%s/%s" % (profile_directory, task_id))


Expand Down
23 changes: 4 additions & 19 deletions server/pulp/server/content/storage.py
Expand Up @@ -6,22 +6,7 @@
from hashlib import sha256

from pulp.server.config import config


def mkdir(path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 let's say no to the code duplication!

"""
Create a directory at the specified path.
Directory (and intermediate) directories are only created if they
don't already exist.

:param path: The absolute path to the leaf directory to be created.
:type path: str
"""
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
from pulp.plugins.util import misc


class ContentStorage(object):
Expand Down Expand Up @@ -124,7 +109,7 @@ def put(self, unit, path, location=None):
destination = unit.storage_path
if location:
destination = os.path.join(destination, location.lstrip('/'))
mkdir(os.path.dirname(destination))
misc.mkdir(os.path.dirname(destination))
fd, temp_destination = tempfile.mkstemp(dir=os.path.dirname(destination))

# to avoid a file descriptor leak, close the one opened by tempfile.mkstemp which we are not
Expand Down Expand Up @@ -212,8 +197,8 @@ def open(self):
Open the shared storage.
The shared storage location is created as needed.
"""
mkdir(self.content_dir)
mkdir(self.links_dir)
misc.mkdir(self.content_dir)
misc.mkdir(self.links_dir)

@property
def shared_dir(self):
Expand Down
3 changes: 1 addition & 2 deletions server/pulp/server/controllers/repository.py
Expand Up @@ -27,15 +27,14 @@
from pulp.plugins.loader import api as plugin_api
from pulp.plugins.loader import exceptions as plugin_exceptions
from pulp.plugins.model import SyncReport
from pulp.plugins.util.misc import paginate
from pulp.plugins.util.misc import paginate, mkdir
from pulp.plugins.util.verification import VerificationException, verify_checksum
from pulp.server import exceptions as pulp_exceptions
from pulp.server.async.tasks import (PulpTask, register_sigterm_handler, Task, TaskResult,
get_current_task_id)
from pulp.server.config import config as pulp_conf
from pulp.server.constants import PULP_STREAM_REQUEST_HEADER
from pulp.server.content.sources.constants import MAX_CONCURRENT, HEADERS, SSL_VALIDATION
from pulp.server.content.storage import mkdir
from pulp.server.controllers import consumer as consumer_controller
from pulp.server.controllers import distributor as dist_controller
from pulp.server.controllers import importer as importer_controller
Expand Down
17 changes: 3 additions & 14 deletions server/pulp/server/managers/content/query.py
Expand Up @@ -4,7 +4,7 @@
import os

from pulp.plugins.types import database as content_types_db
from pulp.plugins.util.misc import paginate
from pulp.plugins.util.misc import mkdir, paginate
from pulp.server import config as pulp_config
from pulp.server.controllers import units as units_controller
from pulp.server.exceptions import InvalidValue, MissingResource
Expand Down Expand Up @@ -246,13 +246,7 @@ def get_root_content_dir(self, content_type):
# I'm partitioning the content on the file system based on content type
storage_dir = pulp_config.config.get('server', 'storage_dir')
root = os.path.join(storage_dir, 'content', content_type)
try:
os.makedirs(root)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise
mkdir(root)
return root

def request_content_unit_file_path(self, content_type, relative_path):
Expand All @@ -273,12 +267,7 @@ def request_content_unit_file_path(self, content_type, relative_path):

unit_path = os.path.join(self.get_root_content_dir(content_type), relative_path)
unit_dir = os.path.dirname(unit_path)
try:
os.makedirs(unit_dir)
except OSError, e:
if e.errno != errno.EEXIST:
raise

mkdir(unit_dir)
return unit_path


Expand Down
5 changes: 3 additions & 2 deletions server/pulp/server/managers/content/upload.py
Expand Up @@ -11,6 +11,8 @@
from pulp.plugins.conduits.upload import UploadConduit
from pulp.plugins.config import PluginCallConfiguration
from pulp.plugins.loader import api as plugin_api, exceptions as plugin_exceptions
from pulp.plugins.util import misc

from pulp.server import config as pulp_config
from pulp.server.async.tasks import Task
from pulp.server.db import model
Expand Down Expand Up @@ -270,8 +272,7 @@ def _upload_storage_dir():
storage_dir = pulp_config.config.get('server', 'storage_dir')
upload_storage_dir = os.path.join(storage_dir, 'uploads')

if not os.path.exists(upload_storage_dir):
os.makedirs(upload_storage_dir)
misc.mkdir(upload_storage_dir)

return upload_storage_dir

Expand Down
4 changes: 2 additions & 2 deletions server/pulp/server/util.py
Expand Up @@ -12,6 +12,7 @@
from pulp.common import error_codes

from pulp.server.exceptions import PulpCodedException, PulpExecutionException
from pulp.plugins.util import misc


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -254,8 +255,7 @@ def copytree(src, dst, symlinks=False, ignore=None):
else:
ignored_names = set()

if not os.path.exists(dst):
os.makedirs(dst)
misc.mkdir(dst)
errors = []
for name in names:
if name in ignored_names:
Expand Down
Expand Up @@ -229,7 +229,7 @@ def test_new_unit(self, unit):
self.assertEqual(created, unit.return_value)

@patch(MODULE + '.shutil')
@patch(MODULE + '.mkdir')
@patch('pulp.plugins.util.misc.mkdir')
@patch('os.path.exists')
def test_migrate(self, path_exists, mkdir, shutil):
unit_id = '123'
Expand Down