Skip to content

Commit

Permalink
Merge pull request #10 from sixt/master
Browse files Browse the repository at this point in the history
add support for private repository credentials
  • Loading branch information
Kristian authored May 29, 2019
2 parents 6e4fae9 + 456c8d0 commit f9d7236
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ resource "aws_iam_role_policy" "task_execution" {
policy = "${data.aws_iam_policy_document.task_execution_permissions.json}"
}

resource "aws_iam_role_policy" "read_repository_credentials" {
count = "${length(var.repository_credentials) != 0 ? 1 : 0}"
name = "${var.name_prefix}-read-repository-credentials"
role = "${aws_iam_role.execution.id}"
policy = "${data.aws_iam_policy_document.read_repository_credentials.json}"
}

# ------------------------------------------------------------------------------
# IAM - Task role, basic. Users of the module will append policies to this role
# when they use the module. S3, Dynamo permissions etc etc.
Expand Down Expand Up @@ -106,6 +113,7 @@ resource "aws_ecs_task_definition" "task" {
[{
"name": "${var.name_prefix}",
"image": "${var.task_container_image}",
${local.repository_credentials_rendered}
"essential": true,
"portMappings": [
{
Expand Down
20 changes: 20 additions & 0 deletions policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ data "aws_iam_policy_document" "task_execution_permissions" {
]
}
}

data "aws_kms_key" "secretsmanager_key" {
key_id = "${var.repository_credentials_kms_key}"
}

data "aws_iam_policy_document" "read_repository_credentials" {
statement {
effect = "Allow"

resources = [
"${var.repository_credentials}",
"${data.aws_kms_key.secretsmanager_key.arn}",
]

actions = [
"secretsmanager:GetSecretValue",
"kms:Decrypt",
]
}
}
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,26 @@ variable "deployment_controller_type" {
type = "string"
description = "Type of deployment controller. Valid values: CODE_DEPLOY, ECS."
}

# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/private-auth.html
variable "repository_credentials" {
default = ""
description = "name or ARN of a secrets manager secret (arn:aws:secretsmanager:region:aws_account_id:secret:secret_name)"
}

variable "repository_credentials_kms_key" {
default = "alias/aws/secretsmanager"
description = "key id, key ARN, alias name or alias ARN of the key that encrypted the repository credentials"
}

locals {
# if the variable is set, create the fragment based on the variable value
# if not, just return a empty string to not mess up the json
repository_credentials_fragment = <<EOF
"repositoryCredentials": {
"credentialsParameter": "${var.repository_credentials}"
},
EOF

repository_credentials_rendered = "${var.repository_credentials == "" ? "" : local.repository_credentials_fragment}"
}

0 comments on commit f9d7236

Please sign in to comment.