|
| 1 | +locals { |
| 2 | + # KMS write actions |
| 3 | + kms_write_actions = [ |
| 4 | + "kms:CancelKeyDeletion", |
| 5 | + "kms:CreateAlias", |
| 6 | + "kms:CreateGrant", |
| 7 | + "kms:CreateKey", |
| 8 | + "kms:DeleteAlias", |
| 9 | + "kms:DeleteImportedKeyMaterial", |
| 10 | + "kms:DisableKey", |
| 11 | + "kms:DisableKeyRotation", |
| 12 | + "kms:EnableKey", |
| 13 | + "kms:EnableKeyRotation", |
| 14 | + "kms:Encrypt", |
| 15 | + "kms:GenerateDataKey", |
| 16 | + "kms:GenerateDataKeyWithoutPlaintext", |
| 17 | + "kms:GenerateRandom", |
| 18 | + "kms:GetKeyPolicy", |
| 19 | + "kms:GetKeyRotationStatus", |
| 20 | + "kms:GetParametersForImport", |
| 21 | + "kms:ImportKeyMaterial", |
| 22 | + "kms:PutKeyPolicy", |
| 23 | + "kms:ReEncryptFrom", |
| 24 | + "kms:ReEncryptTo", |
| 25 | + "kms:RetireGrant", |
| 26 | + "kms:RevokeGrant", |
| 27 | + "kms:ScheduleKeyDeletion", |
| 28 | + "kms:TagResource", |
| 29 | + "kms:UntagResource", |
| 30 | + "kms:UpdateAlias", |
| 31 | + "kms:UpdateKeyDescription", |
| 32 | + ] |
| 33 | + |
| 34 | + # KMS read actions |
| 35 | + kms_read_actions = [ |
| 36 | + "kms:Decrypt", |
| 37 | + "kms:DescribeKey", |
| 38 | + "kms:List*", |
| 39 | + ] |
| 40 | + |
| 41 | + # list of saml users for policies |
| 42 | + saml_user_ids = [ |
| 43 | + "${data.aws_caller_identity.current.user_id}", |
| 44 | + "${data.aws_caller_identity.current.account_id}", |
| 45 | + "${formatlist("%s:%s", data.aws_iam_role.saml_role_ssm.unique_id, var.secrets_saml_users)}", |
| 46 | + ] |
| 47 | + |
| 48 | + # list of role users and saml users for policies |
| 49 | + role_and_saml_ids = [ |
| 50 | + "${aws_iam_role.ecsTaskExecutionRole.unique_id}:*", |
| 51 | + "${local.saml_user_ids}", |
| 52 | + ] |
| 53 | + |
| 54 | + sm_arn = "arn:aws:secretsmanager:${var.region}:${data.aws_caller_identity.current.account_id}:secret:${var.app}-${var.environment}-??????" |
| 55 | +} |
| 56 | + |
| 57 | +# get the saml user info so we can get the unique_id |
| 58 | +data "aws_iam_role" "saml_role_ssm" { |
| 59 | + name = "${var.saml_role}" |
| 60 | +} |
| 61 | + |
| 62 | +# The users (email addresses) from the saml role to give access |
| 63 | +# case sensitive |
| 64 | +variable "secrets_saml_users" { |
| 65 | + type = "list" |
| 66 | +} |
| 67 | + |
| 68 | +# kms key used to encrypt ssm parameters |
| 69 | +resource "aws_kms_key" "ssm" { |
| 70 | + description = "ssm parameters key for ${var.app}-${var.environment}" |
| 71 | + deletion_window_in_days = 7 |
| 72 | + enable_key_rotation = true |
| 73 | + tags = "${var.tags}" |
| 74 | + policy = "${data.aws_iam_policy_document.ssm.json}" |
| 75 | +} |
| 76 | + |
| 77 | +resource "aws_kms_alias" "ssm" { |
| 78 | + name = "alias/${var.app}-${var.environment}" |
| 79 | + target_key_id = "${aws_kms_key.ssm.id}" |
| 80 | +} |
| 81 | + |
| 82 | +data "aws_iam_policy_document" "ssm" { |
| 83 | + statement { |
| 84 | + sid = "DenyWriteToAllExceptSAMLUsers" |
| 85 | + effect = "Deny" |
| 86 | + |
| 87 | + principals { |
| 88 | + type = "AWS" |
| 89 | + identifiers = ["*"] |
| 90 | + } |
| 91 | + |
| 92 | + actions = "${local.kms_write_actions}" |
| 93 | + resources = ["*"] |
| 94 | + |
| 95 | + condition { |
| 96 | + test = "StringNotLike" |
| 97 | + variable = "aws:userId" |
| 98 | + values = ["${local.saml_user_ids}"] |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + statement { |
| 103 | + sid = "DenyReadToAllExceptRoleAndSAMLUsers" |
| 104 | + effect = "Deny" |
| 105 | + |
| 106 | + principals { |
| 107 | + type = "AWS" |
| 108 | + identifiers = ["*"] |
| 109 | + } |
| 110 | + |
| 111 | + actions = "${local.kms_read_actions}" |
| 112 | + resources = ["*"] |
| 113 | + |
| 114 | + condition { |
| 115 | + test = "StringNotLike" |
| 116 | + variable = "aws:userId" |
| 117 | + values = ["${local.role_and_saml_ids}"] |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + statement { |
| 122 | + sid = "AllowWriteToSAMLUsers" |
| 123 | + effect = "Allow" |
| 124 | + |
| 125 | + principals { |
| 126 | + type = "AWS" |
| 127 | + identifiers = ["*"] |
| 128 | + } |
| 129 | + |
| 130 | + actions = "${local.kms_write_actions}" |
| 131 | + resources = ["*"] |
| 132 | + |
| 133 | + condition { |
| 134 | + test = "StringLike" |
| 135 | + variable = "aws:userId" |
| 136 | + values = ["${local.saml_user_ids}"] |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + statement { |
| 141 | + sid = "AllowReadRoleAndSAMLUsers" |
| 142 | + effect = "Allow" |
| 143 | + |
| 144 | + principals { |
| 145 | + type = "AWS" |
| 146 | + identifiers = ["*"] |
| 147 | + } |
| 148 | + |
| 149 | + actions = "${local.kms_read_actions}" |
| 150 | + resources = ["*"] |
| 151 | + |
| 152 | + condition { |
| 153 | + test = "StringLike" |
| 154 | + variable = "aws:userId" |
| 155 | + values = ["${local.role_and_saml_ids}"] |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +# allow ecs task execution role to read this app's parameters |
| 161 | +resource "aws_iam_policy" "ecsTaskExecutionRole_ssm" { |
| 162 | + name = "${var.app}-${var.environment}-ecs-ssm" |
| 163 | + path = "/" |
| 164 | + description = "allow ecs task execution role to read this app's parameters" |
| 165 | + |
| 166 | + policy = <<EOF |
| 167 | +{ |
| 168 | + "Version": "2012-10-17", |
| 169 | + "Statement": [ |
| 170 | + { |
| 171 | + "Effect": "Allow", |
| 172 | + "Action": [ |
| 173 | + "ssm:GetParameters" |
| 174 | + ], |
| 175 | + "Resource": "arn:aws:ssm:${var.region}:${data.aws_caller_identity.current.account_id}:parameter/${var.app}/${var.environment}/*" |
| 176 | + } |
| 177 | + ] |
| 178 | +} |
| 179 | +EOF |
| 180 | +} |
| 181 | + |
| 182 | +resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_ssm" { |
| 183 | + role = "${aws_iam_role.ecsTaskExecutionRole.name}" |
| 184 | + policy_arn = "${aws_iam_policy.ecsTaskExecutionRole_ssm.arn}" |
| 185 | +} |
| 186 | + |
| 187 | +output "ssm_add_secret" { |
| 188 | + value = "aws ssm put-parameter --overwrite --type \"SecureString\" --key-id \"${aws_kms_alias.ssm.name}\" --name \"/${var.app}/${var.environment}/PASSWORD\" --value \"password\"" |
| 189 | +} |
| 190 | + |
| 191 | +output "ssm_add_secret_ref" { |
| 192 | + value = "fargate service env set --secret PASSWORD=/${var.app}/${var.environment}/PASSWORD" |
| 193 | +} |
| 194 | + |
| 195 | +output "ssm_key_id" { |
| 196 | + value = "${aws_kms_key.ssm.key_id}" |
| 197 | +} |
0 commit comments