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

fix(task): grant task-run execution role access to secrets based on tag #3256

Merged
merged 6 commits into from
Feb 14, 2022

Conversation

paragbhingre
Copy link
Contributor

@paragbhingre paragbhingre commented Feb 8, 2022

This PR fixes the issue #2641

Fixed the issue by adding policies to the execution role when --secrets are provided.

Tests -
Following manual tests have been taken into consideration

  1. When we send both --app and --env flags in copilot task run command then secrets are accessible only when copilot-application and copilot-environment tags are present with the correct values on the secret
  2. When we send both --app and --env flags in copilot task run command then secrets are inaccessible only when copilot-application and copilot-environment tags are NOT present on the secret or they have incorrect values

@paragbhingre paragbhingre requested a review from a team as a code owner February 8, 2022 19:45
@paragbhingre paragbhingre requested review from efekarakus and removed request for a team February 8, 2022 19:45
@amazon-ecs-cli-v2-pr-manager amazon-ecs-cli-v2-pr-manager requested review from huanjani and removed request for a team February 8, 2022 19:45
@paragbhingre paragbhingre changed the title Task run secrets fix(task): copilot task run with --secrets Feb 8, 2022
@Lou1415926 Lou1415926 changed the title fix(task): copilot task run with --secrets fix(task): grant task-run execution role access to secrets based on tag Feb 8, 2022
Comment on lines 114 to 116
"ssm:ResourceTag/copilot-application": !If [ HasAppAndEnv, !Ref App, !Ref "AWS::NoValue" ]
"ssm:ResourceTag/copilot-environment": !If [ HasAppAndEnv, !Ref Env, !Ref "AWS::NoValue" ]
"ssm:ResourceTag/copilot-task": !If [ HasAppAndEnv, !Ref "AWS::NoValue", !Ref TaskName ]
Copy link
Contributor

@efekarakus efekarakus Feb 9, 2022

Choose a reason for hiding this comment

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

I really like the changes with allowing access to the copilot-application and copilot-environment tags -- makes total sense to me. I am not sure though how obvious it's going to be when not using a copilot managed cluster, requiring the copilot-task tag for a one-time job seems a bit cumbersome.

What if we had an experience like this?

$ copilot task run --default --secrets hello=/hello,world=arn:aws:secretsmanager:us-west-2:111122223333:secret:aes128-1a2b3c
Looks like you're requesting ssm:GetParameters to the following SSM parameters:
* /hello 
and secretsmanager:GetSecretValue to the following Secrets Manager secrets:
* arn:aws:secretsmanager:us-west-2:111122223333:secret:aes128-1a2b3c

Do you grant permission to the ECS/Fargate agent for these secrets? (y/N)

Or if they'd like to skip the prompt they can pass a flag similar to what the CDK does with --require-approval never

$ copilot task run --default --secrets hello=/hello --require-approval never

Then we will augment the Task Execution role such that only the input resources are granted access.

Let me know what you think!

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you grant permission to the ECS/Fargate agent for these secrets? (y/N)

If they choose "N", do we just error out saying they can't use these secrets without granting access? And what are available options for --require-approval?

Copy link
Contributor

Choose a reason for hiding this comment

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

If they choose "N", do we just error out saying they can't use these secrets without granting access?

I assume we'll abort then, but we can look to what the CDK does as well.

And what are available options for --require-approval?

hmm good point for us none, maybe we can flip the flag --acknowledge-iam

Copy link
Contributor

@efekarakus efekarakus 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 to me! Just to validate we tested it with both secrets (ssm and secretsmanager) and it works right? 😂

Similarly, can we test as well with a secret that's not tagged to make sure it still cannot access it

@paragbhingre
Copy link
Contributor Author

Looks good to me! Just to validate we tested it with both secrets (ssm and secretsmanager) and it works right? 😂

Similarly, can we test as well with a secret that's not tagged to make sure it still cannot access it

Both the secrets (ssm and secretsmanager) and scenarios you mentioned are tested ✅

Comment on lines 46 to 49
HasApp:
!Not [!Equals [!Ref App, ""]]
HasEnv:
!Not [!Equals [!Ref Env, ""]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need hasapp and has env?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually we can remove this and put these conditions directly into HasAppAndEnv condition.

Comment on lines 114 to 130
Condition:
StringEquals:
"ssm:ResourceTag/copilot-application": !If [ HasAppAndEnv, !Ref App, !Ref "AWS::NoValue" ]
"ssm:ResourceTag/copilot-environment": !If [ HasAppAndEnv, !Ref Env, !Ref "AWS::NoValue" ]
Resource:
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: 'Allow'
Action:
- secretsmanager:GetSecretValue
Condition:
StringEquals:
"secretsmanager:ResourceTag/copilot-application": !If [ HasAppAndEnv, !Ref App, !Ref "AWS::NoValue" ]
"secretsmanager:ResourceTag/copilot-environment": !If [ HasAppAndEnv, !Ref Env, !Ref "AWS::NoValue" ]
Resource:
- !Sub arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:*
- !Ref AWS::NoValue
{{- end}}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is awesome!

Comment on lines 112 to 113
"ssm:ResourceTag/copilot-application": !If [ HasAppAndEnv, !Ref App, !Ref "AWS::NoValue" ]
"ssm:ResourceTag/copilot-environment": !If [ HasAppAndEnv, !Ref Env, !Ref "AWS::NoValue" ]
Copy link
Contributor

@Lou1415926 Lou1415926 Feb 10, 2022

Choose a reason for hiding this comment

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

Can we remove these !If conditionals now that we have line 101-102?

Comment on lines 121 to 122
"secretsmanager:ResourceTag/copilot-application": !If [ HasAppAndEnv, !Ref App, !Ref "AWS::NoValue" ]
"secretsmanager:ResourceTag/copilot-environment": !If [ HasAppAndEnv, !Ref Env, !Ref "AWS::NoValue" ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Samesies!

@mergify mergify bot merged commit dc74b80 into aws:mainline Feb 14, 2022
@paragbhingre paragbhingre deleted the taskRunSecrets branch January 26, 2023 06:43
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.

4 participants