Rework agent-dns to service_account_name mode against a static SA
ci/woodpecker/pr/plan Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful

Instead of generating cluster-wide RBAC, the agent-dns role now mints
tokens for a static GitOps-managed service account (argocd-apps#332)
whose per-namespace RoleBindings confine access to exactly the four bind
namespaces. Ordering: the argocd-apps RBAC must sync before these creds
are usable, since Vault mints tokens for an SA that must already exist.

- extend the kubernetes_secret_backend_role module with an optional
  service_account_name; when set, generated_role_rules and
  kubernetes_role_type are omitted (the SA's own bindings supply RBAC).
- switch the agent-dns role to service_account_name agent-dns with
  allowed_kubernetes_namespaces bind-system; drop its generated rules.

Claude-Session: https://claude.ai/code/session_01JUoARVdmhxKQHyyyp1pxeT
This commit is contained in:
2026-08-02 21:45:26 +10:00
parent 41fef29bad
commit 680a0455e5
6 changed files with 17 additions and 30 deletions
@@ -1,4 +1,4 @@
allowed_kubernetes_namespaces:
- "*"
kubernetes_role_type: "ClusterRole"
- "bind-system"
service_account_name: "agent-dns"
extra_labels: {}
+1
View File
@@ -299,6 +299,7 @@ module "kubernetes_secret_backend_role" {
allowed_kubernetes_namespaces = each.value.allowed_kubernetes_namespaces
kubernetes_role_type = each.value.kubernetes_role_type
extra_labels = each.value.extra_labels
service_account_name = each.value.service_account_name
depends_on = [module.kubernetes_secret_backend]
}
@@ -2,6 +2,10 @@ locals {
# Auto-generate role rules path: resources/secret_backend/{backend_path}/roles/{role_name}.yaml
role_rules_file = "resources/secret_backend/${var.backend}/roles/${var.name}.yaml"
# service_account_name mode mints tokens for a pre-existing SA, so the
# generated_role_rules / kubernetes_role_type binding fields must be unset.
use_service_account = var.service_account_name != null
# Auto-generate extra labels based on country/region and role name
auto_labels = merge(var.extra_labels, {
vault-region = "${var.country}-${var.region}"
@@ -13,7 +17,8 @@ resource "vault_kubernetes_secret_backend_role" "role" {
backend = var.backend
name = var.name
allowed_kubernetes_namespaces = var.allowed_kubernetes_namespaces
kubernetes_role_type = var.kubernetes_role_type
generated_role_rules = file("${path.module}/../../../../../../../../${local.role_rules_file}")
kubernetes_role_type = local.use_service_account ? null : var.kubernetes_role_type
generated_role_rules = local.use_service_account ? null : file("${path.module}/../../../../../../../../${local.role_rules_file}")
service_account_name = var.service_account_name
extra_labels = local.auto_labels
}
@@ -34,4 +34,10 @@ variable "extra_labels" {
description = "Additional labels to apply to generated Kubernetes objects"
type = map(string)
default = {}
}
variable "service_account_name" {
description = "Pre-existing service account to mint tokens for. When set, RBAC comes from that SA's own bindings instead of generated_role_rules."
type = string
default = null
}
+1
View File
@@ -285,6 +285,7 @@ variable "kubernetes_secret_backend_role" {
allowed_kubernetes_namespaces = optional(list(string), ["*"])
kubernetes_role_type = optional(string, "Role")
extra_labels = optional(map(string), {})
service_account_name = optional(string)
}))
default = {}
}
@@ -1,26 +0,0 @@
---
rules:
- apiGroups:
- "bind.unkin.net"
resources:
- "*"
verbs:
- "*"
- apiGroups:
- ""
resources:
- "pods"
- "services"
- "configmaps"
- "events"
verbs:
- "get"
- "list"
- "watch"
- apiGroups:
- ""
resources:
- "pods/log"
verbs:
- "get"
- "list"