feat(aws-efs): grant support on FileSystem#14999
Conversation
| this.fileSystemArn = Stack.of(scope).formatArn({ | ||
| service: 'elasticfilesystem', | ||
| resource: 'file-system', | ||
| resourceName: this.fileSystemId, | ||
| }); |
There was a problem hiding this comment.
I believe you should just get the arn from CfnFileSystem.
| this.fileSystemArn = Stack.of(scope).formatArn({ | |
| service: 'elasticfilesystem', | |
| resource: 'file-system', | |
| resourceName: this.fileSystemId, | |
| }); | |
| this.fileSystemArn = filesystem.attrArn; |
There was a problem hiding this comment.
Good catch. Just updated the code.
| super(scope, id); | ||
|
|
||
| this.fileSystemId = attrs.fileSystemId; | ||
| if (!attrs.fileSystemId && !attrs.fileSystemArn) { |
There was a problem hiding this comment.
Either we should change this so that only one of them is provided, or if we want to support providing both, we should validate (later) that they are not different.
There was a problem hiding this comment.
I'm going to throw an error if both are provided (similar to how it's done for DynamoDB tables).
packages/@aws-cdk/aws-efs/README.md
Outdated
| const role = new iam.Role(this, 'Role', { | ||
| assumedBy: new iam.AnyPrincipal(), | ||
| }); | ||
|
|
||
| importedFileSystem.grant(role, 'elasticfilesystem:ClientWrite'); |
There was a problem hiding this comment.
Document the grant API in a separate section.
nija-at
left a comment
There was a problem hiding this comment.
Thanks for making the changes. A few more comments below.
packages/@aws-cdk/aws-efs/README.md
Outdated
| }); | ||
| ``` | ||
|
|
||
| ### Granting fileSystem permissions to resources |
There was a problem hiding this comment.
| ### Granting fileSystem permissions to resources | |
| ### Permissions |
packages/@aws-cdk/aws-efs/README.md
Outdated
| If you need to grant file system permissions to another resource, you can use the `.grant()` method. | ||
| As an example, the following code gives ClientWrite permissions to an IAM role. |
There was a problem hiding this comment.
| If you need to grant file system permissions to another resource, you can use the `.grant()` method. | |
| As an example, the following code gives ClientWrite permissions to an IAM role. | |
| If you need to grant file system permissions to another resource, you can use the `grant()` API. | |
| As an example, the following code gives `elasticfilesystem:ClientWrite` permissions to an IAM role. |
| if (!attrs.fileSystemId && !attrs.fileSystemArn) { | ||
| throw new Error('One of fileSystemId or fileSystemArn must be provided.'); | ||
| } | ||
|
|
||
| if (attrs.fileSystemId && attrs.fileSystemArn) { | ||
| throw new Error('Only one of fileSystemId or fileSystemArn must be provided.'); | ||
| } |
There was a problem hiding this comment.
I believe you can shorten this by
| if (!attrs.fileSystemId && !attrs.fileSystemArn) { | |
| throw new Error('One of fileSystemId or fileSystemArn must be provided.'); | |
| } | |
| if (attrs.fileSystemId && attrs.fileSystemArn) { | |
| throw new Error('Only one of fileSystemId or fileSystemArn must be provided.'); | |
| } | |
| if (!!attrs.fileSystemId === !!attrs.fileSystemArn) { | |
| throw new Error('One of fileSystemId or fileSystemArn, but not both, must be provided.'); | |
| } |
| const parsedArn = Stack.of(scope).parseArn(this.fileSystemArn); | ||
|
|
||
| if (!parsedArn.resourceName) { | ||
| throw new Error('Invalid FileSystem Arn.'); |
There was a problem hiding this comment.
| throw new Error('Invalid FileSystem Arn.'); | |
| throw new Error(`Invalid FileSystem Arn ${this.filesystemArn}`); |
| throw new Error('Invalid FileSystem Arn.'); | ||
| } | ||
|
|
||
| this.fileSystemId = attrs.fileSystemId ?? Stack.of(scope).parseArn(this.fileSystemArn).resourceName!; |
There was a problem hiding this comment.
avoid the non-null assertion operator !. I believe you can now do -
| this.fileSystemId = attrs.fileSystemId ?? Stack.of(scope).parseArn(this.fileSystemArn).resourceName!; | |
| this.fileSystemId = attrs.fileSystemId ?? parsedArn.resourceName; |
| if (!attrs.fileSystemId && !attrs.fileSystemArn) { | ||
| throw new Error('One of fileSystemId or fileSystemArn must be provided.'); | ||
| } | ||
|
|
||
| if (attrs.fileSystemId && attrs.fileSystemArn) { | ||
| throw new Error('Only one of fileSystemId or fileSystemArn must be provided.'); | ||
| } |
|
Updates the code following last recommendations. Also updated the pull request description. |
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
`FileSystem` now have the `fileSystemArn` attribute available. It is also possible to import an existing `FileSystem` by `arn` or `id` using the `fromFileSystemAttributes` method.
You can also grant permission to an existing grantee using the grant method.
See the example below giving an IAM Role permission to write to an imported file system:
```ts
const arn = stack.formatArn({
service: 'elasticfilesystem',
resource: 'file-system',
resourceName: 'fs-12912923',
});
const importedFileSystem = efs.FileSystem.fromFileSystemAttributes(this, 'existingFS', {
fileSystemArn: arn, // You can also use fileSystemArn instead of fileSystemId.
securityGroup: ec2.SecurityGroup.fromSecurityGroupId(this, 'SG', 'sg-123456789', {
allowAllOutbound: false,
}),
});
const role = new iam.Role(this, 'Access Role', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com') });
importedFileSystem.grant(role, 'elasticfilesystem:ClientWrite');
```
Closes aws#14998.
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
`FileSystem` now have the `fileSystemArn` attribute available. It is also possible to import an existing `FileSystem` by `arn` or `id` using the `fromFileSystemAttributes` method.
You can also grant permission to an existing grantee using the grant method.
See the example below giving an IAM Role permission to write to an imported file system:
```ts
const arn = stack.formatArn({
service: 'elasticfilesystem',
resource: 'file-system',
resourceName: 'fs-12912923',
});
const importedFileSystem = efs.FileSystem.fromFileSystemAttributes(this, 'existingFS', {
fileSystemArn: arn, // You can also use fileSystemArn instead of fileSystemId.
securityGroup: ec2.SecurityGroup.fromSecurityGroupId(this, 'SG', 'sg-123456789', {
allowAllOutbound: false,
}),
});
const role = new iam.Role(this, 'Access Role', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com') });
importedFileSystem.grant(role, 'elasticfilesystem:ClientWrite');
```
Closes aws#14998.
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
FileSystemnow have thefileSystemArnattribute available. It is also possible to import an existingFileSystembyarnoridusing thefromFileSystemAttributesmethod.You can also grant permission to an existing grantee using the grant method.
See the example below giving an IAM Role permission to write to an imported file system:
Closes #14998.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license