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

Update genUsername to cap STS usernames at 32 chars #12185

Merged
merged 12 commits into from Aug 9, 2021

Conversation

vinay-gopalan
Copy link
Contributor

@vinay-gopalan vinay-gopalan commented Jul 27, 2021

This PR updates the genUsername function in the AWS secrets engine and updates it to cap STS usernames at 64 characters instead of 32. The PR also refactors the unit tests to reflect the updates and converts them to table tests.

Follow-up to #12066

@vinay-gopalan vinay-gopalan requested a review from a team July 27, 2021 21:01
@vercel vercel bot temporarily deployed to Preview – vault-storybook July 27, 2021 21:03 Inactive
@vercel vercel bot temporarily deployed to Preview – vault July 27, 2021 21:03 Inactive
Copy link
Contributor

@pcman312 pcman312 left a comment

Choose a reason for hiding this comment

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

Looks good overall! Just a few small comments/suggestions

builtin/logical/aws/secret_access_keys.go Outdated Show resolved Hide resolved
builtin/logical/aws/secret_access_keys_test.go Outdated Show resolved Hide resolved
builtin/logical/aws/secret_access_keys_test.go Outdated Show resolved Hide resolved
builtin/logical/aws/secret_access_keys_test.go Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – vault-storybook July 27, 2021 22:36 Inactive
@vercel vercel bot temporarily deployed to Preview – vault July 27, 2021 22:36 Inactive
@vercel vercel bot temporarily deployed to Preview – vault July 27, 2021 22:59 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook July 27, 2021 22:59 Inactive
Copy link
Member

@calvn calvn left a comment

Choose a reason for hiding this comment

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

It looks like for STS federated tokens, the name is capped at 32 chars according to their docs, so we should keep the existing default template.

builtin/logical/aws/secret_access_keys.go Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – vault July 27, 2021 23:58 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook July 27, 2021 23:58 Inactive
Copy link
Member

@calvn calvn left a comment

Choose a reason for hiding this comment

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

Left a few comments, but looking good! Can you also update the PRs title since that's what the squash merge will use?

builtin/logical/aws/secret_access_keys.go Outdated Show resolved Hide resolved
}
case "sts":
// Capped at 32 chars, which leaves only a couple of characters to play
// with, so don't insert display name or policy name at all
// Capped at 32 chars
up, err := template.NewTemplate(template.Template(usernameTemplate))
if err != nil {
return "", "", fmt.Errorf("unable to initialize username template: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

I can't drop comments past here, but on L84 can you add a 32 char length check, similar to what we have in?

@pcman312 mentioned offline that returning an error if custom username templates is above character limit is an option, which I think is okay to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@calvn I can do that, but also wanted to mention that we only ever use the defaultSTSTemplate when generating usernames (which is always capped at 32 by default). Since the length limits are different and the feature ask was specifically for custom IAM user/role names, currently STS names are never generated by a custom user template.

Copy link
Contributor

Choose a reason for hiding this comment

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

currently STS names are never generated by a custom user template

Is there a technical reason for this? If not, this feels like a deficiency in the feature.

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'm not sure if this is a strong enough technical reason, but my understanding is since STS is capped at 32 and IAM is capped at 64, we would have to add another STS template field in the AWS config to give users the ability to set custom templates for both. Otherwise if we expect the same template to work for both the cases, then that template would need to be capped at the minimum length cap and generate both STS and IAM usernames at 32 chars.

At the time of writing this feature the ticket's ask was for IAM, so I didn't add a separate STS custom username field, but should that also be in the scope here?

Copy link
Member

Choose a reason for hiding this comment

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

I didn't add a separate STS custom username field, but should that also be in the scope here?

We should be able to re-use the same username_template parameter for both (actually all three workflows). genUsername is already doing the heavy lifting under the hood. So, the behavior on what's the cap on a specific workflow can be conditionally determined by the userType that's provided to the func call, i.e. within their own specific switch case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having the template at the config level but the credential type at the role level does make this tricky. Off hand I see two options:

  1. Have a single template that takes in the credential type as an argument to the template. This would allow the template to choose the max length based on the credential type
  2. Specify one template for each credential type

I'm not sure I have a strong opinion either way. I don't really like either option all that much, but I also don't dislike either one. Either way we choose to go, I think we should consider other possible use cases similar to this and make sure we're consistent.

Copy link
Member

@calvn calvn Jul 29, 2021

Choose a reason for hiding this comment

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

Option 1 is an interesting take. I assume it takes advantage of conditional within the template based on that value, i.e. ... {{if eq .CredType "sts"}} ... {{else}} ... {{end}}?

I like it from an API cleanliness perspective, but it makes crafting a custom username_template that works for both a bit trickier.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, that's the general idea. It makes things simpler and more complicated at the same time because a user would need to know that a custom template needs to handle all three cases.

Copy link
Member

Choose a reason for hiding this comment

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

If we can craft our default to use this pattern and include an example in the docs, I don't mind going down this route. If user feedback is that it's too complex we can re-visit and add a separate param on the API endpoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@calvn @pcman312 I have added a template that generates usernames of differing lengths based on a conditional template and updated the tests till they all pass. Let me know what you think!

builtin/logical/aws/secret_access_keys.go Outdated Show resolved Hide resolved
changelog/12185.txt Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – vault July 28, 2021 20:28 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook July 28, 2021 20:28 Inactive
@vinay-gopalan vinay-gopalan requested a review from calvn July 28, 2021 20:29
@calvn calvn modified the milestones: 1.8.1, 1.8.2, 1.9 Jul 29, 2021
@vercel vercel bot temporarily deployed to Preview – vault August 6, 2021 21:40 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook August 6, 2021 21:40 Inactive
Copy link
Member

@calvn calvn left a comment

Choose a reason for hiding this comment

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

Looking good! Can we update the title of the PR to have a more accurate description of the latest changeset?

builtin/logical/aws/secret_access_keys_test.go Outdated Show resolved Hide resolved
builtin/logical/aws/path_config_root.go Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – vault-storybook August 6, 2021 22:15 Inactive
@vercel vercel bot temporarily deployed to Preview – vault August 6, 2021 22:15 Inactive
@vinay-gopalan vinay-gopalan changed the title Update genUsername to cap STS usernames at 64 chars Update genUsername to cap STS usernames at 32 chars Aug 6, 2021
return "", "", fmt.Errorf("failed to generate username: %w", err)
return "", fmt.Errorf("failed to generate username: %w", err)
}
// To prevent template from exceeding STS length limits
Copy link
Member

Choose a reason for hiding this comment

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

Nit (same above):

Suggested change
// To prevent template from exceeding STS length limits
// To prevent a custom template from exceeding STS length limits

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good call! Updated

@vercel vercel bot temporarily deployed to Preview – vault August 6, 2021 22:22 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook August 6, 2021 22:22 Inactive
@vinay-gopalan vinay-gopalan merged commit e14d32c into main Aug 9, 2021
@vinay-gopalan vinay-gopalan deleted the update-iam-template-tests branch August 9, 2021 16:40
jartek pushed a commit to jartek/vault that referenced this pull request Sep 11, 2021
* update genUsername to cap STS usernames at 64 chars

* add changelog

* refactor tests into t.Run block

* patch: remove warningExpected bool and include expected string

* patch: revert sts to cap at 32 chars and add assume_role case in genUsername

* update changelog

* update genUsername to return error if username generated exceeds length limits

* update changelog

* add conditional default username template to provide custom STS usernames

* update changelog

* include test for failing STS length case

* update comments for more clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants