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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/pkg/deploy/cloudformation/stack/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
taskEntryPointParamKey = "EntryPoint"
taskOSParamKey = "OS"
taskArchParamKey = "Arch"
taskAppParamKey = "App"
taskEnvParamKey = "Env"

taskLogRetentionInDays = "1"
)
Expand Down Expand Up @@ -115,6 +117,14 @@ func (t *taskStackConfig) Parameters() ([]*cloudformation.Parameter, error) {
ParameterKey: aws.String(taskArchParamKey),
ParameterValue: aws.String(t.Arch),
},
{
ParameterKey: aws.String(taskAppParamKey),
ParameterValue: aws.String(t.App),
},
{
ParameterKey: aws.String(taskEnvParamKey),
ParameterValue: aws.String(t.Env),
},
}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/deploy/cloudformation/stack/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ func TestTaskStackConfig_Parameters(t *testing.T) {
ParameterKey: aws.String(taskArchParamKey),
ParameterValue: aws.String(""),
},
{
ParameterKey: aws.String(taskAppParamKey),
ParameterValue: aws.String(""),
},
{
ParameterKey: aws.String(taskEnvParamKey),
ParameterValue: aws.String(""),
},
}

taskInput := deploy.CreateTaskResourcesInput{
Expand Down
29 changes: 28 additions & 1 deletion internal/pkg/template/templates/task/cf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Parameters:
Type: String
Arch:
Type: String
App:
Type: String
Env:
Type: String
Conditions:
# NOTE: Image cannot be pushed until the ECR repo is created, at which time ContainerImage would be "".
HasImage:
Expand All @@ -39,6 +43,13 @@ Conditions:
!Not [ !Equals [ !Join [ "", !Ref EntryPoint ], "" ] ]
HasCustomPlatform:
!Not [!Equals [!Ref OS, ""]]
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.

HasAppAndEnv: !And
- !Condition HasApp
- !Condition HasEnv
Resources:
TaskDefinition:
Metadata:
Expand Down Expand Up @@ -89,6 +100,22 @@ Resources:
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
{{- if .Secrets}}
Policies:
- PolicyName: 'PullSecrets'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- ssm:GetParameters
Condition:
StringEquals:
"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

Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
{{- end}}
DefaultTaskRole:
Metadata:
'aws:copilot:description': 'An IAM Role for the task to make AWS API calls on your behalf. Policies are required by ECS Exec'
Expand Down Expand Up @@ -154,4 +181,4 @@ Resources:
Outputs:
ECRRepo:
Description: ECR Repo used to store images of task.
Value: !GetAtt ECRRepo.Arn
Value: !GetAtt ECRRepo.Arn