Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error type for not finding basic auth creds and export config path #674

Merged
merged 15 commits into from
Jan 17, 2024
4 changes: 3 additions & 1 deletion registry/remote/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"oras.land/oras-go/v2/registry/remote/retry"
)

var ErrBasicCredentialNotFound = errors.New("basic credential not found")
qweeah marked this conversation as resolved.
Show resolved Hide resolved

Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
// DefaultClient is the default auth-decorated client.
var DefaultClient = &Client{
Client: retry.DefaultClient,
Expand Down Expand Up @@ -280,7 +282,7 @@ func (c *Client) fetchBasicAuth(ctx context.Context, registry string) (string, e
return "", fmt.Errorf("failed to resolve credential: %w", err)
}
if cred == EmptyCredential {
return "", errors.New("credential required for basic auth")
return "", ErrBasicCredentialNotFound
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}
if cred.Username == "" || cred.Password == "" {
return "", errors.New("missing username or password for basic auth")
Expand Down
12 changes: 12 additions & 0 deletions registry/remote/auth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3964,3 +3964,15 @@ func TestClient_StaticCredential_withRefreshToken(t *testing.T) {
t.Errorf("incorrect error: %v, expected %v", err, expectedError)
}
}

func TestClient_fetchBasicAuth(t *testing.T) {
c := &Client{
Credential: func(ctx context.Context, registry string) (Credential, error) {
return EmptyCredential, nil
},
}
_, err := c.fetchBasicAuth(context.Background(), "")
if err != ErrBasicCredentialNotFound {
t.Errorf("incorrect error: %v, expected %v", err, ErrBasicCredentialNotFound)
}
}
5 changes: 5 additions & 0 deletions registry/remote/credentials/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func (cfg *Config) CredentialsStore() string {
return cfg.credentialsStore
}

// Path returns the path to the config file.
func (cfg *Config) Path() string {
return cfg.path
}

// SetCredentialsStore puts the configured credentials store.
func (cfg *Config) SetCredentialsStore(credsStore string) error {
cfg.rwLock.Lock()
Expand Down
10 changes: 10 additions & 0 deletions registry/remote/credentials/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,13 @@ func Test_toHostname(t *testing.T) {
})
}
}

func TestConfig_Path(t *testing.T) {
mockedPath := "/path/to/config.json"
config := Config{
path: mockedPath,
}
if got := config.Path(); got != mockedPath {
t.Errorf("Config.Path() = %v, want %v", got, mockedPath)
}
}
5 changes: 5 additions & 0 deletions registry/remote/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ func (ds *DynamicStore) IsAuthConfigured() bool {
return ds.config.IsAuthConfigured()
}

// ConfigPath returns the path to the config file.
func (ds *DynamicStore) ConfigPath() string {
return ds.config.Path()
Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
}

// getHelperSuffix returns the credential helper suffix for the given server
// address.
func (ds *DynamicStore) getHelperSuffix(serverAddress string) string {
Expand Down
13 changes: 13 additions & 0 deletions registry/remote/credentials/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,19 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) {
}
}

func Test_DynamicStore_ConfigPath(t *testing.T) {
path := "../../testdata/credsStore_config.json"
var err error
store, err := NewStore(path, StoreOptions{})
if err != nil {
t.Fatal("NewFileStore() error =", err)
}
got := store.ConfigPath()
if got != path {
t.Errorf("Config.GetPath() = %v, want %v", got, path)
}
}

func Test_DynamicStore_getStore_nativeStore(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading