Skip to content

Commit

Permalink
fix(s3): add support for uppercase characters in legacy bucket names
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Oct 18, 2024
1 parent 3d650c3 commit b9595de
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::my_legacy_bucket2/*",
"arn:aws:s3:::My_legacy_bucket2/*",
{
"Fn::Join": [
"",
Expand All @@ -35,7 +35,7 @@
{
"Ref": "AWS::Partition"
},
":s3:::my_legacy_bucket1/*"
":s3:::My_legacy_bucket1/*"
]
]
},
Expand All @@ -47,7 +47,7 @@
{
"Ref": "AWS::Partition"
},
":s3:::my_legacy_bucket3/*"
":s3:::My_legacy_bucket3/*"
]
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import * as iam from 'aws-cdk-lib/aws-iam';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-cdk-s3-legacy-name-integ');

const legacyBucketFromName = s3.Bucket.fromBucketName(stack, 'LegacyBucketFromName', 'my_legacy_bucket1');
const legacyBucketFromName = s3.Bucket.fromBucketName(stack, 'LegacyBucketFromName', 'My_legacy_bucket1');

const legacyBucketFromArn = s3.Bucket.fromBucketArn(stack, 'LegacyBucketFromArn', 'arn:aws:s3:::my_legacy_bucket2');
const legacyBucketFromArn = s3.Bucket.fromBucketArn(stack, 'LegacyBucketFromArn', 'arn:aws:s3:::My_legacy_bucket2');

const legacyBucketFromAttributes = s3.Bucket.fromBucketAttributes(stack, 'LegacyBucketFromAttributes', {
bucketName: 'my_legacy_bucket3',
bucketName: 'My_legacy_bucket3',
});

const role = new iam.Role(stack, 'LegacyBucketRole', {
Expand All @@ -37,4 +37,3 @@ new IntegTest(app, 'aws-cdk-s3-integ-test', {
// In the synthesized template, verify:
// 1. The bucket names imported using different methods (Bucket.fromBucketName, Bucket.fromBucketArn, Bucket.fromBucketAttributes) are not modified and contain underscores.
// 2. The policy attached to the role includes the correct bucket ARNs with underscores for all three imported buckets.

39 changes: 26 additions & 13 deletions packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1867,24 +1867,37 @@ export class Bucket extends BucketBase {
if (bucketName.length < 3 || bucketName.length > 63) {
errors.push('Bucket name must be at least 3 and no more than 63 characters');
}
const charsetRegex = allowLegacyBucketNaming ? /[^a-z0-9._-]/ : /[^a-z0-9.-]/;
const charsetMatch = bucketName.match(charsetRegex);
if (charsetMatch) {
errors.push(`Bucket name must only contain lowercase characters and the symbols, period (.)${allowLegacyBucketNaming ? ', underscore (_), ' : ' '}and dash (-) `
+ `(offset: ${charsetMatch.index})`);

const illegalCharsetRegEx = allowLegacyBucketNaming ? /[^A-Za-z0-9._-]/ : /[^a-z0-9.-]/;
const allowedEdgeCharsetRegEx = allowLegacyBucketNaming ? /[A-Za-z0-9]/ : /[a-z0-9]/;

const illegalCharMatch = bucketName.match(illegalCharsetRegEx);
if (illegalCharMatch) {
errors.push(allowLegacyBucketNaming
? 'Bucket name must only contain uppercase or lowercase characters and the symbols, period (.), underscore (_), and dash (-)'
: 'Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-)'
+ ` (offset: ${illegalCharMatch.index})`,
);
}
if (!/[a-z0-9]/.test(bucketName.charAt(0))) {
errors.push('Bucket name must start and end with a lowercase character or number '
+ '(offset: 0)');
if (!allowedEdgeCharsetRegEx.test(bucketName.charAt(0))) {
errors.push(allowLegacyBucketNaming
? 'Bucket name must start and end with an uppercase, lowercase character or number'
: 'Bucket name must start and end with a lowercase character or number'
+ ' (offset: 0)',
);
}
if (!/[a-z0-9]/.test(bucketName.charAt(bucketName.length - 1))) {
errors.push('Bucket name must start and end with a lowercase character or number '
+ `(offset: ${bucketName.length - 1})`);
if (!allowedEdgeCharsetRegEx.test(bucketName.charAt(bucketName.length - 1))) {
errors.push(allowLegacyBucketNaming
? 'Bucket name must start and end with an uppercase, lowercase character or number'
: 'Bucket name must start and end with a lowercase character or number'
+ ` (offset: ${bucketName.length - 1})`,
);
}

const consecSymbolMatch = bucketName.match(/\.-|-\.|\.\./);
if (consecSymbolMatch) {
errors.push('Bucket name must not have dash next to period, or period next to dash, or consecutive periods '
+ `(offset: ${consecSymbolMatch.index})`);
errors.push('Bucket name must not have dash next to period, or period next to dash, or consecutive periods'
+ ` (offset: ${consecSymbolMatch.index})`);
}
if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(bucketName)) {
errors.push('Bucket name must not resemble an IP address');
Expand Down
19 changes: 18 additions & 1 deletion packages/aws-cdk-lib/aws-s3/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,30 @@ describe('bucket', () => {
}).toThrow(/Bucket name must only contain lowercase characters and the symbols, period \(\.\) and dash \(-\)/);
});

test('validateBucketName allows uppercase characters when allowLegacyBucketNaming=true', () => {
expect(() => {
s3.Bucket.validateBucketName('Test_Bucket_Name', true);
}).not.toThrow();
});

test('validateBucketName does not allow uppercase characters when allowLegacyBucketNaming=false', () => {
expect(() => {
s3.Bucket.validateBucketName('Test_Bucket_Name', false);
}).toThrow(/Bucket name must only contain lowercase characters and the symbols, period \(\.\) and dash \(-\)/);
});

test('validateBucketName does not allow underscore by default', () => {
expect(() => {
s3.Bucket.validateBucketName('test_bucket_name');
}).toThrow(/Bucket name must only contain lowercase characters and the symbols, period \(\.\) and dash \(-\)/);
});

test('validateBucketName does not allow uppercase characters by default', () => {
expect(() => {
s3.Bucket.validateBucketName('TestBucketName');
}).toThrow(/Bucket name must only contain lowercase characters and the symbols, period \(\.\) and dash \(-\)/);
});

test('fails if bucket name has less than 3 or more than 63 characters', () => {
const stack = new cdk.Stack();

Expand Down Expand Up @@ -3853,4 +3871,3 @@ describe('bucket', () => {
});
});
});

0 comments on commit b9595de

Please sign in to comment.