Skip to content

Commit

Permalink
S3 - Fix exceptions for VersionID (getmoto#3884)
Browse files Browse the repository at this point in the history
  • Loading branch information
amarjandu committed Apr 28, 2021
1 parent f5e3cd8 commit 3372fab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 10 additions & 1 deletion moto/s3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,21 @@ def __init__(self, key_name):
class MissingVersion(S3ClientError):
code = 404

def __init__(self, *args, **kwargs):
super(MissingVersion, self).__init__(
"NoSuchVersion", "The specified version does not exist.", *args, **kwargs
)


class InvalidVersion(S3ClientError):
code = 400

def __init__(self, version_id, *args, **kwargs):
kwargs.setdefault("template", "argument_error")
kwargs["name"] = "versionId"
kwargs["value"] = version_id
self.templates["argument_error"] = ERROR_WITH_ARGUMENT
super(MissingVersion, self).__init__(
super(InvalidVersion, self).__init__(
"InvalidArgument", "Invalid version id specified", *args, **kwargs
)

Expand Down
8 changes: 7 additions & 1 deletion moto/s3/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

import xmltodict
from uuid import UUID

from moto.packages.httpretty.core import HTTPrettyRequest
from moto.core.responses import _TemplateEnvironmentMixin, ActionAuthenticatorMixin
Expand Down Expand Up @@ -49,6 +50,7 @@
IllegalLocationConstraintException,
InvalidNotificationARN,
InvalidNotificationEvent,
InvalidVersion,
ObjectNotInActiveTierError,
NoSystemTags,
PreconditionFailed,
Expand Down Expand Up @@ -1206,11 +1208,15 @@ def _key_response_get(self, bucket_name, query, key_name, headers):
if_none_match = headers.get("If-None-Match", None)
if_unmodified_since = headers.get("If-Unmodified-Since", None)

try:
UUID(version_id, version=4)
except ValueError:
raise InvalidVersion(version_id)
key = self.backend.get_object(bucket_name, key_name, version_id=version_id)
if key is None and version_id is None:
raise MissingKey(key_name)
elif key is None:
raise MissingVersion(version_id)
raise MissingVersion()

if if_unmodified_since:
if_unmodified_since = str_to_rfc_1123_datetime(if_unmodified_since)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5019,6 +5019,28 @@ def test_get_unknown_version_should_throw_specific_error():
e.value.response["Error"]["ArgumentValue"].should.equal("unknown")


@mock_s3
def test_syntatically_correct_invalid_verion():
bucket_name = "my_bucket"
object_key = "hello.txt"
s3 = boto3.resource("s3", region_name="us-east-1")
client = boto3.client("s3", region_name="us-east-1")
bucket = s3.create_bucket(Bucket=bucket_name)
bucket.Versioning().enable()
content = "some text"
s3.Object(bucket_name, object_key).put(Body=content)
random_version_id = str(uuid.uuid4())

with pytest.raises(ClientError) as e:
client.get_object(
Bucket=bucket_name, Key=object_key, VersionId=random_version_id
)
e.value.response["Error"]["Code"].should.equal("NoSuchVersion")
e.value.response["Error"]["Message"].should.equal(
"The specified version does not exist."
)


@mock_s3
def test_request_partial_content_without_specifying_range_should_return_full_object():
bucket = "bucket"
Expand Down

0 comments on commit 3372fab

Please sign in to comment.