Skip to content

#1148 - Fetch secret from cluster instead of cache on every api request - #1159

Open
kushnaidu wants to merge 2 commits into
kptdev:mainfrom
Nordix:fetch-secret-on-every-api-call
Open

#1148 - Fetch secret from cluster instead of cache on every api request#1159
kushnaidu wants to merge 2 commits into
kptdev:mainfrom
Nordix:fetch-secret-on-every-api-call

Conversation

@kushnaidu

@kushnaidu kushnaidu commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Fetch secret from cluster instead of cache on every api request


Description

  • What changed:
    getAuthMethod now re-reads the repository Secret from the Kubernetes API on every git operation instead of caching it indefinitely. The credential field, forceRefresh parameter, and inner auth-retry in doGitWithAuth were removed as they became redundant.

  • Why it’s needed:
    Previously, when a user updated a repository Secret (e.g., switching from gitea_user1 to gitea_user2), neither porch-server nor porch-controller would pick up the change unless authentication failed first. If the old credentials still worked, the new ones were never used. For correctness and security, we need to always have the latest secret data.

  • How it works:
    Each git operation calls ResolveCredential, which does a single client.Get on the Secret — a lightweight K8s API call (~5ms) that's negligible compared to the git network operation it precedes. The outer retry loops (fetchRemoteRepositoryWithRetry, pushAndCleanup) handle transient failures, and each retry gets fresh credentials automatically.


Related Issue(s)


Type of Change

  • Bug fix
  • New feature
  • Enhancement
  • Refactor
  • Documentation
  • Tests
  • Other: ________

Checklist

  • Code follows project style guidelines
  • Self-reviewed changes
  • Tests added/updated
  • Documentation added/updated
  • All tests and gating checks pass

Testing Instructions (Optional)


Additional Notes (Optional)

  • Known issues:
  • Further improvements:
  • Review notes:

AI Disclosure

  • I have used AI in the creation of this PR.

Kiro

Signed-off-by: Kushal Harish Naidu <kushal.harish.naidu@ericsson.com>
@kushnaidu
kushnaidu requested review from a team August 27, 2026 11:57
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 27, 2026
@netlify

netlify Bot commented Aug 27, 2026

Copy link
Copy Markdown

Deploy Preview for kpt-porch ready!

Name Link
🔨 Latest commit e0a71cc
🔍 Latest deploy log https://app.netlify.com/projects/kpt-porch/deploys/6a90287994faae000798c635
😎 Deploy Preview https://deploy-preview-1159--kpt-porch.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

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

Updates git repository authentication to always re-resolve credentials from the backing Kubernetes Secret for each git operation, ensuring Secret updates are picked up immediately across porch-server and porch-controller.

Changes:

  • Removed indefinite in-memory credential caching in gitRepository.getAuthMethod; credentials are now re-read on every call.
  • Simplified doGitWithAuth by removing the inner “auth failed → force refresh → retry” logic.
  • Added unit tests covering “no secret”, “secret change is picked up”, resolver error propagation, and concurrent access behavior.

Reviewed changes

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

File Description
pkg/externalrepo/git/git.go Always re-fetches credentials from the Secret per git operation and removes inner auth-refresh retry logic.
pkg/externalrepo/git/git_test.go Adds focused tests validating the new credential re-resolution behavior and concurrency expectations.
Suppressed comments (1)

pkg/externalrepo/git/git_test.go:2682

  • Same as above: avoid a direct type assertion here so test failures are clearer and don’t panic if the auth method type changes.
	basicAuth = auth.(*http.BasicAuth)
	if basicAuth.Username != "user2" {
		t.Fatalf("expected user2 after secret change, got %s", basicAuth.Username)
	}

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pkg/externalrepo/git/git.go Outdated
Comment thread pkg/externalrepo/git/git_test.go Outdated
Signed-off-by: Kushal Harish Naidu <kushal.harish.naidu@ericsson.com>
@sonarqubecloud

sonarqubecloud Bot commented Aug 27, 2026

Copy link
Copy Markdown

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

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants