Skip to content

Commit 6278a27

Browse files
committed
feat: extend AuthProvider to allow getting all auth configs
1 parent 60d5001 commit 6278a27

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

pkg/client/auth/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
type Provider interface {
1010
AuthConfig(ref reference.Named) registry.AuthConfig
11+
AuthConfigs() map[string]registry.AuthConfig
1112
}
1213

1314
type ProviderConfig struct {

pkg/client/auth/default.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import (
1111
)
1212

1313
type DefaultAuthProvider struct {
14-
authConfigs map[string]*registry.AuthConfig
14+
authConfigs map[string]registry.AuthConfig
1515
config *ProviderConfig
1616
}
1717

18+
func (d *DefaultAuthProvider) AuthConfigs() map[string]registry.AuthConfig {
19+
return d.authConfigs
20+
}
21+
1822
func (d *DefaultAuthProvider) AuthConfig(ref reference.Named) registry.AuthConfig {
1923
domain := reference.Domain(ref)
2024
if ac, ok := d.authConfigs[domain]; ok {
2125
d.config.Logger.Debugf("using auth config for %s", domain)
22-
return *ac
26+
return ac
2327
}
2428
d.config.Logger.Debugf("no auth config for %s", domain)
2529
return registry.AuthConfig{}
@@ -28,7 +32,7 @@ func (d *DefaultAuthProvider) AuthConfig(ref reference.Named) registry.AuthConfi
2832
func NewDefaultProvider(opts ...Opt) (*DefaultAuthProvider, error) {
2933
config := renderConfig(opts)
3034

31-
authConfigs := map[string]*registry.AuthConfig{}
35+
authConfigs := map[string]registry.AuthConfig{}
3236

3337
cfg, err := dockercfg.LoadDefaultConfig()
3438
if err != nil && !errors.Is(err, os.ErrNotExist) {
@@ -39,7 +43,7 @@ func NewDefaultProvider(opts ...Opt) (*DefaultAuthProvider, error) {
3943
}
4044

4145
for k, v := range cfg.AuthConfigs {
42-
ac := &registry.AuthConfig{
46+
ac := registry.AuthConfig{
4347
Username: v.Username,
4448
Password: v.Password,
4549
Email: v.Email,
@@ -49,7 +53,7 @@ func NewDefaultProvider(opts ...Opt) (*DefaultAuthProvider, error) {
4953
ServerAddress: v.ServerAddress,
5054
}
5155
if ac.Username == "" && ac.Password == "" {
52-
err := getCredentials(k, &cfg, ac)
56+
err := getCredentials(k, &cfg, &ac)
5357
if err != nil {
5458
config.Logger.Errorf("failed to get credentials for registry %s: %v", k, err)
5559
continue
@@ -62,10 +66,12 @@ func NewDefaultProvider(opts ...Opt) (*DefaultAuthProvider, error) {
6266
}
6367

6468
for k := range cfg.CredentialHelpers {
65-
err := getCredentials(k, &cfg, authConfigs[k])
69+
ac := authConfigs[k]
70+
err := getCredentials(k, &cfg, &ac)
6671
if err != nil {
6772
config.Logger.Errorf("failed to get credentials for registry %s: %v", k, err)
6873
}
74+
authConfigs[k] = ac
6975
}
7076
return &DefaultAuthProvider{authConfigs, config}, nil
7177
}

pkg/client/auth/overriding.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func (o *OverridingAuthProvider) WithOverride(domain string, ac registry.AuthCon
2525
return NewOverridingProvider(o.source, overrides)
2626
}
2727

28+
func (o *OverridingAuthProvider) AuthConfigs() map[string]registry.AuthConfig {
29+
authConfigs := o.source.AuthConfigs()
30+
maps.Copy(authConfigs, o.overrides)
31+
return authConfigs
32+
}
33+
2834
func (o *OverridingAuthProvider) AuthConfig(ref reference.Named) registry.AuthConfig {
2935
if ac, ok := o.overrides[reference.Domain(ref)]; ok {
3036
o.config.Logger.Debugf("using override for %s", reference.Domain(ref))

0 commit comments

Comments
 (0)