Skip to content

Redact token exchange secrets on workload export - #7449

Open
Tyagiquamar wants to merge 1 commit into
stacklok:mainfrom
Tyagiquamar:fix/7439-export-redact-token-exchange-secret
Open

Tyagiquamar wants to merge 1 commit into
stacklok:mainfrom
Tyagiquamar:fix/7439-export-redact-token-exchange-secret

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

GET /api/v1beta/workloads/{name}/export redacted OIDC client secrets but returned the token exchange client secret in plain text, both in the canonical token_exchange_config field and inside the serialized parameters of the token exchange middleware entry.

  • redactRunConfigOIDCClientSecrets is renamed to redactRunConfigClientSecrets and now also clears RunConfig.TokenExchangeConfig.ClientSecret on the exported copy.
  • redactAuthMiddlewareConfigs is renamed to redactMiddlewareClientSecrets and now handles tokenexchange middleware entries in addition to auth entries, clearing token_exchange_config.client_secret inside their parameters. Other middleware types pass through unchanged, and the loaded on-disk configuration is never mutated.

Fixes #7439

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Root cause

redactRunConfigOIDCClientSecrets (pkg/api/v1/workloads.go) only cleared OIDCConfig.ClientSecret and the OIDC client secret inside auth middleware parameters. PopulateMiddlewareConfigs embeds the full tokenexchange.Config (including ClientSecret) into the tokenexchange middleware parameters, and RunConfig.TokenExchangeConfig is persisted alongside it, so a workload configured with token exchange exported its client secret to any caller of the export endpoint.

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Local environment has no task/Go toolchain, so checks ran inside golang:1.27 Docker with named Go cache volumes:

  • go build ./pkg/api/v1/ - passed
  • go test ./pkg/api/v1/ -run "TestExportWorkload" -v - passed (2 tests)
  • go test ./pkg/api/v1/ - passed
  • go vet ./pkg/api/v1/ - clean
  • gofmt on the LF-normalized files - clean (working tree is CRLF via core.autocrlf; committed content is LF)

New regression test TestExportWorkload_RedactsTokenExchangeClientSecret saves a workload with TokenExchangeConfig set, verifies the secret is present in both the canonical field and the middleware parameters before export, then asserts the export response contains no secret while the persisted state still has it.

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label is applied and the migration guidance is described above.

Does this introduce a user-facing change?

Yes: the workload export endpoint no longer returns the token exchange client secret. Exported configurations that previously carried client_secret values for token exchange now export it as an empty string, matching the existing OIDC redaction behavior.

Special notes for reviewers

The existing OIDC redaction test was updated only for the function rename; its assertions are unchanged.

The export endpoint cleared OIDC client secrets but returned the token exchange client secret in token_exchange_config and in the token exchange middleware parameters. Clear it in both places, matching the OIDC behavior, without mutating the persisted configuration.

Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com>
@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.85%. Comparing base (6d6ace5) to head (4f260c6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/api/v1/workloads.go 86.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7449      +/-   ##
==========================================
+ Coverage   82.80%   82.85%   +0.05%     
==========================================
  Files         949      952       +3     
  Lines       98157    98445     +288     
==========================================
+ Hits        81280    81569     +289     
+ Misses      16872    16871       -1     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek jhrozek 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.

Thanks for this. Clearing both the typed field and the copy inside the middleware params is the right fix, and the test that checks the secret string never shows up in the response body is a good guard. One thing I'd like changed in the test (inline), plus a few smaller notes.

For the PR description: this changes what a client sees, so it'd help to spell it out under the user-facing section. Export now returns an empty token exchange client_secret, so anyone re-importing an export has to supply it again, same as the OIDC secret today. Also thv export --format json still writes the loaded config unredacted (cmd/thv/app/export.go), which is probably fine for a local 0600 file, but worth saying it's deliberate.

The @description on exportWorkload (workloads.go:695) doesn't mention that credentials are blanked. A short note there would help API consumers. If you change it, task docs needs a re-run so the swagger files match.

require.NoError(t, json.Unmarshal(w.Body.Bytes(), &exported))
require.NotNil(t, exported.TokenExchangeConfig)
assert.Empty(t, exported.TokenExchangeConfig.ClientSecret)
for _, middlewareConfig := range exported.MiddlewareConfigs {

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.

This loop can run zero times, so the test would still pass if export dropped the token exchange middleware entry entirely (the NotContains check above would be satisfied too). The input side has require.True(foundTokenExchangeMiddleware), the response side needs the same.

While you're here, could the fixture put a token exchange entry in AdditionalMiddlewareConfigs as well, with a different secret? That list goes through its own redaction call and isn't covered right now. Then require both entries in the response, check their client IDs/token URLs survived, and assert neither secret appears in the body.

Comment thread pkg/api/v1/workloads.go
return nil, fmt.Errorf("failed to encode auth middleware configuration: %w", err)
}
redacted[i].Parameters = parameters
case tokenexchange.MiddlewareType:

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.

Small thing, not a blocker: this decodes into tokenexchange.MiddlewareParams and re-marshals, so any member of the stored params that this binary doesn't declare gets dropped from the export (it used to pass through untouched). In practice LoadState already drops unknown fields elsewhere in the RunConfig and the auth case above behaves the same way, so I wouldn't hold the PR on it. If you'd rather not drop anything, redacting on a map[string]json.RawMessage (and the nested token_exchange_config) would do it, ideally for both cases. Otherwise a line in the PR description noting the behavior is enough.

This branch has not been deployed

No deployments
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.

Workload export: redact the token exchange client secret

2 participants