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

feat(iam): validate role path at build time #16165

Merged
merged 6 commits into from
May 13, 2022

Conversation

i05nagai
Copy link
Contributor

Role paths can be validated at build time.

According to the API document, u007F, DELETE special char, is valid. However, the creation with a role path /\u007F/ fails due to validation failure. I don't see any use case for the special char, so I ignored the discrepancy.

closes #13747


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

0x007F is valid in the API document. However, role creation failed.
Not sure if the document is not up-to-date or the API has a bug.
@gitpod-io
Copy link

gitpod-io bot commented Aug 22, 2021

@peterwoodworth peterwoodworth added p2 effort/small Small work item – less than a day of effort @aws-cdk/aws-iam Related to AWS Identity and Access Management labels Sep 1, 2021
@rix0rrr rix0rrr added feature-request A feature should be added or improved. p2 and removed p2 @aws-cdk/aws-iam Related to AWS Identity and Access Management effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. labels Mar 4, 2022
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Just a few changes requested. Additionally, I want to look into the issue you brought up in your description about the character /\u007F/.

const validRolePath = /^(\/|\/[\u0021-\u007F]+\/)$/;

if (path.length == 0 || path.length > 512) {
throw new Error(`role path is set to ${path}, but the length must be >= 1 and <= 512.`);
Copy link
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(`role path is set to ${path}, but the length must be >= 1 and <= 512.`);
throw new Error(`Role path must be between 1 and 512 characters. The provided role path is ${path.length} characters.`);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 8d9afeb

if (path.length == 0 || path.length > 512) {
throw new Error(`role path is set to ${path}, but the length must be >= 1 and <= 512.`);
} else if (!validRolePath.test(path)) {
throw new Error(`role path is set to ${path}, but must match ${validRolePath.toString()}.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not totally sure what the best error message is here but providing the regex itself here is not very user friendly. Perhaps you can describe the pattern needed instead of just using the regex. also, please capitalize the beginning of the sentence in the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a bit more explanation. Updated in ea3a324

});
});

test('must be between 1 chars and 512 chars', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This may just be my preference here, but I'd prefer to only see one test case per test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean 1 expect statement in a test? Fixed in ea3a324

const assumedBy = new ServicePrincipal('bla');

new Role(stack, 'MyRole1', { assumedBy, path: '/' });
new Role(stack, 'MyRole2', { assumedBy, path: '/p/a/t/h/' });
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like either of these roles are being used in this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are valid cases, When exceptions are raised, the test framework will catch them and make them fail tests. I made it explicit with expect statement in ea3a324

.toThrow(expected('/' + Array(512).join('a') + '/'));
});

test('must match regular expression', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as the test above. One test case per test, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ea3a324

return;
}

const validRolePath = /^(\/|\/[\u0021-\u007F]+\/)$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm hesitant to allow validation of a character that will throw an error. I've put a ticket in with the IAM team to double check this. Can you provide the specific validation error you get with /\u007F/ to help me look into this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's great. I cannot investigate further on my side. A simple example with CDK is below. I've confirmed the same behavior with python client (boto3). I believe that the issue exists on IAM side.

import * as cdk from 'aws-cdk-lib';
import { aws_iam as iam } from 'aws-cdk-lib';


export class Example1Stack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    const role = new iam.Role(this, "role", {
      assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
      path: "/\u007F/",
    })
  }
}

Error messages

The stack named Example1Stack failed to deploy: UPDATE_ROLLBACK_COMPLETE: The specified value for path is invalid. It must begin and end with / and contain only alphanumeric characters and/or / characters. (Service: AmazonIdentityManagement; Status Code: 400; Error Code: ValidationError; Request ID: 08690aa1-f05c-4830-964d-acd4b2a84532; Proxy: null)

As far as I checked, other control charcters (e.g. /\u0x7E/) work and other patterns with \u0x007F (e. g. /aaaa\u0x007Faaaaa/) fail.

@TheRealAmazonKendra TheRealAmazonKendra added pr-linter/exempt-readme The PR linter will not require README changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Apr 27, 2022
@TheRealAmazonKendra TheRealAmazonKendra changed the title feat(aws-iam): validate role path feat(iam): validate role path at build time Apr 27, 2022
@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. labels Apr 27, 2022
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review May 1, 2022 11:42

Pull request has been modified.

@i05nagai
Copy link
Contributor Author

i05nagai commented May 1, 2022

Thanks for the review! Please have a look again.

@mergify
Copy link
Contributor

mergify bot commented May 13, 2022

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
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 23600f2
  • 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 65a5a46 into aws:master May 13, 2022
@mergify
Copy link
Contributor

mergify bot commented May 13, 2022

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

wphilipw pushed a commit to wphilipw/aws-cdk that referenced this pull request May 23, 2022
Role paths can be validated at build time.

According to the [API document](https://docs.aws.amazon.com/IAM/latest/APIReference/API_Role.html), `u007F`, DELETE special char, is valid. However, the creation with a role path `/\u007F/` fails due to validation failure. I don't see any use case for the special char, so I ignored the discrepancy.

closes aws#13747


----

*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
effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-readme The PR linter will not require README changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-iam): Validate Role path at build time.
5 participants