2020-08-18 11:38:23 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-18 11:38:23 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-17 17:48:52 +02:00
|
|
|
package ecs
|
2020-05-04 15:09:08 +02:00
|
|
|
|
2020-10-19 09:48:23 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/awslabs/goformation/v4/cloudformation"
|
|
|
|
)
|
|
|
|
|
2020-05-12 15:22:17 +02:00
|
|
|
const (
|
2020-08-18 16:56:42 +02:00
|
|
|
ecsTaskExecutionPolicy = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
|
|
|
|
ecrReadOnlyPolicy = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
|
2020-09-07 11:20:41 +02:00
|
|
|
ecsEC2InstanceRole = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
|
2020-05-12 15:22:17 +02:00
|
|
|
|
2020-09-30 14:35:07 +02:00
|
|
|
actionGetSecretValue = "secretsmanager:GetSecretValue"
|
|
|
|
actionGetParameters = "ssm:GetParameters"
|
|
|
|
actionDecrypt = "kms:Decrypt"
|
|
|
|
actionAutoScaling = "application-autoscaling:*"
|
|
|
|
actionGetMetrics = "cloudwatch:GetMetricStatistics"
|
|
|
|
actionDescribeService = "ecs:DescribeServices"
|
|
|
|
actionUpdateService = "ecs:UpdateService"
|
2020-05-12 15:22:17 +02:00
|
|
|
)
|
2020-05-04 15:15:22 +02:00
|
|
|
|
2020-09-30 14:35:07 +02:00
|
|
|
var (
|
|
|
|
ecsTaskAssumeRolePolicyDocument = policyDocument("ecs-tasks.amazonaws.com")
|
|
|
|
ec2InstanceAssumeRolePolicyDocument = policyDocument("ec2.amazonaws.com")
|
|
|
|
ausocalingAssumeRolePolicyDocument = policyDocument("application-autoscaling.amazonaws.com")
|
|
|
|
)
|
2020-05-04 15:09:08 +02:00
|
|
|
|
2020-09-30 14:35:07 +02:00
|
|
|
func policyDocument(service string) PolicyDocument {
|
|
|
|
return PolicyDocument{
|
|
|
|
Version: "2012-10-17", // https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html
|
|
|
|
Statement: []PolicyStatement{
|
|
|
|
{
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: PolicyPrincipal{
|
|
|
|
Service: service,
|
|
|
|
},
|
|
|
|
Action: []string{"sts:AssumeRole"},
|
2020-09-07 11:20:41 +02:00
|
|
|
},
|
|
|
|
},
|
2020-09-30 14:35:07 +02:00
|
|
|
}
|
2020-10-19 09:48:23 +02:00
|
|
|
}
|
2020-09-30 14:35:07 +02:00
|
|
|
|
2020-10-19 09:48:23 +02:00
|
|
|
func volumeMountPolicyDocument(volume string, filesystem string) PolicyDocument {
|
|
|
|
ap := fmt.Sprintf("%sAccessPoint", normalizeResourceName(volume))
|
|
|
|
return PolicyDocument{
|
|
|
|
Version: "2012-10-17", // https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html
|
|
|
|
Statement: []PolicyStatement{
|
|
|
|
{
|
|
|
|
Effect: "Allow",
|
|
|
|
Resource: []string{
|
|
|
|
filesystem,
|
|
|
|
},
|
|
|
|
Action: []string{
|
|
|
|
"elasticfilesystem:ClientMount",
|
|
|
|
"elasticfilesystem:ClientWrite",
|
|
|
|
"elasticfilesystem:ClientRootAccess",
|
|
|
|
},
|
|
|
|
Condition: Condition{
|
|
|
|
StringEquals: map[string]string{
|
|
|
|
"elasticfilesystem:AccessPointArn": cloudformation.Ref(ap),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-07 11:20:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
// PolicyDocument describes an IAM policy document
|
2020-08-13 15:43:24 +02:00
|
|
|
// could alternatively depend on https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/master/cmd/clusterawsadm/api/iam/v1alpha1/types.go
|
2020-05-04 15:09:08 +02:00
|
|
|
type PolicyDocument struct {
|
|
|
|
Version string `json:",omitempty"`
|
|
|
|
Statement []PolicyStatement `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
// PolicyStatement describes an IAM policy statement
|
2020-05-04 15:09:08 +02:00
|
|
|
type PolicyStatement struct {
|
|
|
|
Effect string `json:",omitempty"`
|
|
|
|
Action []string `json:",omitempty"`
|
|
|
|
Principal PolicyPrincipal `json:",omitempty"`
|
|
|
|
Resource []string `json:",omitempty"`
|
2020-10-19 09:48:23 +02:00
|
|
|
Condition Condition `json:",omitempty"`
|
2020-05-04 15:09:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
// PolicyPrincipal describes an IAM policy principal
|
2020-05-04 15:09:08 +02:00
|
|
|
type PolicyPrincipal struct {
|
|
|
|
Service string `json:",omitempty"`
|
|
|
|
}
|
2020-10-19 09:48:23 +02:00
|
|
|
|
|
|
|
// Condition is the map of all conditions in the statement entry.
|
|
|
|
type Condition struct {
|
|
|
|
StringEquals map[string]string `json:",omitempty"`
|
|
|
|
Bool map[string]string `json:",omitempty"`
|
|
|
|
}
|