Skip to content

Commit 511878b

Browse files
committed
move NewCredential to configuration
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent 254f3f6 commit 511878b

File tree

4 files changed

+159
-22
lines changed

4 files changed

+159
-22
lines changed

registry/remote/credentials/file_store.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (fs *FileStore) Get(_ context.Context, serverAddress string) (properties.Cr
6868
if err != nil {
6969
return properties.EmptyCredential, err
7070
}
71-
return NewCredential(authCfg)
71+
return authCfg.Credential()
7272
}
7373

7474
// Put saves credentials into the store for the given server address.
@@ -100,22 +100,3 @@ func validateCredentialFormat(cred properties.Credential) error {
100100
}
101101
return nil
102102
}
103-
104-
// NewCredential creates a CredentialFunc based on authCfg.
105-
func NewCredential(authCfg configuration.AuthConfig) (properties.Credential, error) {
106-
cred := properties.Credential{
107-
Username: authCfg.Username,
108-
Password: authCfg.Password,
109-
RefreshToken: authCfg.IdentityToken,
110-
AccessToken: authCfg.RegistryToken,
111-
}
112-
if authCfg.Auth != "" {
113-
var err error
114-
// override username and password
115-
cred.Username, cred.Password, err = authCfg.DecodeAuth()
116-
if err != nil {
117-
return properties.EmptyCredential, fmt.Errorf("failed to decode auth field: %w: %v", configuration.ErrInvalidConfigFormat, err)
118-
}
119-
}
120-
return cred, nil
121-
}

registry/remote/credentials/memory_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewMemoryStoreFromDockerConfig(c []byte) (Store, error) {
5050
for addr, auth := range cfg.Auths {
5151
// Normalize the auth key to hostname.
5252
hostname := configuration.ToHostname(addr)
53-
cred, err := NewCredential(auth)
53+
cred, err := auth.Credential()
5454
if err != nil {
5555
return nil, err
5656
}

registry/remote/internal/configuration/authconfig.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,22 @@ func (ac AuthConfig) DecodeAuth() (username string, password string, err error)
7474
}
7575
return username, password, nil
7676
}
77+
78+
// Credential creates a Credential based on authCfg.
79+
func (ac AuthConfig) Credential() (properties.Credential, error) {
80+
cred := properties.Credential{
81+
Username: ac.Username,
82+
Password: ac.Password,
83+
RefreshToken: ac.IdentityToken,
84+
AccessToken: ac.RegistryToken,
85+
}
86+
if ac.Auth != "" {
87+
var err error
88+
// override username and password
89+
cred.Username, cred.Password, err = ac.DecodeAuth()
90+
if err != nil {
91+
return properties.EmptyCredential, fmt.Errorf("failed to decode auth field: %w: %v", ErrInvalidConfigFormat, err)
92+
}
93+
}
94+
return cred, nil
95+
}

registry/remote/internal/configuration/authconfig_test.go

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ limitations under the License.
1515

1616
package configuration
1717

18-
import "testing"
18+
import (
19+
"testing"
20+
21+
"oras.land/oras-go/v2/registry/remote/properties"
22+
)
1923

2024
func Test_EncodeAuth(t *testing.T) {
2125
tests := []struct {
@@ -121,3 +125,136 @@ func TestAuthConfig_DecodeAuth(t *testing.T) {
121125
})
122126
}
123127
}
128+
129+
func TestCredential(t *testing.T) {
130+
tests := []struct {
131+
name string
132+
authCfg AuthConfig
133+
want properties.Credential
134+
wantErr bool
135+
}{
136+
{
137+
name: "Username and password",
138+
authCfg: AuthConfig{
139+
Username: "username",
140+
Password: "password",
141+
},
142+
want: properties.Credential{
143+
Username: "username",
144+
Password: "password",
145+
},
146+
},
147+
{
148+
name: "Identity token",
149+
authCfg: AuthConfig{
150+
IdentityToken: "identity_token",
151+
},
152+
want: properties.Credential{
153+
RefreshToken: "identity_token",
154+
},
155+
},
156+
{
157+
name: "Registry token",
158+
authCfg: AuthConfig{
159+
RegistryToken: "registry_token",
160+
},
161+
want: properties.Credential{
162+
AccessToken: "registry_token",
163+
},
164+
},
165+
{
166+
name: "All fields",
167+
authCfg: AuthConfig{
168+
Username: "username",
169+
Password: "password",
170+
IdentityToken: "identity_token",
171+
RegistryToken: "registry_token",
172+
},
173+
want: properties.Credential{
174+
Username: "username",
175+
Password: "password",
176+
RefreshToken: "identity_token",
177+
AccessToken: "registry_token",
178+
},
179+
},
180+
{
181+
name: "Empty auth config",
182+
authCfg: AuthConfig{},
183+
want: properties.Credential{},
184+
},
185+
{
186+
name: "Auth field overrides username and password",
187+
authCfg: AuthConfig{
188+
Auth: "dXNlcm5hbWU6cGFzc3dvcmQ=", // username:password
189+
Username: "old_username",
190+
Password: "old_password",
191+
},
192+
want: properties.Credential{
193+
Username: "username",
194+
Password: "password",
195+
},
196+
},
197+
{
198+
name: "Auth field with identity and registry tokens",
199+
authCfg: AuthConfig{
200+
Auth: "dXNlcm5hbWU6cGFzc3dvcmQ=", // username:password
201+
IdentityToken: "identity_token",
202+
RegistryToken: "registry_token",
203+
},
204+
want: properties.Credential{
205+
Username: "username",
206+
Password: "password",
207+
RefreshToken: "identity_token",
208+
AccessToken: "registry_token",
209+
},
210+
},
211+
{
212+
name: "Invalid auth field",
213+
authCfg: AuthConfig{
214+
Auth: "invalid_base64!@#",
215+
},
216+
want: properties.EmptyCredential,
217+
wantErr: true,
218+
},
219+
{
220+
name: "Auth field bad format",
221+
authCfg: AuthConfig{
222+
Auth: "d2hhdGV2ZXI=", // whatever (no colon)
223+
},
224+
want: properties.EmptyCredential,
225+
wantErr: true,
226+
},
227+
{
228+
name: "Auth field username only",
229+
authCfg: AuthConfig{
230+
Auth: "dXNlcm5hbWU6", // username:
231+
},
232+
want: properties.Credential{
233+
Username: "username",
234+
Password: "",
235+
},
236+
},
237+
{
238+
name: "Auth field password only",
239+
authCfg: AuthConfig{
240+
Auth: "OnBhc3N3b3Jk", // :password
241+
},
242+
want: properties.Credential{
243+
Username: "",
244+
Password: "password",
245+
},
246+
},
247+
}
248+
for _, tt := range tests {
249+
t.Run(tt.name, func(t *testing.T) {
250+
got, err := tt.authCfg.Credential()
251+
if (err != nil) != tt.wantErr {
252+
t.Errorf("Credential() error = %v, wantErr %v", err, tt.wantErr)
253+
return
254+
}
255+
if got != tt.want {
256+
t.Errorf("Credential() = %v, want %v", got, tt.want)
257+
}
258+
})
259+
}
260+
}

0 commit comments

Comments
 (0)