From 533c243c8fb8d3e19f1f9b3dfa3e0a2e2c4d35ef Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Mon, 8 Oct 2012 06:45:04 +0000 Subject: [PATCH] Add a method to validate bucket names 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. --- swift3/middleware.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/swift3/middleware.py b/swift3/middleware.py index 5a63b4b..28ab0d5 100644 --- a/swift3/middleware.py +++ b/swift3/middleware.py @@ -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 @@ -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): """