Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Packaging): Fix support of the artifact S3 uri with region #9411

Merged
merged 3 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/plugins/aws/utils/parse-s3-uri.js
@@ -1,23 +1,24 @@
'use strict';

// https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html
const patterns = [
// S3 URI. Ex: s3://bucket/path/to/artifact.zip
new RegExp('^s3://([^/]+)/(.+)'),
new RegExp('^s3://(?<bucket>[^/]+)/(?<key>.+)'),
Copy link
Contributor

@bishtawi bishtawi Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt realize you could label your matched groups. That is really cool.


// New style S3 URL. Ex: https://bucket.s3.amazonaws.com/path/to/artifact.zip
new RegExp('([^/]+)\\.s3\\.amazonaws\\.com/(.+)'),
// New style S3 URL. Ex: https://bucket.s3.REGION.amazonaws.com/path/to/artifact.zip
new RegExp('(?<bucket>[^/]+)\\.s3(\\.[\\w\\d-]+)?\\.amazonaws\\.com/(?<key>.+)'),

// Old style S3 URL. Ex: https://s3.amazonaws.com/bucket/path/to/artifact.zip
new RegExp('s3\\.amazonaws\\.com/([^/]+)/(.+)'),
// Old style S3 URL. Ex: https://s3.REGION.amazonaws.com/bucket/path/to/artifact.zip
new RegExp('s3(\\.[\\w\\d-]+)?\\.amazonaws\\.com/(?<bucket>[^/]+)/(?<key>.+)'),
Copy link
Contributor

@bishtawi bishtawi Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this regex and the one above, you should probably support urls with dashes separating the s3 from the region as apparently s3-region is a valid url is some aws regions.

Ex: https://my-bucket.s3-us-west-2.amazonaws.com

So ([.-][\\w\\d-]+)?.

];

module.exports = (url) => {
for (const regex of patterns) {
const match = url.match(regex);
if (match) {
return {
Bucket: match[1],
Key: match[2],
Bucket: match.groups.bucket,
Key: match.groups.key,
};
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/lib/plugins/aws/utils/parse-s3-uri.test.js
Expand Up @@ -19,6 +19,14 @@ describe('test/unit/lib/plugins/aws/utils/parse-s3-uri.test.js', () => {
const actual = parseS3URI('https://s3.amazonaws.com/test-bucket/path/to/artifact.zip');
expect(actual).to.deep.equal(expected);
});
it('should parse an old style S3 URL with region', () => {
const expected = {
Bucket: 'test-bucket',
Key: 'path/to/artifact.zip',
};
const actual = parseS3URI('https://s3.us-west-1.amazonaws.com/test-bucket/path/to/artifact.zip');
expect(actual).to.deep.equal(expected);
});
it('should parse a new style S3 URL', () => {
const expected = {
Bucket: 'test-bucket',
Expand All @@ -27,6 +35,14 @@ describe('test/unit/lib/plugins/aws/utils/parse-s3-uri.test.js', () => {
const actual = parseS3URI('https://test-bucket.s3.amazonaws.com/path/to/artifact.zip');
expect(actual).to.deep.equal(expected);
});
it('should parse a new style S3 URL with region', () => {
const expected = {
Bucket: 'test-bucket',
Key: 'path/to/artifact.zip',
};
const actual = parseS3URI('https://test-bucket.s3.eu-west-1.amazonaws.com/path/to/artifact.zip');
expect(actual).to.deep.equal(expected);
});
it('should reject non S3 URLs', () => {
const actual = parseS3URI('https://example.com/path/to/artifact.zip');
expect(actual).to.be.null;
Expand Down