From 93b837067bd69a99548ce258401e4a96cab70876 Mon Sep 17 00:00:00 2001 From: jbellanti Date: Tue, 10 May 2022 10:58:00 -0400 Subject: [PATCH] Update s3 module_util validate_bucket_name to accept 3 character bucket name (#802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update s3 module_util validate_bucket_name to accept 3 character bucket name SUMMARY According to the S3 docs (https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html), the minimum bucket name is 3 characters. The current validate_bucket_name function fails for any bucket name less than 4 characters. This PR updates the check and adds unit test cases for minimum length. ISSUE TYPE Bugfix Pull Request COMPONENT NAME s3 ADDITIONAL INFORMATION To reproduce, attempt to perform an action on a bucket with a 3 character name. Reviewed-by: Alina Buzachis Reviewed-by: Gonéri Le Bouder --- ...validate_bucket_name-to-accept-3-character-bucket-name.yml | 2 ++ plugins/module_utils/s3.py | 4 ++-- tests/unit/module_utils/test_s3.py | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/802-update-s3-module_util-validate_bucket_name-to-accept-3-character-bucket-name.yml diff --git a/changelogs/fragments/802-update-s3-module_util-validate_bucket_name-to-accept-3-character-bucket-name.yml b/changelogs/fragments/802-update-s3-module_util-validate_bucket_name-to-accept-3-character-bucket-name.yml new file mode 100644 index 00000000000..19dddbdad87 --- /dev/null +++ b/changelogs/fragments/802-update-s3-module_util-validate_bucket_name-to-accept-3-character-bucket-name.yml @@ -0,0 +1,2 @@ +bugfixes: + - module.utils.s3 - Update validate_bucket_name minimum length to 3 (https://github.com/ansible-collections/amazon.aws/pull/802). diff --git a/plugins/module_utils/s3.py b/plugins/module_utils/s3.py index 177e9cca0c6..c13c91f25b1 100644 --- a/plugins/module_utils/s3.py +++ b/plugins/module_utils/s3.py @@ -88,8 +88,8 @@ def calculate_etag_content(module, content, etag, s3, bucket, obj, version=None) def validate_bucket_name(module, name): # See: https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html - if len(name) < 4: - module.fail_json(msg='the S3 bucket name is too short') + if len(name) < 3: + module.fail_json(msg='the length of an S3 bucket must be at least 3 characters') if len(name) > 63: module.fail_json(msg='the length of an S3 bucket cannot exceed 63 characters') diff --git a/tests/unit/module_utils/test_s3.py b/tests/unit/module_utils/test_s3.py index b470f613d4d..b078f718db2 100644 --- a/tests/unit/module_utils/test_s3.py +++ b/tests/unit/module_utils/test_s3.py @@ -27,6 +27,8 @@ def test_validate_bucket_name(): assert not module.fail_json.called assert s3.validate_bucket_name(module, "my.example.s3.bucket") is True assert not module.fail_json.called + assert s3.validate_bucket_name(module, "doc") is True + assert not module.fail_json.called module.fail_json.reset_mock() s3.validate_bucket_name(module, "doc_example_bucket") @@ -38,3 +40,5 @@ def test_validate_bucket_name(): module.fail_json.reset_mock() s3.validate_bucket_name(module, "doc-example-bucket-") assert module.fail_json.called + s3.validate_bucket_name(module, "my") + assert module.fail_json.called