Merge pull request #1964 from jbmchuck/bucket-name-length-limit
Check bucket name length at CreateBucket
This commit is contained in:
commit
e9cff5c915
@ -179,7 +179,16 @@ class InvalidStorageClass(S3ClientError):
|
|||||||
"The storage class you specified is not valid",
|
"The storage class you specified is not valid",
|
||||||
*args, **kwargs)
|
*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidBucketName(S3ClientError):
|
||||||
|
code = 400
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(InvalidBucketName, self).__init__(
|
||||||
|
"InvalidBucketName",
|
||||||
|
"The specified bucket is not valid.",
|
||||||
|
|
||||||
|
|
||||||
class DuplicateTagKeys(S3ClientError):
|
class DuplicateTagKeys(S3ClientError):
|
||||||
code = 400
|
code = 400
|
||||||
|
|
||||||
|
@ -14,10 +14,12 @@ import six
|
|||||||
from bisect import insort
|
from bisect import insort
|
||||||
from moto.core import BaseBackend, BaseModel
|
from moto.core import BaseBackend, BaseModel
|
||||||
from moto.core.utils import iso_8601_datetime_with_milliseconds, rfc_1123_datetime
|
from moto.core.utils import iso_8601_datetime_with_milliseconds, rfc_1123_datetime
|
||||||
from .exceptions import BucketAlreadyExists, MissingBucket, InvalidPart, EntityTooSmall, MissingKey, \
|
from .exceptions import BucketAlreadyExists, MissingBucket, InvalidBucketName, InvalidPart, \
|
||||||
InvalidNotificationDestination, MalformedXML, InvalidStorageClass, DuplicateTagKeys
|
EntityTooSmall, MissingKey, InvalidNotificationDestination, MalformedXML, InvalidStorageClass, DuplicateTagKeys
|
||||||
from .utils import clean_key_name, _VersionedKeyStore
|
from .utils import clean_key_name, _VersionedKeyStore
|
||||||
|
|
||||||
|
MAX_BUCKET_NAME_LENGTH = 63
|
||||||
|
MIN_BUCKET_NAME_LENGTH = 3
|
||||||
UPLOAD_ID_BYTES = 43
|
UPLOAD_ID_BYTES = 43
|
||||||
UPLOAD_PART_MIN_SIZE = 5242880
|
UPLOAD_PART_MIN_SIZE = 5242880
|
||||||
STORAGE_CLASS = ["STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA"]
|
STORAGE_CLASS = ["STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA"]
|
||||||
@ -634,6 +636,8 @@ class S3Backend(BaseBackend):
|
|||||||
def create_bucket(self, bucket_name, region_name):
|
def create_bucket(self, bucket_name, region_name):
|
||||||
if bucket_name in self.buckets:
|
if bucket_name in self.buckets:
|
||||||
raise BucketAlreadyExists(bucket=bucket_name)
|
raise BucketAlreadyExists(bucket=bucket_name)
|
||||||
|
if not MIN_BUCKET_NAME_LENGTH <= len(bucket_name) <= MAX_BUCKET_NAME_LENGTH:
|
||||||
|
raise InvalidBucketName()
|
||||||
new_bucket = FakeBucket(name=bucket_name, region_name=region_name)
|
new_bucket = FakeBucket(name=bucket_name, region_name=region_name)
|
||||||
self.buckets[bucket_name] = new_bucket
|
self.buckets[bucket_name] = new_bucket
|
||||||
return new_bucket
|
return new_bucket
|
||||||
|
@ -2618,3 +2618,17 @@ TEST_XML = """\
|
|||||||
</ns0:RoutingRules>
|
</ns0:RoutingRules>
|
||||||
</ns0:WebsiteConfiguration>
|
</ns0:WebsiteConfiguration>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@mock_s3
|
||||||
|
def test_boto3_bucket_name_too_long():
|
||||||
|
s3 = boto3.client('s3', region_name='us-east-1')
|
||||||
|
with assert_raises(ClientError) as exc:
|
||||||
|
s3.create_bucket(Bucket='x'*64)
|
||||||
|
exc.exception.response['Error']['Code'].should.equal('InvalidBucketName')
|
||||||
|
|
||||||
|
@mock_s3
|
||||||
|
def test_boto3_bucket_name_too_short():
|
||||||
|
s3 = boto3.client('s3', region_name='us-east-1')
|
||||||
|
with assert_raises(ClientError) as exc:
|
||||||
|
s3.create_bucket(Bucket='x'*2)
|
||||||
|
exc.exception.response['Error']['Code'].should.equal('InvalidBucketName')
|
||||||
|
Loading…
Reference in New Issue
Block a user