Skip to content

feat: auto-create ServiceAccount for SPIRE-enabled workloads - #142

Merged
mrsabath merged 4 commits into
rossoctl:mainfrom
mrsabath:feat/webhook-auto-serviceaccount
Feb 23, 2026
Merged

mrsabath merged 4 commits into
rossoctl:mainfrom
mrsabath:feat/webhook-auto-serviceaccount

Conversation

@mrsabath

Copy link
Copy Markdown
Member

Summary

  • When the AuthBridge webhook injects sidecars with SPIRE enabled (kagenti.io/spire: enabled), it now auto-creates a dedicated ServiceAccount matching the workload name and sets podSpec.ServiceAccountName
  • This ensures the SPIFFE ID reflects the workload identity (e.g. spiffe://.../sa/git-issue-agent) instead of the generic spiffe://.../sa/default
  • The SA is only created when the pod uses the default ServiceAccount; existing non-default ServiceAccountNames are respected
  • RBAC updated in both Kustomize config and Helm chart to grant serviceaccounts get/create

Changes

File Change
kagenti-webhook/internal/webhook/injector/pod_mutator.go Add ensureServiceAccount() method; call it from InjectAuthBridge() when SPIRE enabled and SA is empty/default
kagenti-webhook/config/rbac/role.yaml Add serviceaccounts get/create permission
charts/kagenti-webhook/templates/clusterrole.yaml Same RBAC addition for Helm chart

Test plan

  • go build ./... passes
  • Deploy to Kind cluster, create SPIRE-enabled agent deployment
  • Verify webhook logs show Set ServiceAccountName for SPIRE identity
  • Verify client-registration logs show correct SPIFFE ID: spiffe://localtest.me/ns/team1/sa/git-issue-agent
  • Verify Keycloak client registered with workload-specific SPIFFE ID
  • Verify idempotency: redeploy same workload, webhook logs show ServiceAccount already exists
  • Verify non-SPIRE workloads are unaffected (no SA creation, no serviceAccountName override)
  • Verify workloads with explicit serviceAccountName are not overridden

Made with Cursor

When the AuthBridge webhook injects sidecars with SPIRE enabled, it now
creates a dedicated ServiceAccount matching the workload name and sets
podSpec.ServiceAccountName. This ensures the SPIFFE ID reflects the
workload identity (e.g. spiffe://.../sa/git-issue-agent) instead of
the generic spiffe://.../sa/default.

The SA is only created when SPIRE is enabled and the pod uses the
default ServiceAccount. Existing non-default ServiceAccountNames are
respected. The creation is idempotent (no-op if the SA already exists).

RBAC is updated in both Kustomize and Helm to grant serviceaccounts
get/create permissions.

Signed-off-by: Mariusz Sabath <[email protected]>
Co-authored-by: Cursor <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds auto-creation of dedicated ServiceAccounts for SPIRE-enabled workloads to ensure SPIFFE IDs reflect workload identity rather than using the generic "default" ServiceAccount. The webhook now creates a ServiceAccount matching the workload name when SPIRE is enabled and the pod uses the default ServiceAccount, setting podSpec.ServiceAccountName accordingly.

Changes:

  • Auto-creates ServiceAccounts for SPIRE-enabled workloads to provide unique SPIFFE identities
  • Adds RBAC permissions for serviceaccounts get/create operations in both Kustomize and Helm configurations
  • Implements idempotent ServiceAccount creation with proper error handling for already-exists scenarios

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
kagenti-webhook/internal/webhook/injector/pod_mutator.go Adds ensureServiceAccount() method and integrates it into InjectAuthBridge() to create dedicated ServiceAccounts for SPIRE-enabled workloads
kagenti-webhook/config/rbac/role.yaml Grants webhook serviceaccounts get/create permissions for auto-creation functionality
charts/kagenti-webhook/templates/clusterrole.yaml Mirrors RBAC changes in Helm chart for consistency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kagenti-webhook/config/rbac/role.yaml Resolved
cwiklik and others added 3 commits February 23, 2026 16:02
Address Copilot review feedback on PR #142:
- ensureServiceAccount now fetches existing SAs on AlreadyExists and
  logs a warning if the SA is not managed by this webhook
- Extract label key/value into constants (managedByLabel/managedByValue)
- Add comprehensive unit tests for ensureServiceAccount (creation,
  idempotency, unmanaged SA warning) and InjectAuthBridge SA logic
  (SA name assignment, respecting existing non-default SA, no SA
  creation without SPIRE, overriding "default" SA name)

Signed-off-by: Mariusz Sabath <[email protected]>
Co-authored-by: Cursor <[email protected]>

@cwiklik cwiklik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mrsabath
mrsabath merged commit 7a570a8 into rossoctl:main Feb 23, 2026
3 checks passed
@mrsabath
mrsabath deleted the feat/webhook-auto-serviceaccount branch February 23, 2026 21:21
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
Under the DENY-aware PRB the shared pipeline fixture failed closed: the
audit task raised PolicyContradictionError for the github-agent policy,
erroring out all 43 test_policy_pipeline.py cases at setup. Root cause
was policy.explicit.md granting developer the coarse issues-access
(read+write bundle) but only fine issues-read, which the auditor read as
a coarse-scope prohibition.

Keep the fixture ALLOW-only for now (matching the UC-1 demo.md shape),
deferring the ALLOW+DENY path to sibling issue #142:

- policy.explicit.md: drop the coarse '## Users -> agent capabilities'
  block; keep purely-positive fine-grained grants. Inbound mapping (a)
  is now derived from the fine grants, so no coarse read+write bundle
  can be partly-prohibited by a read-only intent.
- policy.abstract.md: remove prohibition prose (exclusively / do not
  work in source / no access to source / read-only); rely on
  deny-by-default for absences. Facts unchanged.
- policy-pipeline.md: revert Expected-output + Further-Notes to
  ALLOW-only (both variants, empty *_deny_scopes), point deny-extraction
  at #142, and sync the embedded policy.md variants + surrounding prose.

scenario.py needs no change: its pair-lists already match the demo
facts. Unit keep-green gate stays green (528 passed); the 43 integration
cases collect cleanly.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
…t loop

Rewrite the policy-pipeline (5.3) and uc1-onboarding-pipeline (5.4.x)
integration-test specs so they describe the current live enforcement
loop instead of the retired "generate Rego, then `opa eval` offline"
model. The live drivers were already migrated (handoff 08); the specs
were the only artifacts still describing the old architecture.

Both specs now describe: onboard through the in-cluster UC-1 Controller
(POST /apply/service/{id}, upserting the AuthorizationPolicy CR) ->
enable the outbound token-exchange leg -> drive real HTTP through
AuthBridge -> assert the deployed OPA plugin's allow/deny, with
AuthBridge's own parsers (jwt-validation, token-exchange, mcp-parser)
building the input documents. Removed opa eval / standalone opa binary /
$OPA_BIN / .rego dumps / REGO_OUTPUT_DIR / rego_out / kubectl cp of
/rego / probe.rego / probe_uc1.rego / uvicorn subprocesses / the
filesystem-stub writer as the test mechanism. Skip condition is now
"cluster/pipeline not wired or env unset," matching aiac/CLAUDE.md.

policy-pipeline now describes a single live policy.md (cross-variant
equivalence deferred to rung 5.4.4); uc1-onboarding no longer claims
"live enforcement out of scope" / "no A2A message is ever sent."
Scenario facts, truth tables, devops deny-by-default, the ALLOW-only
fixture framing (#142), UC-1 discovery semantics, and
onboarding-order-independence are preserved unchanged. Doc-only; no
code, driver, k8s/ file, or aiac/CLAUDE.md touched.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
Add test/integration/test_policy_pipeline_denyworld.py, the ALLOW-default
sibling of test_policy_pipeline.py (#151, epic #142). It onboards Policy B
(permissive-default prose with prohibitions + exclusive scoping) through the
real UC-1 pipeline via the #149 policy-agnostic harness
(onboarded_stack(..., policy_md=POLICY_DENYWORLD, default_effect=ALLOW,
ready_signals=...)), drives real HTTP through AuthBridge, and asserts the
deployed OPA plugin enforces the §6 allow/deny matrix from the #148 oracle.

Why default_effect=ALLOW: under deny-by-default an explicit DENY is invisible
at the enforced seam (an ungranted pair is already denied), so flipping the
default makes each DENY the only thing that can deny a pair — proving DENY
enforcement end-to-end through PRB -> PCE -> Rego -> bundle -> OPA. The
devops -> issues-* = allow cells are the default-flip tracer (deny under
Policy A).

- pytestmark = pytest.mark.integration (excluded from the keep-green lane).
- Expected verdicts computed from the #148 oracle functions
  (expected_inbound_denyworld / expected_outbound_denyworld_bare) — no second
  copy of the matrix; the fixture-independent §6 contract stays in the #148
  test_scenario_uc1_denyworld.py unit module.
- Policy-B-aware convergence signals (dev-user inbound allow; devops-user
  issues-read allow tracer; test-user source-read deny).
- Negative controls resolved against the ALLOW-mode Rego: an unmatched tool
  name is in no deny map, so no deny gate fires and it falls through to the
  permissive default = allow (the mirror of the deny-by-default control).
- Teardown (in the harness) resets default_effect to Deny + cleans Keycloak so
  a subsequent Policy-A run on the shared stack is unaffected.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
Under the DENY-aware PRB the shared pipeline fixture failed closed: the
audit task raised PolicyContradictionError for the github-agent policy,
erroring out all 43 test_policy_pipeline.py cases at setup. Root cause
was policy.explicit.md granting developer the coarse issues-access
(read+write bundle) but only fine issues-read, which the auditor read as
a coarse-scope prohibition.

Keep the fixture ALLOW-only for now (matching the UC-1 demo.md shape),
deferring the ALLOW+DENY path to sibling issue #142:

- policy.explicit.md: drop the coarse '## Users -> agent capabilities'
  block; keep purely-positive fine-grained grants. Inbound mapping (a)
  is now derived from the fine grants, so no coarse read+write bundle
  can be partly-prohibited by a read-only intent.
- policy.abstract.md: remove prohibition prose (exclusively / do not
  work in source / no access to source / read-only); rely on
  deny-by-default for absences. Facts unchanged.
- policy-pipeline.md: revert Expected-output + Further-Notes to
  ALLOW-only (both variants, empty *_deny_scopes), point deny-extraction
  at #142, and sync the embedded policy.md variants + surrounding prose.

scenario.py needs no change: its pair-lists already match the demo
facts. Unit keep-green gate stays green (528 passed); the 43 integration
cases collect cleanly.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
…t loop

Rewrite the policy-pipeline (5.3) and uc1-onboarding-pipeline (5.4.x)
integration-test specs so they describe the current live enforcement
loop instead of the retired "generate Rego, then `opa eval` offline"
model. The live drivers were already migrated (handoff 08); the specs
were the only artifacts still describing the old architecture.

Both specs now describe: onboard through the in-cluster UC-1 Controller
(POST /apply/service/{id}, upserting the AuthorizationPolicy CR) ->
enable the outbound token-exchange leg -> drive real HTTP through
AuthBridge -> assert the deployed OPA plugin's allow/deny, with
AuthBridge's own parsers (jwt-validation, token-exchange, mcp-parser)
building the input documents. Removed opa eval / standalone opa binary /
$OPA_BIN / .rego dumps / REGO_OUTPUT_DIR / rego_out / kubectl cp of
/rego / probe.rego / probe_uc1.rego / uvicorn subprocesses / the
filesystem-stub writer as the test mechanism. Skip condition is now
"cluster/pipeline not wired or env unset," matching aiac/CLAUDE.md.

policy-pipeline now describes a single live policy.md (cross-variant
equivalence deferred to rung 5.4.4); uc1-onboarding no longer claims
"live enforcement out of scope" / "no A2A message is ever sent."
Scenario facts, truth tables, devops deny-by-default, the ALLOW-only
fixture framing (#142), UC-1 discovery semantics, and
onboarding-order-independence are preserved unchanged. Doc-only; no
code, driver, k8s/ file, or aiac/CLAUDE.md touched.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
anatolykoyfman added a commit that referenced this pull request Aug 26, 2026
Add test/integration/test_policy_pipeline_denyworld.py, the ALLOW-default
sibling of test_policy_pipeline.py (#151, epic #142). It onboards Policy B
(permissive-default prose with prohibitions + exclusive scoping) through the
real UC-1 pipeline via the #149 policy-agnostic harness
(onboarded_stack(..., policy_md=POLICY_DENYWORLD, default_effect=ALLOW,
ready_signals=...)), drives real HTTP through AuthBridge, and asserts the
deployed OPA plugin enforces the §6 allow/deny matrix from the #148 oracle.

Why default_effect=ALLOW: under deny-by-default an explicit DENY is invisible
at the enforced seam (an ungranted pair is already denied), so flipping the
default makes each DENY the only thing that can deny a pair — proving DENY
enforcement end-to-end through PRB -> PCE -> Rego -> bundle -> OPA. The
devops -> issues-* = allow cells are the default-flip tracer (deny under
Policy A).

- pytestmark = pytest.mark.integration (excluded from the keep-green lane).
- Expected verdicts computed from the #148 oracle functions
  (expected_inbound_denyworld / expected_outbound_denyworld_bare) — no second
  copy of the matrix; the fixture-independent §6 contract stays in the #148
  test_scenario_uc1_denyworld.py unit module.
- Policy-B-aware convergence signals (dev-user inbound allow; devops-user
  issues-read allow tracer; test-user source-read deny).
- Negative controls resolved against the ALLOW-mode Rego: an unmatched tool
  name is in no deny map, so no deny gate fires and it falls through to the
  permissive default = allow (the mirror of the deny-by-default control).
- Teardown (in the harness) resets default_effect to Deny + cleans Keycloak so
  a subsequent Policy-A run on the shared stack is unaffected.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Anatoly Koyfman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants