Skip to content

fix(runner): delete the previous refresh-token secret on rotation - #7052

Merged
ChrisJBurns merged 4 commits into
stacklok:mainfrom
feiiiiii5:fix/refresh-token-secret-cleanup
Nov 27, 2026
Merged

ChrisJBurns merged 4 commits into
stacklok:mainfrom
feiiiiii5:fix/refresh-token-secret-cleanup

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Summary

  • The runner generated a new OAUTH_REFRESH_TOKEN_<workload>_<timestamp>_<rand> secret on every refresh/restart and never deleted the secret referenced by the previous CachedRefreshTokenRef, so the workload namespace accumulated one secret per token rotation.
  • When persisting a rotated token, capture the old ref from the stored state, save the new reference, and best-effort delete the old secret.

Fixes #3886

Type of change

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

Test plan

  • Unit tests (go test ./pkg/runner -count=1 passes)
  • E2E tests (task test-e2e)
  • Linting (go vet ./pkg/runner and gofmt clean)
  • Manual testing (describe below)

The new TestRunner_PersistRefreshTokenDeletesPreviousSecret seeds stored state with an old ref, runs persistRefreshToken against the mock provider, and asserts the new secret is written, state is saved, and the old secret is deleted. Before the fix the test fails on the missing DeleteSecret call; after the fix it passes. Delete failure is only logged, so a provider that cannot delete does not block the new token.

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.

Changes

File Change
pkg/runner/runner.go Capture the previous refresh-token ref and delete the old secret after the new one is persisted (best effort)
pkg/runner/persist_refresh_token_test.go Regression asserting the old secret is deleted after a successful rotation

Does this introduce a user-facing change?

No. The visible token behaviour is unchanged; only the stale secret is removed.

Implementation plan

Approved implementation plan
  1. Read the current persistRefreshToken flow and the stored state contract.
  2. Capture the old CachedRefreshTokenRef before overwriting, and delete it only after the new secret and state are saved.
  3. Keep delete failures non-fatal (log with the secret name, no secret contents).
  4. Add the mock-based regression and run go test ./pkg/runner.

Special notes for reviewers

The previous ref is read from the stored workload state rather than kept in memory across restarts, so the cleanup also works for the restart path. The delete is deliberately after SaveState: if saving the new reference fails, the old secret is still the one the stored state points at.

persistRefreshToken generated a unique secret name every refresh/restart and never removed the secret referenced by the previous CachedRefreshTokenRef, so OAUTH_REFRESH_TOKEN secrets accumulated. Capture the old ref from stored state, save the new one, then best-effort delete the old secret; a delete failure only warns. Add a mock-based regression asserting the delete.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-04T11:54:24.848882Z
We couldn’t complete this review. Comment @codex review to try again.
ca873d8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.56%. Comparing base (0a03209) to head (d9cd809).
⚠️ Report is 204 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7052      +/-   ##
==========================================
+ Coverage   80.64%   82.56%   +1.92%     
==========================================
  Files         858      940      +82     
  Lines       87646    96657    +9011     
==========================================
+ Hits        70682    79805    +9123     
+ Misses      16959    16847     -112     
  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.

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

See the inline comments.

Comment thread pkg/runner/runner.go Resolved
@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed finding — it was correct. I added IsWorkloadOwnedSecretName() next to GenerateUniqueSecretNameWithPrefix and gated the deletion on it, so a config exported from another workload no longer deletes that workload's live refresh token. The name pattern accepts only the exact base name or the generator's <base>_<digits>_<8 hex> form, anchored, so foo cannot match foo_1. Base-red test reproduced the cross-workload deletion; with the fix both packages pass (748 checks) and go vet/gofmt are clean.

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

  • Possibly important · correctness · pre-existing — Secret stored before a SaveState failure is left orphaned, with no cleanup on the error path (pkg/runner/runner.go:1034)

    If SetSecret succeeds but updatedConfig.SaveState fails, the function returns an error and leaves the newly stored secretName in the provider with nothing referencing it. The live config keeps the previous ref, so nothing will ever delete the new secret. Since this PR is about stopping OAUTH_REFRESH_TOKEN_* orphans, this error path is a remaining source of them.

    Recommendation: On SaveState failure, best-effort DeleteSecret(ctx, secretName) when secretName != previousSecretName, and log the failure. Also extend TestRunner_PersistRefreshToken_SaveStateFailurePreservesLiveConfig to assert the previous secret is not deleted.

Comment thread pkg/auth/secrets/secrets.go Resolved
@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Addressed the review finding on the SaveState error path. persistRefreshToken now best-effort deletes the just-stored secretName when SaveState fails and secretName != previousSecretName, logging any deletion failure; the original storage error is still what gets returned. The regression test now expects that deletion and explicitly expects no deletion of the previous generation's secret.

Verification: base-red 1 failed (gomock missing DeleteSecret(failed-refresh) call); with fix the focused TestRunner_PersistRefreshToken* tests and the full pkg/runner package pass; go vet and gofmt -l clean. Branch head updated to the amended commit.

@ChrisJBurns

Copy link
Copy Markdown
Collaborator

/retest

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.

Refresh token secrets accumulate

2 participants