Skip to content

Commit

Permalink
fix: Updated conditions are not detected as a change (#6)
Browse files Browse the repository at this point in the history
* fix: Updated condiitons are not detected as a change

* clenaup
  • Loading branch information
npalm authored Mar 1, 2022
1 parent 4dff469 commit 97477a2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 51 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ The module creates a role with an assume role policy to check the OIDC claims fo
- `deny_pull_request`: Denies assuming the role for a pull request.
- `allow_all` : Allow GitHub Actions for any claim for the repository. Be careful, this allows forks as well to assume the role!


## Usages

In case there is not OpenID Connect provider already created in the AWS account, create one via the submodule.


```hcl
module "oidc_provider" {
source = "github.com/philips-labs/terraform-aws-github-oidc/?ref=<version>//modules/provider"
Expand All @@ -38,16 +36,26 @@ module "oidc_provider" {

Nest you ca pass the output the one or multiple instances of the module.

```
```hcl
module "oidc_repo_s3" {
source = "github.com/philips-labs/terraform-aws-github-oidc?ref=<version>"
openid_connect_provider_arn = module.oidc_provider. repo = var.repo_s3
role_name = "repo-s3"
# optional
# override default conditions
default_conditions = ["allow_main"]
# add extra conditions, will be merged with the default_conditions
conditions = [{
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:my-org/my-repo:pull_request"]
}]
}
```


## Examples

Check out the [example](examples/default/README.md) for a full example of using the module.
Expand Down
7 changes: 1 addition & 6 deletions examples/default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ module "oidc_provider" {

module "oidc_repo_s3" {
source = "../../"
depends_on = [
module.oidc_provider
]

openid_connect_provider_arn = module.oidc_provider.openid_connect_provider.arn
repo = var.repo_s3
Expand All @@ -15,9 +12,7 @@ module "oidc_repo_s3" {

module "oidc_repo_ecr" {
source = "../../"
depends_on = [
module.oidc_provider
]

openid_connect_provider_arn = module.oidc_provider.openid_connect_provider.arn
repo = var.repo_ecr
default_conditions = ["allow_environment"]
Expand Down
71 changes: 30 additions & 41 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ resource "random_string" "random" {
locals {
github_environments = (length(var.github_environments) > 0 && var.repo != null) ? [for e in var.github_environments : "repo:${var.repo}:environment:${e}"] : ["ensurethereisnotmatch"]
role_name = (var.repo != null && var.role_name != null) ? var.role_name : "${substr(replace(var.repo != null ? var.repo : "", "/", "-"), 0, 64 - 8)}-${random_string.random[0].id}"
variable_sub = "token.actions.githubusercontent.com:sub"
default_allow_main = contains(var.default_conditions, "allow_main") ? [{
test = "StringLike"
variable = local.variable_sub
values = ["repo:${var.repo}:ref:refs/heads/main"]
}] : []
default_allow_environment = contains(var.default_conditions, "allow_environment") ? [{
test = "StringLike"
variable = local.variable_sub
values = local.github_environments
}] : []
default_allow_all = contains(var.default_conditions, "allow_all") ? [{
test = "StringLike"
variable = local.variable_sub
values = ["repo:${var.repo}:*"]
}] : []
default_deny_pull_request = contains(var.default_conditions, "deny_pull_request") ? [{
test = "StringNotLike"
variable = local.variable_sub
values = ["repo:${var.repo}:pull_request"]
}] : []
conditions = setunion(local.default_allow_main, local.default_allow_environment, local.default_allow_all, local.default_deny_pull_request, var.conditions)
}
data "aws_iam_policy_document" "github_actions_assume_role_policy" {
Expand All @@ -30,47 +59,7 @@ data "aws_iam_policy_document" "github_actions_assume_role_policy" {
}

dynamic "condition" {
for_each = contains(var.default_conditions, "allow_main") ? ["create"] : []

content {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:${var.repo}:ref:refs/heads/main"]
}
}

dynamic "condition" {
for_each = contains(var.default_conditions, "allow_environment") ? ["create"] : []

content {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = local.github_environments
}
}

dynamic "condition" {
for_each = contains(var.default_conditions, "allow_all") ? ["create"] : []

content {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:${var.repo}:*"]
}
}

dynamic "condition" {
for_each = contains(var.default_conditions, "deny_pull_request") ? ["create"] : []

content {
test = "StringNotLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:${var.repo}:pull_request"]
}
}

dynamic "condition" {
for_each = toset(var.conditions)
for_each = local.conditions

content {
test = condition.value.test
Expand Down

0 comments on commit 97477a2

Please sign in to comment.