From b20193ed40241ab6f0d93c7dd00814c9e4d79efd Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 22:26:23 +1100 Subject: [PATCH 1/6] Add GitHub Actions JWT auth mount for IUCLabs --- README.md | 17 ++++++ .../auth_jwt_backend/jwt/github/iuclabs.yaml | 6 ++ config/config.hcl | 13 ++++ environments/au/syd1/terragrunt.hcl | 2 + modules/vault_cluster/main.tf | 32 ++++++++++ .../modules/auth_jwt_backend/main.tf | 13 ++++ .../modules/auth_jwt_backend/terraform.tf | 9 +++ .../modules/auth_jwt_backend/variables.tf | 42 +++++++++++++ .../modules/auth_jwt_role/main.tf | 12 ++++ .../modules/auth_jwt_role/terraform.tf | 9 +++ .../modules/auth_jwt_role/variables.tf | 61 +++++++++++++++++++ modules/vault_cluster/variables.tf | 28 +++++++++ tests/test_auth_jwt_role.py | 51 ++++++++++++++++ 13 files changed, 295 insertions(+) create mode 100644 config/auth_jwt_backend/jwt/github/iuclabs.yaml create mode 100644 modules/vault_cluster/modules/auth_jwt_backend/main.tf create mode 100644 modules/vault_cluster/modules/auth_jwt_backend/terraform.tf create mode 100644 modules/vault_cluster/modules/auth_jwt_backend/variables.tf create mode 100644 modules/vault_cluster/modules/auth_jwt_role/main.tf create mode 100644 modules/vault_cluster/modules/auth_jwt_role/terraform.tf create mode 100644 modules/vault_cluster/modules/auth_jwt_role/variables.tf create mode 100644 tests/test_auth_jwt_role.py diff --git a/README.md b/README.md index 20f70ed..45d1a1b 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,20 @@ You can check the Consul KV store by accessing the Consul UI or using the consul ```bash consul kv get terraform/state ``` + +# GitHub Actions JWT roles + +`auth/jwt/github/iuclabs` trusts GitHub Actions OIDC tokens from the IUCLabs org. Add one role per consumer: + +```yaml +# config/auth_jwt_role/jwt/github/iuclabs/.yaml +bound_audiences: ["https://github.com/IUCLabs"] +bound_claims: + repository: IUCLabs/ + ref: refs/heads/main # or environment: +bound_claims_type: string # or glob +token_ttl: 900 +token_max_ttl: 3600 +``` + +`bound_claims` must set `repository` plus `ref` or `environment`, so pull_request runs (`refs/pull/N/merge`) never get a token. Grant policies via the policy yaml `auth:` map under `jwt/github/iuclabs: []`. diff --git a/config/auth_jwt_backend/jwt/github/iuclabs.yaml b/config/auth_jwt_backend/jwt/github/iuclabs.yaml new file mode 100644 index 0000000..c7d96cc --- /dev/null +++ b/config/auth_jwt_backend/jwt/github/iuclabs.yaml @@ -0,0 +1,6 @@ +# GitHub Actions OIDC for the IUCLabs org. Roles live in +# config/auth_jwt_role/jwt/github/iuclabs/ and must pin repository + ref/environment. +--- +oidc_discovery_url: "https://token.actions.githubusercontent.com" +bound_issuer: "https://token.actions.githubusercontent.com" +description: "GitHub Actions OIDC - IUCLabs" diff --git a/config/config.hcl b/config/config.hcl index ccfaadc..0022888 100644 --- a/config/config.hcl +++ b/config/config.hcl @@ -118,6 +118,19 @@ locals { }) if startswith(file_path, "auth_oidc_group/") } + auth_jwt_backend = { + for file_path, content in local.all_configs : + trimsuffix(replace(file_path, "auth_jwt_backend/", ""), ".yaml") => content + if startswith(file_path, "auth_jwt_backend/") + } + auth_jwt_role = { + for file_path, content in local.all_configs : + trimsuffix(replace(file_path, "auth_jwt_role/", ""), ".yaml") => merge(content, { + role_name = trimsuffix(basename(file_path), ".yaml") + backend = dirname(replace(file_path, "auth_jwt_role/", "")) + }) + if startswith(file_path, "auth_jwt_role/") + } auth_kubernetes_backend = { for file_path, content in local.all_configs : trimsuffix(replace(file_path, "auth_kubernetes_backend/", ""), ".yaml") => content diff --git a/environments/au/syd1/terragrunt.hcl b/environments/au/syd1/terragrunt.hcl index 326d6f5..65d4f35 100644 --- a/environments/au/syd1/terragrunt.hcl +++ b/environments/au/syd1/terragrunt.hcl @@ -63,6 +63,8 @@ inputs = { auth_oidc_backend = local.config.auth_oidc_backend auth_oidc_role = local.config.auth_oidc_role auth_oidc_group = local.config.auth_oidc_group + auth_jwt_backend = local.config.auth_jwt_backend + auth_jwt_role = local.config.auth_jwt_role auth_kubernetes_backend = local.config.auth_kubernetes_backend auth_kubernetes_role = local.config.auth_kubernetes_role kv_secret_backend = local.config.kv_secret_backend diff --git a/modules/vault_cluster/main.tf b/modules/vault_cluster/main.tf index 9ebe451..3b8311d 100644 --- a/modules/vault_cluster/main.tf +++ b/modules/vault_cluster/main.tf @@ -113,6 +113,38 @@ module "auth_oidc_group" { depends_on = [module.auth_oidc_backend] } +module "auth_jwt_backend" { + source = "./modules/auth_jwt_backend" + + for_each = var.auth_jwt_backend + + path = each.key + description = each.value.description + oidc_discovery_url = each.value.oidc_discovery_url + bound_issuer = each.value.bound_issuer + listing_visibility = each.value.listing_visibility + default_lease_ttl = each.value.default_lease_ttl + max_lease_ttl = each.value.max_lease_ttl +} + +module "auth_jwt_role" { + source = "./modules/auth_jwt_role" + + for_each = var.auth_jwt_role + + backend = each.value.backend + role_name = each.value.role_name + user_claim = each.value.user_claim + bound_audiences = each.value.bound_audiences + bound_claims = each.value.bound_claims + bound_claims_type = each.value.bound_claims_type + token_ttl = each.value.token_ttl + token_max_ttl = each.value.token_max_ttl + token_policies = var.policy_auth_map[each.value.backend][each.value.role_name] + + depends_on = [module.auth_jwt_backend] +} + module "auth_kubernetes_backend" { source = "./modules/auth_kubernetes_backend" diff --git a/modules/vault_cluster/modules/auth_jwt_backend/main.tf b/modules/vault_cluster/modules/auth_jwt_backend/main.tf new file mode 100644 index 0000000..b912e39 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_backend/main.tf @@ -0,0 +1,13 @@ +resource "vault_jwt_auth_backend" "jwt" { + path = var.path + type = "jwt" + description = var.description + oidc_discovery_url = var.oidc_discovery_url + bound_issuer = var.bound_issuer + + tune { + default_lease_ttl = var.default_lease_ttl + max_lease_ttl = var.max_lease_ttl + listing_visibility = var.listing_visibility + } +} diff --git a/modules/vault_cluster/modules/auth_jwt_backend/terraform.tf b/modules/vault_cluster/modules/auth_jwt_backend/terraform.tf new file mode 100644 index 0000000..ce22437 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_backend/terraform.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.10" + required_providers { + vault = { + source = "hashicorp/vault" + version = "5.6.0" + } + } +} diff --git a/modules/vault_cluster/modules/auth_jwt_backend/variables.tf b/modules/vault_cluster/modules/auth_jwt_backend/variables.tf new file mode 100644 index 0000000..aadab25 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_backend/variables.tf @@ -0,0 +1,42 @@ +variable "path" { + description = "Mount path of the JWT auth backend" + type = string +} + +variable "description" { + description = "Human-readable description of the auth mount" + type = string + default = null +} + +variable "oidc_discovery_url" { + description = "OIDC discovery URL used to fetch the issuer's signing keys" + type = string +} + +variable "bound_issuer" { + description = "Value the iss claim of every presented token must match" + type = string +} + +variable "listing_visibility" { + description = "Specifies whether to show this mount in the UI-specific listing endpoint. Valid values are 'unauth' or 'hidden'" + type = string + default = null + validation { + condition = var.listing_visibility == null || contains(["unauth", "hidden"], var.listing_visibility) + error_message = "listing_visibility must be either 'unauth' or 'hidden'." + } +} + +variable "default_lease_ttl" { + description = "Specifies the default time-to-live. If set, this overrides the global default. Must be a valid duration string" + type = string + default = null +} + +variable "max_lease_ttl" { + description = "Specifies the maximum time-to-live. If set, this overrides the global default. Must be a valid duration string" + type = string + default = null +} diff --git a/modules/vault_cluster/modules/auth_jwt_role/main.tf b/modules/vault_cluster/modules/auth_jwt_role/main.tf new file mode 100644 index 0000000..5b328a5 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_role/main.tf @@ -0,0 +1,12 @@ +resource "vault_jwt_auth_backend_role" "role" { + backend = var.backend + role_name = var.role_name + role_type = "jwt" + user_claim = var.user_claim + bound_audiences = var.bound_audiences + bound_claims = var.bound_claims + bound_claims_type = var.bound_claims_type + token_ttl = var.token_ttl + token_max_ttl = var.token_max_ttl + token_policies = var.token_policies +} diff --git a/modules/vault_cluster/modules/auth_jwt_role/terraform.tf b/modules/vault_cluster/modules/auth_jwt_role/terraform.tf new file mode 100644 index 0000000..ce22437 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_role/terraform.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.10" + required_providers { + vault = { + source = "hashicorp/vault" + version = "5.6.0" + } + } +} diff --git a/modules/vault_cluster/modules/auth_jwt_role/variables.tf b/modules/vault_cluster/modules/auth_jwt_role/variables.tf new file mode 100644 index 0000000..7d124eb --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_role/variables.tf @@ -0,0 +1,61 @@ +variable "backend" { + description = "The unique path of the JWT auth backend to configure" + type = string +} + +variable "role_name" { + description = "The name of the role" + type = string +} + +variable "user_claim" { + description = "Claim used as the entity alias name" + type = string + default = "job_workflow_ref" +} + +variable "bound_audiences" { + description = "List of audiences (aud claim) accepted in the token" + type = list(string) + validation { + condition = length(var.bound_audiences) > 0 + error_message = "bound_audiences must not be empty." + } +} + +variable "bound_claims" { + description = "Claims the token must carry; must pin repository plus ref or environment" + type = map(string) + validation { + condition = contains(keys(var.bound_claims), "repository") && (contains(keys(var.bound_claims), "ref") || contains(keys(var.bound_claims), "environment")) + error_message = "bound_claims must set repository and at least one of ref or environment." + } +} + +variable "bound_claims_type" { + description = "How bound_claims values are matched: 'string' or 'glob'" + type = string + default = "string" + validation { + condition = contains(["string", "glob"], var.bound_claims_type) + error_message = "bound_claims_type must be either 'string' or 'glob'." + } +} + +variable "token_ttl" { + description = "The TTL period of tokens issued using this role, in seconds" + type = number + default = 900 +} + +variable "token_max_ttl" { + description = "The maximum lifetime for generated tokens in number of seconds" + type = number + default = 3600 +} + +variable "token_policies" { + description = "List of policies to assign to the role (passed from policy_auth_map)" + type = list(string) + default = [] +} diff --git a/modules/vault_cluster/variables.tf b/modules/vault_cluster/variables.tf index cdcee55..dcd4c68 100644 --- a/modules/vault_cluster/variables.tf +++ b/modules/vault_cluster/variables.tf @@ -102,6 +102,34 @@ variable "auth_oidc_group" { default = {} } +variable "auth_jwt_backend" { + description = "Map of JWT auth backends (machine OIDC token login) to create" + type = map(object({ + oidc_discovery_url = string + bound_issuer = string + description = optional(string) + listing_visibility = optional(string) + default_lease_ttl = optional(string) + max_lease_ttl = optional(string) + })) + default = {} +} + +variable "auth_jwt_role" { + description = "Map of JWT auth roles to create" + type = map(object({ + role_name = string + backend = string + bound_audiences = list(string) + bound_claims = map(string) + bound_claims_type = optional(string, "string") + user_claim = optional(string, "job_workflow_ref") + token_ttl = optional(number, 900) + token_max_ttl = optional(number, 3600) + })) + default = {} +} + variable "auth_kubernetes_backend" { description = "Map of Kubernetes auth backends to create" type = map(object({ diff --git a/tests/test_auth_jwt_role.py b/tests/test_auth_jwt_role.py new file mode 100644 index 0000000..f6ae098 --- /dev/null +++ b/tests/test_auth_jwt_role.py @@ -0,0 +1,51 @@ +"""Validate GitHub Actions JWT role definitions under config/auth_jwt_role/.""" +import unittest +from pathlib import Path + +import yaml + +REPO_ROOT = Path(__file__).resolve().parent.parent + + +def claim_problem(role): + """Why a role's bound_claims are too loose to hand out a token, or None.""" + claims = role.get("bound_claims") or {} + if "repository" not in claims: + return "bound_claims must set repository" + if "ref" not in claims and "environment" not in claims: + return "bound_claims must set ref or environment" + return None + + +class RepoTests(unittest.TestCase): + def test_repo_roles(self): + for f in sorted(REPO_ROOT.glob("config/auth_jwt_role/**/*.yaml")): + with self.subTest(path=f.relative_to(REPO_ROOT).as_posix()): + self.assertIsNone(claim_problem(yaml.safe_load(f.read_text()) or {})) + + +class ClaimProblemTests(unittest.TestCase): + def test_repository_and_ref(self): + self.assertIsNone(claim_problem({"bound_claims": {"repository": "IUCLabs/x", "ref": "refs/heads/main"}})) + + def test_repository_and_environment(self): + self.assertIsNone(claim_problem({"bound_claims": {"repository": "IUCLabs/x", "environment": "prod"}})) + + def test_missing_repository(self): + self.assertEqual( + claim_problem({"bound_claims": {"ref": "refs/heads/main"}}), + "bound_claims must set repository", + ) + + def test_repository_only(self): + self.assertEqual( + claim_problem({"bound_claims": {"repository": "IUCLabs/x"}}), + "bound_claims must set ref or environment", + ) + + def test_no_bound_claims(self): + self.assertEqual(claim_problem({}), "bound_claims must set repository") + + +if __name__ == "__main__": + unittest.main() -- 2.47.3 From 7e17e51a180b574afad0625da568e540efd980dc Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 22:36:34 +1100 Subject: [PATCH 2/6] Require exact repository, ref and event_name on JWT roles --- .pre-commit-config.yaml | 6 + Makefile | 1 + README.md | 9 +- modules/vault_cluster/main.tf | 17 ++- .../modules/auth_jwt_role/main.tf | 2 +- .../tests/bound_claims.tftest.hcl | 112 ++++++++++++++++++ .../modules/auth_jwt_role/variables.tf | 24 ++-- modules/vault_cluster/variables.tf | 15 ++- tests/test_auth_jwt_role.py | 51 -------- 9 files changed, 154 insertions(+), 83 deletions(-) create mode 100644 modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl delete mode 100644 tests/test_auth_jwt_role.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 03a38a4..e52510e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,3 +31,9 @@ repos: additional_dependencies: [pyyaml, pydantic] pass_filenames: false always_run: true + - id: tofu-module-tests + name: tofu module tests pass + entry: sh -c 'cd modules/vault_cluster/modules/auth_jwt_role && tofu init -backend=false -input=false >/dev/null && tofu test' + language: system + pass_filenames: false + always_run: true diff --git a/Makefile b/Makefile index da57e04..6d03509 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ apply: init test: @uv run --with pyyaml --with pydantic python -m unittest discover -s tests -t . + @cd modules/vault_cluster/modules/auth_jwt_role && tofu init -backend=false -input=false >/dev/null && tofu test format: @echo "Formatting OpenTofu files..." diff --git a/README.md b/README.md index 45d1a1b..e75d2ef 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,13 @@ consul kv get terraform/state bound_audiences: ["https://github.com/IUCLabs"] bound_claims: repository: IUCLabs/ - ref: refs/heads/main # or environment: -bound_claims_type: string # or glob + ref: refs/heads/main + event_name: push,workflow_dispatch + environment: # optional token_ttl: 900 token_max_ttl: 3600 ``` -`bound_claims` must set `repository` plus `ref` or `environment`, so pull_request runs (`refs/pull/N/merge`) never get a token. Grant policies via the policy yaml `auth:` map under `jwt/github/iuclabs: []`. +`bound_claims` must set an exact `repository` (`IUCLabs/`), exact `ref` (`refs/heads/*` or `refs/tags/*`) and `event_name` from `push`, `workflow_dispatch`, `schedule`, `release`, `merge_group`; `environment` is optional. Values are matched exactly (comma-separated = list); globs and pull_request/workflow_run events are rejected at plan. + +Each role needs a policy yaml granting it under `auth: {jwt/github/iuclabs: []}`, or plan fails with "Invalid index". diff --git a/modules/vault_cluster/main.tf b/modules/vault_cluster/main.tf index 3b8311d..ee34f11 100644 --- a/modules/vault_cluster/main.tf +++ b/modules/vault_cluster/main.tf @@ -132,15 +132,14 @@ module "auth_jwt_role" { for_each = var.auth_jwt_role - backend = each.value.backend - role_name = each.value.role_name - user_claim = each.value.user_claim - bound_audiences = each.value.bound_audiences - bound_claims = each.value.bound_claims - bound_claims_type = each.value.bound_claims_type - token_ttl = each.value.token_ttl - token_max_ttl = each.value.token_max_ttl - token_policies = var.policy_auth_map[each.value.backend][each.value.role_name] + backend = each.value.backend + role_name = each.value.role_name + user_claim = each.value.user_claim + bound_audiences = each.value.bound_audiences + bound_claims = each.value.bound_claims + token_ttl = each.value.token_ttl + token_max_ttl = each.value.token_max_ttl + token_policies = var.policy_auth_map[each.value.backend][each.value.role_name] depends_on = [module.auth_jwt_backend] } diff --git a/modules/vault_cluster/modules/auth_jwt_role/main.tf b/modules/vault_cluster/modules/auth_jwt_role/main.tf index 5b328a5..d290123 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/main.tf +++ b/modules/vault_cluster/modules/auth_jwt_role/main.tf @@ -5,7 +5,7 @@ resource "vault_jwt_auth_backend_role" "role" { user_claim = var.user_claim bound_audiences = var.bound_audiences bound_claims = var.bound_claims - bound_claims_type = var.bound_claims_type + bound_claims_type = "string" token_ttl = var.token_ttl token_max_ttl = var.token_max_ttl token_policies = var.token_policies diff --git a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl new file mode 100644 index 0000000..bc2163e --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl @@ -0,0 +1,112 @@ +mock_provider "vault" {} + +variables { + backend = "jwt/github/iuclabs" + role_name = "test" + bound_audiences = ["https://github.com/IUCLabs"] +} + +run "accept_repo_ref_event" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push" } } +} + +run "accept_with_environment" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "prod" } } +} + +run "accept_event_list" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push, workflow_dispatch" } } +} + +run "reject_missing_repository" { + command = plan + variables { bound_claims = { ref = "refs/heads/main", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_missing_ref" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_missing_event_name" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_glob" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/*", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_pull" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/pull/1/merge", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_empty" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_list_with_pull" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main,refs/pull/1/merge", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_repository_glob" { + command = plan + variables { bound_claims = { repository = "IUCLabs/*", ref = "refs/heads/main", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_repository_other_org" { + command = plan + variables { bound_claims = { repository = "other/repo", ref = "refs/heads/main", event_name = "push" } } + expect_failures = [var.bound_claims] +} + +run "reject_event_pull_request_target" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "pull_request_target" } } + expect_failures = [var.bound_claims] +} + +run "reject_event_pull_request" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "pull_request" } } + expect_failures = [var.bound_claims] +} + +run "reject_event_workflow_run" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "workflow_run" } } + expect_failures = [var.bound_claims] +} + +run "reject_event_list_with_pull_request" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push,pull_request" } } + expect_failures = [var.bound_claims] +} + +run "reject_environment_glob" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "prod*" } } + expect_failures = [var.bound_claims] +} + +run "reject_environment_empty" { + command = plan + variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "" } } + expect_failures = [var.bound_claims] +} diff --git a/modules/vault_cluster/modules/auth_jwt_role/variables.tf b/modules/vault_cluster/modules/auth_jwt_role/variables.tf index 7d124eb..dc39f39 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/variables.tf +++ b/modules/vault_cluster/modules/auth_jwt_role/variables.tf @@ -24,21 +24,23 @@ variable "bound_audiences" { } variable "bound_claims" { - description = "Claims the token must carry; must pin repository plus ref or environment" + description = "Claims the token must carry; comma-separated values are split into a list of exact matches" type = map(string) validation { - condition = contains(keys(var.bound_claims), "repository") && (contains(keys(var.bound_claims), "ref") || contains(keys(var.bound_claims), "environment")) - error_message = "bound_claims must set repository and at least one of ref or environment." + condition = alltrue([for v in split(",", lookup(var.bound_claims, "repository", "")) : can(regex("^IUCLabs/[A-Za-z0-9._-]+$", trimspace(v)))]) + error_message = "bound_claims.repository must be exact IUCLabs/ values." } -} - -variable "bound_claims_type" { - description = "How bound_claims values are matched: 'string' or 'glob'" - type = string - default = "string" validation { - condition = contains(["string", "glob"], var.bound_claims_type) - error_message = "bound_claims_type must be either 'string' or 'glob'." + condition = alltrue([for v in split(",", lookup(var.bound_claims, "ref", "")) : can(regex("^refs/(heads|tags)/[A-Za-z0-9._/-]+$", trimspace(v)))]) + error_message = "bound_claims.ref must be exact refs/heads/* or refs/tags/* values." + } + validation { + condition = alltrue([for v in split(",", lookup(var.bound_claims, "event_name", "")) : contains(["push", "workflow_dispatch", "schedule", "release", "merge_group"], trimspace(v))]) + error_message = "bound_claims.event_name must only list push, workflow_dispatch, schedule, release or merge_group." + } + validation { + condition = alltrue([for v in split(",", lookup(var.bound_claims, "environment", "x")) : can(regex("^[^*?\\[]+$", trimspace(v)))]) + error_message = "bound_claims.environment must be exact non-empty values." } } diff --git a/modules/vault_cluster/variables.tf b/modules/vault_cluster/variables.tf index dcd4c68..f5474dc 100644 --- a/modules/vault_cluster/variables.tf +++ b/modules/vault_cluster/variables.tf @@ -118,14 +118,13 @@ variable "auth_jwt_backend" { variable "auth_jwt_role" { description = "Map of JWT auth roles to create" type = map(object({ - role_name = string - backend = string - bound_audiences = list(string) - bound_claims = map(string) - bound_claims_type = optional(string, "string") - user_claim = optional(string, "job_workflow_ref") - token_ttl = optional(number, 900) - token_max_ttl = optional(number, 3600) + role_name = string + backend = string + bound_audiences = list(string) + bound_claims = map(string) + user_claim = optional(string, "job_workflow_ref") + token_ttl = optional(number, 900) + token_max_ttl = optional(number, 3600) })) default = {} } diff --git a/tests/test_auth_jwt_role.py b/tests/test_auth_jwt_role.py deleted file mode 100644 index f6ae098..0000000 --- a/tests/test_auth_jwt_role.py +++ /dev/null @@ -1,51 +0,0 @@ -"""Validate GitHub Actions JWT role definitions under config/auth_jwt_role/.""" -import unittest -from pathlib import Path - -import yaml - -REPO_ROOT = Path(__file__).resolve().parent.parent - - -def claim_problem(role): - """Why a role's bound_claims are too loose to hand out a token, or None.""" - claims = role.get("bound_claims") or {} - if "repository" not in claims: - return "bound_claims must set repository" - if "ref" not in claims and "environment" not in claims: - return "bound_claims must set ref or environment" - return None - - -class RepoTests(unittest.TestCase): - def test_repo_roles(self): - for f in sorted(REPO_ROOT.glob("config/auth_jwt_role/**/*.yaml")): - with self.subTest(path=f.relative_to(REPO_ROOT).as_posix()): - self.assertIsNone(claim_problem(yaml.safe_load(f.read_text()) or {})) - - -class ClaimProblemTests(unittest.TestCase): - def test_repository_and_ref(self): - self.assertIsNone(claim_problem({"bound_claims": {"repository": "IUCLabs/x", "ref": "refs/heads/main"}})) - - def test_repository_and_environment(self): - self.assertIsNone(claim_problem({"bound_claims": {"repository": "IUCLabs/x", "environment": "prod"}})) - - def test_missing_repository(self): - self.assertEqual( - claim_problem({"bound_claims": {"ref": "refs/heads/main"}}), - "bound_claims must set repository", - ) - - def test_repository_only(self): - self.assertEqual( - claim_problem({"bound_claims": {"repository": "IUCLabs/x"}}), - "bound_claims must set ref or environment", - ) - - def test_no_bound_claims(self): - self.assertEqual(claim_problem({}), "bound_claims must set repository") - - -if __name__ == "__main__": - unittest.main() -- 2.47.3 From 0608019276152f6a76027c5d8b8cc3a712f68231 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 22:44:21 +1100 Subject: [PATCH 3/6] Replace per-repo JWT roles with an org role reading templated KV paths --- README.md | 35 +++-- .../auth_jwt_backend/jwt/github/iuclabs.yaml | 4 +- .../jwt/github/iuclabs/iuclabs.yaml | 22 +++ modules/vault_cluster/main.tf | 18 ++- .../modules/auth_jwt_backend/outputs.tf | 3 + .../modules/auth_jwt_role/main.tf | 21 ++- .../tests/bound_claims.tftest.hcl | 148 +++++++++++------- .../modules/auth_jwt_role/variables.tf | 36 +++-- modules/vault_cluster/variables.tf | 3 +- 9 files changed, 201 insertions(+), 89 deletions(-) create mode 100644 config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml create mode 100644 modules/vault_cluster/modules/auth_jwt_backend/outputs.tf diff --git a/README.md b/README.md index e75d2ef..a9abf16 100644 --- a/README.md +++ b/README.md @@ -38,20 +38,31 @@ consul kv get terraform/state # GitHub Actions JWT roles -`auth/jwt/github/iuclabs` trusts GitHub Actions OIDC tokens from the IUCLabs org. Add one role per consumer: +`auth/jwt/github/iuclabs` trusts GitHub Actions OIDC tokens. One org-wide role, `iuclabs` (`config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml`), accepts any IUCLabs repo for `push`, `release`, `schedule` and `pull_request` runs on `refs/heads/*`, `refs/tags/*` or `refs/pull/*/merge`. Role validation rejects any other event (`pull_request_target`, `workflow_run`, comment events), any other ref glob, and globs in any other claim. Fork pull requests can't mint OIDC tokens unless "Send write tokens to workflows from pull requests" is enabled on the repo. + +Workflows log in with audience `https://vault.service.consul:8200`: ```yaml -# config/auth_jwt_role/jwt/github/iuclabs/.yaml -bound_audiences: ["https://github.com/IUCLabs"] -bound_claims: - repository: IUCLabs/ - ref: refs/heads/main - event_name: push,workflow_dispatch - environment: # optional -token_ttl: 900 -token_max_ttl: 3600 +permissions: + id-token: write + contents: read +steps: + - uses: hashicorp/vault-action@v3 + with: + url: https://vault.service.consul:8200 + method: jwt + path: jwt/github/iuclabs + role: iuclabs + jwtGithubAudience: https://vault.service.consul:8200 + secrets: | + kv/data/github/IUCLabs//event/push/deploy token | DEPLOY_TOKEN ``` -`bound_claims` must set an exact `repository` (`IUCLabs/`), exact `ref` (`refs/heads/*` or `refs/tags/*`) and `event_name` from `push`, `workflow_dispatch`, `schedule`, `release`, `merge_group`; `environment` is optional. Values are matched exactly (comma-separated = list); globs and pull_request/workflow_run events are rejected at plan. +The role attaches a templated read-only policy, so each run reads only its own KV-v2 secrets: -Each role needs a policy yaml granting it under `auth: {jwt/github/iuclabs: []}`, or plan fails with "Invalid index". +- `kv/github///event//*` +- `kv/github///ref//*`, e.g. `ref/refs/heads/main/`, `ref/refs/tags/v1.2.3/` + +`user_claim` is `jti`, so every run creates its own Vault identity entity; Vault never garbage-collects these, so entity count grows with CI volume. + +Extra policies are granted via a policy yaml `auth: {jwt/github/iuclabs: [iuclabs]}`; the KV read policy is attached automatically. diff --git a/config/auth_jwt_backend/jwt/github/iuclabs.yaml b/config/auth_jwt_backend/jwt/github/iuclabs.yaml index c7d96cc..5a70073 100644 --- a/config/auth_jwt_backend/jwt/github/iuclabs.yaml +++ b/config/auth_jwt_backend/jwt/github/iuclabs.yaml @@ -1,5 +1,5 @@ -# GitHub Actions OIDC for the IUCLabs org. Roles live in -# config/auth_jwt_role/jwt/github/iuclabs/ and must pin repository + ref/environment. +# GitHub Actions OIDC for the IUCLabs org; the org-wide role lives in +# config/auth_jwt_role/jwt/github/iuclabs/. --- oidc_discovery_url: "https://token.actions.githubusercontent.com" bound_issuer: "https://token.actions.githubusercontent.com" diff --git a/config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml b/config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml new file mode 100644 index 0000000..7175e85 --- /dev/null +++ b/config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml @@ -0,0 +1,22 @@ +--- +bound_audiences: ["https://vault.service.consul:8200"] +bound_claims: + repository_owner: IUCLabs + event_name: push,release,schedule,pull_request + ref: refs/heads/*,refs/tags/*,refs/pull/*/merge +claim_mappings: + repository: repository + repository_owner: repository_owner + ref: ref + ref_type: ref_type + event_name: event_name + workflow: workflow + job_workflow_ref: job_workflow_ref + run_id: run_id + run_attempt: run_attempt + sha: sha + actor: actor + environment: environment +user_claim: jti +token_ttl: 900 +token_max_ttl: 3600 diff --git a/modules/vault_cluster/main.tf b/modules/vault_cluster/main.tf index ee34f11..dc5ba64 100644 --- a/modules/vault_cluster/main.tf +++ b/modules/vault_cluster/main.tf @@ -132,14 +132,16 @@ module "auth_jwt_role" { for_each = var.auth_jwt_role - backend = each.value.backend - role_name = each.value.role_name - user_claim = each.value.user_claim - bound_audiences = each.value.bound_audiences - bound_claims = each.value.bound_claims - token_ttl = each.value.token_ttl - token_max_ttl = each.value.token_max_ttl - token_policies = var.policy_auth_map[each.value.backend][each.value.role_name] + backend = each.value.backend + role_name = each.value.role_name + user_claim = each.value.user_claim + bound_audiences = each.value.bound_audiences + backend_accessor = module.auth_jwt_backend[each.value.backend].accessor + bound_claims = each.value.bound_claims + claim_mappings = each.value.claim_mappings + token_ttl = each.value.token_ttl + token_max_ttl = each.value.token_max_ttl + token_policies = try(var.policy_auth_map[each.value.backend][each.value.role_name], []) depends_on = [module.auth_jwt_backend] } diff --git a/modules/vault_cluster/modules/auth_jwt_backend/outputs.tf b/modules/vault_cluster/modules/auth_jwt_backend/outputs.tf new file mode 100644 index 0000000..c87a404 --- /dev/null +++ b/modules/vault_cluster/modules/auth_jwt_backend/outputs.tf @@ -0,0 +1,3 @@ +output "accessor" { + value = vault_jwt_auth_backend.jwt.accessor +} diff --git a/modules/vault_cluster/modules/auth_jwt_role/main.tf b/modules/vault_cluster/modules/auth_jwt_role/main.tf index d290123..e39a643 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/main.tf +++ b/modules/vault_cluster/modules/auth_jwt_role/main.tf @@ -1,3 +1,19 @@ +locals { + alias = "identity.entity.aliases.${var.backend_accessor}.metadata" + kv_prefixes = [ + "github/{{${local.alias}.repository}}/event/{{${local.alias}.event_name}}", + "github/{{${local.alias}.repository}}/ref/{{${local.alias}.ref}}", + ] +} + +resource "vault_policy" "kv" { + name = "${var.backend}/${var.role_name}/kv-read" + policy = join("\n", flatten([for p in local.kv_prefixes : [ + "path \"kv/data/${p}/*\" {\n capabilities = [\"read\"]\n}", + "path \"kv/metadata/${p}/*\" {\n capabilities = [\"read\", \"list\"]\n}", + ]])) +} + resource "vault_jwt_auth_backend_role" "role" { backend = var.backend role_name = var.role_name @@ -5,8 +21,9 @@ resource "vault_jwt_auth_backend_role" "role" { user_claim = var.user_claim bound_audiences = var.bound_audiences bound_claims = var.bound_claims - bound_claims_type = "string" + bound_claims_type = "glob" + claim_mappings = var.claim_mappings token_ttl = var.token_ttl token_max_ttl = var.token_max_ttl - token_policies = var.token_policies + token_policies = concat(var.token_policies, [vault_policy.kv.name]) } diff --git a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl index bc2163e..bfbfb37 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl +++ b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl @@ -1,112 +1,152 @@ mock_provider "vault" {} variables { - backend = "jwt/github/iuclabs" - role_name = "test" - bound_audiences = ["https://github.com/IUCLabs"] + backend = "jwt/github/iuclabs" + role_name = "iuclabs" + backend_accessor = "auth_jwt_test" + bound_audiences = ["https://vault.service.consul:8200"] } -run "accept_repo_ref_event" { +run "accept_org_role" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } + + assert { + condition = vault_policy.kv.policy == chomp(<<-EOT + path "kv/data/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/event/{{identity.entity.aliases.auth_jwt_test.metadata.event_name}}/*" { + capabilities = ["read"] + } + path "kv/metadata/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/event/{{identity.entity.aliases.auth_jwt_test.metadata.event_name}}/*" { + capabilities = ["read", "list"] + } + path "kv/data/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/*" { + capabilities = ["read"] + } + path "kv/metadata/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/*" { + capabilities = ["read", "list"] + } + EOT + ) + error_message = "rendered KV policy paths changed" + } + assert { + condition = contains(vault_jwt_auth_backend_role.role.token_policies, "jwt/github/iuclabs/iuclabs/kv-read") + error_message = "role must carry the KV read policy" + } + assert { + condition = vault_jwt_auth_backend_role.role.bound_claims_type == "glob" && vault_jwt_auth_backend_role.role.user_claim == "jti" + error_message = "role must glob-match claims and alias on jti" + } } -run "accept_with_environment" { +run "accept_exact_refs" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "prod" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/main,refs/tags/v1.2.3,refs/pull/7/merge" } } } -run "accept_event_list" { +run "accept_with_repository" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push, workflow_dispatch" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge", repository = "IUCLabs/app" } } } -run "reject_missing_repository" { +run "reject_missing_repository_owner" { command = plan - variables { bound_claims = { ref = "refs/heads/main", event_name = "push" } } + variables { bound_claims = { event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } run "reject_missing_ref" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", event_name = "push" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request" } } expect_failures = [var.bound_claims] } run "reject_missing_event_name" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main" } } + variables { bound_claims = { repository_owner = "IUCLabs", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } -run "reject_ref_glob" { +run "reject_repository_owner_wildcard" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/*", event_name = "push" } } + variables { bound_claims = { repository_owner = "*", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } -run "reject_ref_pull" { +run "reject_repository_owner_glob" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/pull/1/merge", event_name = "push" } } - expect_failures = [var.bound_claims] -} - -run "reject_ref_empty" { - command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "", event_name = "push" } } - expect_failures = [var.bound_claims] -} - -run "reject_ref_list_with_pull" { - command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main,refs/pull/1/merge", event_name = "push" } } - expect_failures = [var.bound_claims] -} - -run "reject_repository_glob" { - command = plan - variables { bound_claims = { repository = "IUCLabs/*", ref = "refs/heads/main", event_name = "push" } } - expect_failures = [var.bound_claims] -} - -run "reject_repository_other_org" { - command = plan - variables { bound_claims = { repository = "other/repo", ref = "refs/heads/main", event_name = "push" } } + variables { bound_claims = { repository_owner = "IUC*", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } run "reject_event_pull_request_target" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "pull_request_target" } } - expect_failures = [var.bound_claims] -} - -run "reject_event_pull_request" { - command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "pull_request" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,pull_request_target", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } run "reject_event_workflow_run" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "workflow_run" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "workflow_run", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } -run "reject_event_list_with_pull_request" { +run "reject_event_issue_comment" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push,pull_request" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "issue_comment", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } expect_failures = [var.bound_claims] } -run "reject_environment_glob" { +run "reject_event_workflow_dispatch" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "prod*" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "workflow_dispatch", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_star" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/*" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_bare_star" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "*" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_pull_star" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/pull/*" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_pull_head" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/pull/*/head" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_heads_prefix_glob" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/rel*" } } + expect_failures = [var.bound_claims] +} + +run "reject_ref_empty" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "" } } + expect_failures = [var.bound_claims] +} + +run "reject_repository_glob" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge", repository = "IUCLabs/*" } } expect_failures = [var.bound_claims] } run "reject_environment_empty" { command = plan - variables { bound_claims = { repository = "IUCLabs/app", ref = "refs/heads/main", event_name = "push", environment = "" } } + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge", environment = "" } } expect_failures = [var.bound_claims] } diff --git a/modules/vault_cluster/modules/auth_jwt_role/variables.tf b/modules/vault_cluster/modules/auth_jwt_role/variables.tf index dc39f39..428fffc 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/variables.tf +++ b/modules/vault_cluster/modules/auth_jwt_role/variables.tf @@ -8,10 +8,21 @@ variable "role_name" { type = string } +variable "backend_accessor" { + description = "Accessor of the JWT auth mount, used to template the KV read policy" + type = string +} + variable "user_claim" { description = "Claim used as the entity alias name" type = string - default = "job_workflow_ref" + default = "jti" +} + +variable "claim_mappings" { + description = "Token claims copied into entity alias metadata" + type = map(string) + default = {} } variable "bound_audiences" { @@ -24,23 +35,28 @@ variable "bound_audiences" { } variable "bound_claims" { - description = "Claims the token must carry; comma-separated values are split into a list of exact matches" + description = "Claims the token must carry, glob-matched; only ref may use the allowed glob patterns, comma-separated values form a list" type = map(string) validation { - condition = alltrue([for v in split(",", lookup(var.bound_claims, "repository", "")) : can(regex("^IUCLabs/[A-Za-z0-9._-]+$", trimspace(v)))]) - error_message = "bound_claims.repository must be exact IUCLabs/ values." + condition = can(regex("^[A-Za-z0-9-]+$", lookup(var.bound_claims, "repository_owner", ""))) + error_message = "bound_claims.repository_owner must be one exact org name." } validation { - condition = alltrue([for v in split(",", lookup(var.bound_claims, "ref", "")) : can(regex("^refs/(heads|tags)/[A-Za-z0-9._/-]+$", trimspace(v)))]) - error_message = "bound_claims.ref must be exact refs/heads/* or refs/tags/* values." + condition = alltrue([for v in split(",", lookup(var.bound_claims, "event_name", "")) : contains(["push", "release", "schedule", "pull_request"], trimspace(v))]) + error_message = "bound_claims.event_name must only list push, release, schedule or pull_request." } validation { - condition = alltrue([for v in split(",", lookup(var.bound_claims, "event_name", "")) : contains(["push", "workflow_dispatch", "schedule", "release", "merge_group"], trimspace(v))]) - error_message = "bound_claims.event_name must only list push, workflow_dispatch, schedule, release or merge_group." + condition = alltrue([for v in split(",", lookup(var.bound_claims, "ref", "")) : + contains(["refs/heads/*", "refs/tags/*", "refs/pull/*/merge"], trimspace(v)) || + can(regex("^(refs/(heads|tags)/[A-Za-z0-9._/-]+|refs/pull/[0-9]+/merge)$", trimspace(v))) + ]) + error_message = "bound_claims.ref must be refs/heads/*, refs/tags/*, refs/pull/*/merge or exact refs of those shapes." } validation { - condition = alltrue([for v in split(",", lookup(var.bound_claims, "environment", "x")) : can(regex("^[^*?\\[]+$", trimspace(v)))]) - error_message = "bound_claims.environment must be exact non-empty values." + condition = alltrue(flatten([for k, v in var.bound_claims : [ + for e in split(",", v) : trimspace(e) != "" && !strcontains(e, "*") + ] if k != "ref"])) + error_message = "bound_claims values other than ref must be exact and non-empty." } } diff --git a/modules/vault_cluster/variables.tf b/modules/vault_cluster/variables.tf index f5474dc..6f4fb66 100644 --- a/modules/vault_cluster/variables.tf +++ b/modules/vault_cluster/variables.tf @@ -122,7 +122,8 @@ variable "auth_jwt_role" { backend = string bound_audiences = list(string) bound_claims = map(string) - user_claim = optional(string, "job_workflow_ref") + claim_mappings = optional(map(string), {}) + user_claim = optional(string, "jti") token_ttl = optional(number, 900) token_max_ttl = optional(number, 3600) })) -- 2.47.3 From d6c1e24e70e6d7f79e76f5cd25d4d8875f6d7480 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 22:51:30 +1100 Subject: [PATCH 4/6] Terminate ref KV paths with a .secrets segment --- README.md | 2 +- .../vault_cluster/modules/auth_jwt_role/main.tf | 2 +- .../auth_jwt_role/tests/bound_claims.tftest.hcl | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a9abf16..2b55fdb 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ steps: The role attaches a templated read-only policy, so each run reads only its own KV-v2 secrets: - `kv/github///event//*` -- `kv/github///ref//*`, e.g. `ref/refs/heads/main/`, `ref/refs/tags/v1.2.3/` +- `kv/github///ref//.secrets/*`, e.g. `ref/refs/heads/main/.secrets/`, `ref/refs/tags/v1.2.3/.secrets/`; git refnames can't contain a `.`-prefixed component, so branch `x` can never read branch `x/y`'s secrets `user_claim` is `jti`, so every run creates its own Vault identity entity; Vault never garbage-collects these, so entity count grows with CI volume. diff --git a/modules/vault_cluster/modules/auth_jwt_role/main.tf b/modules/vault_cluster/modules/auth_jwt_role/main.tf index e39a643..9e4d1b1 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/main.tf +++ b/modules/vault_cluster/modules/auth_jwt_role/main.tf @@ -2,7 +2,7 @@ locals { alias = "identity.entity.aliases.${var.backend_accessor}.metadata" kv_prefixes = [ "github/{{${local.alias}.repository}}/event/{{${local.alias}.event_name}}", - "github/{{${local.alias}.repository}}/ref/{{${local.alias}.ref}}", + "github/{{${local.alias}.repository}}/ref/{{${local.alias}.ref}}/.secrets", ] } diff --git a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl index bfbfb37..7e945ff 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl +++ b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl @@ -19,10 +19,10 @@ run "accept_org_role" { path "kv/metadata/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/event/{{identity.entity.aliases.auth_jwt_test.metadata.event_name}}/*" { capabilities = ["read", "list"] } - path "kv/data/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/*" { + path "kv/data/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/.secrets/*" { capabilities = ["read"] } - path "kv/metadata/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/*" { + path "kv/metadata/github/{{identity.entity.aliases.auth_jwt_test.metadata.repository}}/ref/{{identity.entity.aliases.auth_jwt_test.metadata.ref}}/.secrets/*" { capabilities = ["read", "list"] } EOT @@ -39,6 +39,16 @@ run "accept_org_role" { } } +run "ref_paths_cannot_overlap_child_refs" { + command = plan + variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push", ref = "refs/heads/*" } } + + assert { + condition = length(regexall("metadata\\.ref}}/[^.]", vault_policy.kv.policy)) == 0 + error_message = "ref paths must end in a dot-prefixed segment git refnames cannot contain" + } +} + run "accept_exact_refs" { command = plan variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/main,refs/tags/v1.2.3,refs/pull/7/merge" } } -- 2.47.3 From 5c33f2d433d2356f2e59ff6d74ed010125ec941f Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 23:06:11 +1100 Subject: [PATCH 5/6] Document event-path scope and + ref caveat; assert exact refs --- README.md | 6 ++++-- .../modules/auth_jwt_role/tests/bound_claims.tftest.hcl | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b55fdb..e30cd98 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,16 @@ steps: role: iuclabs jwtGithubAudience: https://vault.service.consul:8200 secrets: | - kv/data/github/IUCLabs//event/push/deploy token | DEPLOY_TOKEN + kv/data/github/IUCLabs//ref/refs/heads/main/.secrets/deploy token | DEPLOY_TOKEN ``` The role attaches a templated read-only policy, so each run reads only its own KV-v2 secrets: -- `kv/github///event//*` +- `kv/github///event//*` is not ref-scoped: any run of that event in the repo, on any branch or tag a writer pushes, can read it; keep it for secrets every such run may use - `kv/github///ref//.secrets/*`, e.g. `ref/refs/heads/main/.secrets/`, `ref/refs/tags/v1.2.3/.secrets/`; git refnames can't contain a `.`-prefixed component, so branch `x` can never read branch `x/y`'s secrets +On OpenBao older than the fork's v2.6, a ref path component of exactly `+` acts as a single-segment wildcard in the templated policy. Public IUCLabs repos block such refs via a terraform-git ruleset; private repos are not protected, so a writer in a private repo can read that repo's other refs' secrets. + `user_claim` is `jti`, so every run creates its own Vault identity entity; Vault never garbage-collects these, so entity count grows with CI volume. Extra policies are granted via a policy yaml `auth: {jwt/github/iuclabs: [iuclabs]}`; the KV read policy is attached automatically. diff --git a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl index 7e945ff..4041746 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl +++ b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl @@ -52,6 +52,11 @@ run "ref_paths_cannot_overlap_child_refs" { run "accept_exact_refs" { command = plan variables { bound_claims = { repository_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/main,refs/tags/v1.2.3,refs/pull/7/merge" } } + + assert { + condition = vault_jwt_auth_backend_role.role.bound_claims.ref == "refs/heads/main,refs/tags/v1.2.3,refs/pull/7/merge" + error_message = "exact refs must pass through to bound_claims unchanged" + } } run "accept_with_repository" { -- 2.47.3 From 9e3c7d250d7fae15a5248a5591a9a7d3b8649cf1 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Tue, 10 Nov 2026 23:17:01 +1100 Subject: [PATCH 6/6] Rename JWT role to default and set it as mount default_role --- README.md | 5 ++--- config/auth_jwt_backend/jwt/github/iuclabs.yaml | 1 + .../jwt/github/iuclabs/{iuclabs.yaml => default.yaml} | 0 modules/vault_cluster/main.tf | 1 + modules/vault_cluster/modules/auth_jwt_backend/main.tf | 1 + modules/vault_cluster/modules/auth_jwt_backend/variables.tf | 6 ++++++ .../modules/auth_jwt_role/tests/bound_claims.tftest.hcl | 4 ++-- modules/vault_cluster/variables.tf | 1 + 8 files changed, 14 insertions(+), 5 deletions(-) rename config/auth_jwt_role/jwt/github/iuclabs/{iuclabs.yaml => default.yaml} (100%) diff --git a/README.md b/README.md index e30cd98..a9fb026 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ consul kv get terraform/state # GitHub Actions JWT roles -`auth/jwt/github/iuclabs` trusts GitHub Actions OIDC tokens. One org-wide role, `iuclabs` (`config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml`), accepts any IUCLabs repo for `push`, `release`, `schedule` and `pull_request` runs on `refs/heads/*`, `refs/tags/*` or `refs/pull/*/merge`. Role validation rejects any other event (`pull_request_target`, `workflow_run`, comment events), any other ref glob, and globs in any other claim. Fork pull requests can't mint OIDC tokens unless "Send write tokens to workflows from pull requests" is enabled on the repo. +`auth/jwt/github/iuclabs` trusts GitHub Actions OIDC tokens. One org-wide role, `default` (`config/auth_jwt_role/jwt/github/iuclabs/default.yaml`, the mount's `default_role`, so workflows needn't name it), accepts any IUCLabs repo for `push`, `release`, `schedule` and `pull_request` runs on `refs/heads/*`, `refs/tags/*` or `refs/pull/*/merge`. Role validation rejects any other event (`pull_request_target`, `workflow_run`, comment events), any other ref glob, and globs in any other claim. Fork pull requests can't mint OIDC tokens unless "Send write tokens to workflows from pull requests" is enabled on the repo. Workflows log in with audience `https://vault.service.consul:8200`: @@ -52,7 +52,6 @@ steps: url: https://vault.service.consul:8200 method: jwt path: jwt/github/iuclabs - role: iuclabs jwtGithubAudience: https://vault.service.consul:8200 secrets: | kv/data/github/IUCLabs//ref/refs/heads/main/.secrets/deploy token | DEPLOY_TOKEN @@ -67,4 +66,4 @@ On OpenBao older than the fork's v2.6, a ref path component of exactly `+` acts `user_claim` is `jti`, so every run creates its own Vault identity entity; Vault never garbage-collects these, so entity count grows with CI volume. -Extra policies are granted via a policy yaml `auth: {jwt/github/iuclabs: [iuclabs]}`; the KV read policy is attached automatically. +Extra policies are granted via a policy yaml `auth: {jwt/github/iuclabs: [default]}`; the KV read policy is attached automatically. diff --git a/config/auth_jwt_backend/jwt/github/iuclabs.yaml b/config/auth_jwt_backend/jwt/github/iuclabs.yaml index 5a70073..f05c09d 100644 --- a/config/auth_jwt_backend/jwt/github/iuclabs.yaml +++ b/config/auth_jwt_backend/jwt/github/iuclabs.yaml @@ -4,3 +4,4 @@ oidc_discovery_url: "https://token.actions.githubusercontent.com" bound_issuer: "https://token.actions.githubusercontent.com" description: "GitHub Actions OIDC - IUCLabs" +default_role: "default" diff --git a/config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml b/config/auth_jwt_role/jwt/github/iuclabs/default.yaml similarity index 100% rename from config/auth_jwt_role/jwt/github/iuclabs/iuclabs.yaml rename to config/auth_jwt_role/jwt/github/iuclabs/default.yaml diff --git a/modules/vault_cluster/main.tf b/modules/vault_cluster/main.tf index dc5ba64..21aca8b 100644 --- a/modules/vault_cluster/main.tf +++ b/modules/vault_cluster/main.tf @@ -122,6 +122,7 @@ module "auth_jwt_backend" { description = each.value.description oidc_discovery_url = each.value.oidc_discovery_url bound_issuer = each.value.bound_issuer + default_role = each.value.default_role listing_visibility = each.value.listing_visibility default_lease_ttl = each.value.default_lease_ttl max_lease_ttl = each.value.max_lease_ttl diff --git a/modules/vault_cluster/modules/auth_jwt_backend/main.tf b/modules/vault_cluster/modules/auth_jwt_backend/main.tf index b912e39..cc874f9 100644 --- a/modules/vault_cluster/modules/auth_jwt_backend/main.tf +++ b/modules/vault_cluster/modules/auth_jwt_backend/main.tf @@ -4,6 +4,7 @@ resource "vault_jwt_auth_backend" "jwt" { description = var.description oidc_discovery_url = var.oidc_discovery_url bound_issuer = var.bound_issuer + default_role = var.default_role tune { default_lease_ttl = var.default_lease_ttl diff --git a/modules/vault_cluster/modules/auth_jwt_backend/variables.tf b/modules/vault_cluster/modules/auth_jwt_backend/variables.tf index aadab25..071bee8 100644 --- a/modules/vault_cluster/modules/auth_jwt_backend/variables.tf +++ b/modules/vault_cluster/modules/auth_jwt_backend/variables.tf @@ -19,6 +19,12 @@ variable "bound_issuer" { type = string } +variable "default_role" { + description = "Role used when a login request names none" + type = string + default = null +} + variable "listing_visibility" { description = "Specifies whether to show this mount in the UI-specific listing endpoint. Valid values are 'unauth' or 'hidden'" type = string diff --git a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl index 4041746..b5fa9e1 100644 --- a/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl +++ b/modules/vault_cluster/modules/auth_jwt_role/tests/bound_claims.tftest.hcl @@ -2,7 +2,7 @@ mock_provider "vault" {} variables { backend = "jwt/github/iuclabs" - role_name = "iuclabs" + role_name = "default" backend_accessor = "auth_jwt_test" bound_audiences = ["https://vault.service.consul:8200"] } @@ -30,7 +30,7 @@ run "accept_org_role" { error_message = "rendered KV policy paths changed" } assert { - condition = contains(vault_jwt_auth_backend_role.role.token_policies, "jwt/github/iuclabs/iuclabs/kv-read") + condition = contains(vault_jwt_auth_backend_role.role.token_policies, "jwt/github/iuclabs/default/kv-read") error_message = "role must carry the KV read policy" } assert { diff --git a/modules/vault_cluster/variables.tf b/modules/vault_cluster/variables.tf index 6f4fb66..c5495df 100644 --- a/modules/vault_cluster/variables.tf +++ b/modules/vault_cluster/variables.tf @@ -107,6 +107,7 @@ variable "auth_jwt_backend" { type = map(object({ oidc_discovery_url = string bound_issuer = string + default_role = optional(string) description = optional(string) listing_visibility = optional(string) default_lease_ttl = optional(string) -- 2.47.3