-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Assets: upload to and grant permissions on prefix #510
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0ed0f8
Assets: upload to and grant permissions on prefix
6ce1fa0
Merge remote-tracking branch 'origin/master' into huijbers/asset-prefix
b5c3f6f
Toolkit: add caching for AWS credential providers.
3a7dac9
Combine key and prefix back into a single parameter
7eb3d71
Fix Lambda asset test
dacefbb
WIP
24a445c
Add unit test on assets
f88dcb3
Update integ expectations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ export class Asset extends cdk.Construct { | |
|
||
private readonly bucket: s3.BucketRef; | ||
|
||
private readonly s3PrefixParam: cdk.Parameter; | ||
|
||
constructor(parent: cdk.Construct, id: string, props: GenericAssetProps) { | ||
super(parent, id); | ||
|
||
|
@@ -84,33 +86,45 @@ export class Asset extends cdk.Construct { | |
description: `S3 bucket for asset "${this.path}"`, | ||
}); | ||
|
||
const keyParam = new cdk.Parameter(this, 'S3ObjectKey', { | ||
this.s3PrefixParam = new cdk.Parameter(this, 'S3Prefix', { | ||
type: 'String', | ||
description: `S3 prefix for asset "${this.path}"` | ||
}); | ||
|
||
const keyParam = new cdk.Parameter(this, 'S3VersionKey', { | ||
type: 'String', | ||
description: `S3 object for asset "${this.path}"` | ||
description: `S3 key for asset version "${this.path}"` | ||
}); | ||
|
||
this.s3BucketName = bucketParam.value; | ||
this.s3ObjectKey = keyParam.value; | ||
|
||
// grant the lambda's role read permissions on the code s3 object | ||
|
||
this.bucket = s3.BucketRef.import(parent, 'AssetBucket', { | ||
bucketName: this.s3BucketName | ||
}); | ||
|
||
// form the s3 URL of the object key | ||
this.s3Url = this.bucket.urlForObject(this.s3ObjectKey); | ||
|
||
// Get a unique identifier for this asset, which will be used | ||
// as a folder to group different versions of the same asset together. | ||
// | ||
// Even though this code looks horrible, this is actually not a terrible thing | ||
// to do. The generated ID will contain the *stack name* as well, which is a | ||
// perfectly nice thing to do to disambiguate similarly named assets in different stacks. | ||
const uniqueId = new cdk.HashedAddressingScheme().allocateAddress(this.path.split('/')); | ||
|
||
// attach metadata to the lambda function which includes information | ||
// for tooling to be able to package and upload a directory to the | ||
// s3 bucket and plug in the bucket name and key in the correct | ||
// parameters. | ||
|
||
const asset: cxapi.AssetMetadataEntry = { | ||
path: this.assetPath, | ||
id: uniqueId, | ||
packaging: props.packaging, | ||
s3BucketParameter: bucketParam.logicalId, | ||
s3KeyParameter: keyParam.logicalId, | ||
s3PrefixParameter: this.s3PrefixParam.logicalId | ||
}; | ||
|
||
this.addMetadata(cxapi.ASSET_METADATA, asset); | ||
|
@@ -124,7 +138,11 @@ export class Asset extends cdk.Construct { | |
* Grants read permissions to the principal on the asset's S3 object. | ||
*/ | ||
public grantRead(principal?: iam.IPrincipal) { | ||
this.bucket.grantRead(principal, this.s3ObjectKey); | ||
// We give permissions on all files with the same prefix. Presumably | ||
// different versions of the same file will have the same prefix | ||
// and we don't want to accidentally revoke permission on old versions | ||
// when deploying a new version. | ||
this.bucket.grantRead(principal, new cdk.FnConcat(this.s3PrefixParam.value, "*")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should implement #24 |
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I keep wondering if we should just expose a
uniqueId
in theConstruct
level (and use forresource.logicalId
). This keeps coming back and I think might be useful generally, and not only for CloudFormation resource identifiers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but their scopes are different, which complicates it a bit.
In a tree like this:
The
.path
is from the root to the construct, but the logical ID is only calculated off the nearest enclosing stack to the construct. If we didn't do that, all enclosing logical IDs would being with the stack name, which is not terrible for the IDs that they are, but they're also used to generate human-readable names, where that would be quite ugly (and in fact CFN sometimes--but not always!--prepends the stack name itself as well).So we should probably still keep them separated, or have something like
uniqueIdRelativeTo(ancestor)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could allow constructs to opt-out of the human portion of name generation, so they will be "skipped" when producing the human portion (something like
hideName = true
).