Skip to content

Enable SPIRE in Keycloak - #238

Merged
rubambiza merged 19 commits into
rossoctl:mainfrom
Alan-Cha:spiffe-keycloak-consolidation
Mar 23, 2026
Merged

rubambiza merged 19 commits into
rossoctl:mainfrom
Alan-Cha:spiffe-keycloak-consolidation

Conversation

@Alan-Cha

@Alan-Cha Alan-Cha commented Mar 16, 2026 •

Copy link
Copy Markdown
Member

Enable SPIFFE/Keycloak Federated JWT Authentication

Fixes #82
Replaces #187
Replaces #159

This PR enables AuthBridge workloads to authenticate to Keycloak using SPIFFE JWT-SVIDs instead of traditional client secrets, enabling true zero-trust workload identity.

Changes

Core SPIFFE Integration

  • client-registration: Add CLIENT_AUTH_TYPE support for federated-jwt authentication mode

    • Workloads can now authenticate using JWT-SVIDs from SPIRE
    • Configurable via CLIENT_AUTH_TYPE env var (client-secret or federated-jwt)
    • Adds SPIFFE_IDP_ALIAS and JWT_AUDIENCE configuration
  • go-processor: Enable SPIFFE JWT-SVID support for token exchange

    • Read JWT-SVIDs from /opt/jwt_svid.token
    • Support federated JWT authentication in token exchange flows
    • Improved credential file reading with retries
  • container-builder: Inject SPIFFE configuration env vars

    • CLIENT_AUTH_TYPE - authentication mode selection
    • SPIFFE_IDP_ALIAS - Keycloak IdP alias for SPIFFE validation
    • JWT_AUDIENCE - expected audience for JWT-SVID validation

Shared Volume Access

  • Add fsGroup support to enable multiple containers to access SPIFFE credentials
    • Set fsGroup: 1000 on pod security context
    • Allows spiffe-helper, client-registration, and envoy-proxy to share JWT-SVIDs

Testing & Documentation

  • Local testing scripts (verify-spire-keycloak.sh, local-build-and-test.sh)
  • Comprehensive testing guide (LOCAL_TESTING_GUIDE.md)

Architecture

SPIFFE Authentication Flow:

  1. spiffe-helper → writes JWT-SVID to /opt/jwt_svid.token
  2. client-registration → reads JWT-SVID, authenticates to Keycloak using federated-jwt
  3. envoy-proxy → performs token exchange using JWT-SVID credentials
  4. fsGroup: 1000 → enables all containers to share the JWT-SVID file

Related PRs

  • kagenti: [Link to kagenti PR with SPIRE configuration]

Testing

Tested locally with SPIRE and Keycloak using the provided test scripts. Verified:

  • ✅ JWT-SVID authentication to Keycloak
  • ✅ Token exchange with federated JWT
  • ✅ Shared volume access between sidecars

@Alan-Cha
Alan-Cha force-pushed the spiffe-keycloak-consolidation branch from 2e24cb9 to 67bbfef Compare March 17, 2026 03:07
@Alan-Cha
Alan-Cha marked this pull request as ready for review March 17, 2026 05:22
@Alan-Cha
Alan-Cha requested a review from mrsabath March 17, 2026 05:22
@Alan-Cha
Alan-Cha requested a review from maia-iyer March 17, 2026 05:23
@Alan-Cha

Copy link
Copy Markdown
Member Author

To test, checkout this branch and the Kagenti branch (both named spiffe-keycloak-consolidation).

Follow the instructions in https://github.com/kagenti/kagenti-extensions/pull/238/changes#diff-2fbfbab126361511fee5f62d347944bf76c5ac003f078bb1c422e2dbc85f9e70 which will describe how to:

  • create a new cluster
  • build new local images
  • install Kagenti with support for federated client authentication
  • create a test deployment
  • obtain a token using federated client auth through the test deployment.

After the PR has been validated, I will remove the following files:

  • ./LOCAL_TESTING_GUIDE.md
  • ./verify-spire-keycloak.sh
  • kagenti/deployments/envs/dev_values_local_images.yaml

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good work on the SPIFFE/Keycloak federated JWT integration. The core changes to go-processor/main.go and client_registration.py are well-structured, and the webhook mutations (container_builder.go, pod_mutator.go) correctly inject env vars and fsGroup with proper defensive checks. The bulk realm rename from demo to kagenti is consistent.

One blocking issue: hardcoded developer path in the build script. Two additional suggestions below.

Also note: Two stale realms/demo references remain in commented-out lines:

  • AuthBridge/k8s/configmaps-webhook.yaml line 25
  • AuthBridge/demos/webhook/k8s/configmaps-webhook.yaml line 38

Areas reviewed: Go, Python, Shell, YAML/K8s, Docs, Security
Commits: 12, all signed-off: yes
CI: All passing

Comment thread local-build-and-test.sh Resolved
Comment thread local-build-and-test.sh Resolved
Comment thread AuthBridge/keycloak_sync.py Resolved

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review Summary (Follow-up)

Previous REQUEST_CHANGES issues all addressed:

  • ✅ Hardcoded developer path → now uses KAGENTI_DIR env var with auto-detection
  • ✅ Stale help text (default: demo) → updated to (default: kagenti)
  • ✅ Stale realms/demo references → all cleaned up

New observations

suggestion: DEFAULT_OUTBOUND_POLICY validation was removed (line ~776 of main.go). The old code validated "exchange" or "passthrough" and warned on unknown values. The new code accepts any string — a typo like exchnage would silently break routing. Consider restoring the validation.

praise: Webhook changes are well-done:

  • container_builder.go: SPIFFE env vars injected from authbridge-config ConfigMap with Optional: ptr.To(true) — won't break non-SPIFFE pods
  • pod_mutator.go: ensureFSGroup() is defensive — checks nil SecurityContext and only sets if not already present
  • client_registration.py: derive_keycloak_config_from_token_url() is a clean backward-compat helper

nit: getConfig() now returns 7 values — consider returning the Config struct (or a copy) instead. Not blocking.

Areas reviewed: Go (AuthProxy + webhook), Python, Shell, K8s YAML, Docs, Security
Commits: 14, all signed-off
CI status: 17/17 passing

LGTM — SPIFFE federated JWT is a solid addition with proper feature gating.

Alan-Cha added 15 commits March 20, 2026 09:13
Updated LOCAL_TESTING_GUIDE.md to:
- Add prominent warning to use ./local-build-and-test.sh script
- Clarify that script builds from both kagenti and kagenti-extensions repos
- Add "Common Mistakes to Avoid" section
- Add comprehensive troubleshooting section for:
  - ErrImageNeverPull errors
  - RBAC permission issues
  - Token exchange failures
  - Client configuration problems

Added FEDERATED_JWT_FIXES.md documenting:
- All issues found during federated-jwt testing
- Manual fixes applied during testing
- Required changes to charts and scripts
- Current status of each fix (done, missing, to verify)

These docs prevent users from making the same mistakes and provide
clear troubleshooting paths when issues occur.

Signed-off-by: Alan Cha <[email protected]>
Signed-off-by: Alan Cha <[email protected]>
Signed-off-by: Alan Cha <[email protected]>
Signed-off-by: Alan Cha <[email protected]>
Signed-off-by: Alan Cha <[email protected]>
The test deployment in LOCAL_TESTING_GUIDE.md was missing the
shared-data volume and /shared mount that client-registration needs
to write client-secret.txt.

This caused an error:
  Error writing secret to file: [Errno 2] No such file or directory: '/shared/client-secret.txt'

Changes:
- Add shared-data emptyDir volume
- Mount at /shared in client-registration container (read-write)
- Mount at /shared in tools container (read-only)

Now matches the volume structure injected by the webhook's
container_builder.go (svid-output at /opt, shared-data at /shared).

Signed-off-by: Alan Cha <[email protected]>
@Alan-Cha
Alan-Cha force-pushed the spiffe-keycloak-consolidation branch from 6b83be9 to ad56660 Compare March 20, 2026 14:27
After merging actor token support with JWT-SVID reload functionality,
exchangeToken now requires 9 parameters instead of 7:
- Added: spireEnabled (bool)
- Added: jwtSvidPath (string)

Updated test calls to pass false/empty for these new parameters since
the actor token tests don't use SPIRE JWT-SVID authentication.

Fixes CI error:
  not enough arguments in call to exchangeToken
  have (string, string, string, string, string, string, string)
  want (string, string, string, string, string, string, bool, string, string)

Signed-off-by: Alan Cha <[email protected]>
Address code review feedback:

1. Add DEFAULT_OUTBOUND_POLICY validation (suggestion)
   - Now validates "exchange" or "passthrough"
   - Invalid values fallback to "passthrough" with warning
   - Prevents silent failures from typos like "exchnage"

2. Refactor getConfig() to return struct (nit)
   - Changed from returning 7 individual values to Config struct
   - Improves readability and maintainability
   - Easier to extend in the future

Changes:
- main.go:247-256: getConfig() now returns Config struct
- main.go:636-743: Updated call sites to use config.Field syntax
- main.go:824-837: Updated inbound JWT validation setup
- main.go:852-859: Added DEFAULT_OUTBOUND_POLICY validation

All tests passing.

Signed-off-by: Alan Cha <[email protected]>
Exclude JWT rotation test script from version control.
The script is useful for local testing but not intended to be checked in.

Signed-off-by: Alan Cha <[email protected]>
@rubambiza

rubambiza commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Apologies for the bit of tangled history (i.e., merging main into the current branch). I fell for the "fix in web" trick 😅 Merging as one of the action items for weekly report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Use SPIRE JWT to authenticate with Keycloak (use Federated Client Auth)

3 participants