Skip to content

fix: resolve credentials for protected HTTP(S) repositories#3637

Open
Ankitsinghsisodya wants to merge 3 commits intoknative:mainfrom
Ankitsinghsisodya:fix/git-credential-helper
Open

fix: resolve credentials for protected HTTP(S) repositories#3637
Ankitsinghsisodya wants to merge 3 commits intoknative:mainfrom
Ankitsinghsisodya:fix/git-credential-helper

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

@Ankitsinghsisodya Ankitsinghsisodya commented Apr 22, 2026

Closes #3415

Summary

  • On HTTP(S) clone failure with ErrAuthenticationRequired, retry using credentials sourced from ~/.git-credentials (git credential store format) and ~/.netrc.
  • Credential lookup is pure-Go — no subprocesses spawned.
  • Both FilesystemFromRepo (in-memory clone) and Repository.Write (disk clone) are covered.

Problem

kn func create --repository <internal-gitlab-url> failed with authentication required even when the user had credential.helper = store configured and ~/.git-credentials populated, because go-git does not consult the system credential helpers on its own.

Approach

  1. Attempt clone without credentials (unchanged behaviour for public repos).
  2. On ErrAuthenticationRequired, call credentialsForURL which reads, in order:
    • ~/.git-credentials — matches on scheme + hostname
    • ~/.netrc — matches on hostname, falls back to default stanza
  3. Retry the clone with the resolved BasicAuth.

Testing

  • Unit tests for credentialsForURL, credentialsFromGitStore, credentialsFromNetRC, scanNetRC, and isAuthError added in pkg/functions/repository_credentials_test.go.
  • Covers: HTTPS match, token auth (x-oauth-basic), no-match, non-HTTP schemes, missing file, netrc fallback, and wrapped error detection.

Notes

This fix targets credential.helper = store (flat-file) users. SSH and system keychain helpers are out of scope for this change.

Copilot AI review requested due to automatic review settings April 22, 2026 21:07
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos April 22, 2026 21:07
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented Apr 22, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign matzew for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the size/L 🤖 PR changes 100-499 lines, ignoring generated files. label Apr 22, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented Apr 22, 2026

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@knative-prow knative-prow Bot added the needs-ok-to-test 🤖 Needs an org member to approve testing label Apr 22, 2026
@Ankitsinghsisodya Ankitsinghsisodya force-pushed the fix/git-credential-helper branch 2 times, most recently from ce89b19 to f3791f9 Compare April 22, 2026 21:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds HTTP(S) credential resolution for protected template repositories by retrying go-git clones with BasicAuth sourced from ~/.git-credentials and ~/.netrc, addressing cases where go-git doesn’t consult system credential helpers.

Changes:

  • Retry git.Clone / git.PlainClone on transport.ErrAuthenticationRequired using credentials resolved from ~/.git-credentials, then ~/.netrc.
  • Implement pure-Go parsers for git credential-store lines and a minimal netrc scanner.
  • Add unit tests covering core credentialsForURL behavior and isAuthError.

Reviewed changes

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

File Description
pkg/functions/repository.go Adds credential lookup + auth-retry logic for in-memory and disk clones; introduces git-credentials and netrc parsing helpers.
pkg/functions/repository_credentials_test.go Adds tests for credential resolution and auth-error detection.

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

Comment thread pkg/functions/repository.go
Comment thread pkg/functions/repository.go
Comment thread pkg/functions/repository.go Outdated
Comment thread pkg/functions/repository.go
Comment thread pkg/functions/repository.go
Comment thread pkg/functions/repository_credentials_test.go
Comment on lines +31 to +33
// TestCredentialsForURL_HTTPS verifies that credentials stored in
// ~/.git-credentials are returned for a matching HTTPS URL.
func TestCredentialsForURL_HTTPS(t *testing.T) {
@Ankitsinghsisodya Ankitsinghsisodya force-pushed the fix/git-credential-helper branch from f3791f9 to 20d0d3c Compare April 22, 2026 21:13
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 64.18919% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.66%. Comparing base (33a9d85) to head (dd8c16e).

Files with missing lines Patch % Lines
pkg/functions/repository.go 64.18% 40 Missing and 13 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3637      +/-   ##
==========================================
+ Coverage   56.61%   56.66%   +0.05%     
==========================================
  Files         181      181              
  Lines       20853    20992     +139     
==========================================
+ Hits        11806    11896      +90     
- Misses       7833     7871      +38     
- Partials     1214     1225      +11     
Flag Coverage Δ
e2e 36.01% <6.08%> (-0.26%) ⬇️
e2e go 32.66% <0.00%> (-0.26%) ⬇️
e2e node 28.40% <0.00%> (-0.22%) ⬇️
e2e python 33.03% <7.31%> (-0.25%) ⬇️
e2e quarkus 28.54% <0.00%> (-0.23%) ⬇️
e2e rust 27.95% <0.00%> (-0.22%) ⬇️
e2e springboot 26.45% <0.00%> (-0.19%) ⬇️
e2e typescript 28.50% <0.00%> (-0.23%) ⬇️
e2e-config-ci 17.84% <0.00%> (-0.14%) ⬇️
integration 17.23% <7.31%> (-0.10%) ⬇️
unit macos-14 44.79% <65.04%> (+0.13%) ⬆️
unit macos-latest 44.79% <65.04%> (+0.13%) ⬆️
unit ubuntu-24.04-arm 45.02% <64.18%> (+0.11%) ⬆️
unit ubuntu-latest 45.63% <65.04%> (+0.12%) ⬆️
unit windows-latest 44.77% <65.04%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@matejvasek
Copy link
Copy Markdown
Contributor

matejvasek commented Apr 29, 2026

Nice addition!

One thing: it appears you are matching only host, not port. Port also should match (with possible exception of implicit ports 80 and 443). On the other hand scheme matching seems to be too restrictive. Credentials valid for http://localhost should match https://localhost in my opinion.

Examples:
http://example.com matches https://example.com
http://example.com:8080 does not match http://example.com

@Ankitsinghsisodya Ankitsinghsisodya force-pushed the fix/git-credential-helper branch from 20d0d3c to ea9b27d Compare May 6, 2026 05:49
@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor Author

Nice addition!

One thing: it appears you are matching only host, not port. Port also should match (with possible exception of implicit ports 80 and 443). On the other hand scheme matching seems to be too restrictive. Credentials valid for http://localhost should match https://localhost in my opinion.

Examples: http://example.com matches https://example.com http://example.com:8080 does not match http://example.com

Done sir

// scanNetRC is a minimal netrc(5) token scanner. It returns the login and
// password for the first "machine" stanza whose name matches host. Falls back
// to the "default" stanza when no exact match is found.
func scanNetRC(content, host string) (login, password string) {
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.

btw would it be worth simply adding a ignore case for line starting w #? AFAIK netrc does not officially support comments but apparently programs like curl do actually mention that its ok to use and they do ignore them. Python has some issues i found python/cpython#104511 but also supports them.

So maybe adding that would make this code a bit more robust. just a thought
cc @matejvasek

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.

ignore case for line starting w #

you mean skip lines starting with # ?
First time I read that I thought you wanted to turn down case sensitivity 😄
Yes we should skip lines starting with # IMO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have handled the problem. scanNetRC skips lines starting with # before tokenising... I have also added a test TestScanNetRC_CommentLinesIgnored to cover it...

Comment thread pkg/functions/repository.go
@Ankitsinghsisodya Ankitsinghsisodya force-pushed the fix/git-credential-helper branch from ea9b27d to 8c8c32d Compare May 7, 2026 11:15
@knative-prow knative-prow Bot added size/XL 🤖 PR changes 500-999 lines, ignoring generated files. and removed size/L 🤖 PR changes 100-499 lines, ignoring generated files. labels May 7, 2026
When --repository points to a protected HTTP(S) remote, go-git's pure-Go
clone had no access to the system credential stack, causing authentication
failures even when the user had credentials configured via
~/.git-credentials or ~/.netrc.

Adds credentialsForURL which reads credentials from, in order:
  1. ~/.git-credentials (git credential store helper format)
  2. ~/.netrc

The retry follows git's own challenge/response model: an anonymous clone
is attempted first; credentialsForURL is only called when the server
responds with HTTP 401 (transport.ErrAuthenticationRequired). This avoids
sending credentials to servers that do not require them.

No subprocesses are spawned and no git binary on PATH is required.

Fixes knative#3415

Signed-off-by: Ankitsinghsisodya <ankitsingh24012005@gmail.com>
Introduces new tests to verify credential matching behavior for different URL schemes and port configurations. The tests ensure that:
- Credentials stored under `http://` are returned for `https://` requests and vice-versa.
- Credentials with explicit non-standard ports do not match URLs on different ports.
- Credentials with explicit ports match requests with the same port.
- Implicit default ports (80 for http, 443 for https) are treated as equivalent to no port.

These changes enhance the robustness of the credential handling logic in the repository.
Introduces new tests for the scanNetRC function to verify its behavior when parsing .netrc files. The tests ensure that:
- The first matching machine stanza is returned when multiple stanzas match the same host.
- Lines beginning with '#' are correctly ignored as comments during parsing.

These additions enhance the reliability of the credential retrieval process from .netrc files.
@Ankitsinghsisodya Ankitsinghsisodya force-pushed the fix/git-credential-helper branch from 8c8c32d to dd8c16e Compare May 7, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/XL 🤖 PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to clone git repository as template if the repo is protected

4 participants