Skip to content
This repository has been archived by the owner on Jun 27, 2018. It is now read-only.

Commit

Permalink
Merge pull request #59 from TeliaSoneraNorge/multi-account-support-fo…
Browse files Browse the repository at this point in the history
…r-roles

Updated roles to support multiple thrusted accounts
  • Loading branch information
mikael-lindstrom authored May 28, 2018
2 parents 3916ef5 + e8eb76c commit 5f0f8d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
13 changes: 7 additions & 6 deletions iam/admin/role.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ variable "prefix" {
description = "Prefix added to the role name."
}

variable "trusted_account" {
description = "ID of the account which is trusted with access to assume this role."
variable "trusted_accounts" {
type = "list"
description = "List of IDs for the accounts which is trusted with access to assume this role."
}

variable "mfa_window" {
Expand All @@ -25,10 +26,10 @@ variable "users" {
module "role" {
source = "../role"

prefix = "${var.prefix}-admin"
trusted_account = "${var.trusted_account}"
mfa_window = "${var.mfa_window}"
users = "${var.users}"
prefix = "${var.prefix}-admin"
trusted_accounts = "${var.trusted_accounts}"
mfa_window = "${var.mfa_window}"
users = "${var.users}"
}

resource "aws_iam_role_policy_attachment" "admin" {
Expand Down
13 changes: 7 additions & 6 deletions iam/developer/role.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ variable "prefix" {
description = "Prefix added to the role name."
}

variable "trusted_account" {
description = "ID of the account which is trusted with access to assume this role."
variable "trusted_accounts" {
type = "list"
description = "List of IDs for the accounts which is trusted with access to assume this role."
}

variable "mfa_window" {
Expand All @@ -25,10 +26,10 @@ variable "users" {
module "role" {
source = "../role"

prefix = "${var.prefix}-developer"
trusted_account = "${var.trusted_account}"
mfa_window = "${var.mfa_window}"
users = "${var.users}"
prefix = "${var.prefix}-developer"
trusted_accounts = "${var.trusted_accounts}"
mfa_window = "${var.mfa_window}"
users = "${var.users}"
}

resource "aws_iam_role_policy" "protect_role" {
Expand Down
23 changes: 18 additions & 5 deletions iam/role/role.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ variable "prefix" {
description = "Prefix added to the role name."
}

variable "trusted_account" {
description = "ID of the account which is trusted with access to assume this role."
variable "trusted_accounts" {
type = "list"
description = "List of IDs for the accounts which is trusted with access to assume this role."
}

variable "mfa_window" {
Expand All @@ -24,11 +25,25 @@ variable "role_description" {
default = "Terraform created role"
}

locals {
trusted_accounts_size = "${length( var.trusted_accounts )}"
users_size = "${length( var.users )}"
arn_list_size = "${local.accounts_size * local.users_size}"
}

# ------------------------------------------------------------------------------
# Resources
# ------------------------------------------------------------------------------
data "aws_iam_account_alias" "current" {}

data "null_data_source" "users" {
count = "${local.arn_list_size}"

inputs = {
users = "${format("arn:aws:iam::%s:user/%s", var.trusted_accounts[count.index % local.accounts_size], var.users[count.index / local.accounts_size])}"
}
}

resource "aws_iam_role" "main" {
name = "${var.prefix}-role"
assume_role_policy = "${data.aws_iam_policy_document.assume.json}"
Expand All @@ -44,9 +59,7 @@ data "aws_iam_policy_document" "assume" {
principals {
type = "AWS"

identifiers = [
"${formatlist("arn:aws:iam::%s:user/%s", var.trusted_account, var.users)}",
]
identifiers = "${data.null_data_source.users.*.outputs.users}"
}

condition = {
Expand Down

0 comments on commit 5f0f8d2

Please sign in to comment.