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 @@
"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 @@
return "", fmt.Errorf("failed to resolve credential: %w", err)
}
if cred == EmptyCredential {
return "", errors.New("credential required for basic auth")
return "", ErrBasicCredentialNotFound

Check warning on line 285 in registry/remote/auth/client.go

View check run for this annotation

Codecov / codecov/patch

registry/remote/auth/client.go#L285

Added line #L285 was not covered by tests
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
14 changes: 12 additions & 2 deletions registry/remote/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Store interface {
// in the config file.
type DynamicStore struct {
config *config.Config
configPath string
qweeah marked this conversation as resolved.
Show resolved Hide resolved
options StoreOptions
detectedCredsStore string
setCredsStoreOnce sync.Once
Expand Down Expand Up @@ -100,8 +101,9 @@ func NewStore(configPath string, opts StoreOptions) (*DynamicStore, error) {
return nil, err
}
ds := &DynamicStore{
config: cfg,
options: opts,
config: cfg,
configPath: configPath,
options: opts,
}
if opts.DetectDefaultNativeStore && !cfg.IsAuthConfigured() {
// no authentication configured, detect the default credentials store
Expand Down Expand Up @@ -167,6 +169,14 @@ func (ds *DynamicStore) IsAuthConfigured() bool {
return ds.config.IsAuthConfigured()
}

// ConfigPath returns the path to the config file.
func (ds *DynamicStore) ConfigPath() (string, error) {
if ds.config == nil {
return "", fmt.Errorf("config is not loaded")
}
qweeah marked this conversation as resolved.
Show resolved Hide resolved
return ds.configPath, nil
}

// getHelperSuffix returns the credential helper suffix for the given server
// address.
func (ds *DynamicStore) getHelperSuffix(serverAddress string) string {
Expand Down
23 changes: 23 additions & 0 deletions registry/remote/credentials/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,29 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) {
})
}
}
func Test_DynamicStore_GetConfigPath(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, err := store.ConfigPath()
if err != nil {
t.Errorf("Config.GetPath() error = %v", err)
}
if got != path {
t.Errorf("Config.GetPath() = %v, want %v", got, path)
}
}

func Test_DynamicStore_Path_nil(t *testing.T) {
var store DynamicStore
_, err := store.ConfigPath()
if err == nil {
t.Error("expecting error, got nil")
}
}

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