Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More aggressively validate ambiguous file paths #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jtratner
Copy link
Contributor

@jtratner jtratner commented Jan 20, 2018

Closes #52

Generally, validate ambiguous paths more robustly and almost always error when they end in trailing slash.

API Additions

  • Add stor.test.TestCase that allows mocking out of both swift AND s3
    functionality in a test case.

Bug Fixes

  • open() will raise with an OBS path that ends in trailing slash, rather
    than generating an invalid/difficult OBS object.

Deprecations

  • Add warning about calling open() or copy() with OBS or filesystem
    paths that are ambiguous or end in a trailing slash. In the next version of
    stor, this will be an error, to prevent differences between local and OBS
    code. Set stor:raise_on_ambiguous_filesystem_path to get this behavior
    now.
  • is_ambiguous() is now deprecated.

cc @kristjaneerik

@jtratner jtratner force-pushed the do-not-allow-writing-with-trailing-slash branch 4 times, most recently from f6dfb91 to 32921e7 Compare January 20, 2018 06:52
Closes counsyl#52

* Error on filepaths with trailing slash because they do not have a name
  and thus appear to be directories.
* Add validation for bad paths to ``OBSFile``.
* Warn if filesystem paths would not pass this validation (otherwise you
  have the possibility of something that works locally and not on OBS).
  This can be set to raise instead if you set
  ``stor.raise_on_ambiguous_filesystem_path`` to be True
* Deprecate ``is_ambiguous()`` which didn't 100% serve the purpose we
  wanted anyways (since you can always tell when user means directory
  because of the user of ``copytree()`` and/or ``-r``)

Sem-Ver: feature
@jtratner jtratner force-pushed the do-not-allow-writing-with-trailing-slash branch from 32921e7 to 4a498bf Compare January 24, 2018 09:20
"""
kwargs.pop('swift_upload_kwargs', None)
if kwargs.pop('swift_upload_kwargs', None):
warnings.warn('swift_upload_kwargs will be removed in stor 2.0')
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't seem discussed elsewhere in PR?

"""
if mode not in self._VALID_MODES:
raise ValueError('invalid mode for file: %r' % mode)
if six.PY2 and encoding: # pragma: no cover
raise TypeError('encoding not supported in Python 2')
# TODO (jtratner): in stor 2.0 remove this explicit check and the warning
if pth.endswith('/'):
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you want the and not self.ext part from is_ambiguous ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't call to utils.validate_file_path right below sufficient?

passed as keyword args to `SwiftPath.upload` if any writes
occur on the opened resource.

swift_upload_options (dict): DEPRECATED (use `stor.settings.use()` instead).
Copy link
Collaborator

Choose a reason for hiding this comment

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

stor.settings.use() already added correct?

# we use a try/except so that it's easier for the reader to tell why the exception was
# generated.
try:
if not dest.name:
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it slightly less explicit to rely on dest.name to verify no trailing slash rather than doing directly?

@@ -312,6 +313,52 @@ def is_writeable(path, swift_retry_options=None):
return answer


def validate_file_path(dest, _warn=False):
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: dest name implies this is always a check on destination path but it's called on both destination and source paths.

if not dest.ext:
raise ValueError('file paths without extension are ambiguous')
except ValueError as e:
if ((_warn or is_filesystem_path(dest)) and
Copy link
Collaborator

Choose a reason for hiding this comment

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

should is_filesystem_path be moved earlier on since ambiguous paths are fine in that setting so no need to do any validations?

if not dest.name:
raise ValueError('file paths may not end with trailing slash')
if not dest.ext:
raise ValueError('file paths without extension are ambiguous')
Copy link
Collaborator

Choose a reason for hiding this comment

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

requiring extensions for everything seems more restrictive that necessary and beyond what's required to resolve issue no?

Copy link
Collaborator

@krhaas krhaas left a comment

Choose a reason for hiding this comment

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

Made a pass, back to you

Copy link
Collaborator

@krhaas krhaas left a comment

Choose a reason for hiding this comment

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

back to you @jtratner

Copy link
Contributor

@pkaleta pkaleta left a comment

Choose a reason for hiding this comment

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

adding empty review to remove this from my gh PRs list ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants