Skip to content

Commit 9fc3dfb

Browse files
authored
fix(remote): guard GetCredentialFunc against nil store (#1184)
## Summary A `nil` `credentials.Store` passed to `GetCredentialFunc` would cause a deferred panic inside the returned closure at authentication time, making it hard to diagnose. This PR adds an early nil check that returns a no-op `CredentialFunc` yielding `EmptyCredential` when `store` is `nil`, matching the documented semantics. Also fixes a small typo in the `buildRepositoryBlobMountURL` doc comment (missing space). Split out from #1115 — the other half (Ingest temp-file cleanup) is in a separate PR. ## Test plan - [x] Added `TestGetCredentialFunc_NilStore` to verify nil store returns `EmptyCredential` without error - [x] Unit tests pass (`make test`) --------- Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent d04d858 commit 9fc3dfb

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

registry/remote/auth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ func Logout(ctx context.Context, store credentials.Store, registryName string) e
6868
return nil
6969
}
7070

71-
// GetCredentialFunc returns a GetCredentialFunc() function that can be used by auth.Client.
71+
// GetCredentialFunc returns a CredentialFunc that retrieves credentials from
72+
// the given store. If store is nil, the returned function always returns
73+
// EmptyCredential without error.
7274
func GetCredentialFunc(store credentials.Store) credentials.CredentialFunc {
75+
if store == nil {
76+
return func(context.Context, string) (credentials.Credential, error) {
77+
return credentials.EmptyCredential, nil
78+
}
79+
}
7380
return func(ctx context.Context, hostport string) (credentials.Credential, error) {
7481
hostport = ServerAddressFromHostname(hostport)
7582
if hostport == "" {

registry/remote/auth_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ func Test_mapHostname(t *testing.T) {
205205
}
206206
}
207207

208+
func TestGetCredentialFunc_NilStore(t *testing.T) {
209+
fn := GetCredentialFunc(nil)
210+
got, err := fn(context.Background(), "localhost:5000")
211+
if err != nil {
212+
t.Fatalf("GetCredentialFunc(nil) returned error: %v", err)
213+
}
214+
if got != credentials.EmptyCredential {
215+
t.Errorf("GetCredentialFunc(nil) = %v, want EmptyCredential", got)
216+
}
217+
}
218+
208219
func TestCredential(t *testing.T) {
209220
// create a test store
210221
s := &testStore{}

registry/remote/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func buildRepositoryBlobUploadURL(plainHTTP bool, ref registry.Reference) string
8888
return buildRepositoryBaseURL(plainHTTP, ref) + "/blobs/uploads/"
8989
}
9090

91-
// buildRepositoryBlobMountURLbuilds the URL for cross-repository mounting.
91+
// buildRepositoryBlobMountURL builds the URL for cross-repository mounting.
9292
// Format: <scheme>://<registry>/v2/<repository>/blobs/uploads/?mount=<digest>&from=<other_repository>
9393
// Reference: https://distribution.github.io/distribution/spec/api/#blob
9494
func buildRepositoryBlobMountURL(plainHTTP bool, ref registry.Reference, d digest.Digest, fromRepo string) string {

0 commit comments

Comments
 (0)