Skip to content

Commit

Permalink
Cross account role (#15)
Browse files Browse the repository at this point in the history
* Add ability to assume role

* Improve example

* Fix github actions system path
  • Loading branch information
dojci authored Nov 26, 2020
1 parent 2acda21 commit 3221fa4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- shell: bash
name: "SETUP: Go path"
run: echo '::add-path::~/go/bin/'
run: echo '~/go/bin/' >> $GITHUB_PATH

- uses: actions/checkout@v1
name: Checkout source code
Expand Down Expand Up @@ -84,8 +84,7 @@ jobs:
- shell: bash
name: "SETUP: TFLint path"
run: |
echo '::add-path::~/tflint/bin/'
run: echo '~/tflint/bin/' >> $GITHUB_PATH

- uses: pre-commit/action@v2.0.0
name: "RUN: pre-commit"
Expand Down
6 changes: 6 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ module "extenral_dns" {
# "extraEnv[2].valueFrom.secretKeyRef.name" = "existing-secret"
# "extraEnv[2].valueFrom.secretKeyRef.key" = "varname3-key"

# domainFilters:
# - foo.com
# - bar.com
"domainFilters[0]" = "foo.com"
"domainFilters[1]" = "bar.com"

}

}
51 changes: 41 additions & 10 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# aws.assumeRoleArn

locals {
assume_role = length(try(var.settings["aws.assumeRoleArn"], "")) > 0 ? true : false
}

resource "kubernetes_namespace" "external_dns" {
depends_on = [var.mod_dependency]
count = (var.enabled && var.k8s_create_namespace && var.k8s_namespace != "kube-system") ? 1 : 0
Expand All @@ -10,7 +16,7 @@ resource "kubernetes_namespace" "external_dns" {
### iam ###
# Policy
data "aws_iam_policy_document" "external_dns" {
count = var.enabled ? 1 : 0
count = var.enabled && ! local.assume_role ? 1 : 0

statement {
sid = "ChangeResourceRecordSets"
Expand Down Expand Up @@ -41,18 +47,39 @@ data "aws_iam_policy_document" "external_dns" {
}
}

data "aws_iam_policy_document" "external_dns_assume" {
count = var.enabled && local.assume_role ? 1 : 0

statement {
sid = "AllowAssumeExternalDNSRole"

effect = "Allow"

actions = [
"sts:AssumeRole"
]

resources = [
var.settings["aws.assumeRoleArn"]
]
}
}


resource "aws_iam_policy" "external_dns" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = var.enabled ? 1 : 0

name = "${var.cluster_name}-external-dns"
path = "/"
description = "Policy for external-dns service"

policy = data.aws_iam_policy_document.external_dns[0].json
policy = local.assume_role ? data.aws_iam_policy_document.external_dns_assume[0].json : data.aws_iam_policy_document.external_dns[0].json

depends_on = [var.mod_dependency]
}

# Role
data "aws_iam_policy_document" "external_dns_assume" {
data "aws_iam_policy_document" "external_dns_irsa" {
count = var.enabled ? 1 : 0

statement {
Expand All @@ -77,15 +104,19 @@ data "aws_iam_policy_document" "external_dns_assume" {
}

resource "aws_iam_role" "external_dns" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = var.enabled ? 1 : 0

name = "${var.cluster_name}-external-dns"
assume_role_policy = data.aws_iam_policy_document.external_dns_assume[0].json
assume_role_policy = data.aws_iam_policy_document.external_dns_irsa[0].json

depends_on = [var.mod_dependency]
}

resource "aws_iam_role_policy_attachment" "external_dns" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = var.enabled ? 1 : 0

role = aws_iam_role.external_dns[0].name
policy_arn = aws_iam_policy.external_dns[0].arn

depends_on = [var.mod_dependency]
}

0 comments on commit 3221fa4

Please sign in to comment.