Skip to content

Commit

Permalink
feat: error type for not finding basic auth creds and export config p…
Browse files Browse the repository at this point in the history
…ath (#674)

Two changes proposed in this PR
1) When failed to fetch credentials for basic auth, return a new error
2) Add a new function to DynamicStore

An example usage of the proposed change: oras-project/oras#1235,
resolves #676

Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Jan 17, 2024
1 parent 3b1dd0e commit d8783fe
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion registry/remote/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
"oras.land/oras-go/v2/registry/remote/retry"
)

// ErrBasicCredentialNotFound is returned when the credential is not found for
// basic auth.
var ErrBasicCredentialNotFound = errors.New("basic credential not found")

// DefaultClient is the default auth-decorated client.
var DefaultClient = &Client{
Client: retry.DefaultClient,
Expand Down Expand Up @@ -280,7 +284,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
}
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()
}

// 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

0 comments on commit d8783fe

Please sign in to comment.