iam.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ecs
  14. import (
  15. "fmt"
  16. "github.com/awslabs/goformation/v4/cloudformation"
  17. )
  18. const (
  19. ecsTaskExecutionPolicy = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
  20. ecrReadOnlyPolicy = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
  21. ecsEC2InstanceRole = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
  22. actionGetSecretValue = "secretsmanager:GetSecretValue"
  23. actionGetParameters = "ssm:GetParameters"
  24. actionDecrypt = "kms:Decrypt"
  25. actionAutoScaling = "application-autoscaling:*"
  26. actionGetMetrics = "cloudwatch:GetMetricStatistics"
  27. actionDescribeService = "ecs:DescribeServices"
  28. actionUpdateService = "ecs:UpdateService"
  29. )
  30. var (
  31. ecsTaskAssumeRolePolicyDocument = policyDocument("ecs-tasks.amazonaws.com")
  32. ec2InstanceAssumeRolePolicyDocument = policyDocument("ec2.amazonaws.com")
  33. ausocalingAssumeRolePolicyDocument = policyDocument("application-autoscaling.amazonaws.com")
  34. )
  35. func policyDocument(service string) PolicyDocument {
  36. return PolicyDocument{
  37. Version: "2012-10-17", // https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html
  38. Statement: []PolicyStatement{
  39. {
  40. Effect: "Allow",
  41. Principal: PolicyPrincipal{
  42. Service: service,
  43. },
  44. Action: []string{"sts:AssumeRole"},
  45. },
  46. },
  47. }
  48. }
  49. func volumeMountPolicyDocument(volume string, filesystem string) PolicyDocument {
  50. ap := fmt.Sprintf("%sAccessPoint", normalizeResourceName(volume))
  51. return PolicyDocument{
  52. Version: "2012-10-17", // https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html
  53. Statement: []PolicyStatement{
  54. {
  55. Effect: "Allow",
  56. Resource: []string{
  57. filesystem,
  58. },
  59. Action: []string{
  60. "elasticfilesystem:ClientMount",
  61. "elasticfilesystem:ClientWrite",
  62. "elasticfilesystem:ClientRootAccess",
  63. },
  64. Condition: Condition{
  65. StringEquals: map[string]string{
  66. "elasticfilesystem:AccessPointArn": cloudformation.Ref(ap),
  67. },
  68. },
  69. },
  70. },
  71. }
  72. }
  73. // PolicyDocument describes an IAM policy document
  74. // could alternatively depend on https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/master/cmd/clusterawsadm/api/iam/v1alpha1/types.go
  75. type PolicyDocument struct {
  76. Version string `json:",omitempty"`
  77. Statement []PolicyStatement `json:",omitempty"`
  78. }
  79. // PolicyStatement describes an IAM policy statement
  80. type PolicyStatement struct {
  81. Effect string `json:",omitempty"`
  82. Action []string `json:",omitempty"`
  83. Principal PolicyPrincipal `json:",omitempty"`
  84. Resource []string `json:",omitempty"`
  85. Condition Condition `json:",omitempty"`
  86. }
  87. // PolicyPrincipal describes an IAM policy principal
  88. type PolicyPrincipal struct {
  89. Service string `json:",omitempty"`
  90. }
  91. // Condition is the map of all conditions in the statement entry.
  92. type Condition struct {
  93. StringEquals map[string]string `json:",omitempty"`
  94. Bool map[string]string `json:",omitempty"`
  95. }