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 all 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
37 changes: 36 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,9 @@ Conditions:
!Not [ !Equals [ !Join [ "", !Ref EntryPoint ], "" ] ]
HasCustomPlatform:
!Not [!Equals [!Ref OS, ""]]
HasAppAndEnv: !And
- !Not [!Equals [!Ref App, ""]]
- !Not [!Equals [!Ref Env, ""]]
Resources:
TaskDefinition:
Metadata:
Expand Down Expand Up @@ -89,6 +96,34 @@ Resources:
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
{{- if .Secrets}}
Policies:
- !If
- HasAppAndEnv
- PolicyName: 'PullSecrets'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- ssm:GetParameters
Condition:
StringEquals:
"ssm:ResourceTag/copilot-application": !Ref App
"ssm:ResourceTag/copilot-environment": !Ref Env
Resource:
- !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: 'Allow'
Action:
- secretsmanager:GetSecretValue
Condition:
StringEquals:
"secretsmanager:ResourceTag/copilot-application": !Ref App
"secretsmanager:ResourceTag/copilot-environment": !Ref Env
Resource:
- !Sub arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:*
- !Ref AWS::NoValue
{{- 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 +189,4 @@ Resources:
Outputs:
ECRRepo:
Description: ECR Repo used to store images of task.
Value: !GetAtt ECRRepo.Arn
Value: !GetAtt ECRRepo.Arn