Skip to content

Commit

Permalink
Add a method to validate bucket names
Browse files Browse the repository at this point in the history
This change adds a method validate_bucket_name which can be used
to determine if the bucket names meet S3 standards or not.

It is currently unused, but in a future commit it will be called
from BucketController's PUT method.
  • Loading branch information
fifieldt authored and fujita committed Oct 15, 2012
1 parent 14fdcc2 commit 533c243
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions swift3/middleware.py
Expand Up @@ -59,6 +59,7 @@
from simplejson import loads
import email.utils
import datetime
import re

from swift.common.utils import split_path
from swift.common.utils import get_logger
Expand Down Expand Up @@ -292,6 +293,35 @@ def swift_acl_translate(acl, group='', user=''):

return swift_acl[acl]

def validate_bucket_name(name):
"""
Validates the name of the bucket against S3 criteria,
http://docs.amazonwebservices.com/AmazonS3/latest/BucketRestrictions.html
True if valid, False otherwise
"""

if '_' in name or len(name) < 3 or len(name) > 63 or not name[-1].isalnum():
"""
Bucket names should not contain underscores (_)
Bucket names must end with a lowercase letter or number
Bucket names should be between 3 and 63 characters long
"""
return False
elif '.-' in name or '-.' in name or '..' in name or not name[0].isalnum():
"""
Bucket names cannot contain dashes next to periods
Bucket names cannot contain two adjacent periods
Bucket names Must start with a lowercase letter or a number
"""
return False
elif re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"\
"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", name):
"""
Bucket names cannot be formatted as an IP Address
"""
return False
else:
return True

class ServiceController(WSGIContext):
"""
Expand Down

0 comments on commit 533c243

Please sign in to comment.