-
-
Notifications
You must be signed in to change notification settings - Fork 127
terraform-iam: configure OIDC for NixOS/nix releases #879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zimbatm
wants to merge
1
commit into
main
Choose a base branch
from
nix-releases-oidc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
terraform-iam/assume_github_actions_policy_document/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| terraform { | ||
| required_providers { | ||
| aws = { | ||
| source = "hashicorp/aws" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| variable "subject_filter" { | ||
| type = list(string) | ||
| } | ||
|
|
||
| data "aws_caller_identity" "current" {} | ||
|
|
||
| data "aws_iam_openid_connect_provider" "github_actions" { | ||
| url = "https://token.actions.githubusercontent.com" | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "assume_github_actions" { | ||
|
|
||
| statement { | ||
| effect = "Allow" | ||
| actions = ["sts:AssumeRoleWithWebIdentity"] | ||
|
|
||
| principals { | ||
| type = "Federated" | ||
| identifiers = [data.aws_iam_openid_connect_provider.github_actions.arn] | ||
| } | ||
|
|
||
| condition { | ||
| test = "StringLike" | ||
| variable = "token.actions.githubusercontent.com:sub" | ||
| values = var.subject_filter | ||
| } | ||
| } | ||
| } | ||
|
|
||
| output "json" { | ||
| value = data.aws_iam_policy_document.assume_github_actions.json | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
terraform-iam/assume_identity_center_permission_policy/main.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| terraform { | ||
| required_providers { | ||
| aws = { | ||
| source = "hashicorp/aws" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| variable "target_account_id" { | ||
| description = "AWS account ID where the reserved SSO roles exist (the target account)." | ||
| type = string | ||
| } | ||
|
|
||
| variable "sso_region" { | ||
| description = "Region of the AWS IAM Identity Center instance." | ||
| type = string | ||
| default = "eu-north-1" | ||
| } | ||
|
|
||
| variable "permission_set_name" { | ||
| description = "Name of the IAM Identity Center permission set (without the AWSReservedSSO_ prefix)." | ||
| type = string | ||
| } | ||
|
|
||
| locals { | ||
| reserved_role_pattern = format( | ||
| "arn:aws:iam::%s:role/aws-reserved/sso.amazonaws.com/%s/AWSReservedSSO_%s_*", | ||
| var.target_account_id, | ||
| var.sso_region, | ||
| var.permission_set_name, | ||
| ) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "this" { | ||
| statement { | ||
| effect = "Allow" | ||
| actions = ["sts:AssumeRole"] | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = [format("arn:aws:iam::%s:root", var.target_account_id)] | ||
| } | ||
| condition { | ||
| test = "ArnLike" | ||
| variable = "aws:PrincipalArn" | ||
| values = [local.reserved_role_pattern] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| output "json" { | ||
| value = data.aws_iam_policy_document.this.json | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # In this document we configure OIDC from GitHub to allow automatically | ||
| # publishing NixOS/nix releases using GitHub Actions. | ||
| # | ||
| # This means that everyone with merge rights in the Nix repo can publish | ||
| # releases (with a public trail). | ||
|
|
||
| resource "aws_iam_openid_connect_provider" "github_actions" { | ||
| url = "https://token.actions.githubusercontent.com" | ||
|
|
||
| client_id_list = ["sts.amazonaws.com"] | ||
| thumbprint_list = [ | ||
| # https://github.com/aws-actions/configure-aws-credentials/issues/357#issuecomment-1626357333 | ||
| "6938fd4d98bab03faadb97b34396831e3780aea1", | ||
| ] | ||
| } | ||
|
|
||
| import { | ||
| to = aws_iam_openid_connect_provider.github_actions | ||
| id = format( | ||
| "arn:aws:iam::%s:oidc-provider/token.actions.githubusercontent.com", | ||
| data.aws_caller_identity.current.account_id, | ||
| ) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "nix_release" { | ||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:GetObject", | ||
| "s3:PutObject" | ||
| ] | ||
| # Only allow uploading in the /nix/ prefix in the bucket | ||
| resources = ["arn:aws:s3:::nix-releases/nix/*"] | ||
| } | ||
|
|
||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:ListBucket", | ||
| "s3:ListBucketMultipartUploads" | ||
| ] | ||
| resources = ["arn:aws:s3:::nix-releases"] | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "s3:prefix" | ||
| values = ["nix/*"] | ||
| } | ||
| } | ||
|
|
||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:PutObject" | ||
| ] | ||
| # The release also publishes the install script when it's the latest | ||
| # release. | ||
| resources = ["arn:aws:s3:::nix-channels/nix-latest/install"] | ||
| } | ||
|
|
||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:ListBucket" | ||
| ] | ||
| resources = ["arn:aws:s3:::nix-channels"] | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "s3:prefix" | ||
| values = ["nix-latest/*"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "nix_release" { | ||
| name = "nix-release" | ||
| policy = data.aws_iam_policy_document.nix_release.json | ||
| } | ||
|
|
||
| data "aws_caller_identity" "current" {} | ||
|
|
||
| module "assume_nix_releases_permission" { | ||
| source = "./assume_identity_center_permission_policy" | ||
| target_account_id = data.aws_caller_identity.current.account_id | ||
| permission_set_name = "NixReleases" | ||
| sso_region = "eu-north-1" | ||
| } | ||
|
|
||
| module "assume_nix_release" { | ||
| source = "./assume_github_actions_policy_document" | ||
|
|
||
| # Only allow to assume this role in the NixOS/nix repo, and while running | ||
| # in the "releases" environment. | ||
| subject_filter = ["repo:NixOS/nix:environment:releases"] | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "assume_nix_release" { | ||
| source_policy_documents = [ | ||
| module.assume_nix_releases_permission.json, | ||
| module.assume_nix_release.json, | ||
| ] | ||
| } | ||
|
|
||
| resource "aws_iam_role" "nix_release" { | ||
| name = "nix-release" | ||
| assume_role_policy = data.aws_iam_policy_document.assume_nix_release.json | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "nix_release_managed_policy" { | ||
| role = aws_iam_role.nix_release.name | ||
| policy_arn = aws_iam_policy.nix_release.arn | ||
| } | ||
|
|
||
| output "nix_release_role_arn" { | ||
| value = aws_iam_role.nix_release.arn | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.