Skip to content

feat(aws-efs): grant support on FileSystem#14999

Merged
mergify[bot] merged 8 commits intoaws:masterfrom
DaWyz:DaWyz/efs-file-system-arn-attribute
Jul 15, 2021
Merged

feat(aws-efs): grant support on FileSystem#14999
mergify[bot] merged 8 commits intoaws:masterfrom
DaWyz:DaWyz/efs-file-system-arn-attribute

Conversation

@DaWyz
Copy link
Copy Markdown
Contributor

@DaWyz DaWyz commented Jun 6, 2021

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:

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 #14998.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link
Copy Markdown

gitpod-io bot commented Jun 6, 2021

@peterwoodworth peterwoodworth added the @aws-cdk/aws-efs Related to Amazon Elastic File System label Jun 8, 2021
@nija-at nija-at changed the title feat(aws-efs): Add arn attribute and grant method to FileSystem construct feat(aws-efs): grant support on FileSystem Jun 21, 2021
Copy link
Copy Markdown
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @DaWyz. Looks mostly good. A few comments below.

Comment on lines +336 to +340
this.fileSystemArn = Stack.of(scope).formatArn({
service: 'elasticfilesystem',
resource: 'file-system',
resourceName: this.fileSystemId,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you should just get the arn from CfnFileSystem.

Suggested change
this.fileSystemArn = Stack.of(scope).formatArn({
service: 'elasticfilesystem',
resource: 'file-system',
resourceName: this.fileSystemId,
});
this.fileSystemArn = filesystem.attrArn;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Just updated the code.

super(scope, id);

this.fileSystemId = attrs.fileSystemId;
if (!attrs.fileSystemId && !attrs.fileSystemArn) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm going to throw an error if both are provided (similar to how it's done for DynamoDB tables).

Comment on lines +60 to +64
const role = new iam.Role(this, 'Role', {
assumedBy: new iam.AnyPrincipal(),
});

importedFileSystem.grant(role, 'elasticfilesystem:ClientWrite');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Document the grant API in a separate section.

@mergify mergify bot dismissed nija-at’s stale review June 21, 2021 23:22

Pull request has been modified.

@DaWyz DaWyz requested a review from nija-at June 22, 2021 03:11
Copy link
Copy Markdown
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes. A few more comments below.

});
```

### Granting fileSystem permissions to resources
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
### Granting fileSystem permissions to resources
### Permissions

Comment on lines +63 to +64
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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.

Comment on lines +402 to +408
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.');
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you can shorten this by

Suggested change
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.');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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!;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

avoid the non-null assertion operator !. I believe you can now do -

Suggested change
this.fileSystemId = attrs.fileSystemId ?? Stack.of(scope).parseArn(this.fileSystemArn).resourceName!;
this.fileSystemId = attrs.fileSystemId ?? parsedArn.resourceName;

Comment on lines +402 to +408
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.');
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add unit tests for these

@mergify mergify bot dismissed nija-at’s stale review June 22, 2021 13:41

Pull request has been modified.

@DaWyz
Copy link
Copy Markdown
Contributor Author

DaWyz commented Jun 22, 2021

Updates the code following last recommendations. Also updated the pull request description.

@DaWyz DaWyz requested a review from nija-at June 22, 2021 15:01
Copy link
Copy Markdown
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

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

Awesome! Looks great.

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jun 22, 2021

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).

Copy link
Copy Markdown
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

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

@DaWyz - can you take a look at the failing build with the latest changes.

It's likely because you need to update the tests to use the new assertion framework - 367fe7e

@mergify mergify bot dismissed nija-at’s stale review June 24, 2021 00:01

Pull request has been modified.

@DaWyz DaWyz requested a review from nija-at June 24, 2021 06:16
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jul 15, 2021

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-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 936c769
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 09591c6 into aws:master Jul 15, 2021
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jul 15, 2021

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).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Aug 3, 2021
`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*
hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Aug 26, 2021
`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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@aws-cdk/aws-efs Related to Amazon Elastic File System

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(aws-efs): Add fileSystemArn property and grant method for FileSystem construct

4 participants