Skip to content

Commit

Permalink
feat: all more options when creating the trust relationship (#525)
Browse files Browse the repository at this point in the history
* feat: allow github actions to attach more assume role policies

* more options
  • Loading branch information
jakeyheath authored Oct 30, 2023
1 parent a2d7c4a commit edfff23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 22 additions & 1 deletion aws-iam-role-github-action/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ locals {

// https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services#adding-the-identity-provider-to-aws
data "aws_iam_policy_document" "assume_role" {
dynamic "statement" {
for_each = var.authorized_aws_accounts

content {
sid = "AllowAssumeRoleFrom${statement.key}"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${statement.value}:root"]
}
actions = ["sts:AssumeRole", "sts:TagSession"]
effect = "Allow"
}
}
dynamic "statement" {
for_each = var.authorized_github_repos

content {
sid = "AllowGithubActionsToAssumeRole"
principals {
type = "Federated"
identifiers = [local.idp_arn]
Expand All @@ -30,12 +44,19 @@ data "aws_iam_policy_document" "assume_role" {
}
}

data "aws_iam_policy_document" "this" {
source_policy_documents = compact([
data.aws_iam_policy_document.assume_role.json,
var.additional_assume_role_policies_json,
])
}

resource "aws_iam_role" "role" {
name = var.role.name

tags = var.tags

assume_role_policy = data.aws_iam_policy_document.assume_role.json
assume_role_policy = data.aws_iam_policy_document.this.json
max_session_duration = 60 * 60 // 1 hour, not sure what max github action exec time is

# We have to force detach policies in order to recreate roles.
Expand Down
12 changes: 12 additions & 0 deletions aws-iam-role-github-action/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ variable "tags" {

description = "Standard tagging."
}

variable "authorized_aws_accounts" {
type = map(string)
description = "The map of authorized AWS accounts to assume the created role."
default = {}
}

variable "additional_assume_role_policies_json" {
type = string
description = "The JSON string of any other additional assume role policies to add to the Github Actions role"
default = ""
}

0 comments on commit edfff23

Please sign in to comment.