Add GitHub Actions JWT auth mount for IUCLabs (#175)
ci/woodpecker/push/apply Pipeline failed

IUCLabs GitHub Actions workflows need read access to their own KV secrets without per-repo role and policy plumbing.

- add `auth/jwt/github/iuclabs` JWT mount trusting GitHub Actions OIDC
- add org-wide `default` role (mount `default_role`): audience `https://vault.service.consul:8200`, `user_claim=jti`, metadata mappings, 15m/1h TTLs
- restrict to `repository_owner=IUCLabs`, events push/release/schedule/pull_request, refs heads/tags/pull-merge; validation rejects anything wider
- attach templated read-only policy on `kv/github/<repo>/event/<event>/*` and `kv/github/<repo>/ref/<ref>/.secrets/*`
- add `tofu test` cases, run by `make test` and pre-commit
- document login, KV layout, event-path scope, `+` ref caveat and entity growth

Pairs with the terraform-git ref-name ruleset.

Reviewed-on: #175
Co-authored-by: unkin-agent <unkin-agent@unkin.net>
Co-committed-by: unkin-agent <unkin-agent@unkin.net>
This commit was merged in pull request #175.
This commit is contained in:
2026-11-10 23:25:31 +11:00
committed by BenVincent
parent be041f7112
commit b9ae4a6802
17 changed files with 504 additions and 0 deletions
+6
View File
@@ -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
+1
View File
@@ -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..."
+32
View File
@@ -35,3 +35,35 @@ 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. 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`:
```yaml
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
jwtGithubAudience: https://vault.service.consul:8200
secrets: |
kv/data/github/IUCLabs/<repo>/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/<org>/<repo>/event/<event_name>/*` 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/<org>/<repo>/ref/<full 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: [default]}`; the KV read policy is attached automatically.
@@ -0,0 +1,7 @@
# 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"
description: "GitHub Actions OIDC - IUCLabs"
default_role: "default"
@@ -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
+13
View File
@@ -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
+2
View File
@@ -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
+34
View File
@@ -113,6 +113,40 @@ 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
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
}
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
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]
}
module "auth_kubernetes_backend" {
source = "./modules/auth_kubernetes_backend"
@@ -0,0 +1,14 @@
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
default_role = var.default_role
tune {
default_lease_ttl = var.default_lease_ttl
max_lease_ttl = var.max_lease_ttl
listing_visibility = var.listing_visibility
}
}
@@ -0,0 +1,3 @@
output "accessor" {
value = vault_jwt_auth_backend.jwt.accessor
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.10"
required_providers {
vault = {
source = "hashicorp/vault"
version = "5.6.0"
}
}
}
@@ -0,0 +1,48 @@
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 "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
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
}
@@ -0,0 +1,29 @@
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}}/.secrets",
]
}
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
role_type = "jwt"
user_claim = var.user_claim
bound_audiences = var.bound_audiences
bound_claims = var.bound_claims
bound_claims_type = "glob"
claim_mappings = var.claim_mappings
token_ttl = var.token_ttl
token_max_ttl = var.token_max_ttl
token_policies = concat(var.token_policies, [vault_policy.kv.name])
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.10"
required_providers {
vault = {
source = "hashicorp/vault"
version = "5.6.0"
}
}
}
@@ -0,0 +1,167 @@
mock_provider "vault" {}
variables {
backend = "jwt/github/iuclabs"
role_name = "default"
backend_accessor = "auth_jwt_test"
bound_audiences = ["https://vault.service.consul:8200"]
}
run "accept_org_role" {
command = plan
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}}/.secrets/*" {
capabilities = ["read"]
}
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
)
error_message = "rendered KV policy paths changed"
}
assert {
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 {
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 "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" } }
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" {
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/app" } }
}
run "reject_missing_repository_owner" {
command = plan
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_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_owner = "IUCLabs", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } }
expect_failures = [var.bound_claims]
}
run "reject_repository_owner_wildcard" {
command = plan
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_repository_owner_glob" {
command = plan
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_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_owner = "IUCLabs", event_name = "workflow_run", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge" } }
expect_failures = [var.bound_claims]
}
run "reject_event_issue_comment" {
command = plan
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_event_workflow_dispatch" {
command = plan
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_owner = "IUCLabs", event_name = "push,release,schedule,pull_request", ref = "refs/heads/*,refs/tags/*,refs/pull/*/merge", environment = "" } }
expect_failures = [var.bound_claims]
}
@@ -0,0 +1,79 @@
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 "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 = "jti"
}
variable "claim_mappings" {
description = "Token claims copied into entity alias metadata"
type = map(string)
default = {}
}
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, glob-matched; only ref may use the allowed glob patterns, comma-separated values form a list"
type = map(string)
validation {
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, "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, "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(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."
}
}
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 = []
}
+29
View File
@@ -102,6 +102,35 @@ 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
default_role = optional(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)
claim_mappings = optional(map(string), {})
user_claim = optional(string, "jti")
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({