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
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"
)

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
8 changes: 8 additions & 0 deletions registry/remote/credentials/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func Load(configPath string) (*Config, error) {
return cfg, nil
}

// Path returns the path to the config file.
func (cfg *Config) Path() (string, error) {
if cfg == nil {
return "", errors.New("config file is nil")
}
return cfg.path, nil
}

// GetAuthConfig returns an auth.Credential for serverAddress.
func (cfg *Config) GetCredential(serverAddress string) (auth.Credential, error) {
cfg.rwLock.RLock()
Expand Down
22 changes: 22 additions & 0 deletions registry/remote/credentials/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,28 @@ func TestConfig_GetCredentialHelper(t *testing.T) {
}
}

func TestConfig_Path(t *testing.T) {
cfg := Config{
path: "/path/to/config.json",
}
got, err := cfg.Path()

if err != nil {
t.Errorf("Config.GetPath() error = %v", err)
}
if got != cfg.path {
t.Errorf("Config.GetPath() = %v, want %v", got, cfg.path)
}
}

func TestConfig_Path_nil(t *testing.T) {
var cfg *Config
_, err := cfg.Path()
if err == nil {
t.Error("expecting error, got nil")
}
}

func TestConfig_CredentialsStore(t *testing.T) {
tests := []struct {
name string
Expand Down
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()
}

// GetConfigPath returns the path to the config file.
func (ds *DynamicStore) GetConfigPath() (string, error) {
return ds.config.Path()
}

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

"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/credentials/internal/config"
"oras.land/oras-go/v2/registry/remote/credentials/internal/config/configtest"
)

Expand Down Expand Up @@ -611,6 +612,30 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) {
})
}
}
func Test_DynamicStore_GetConfigPath(t *testing.T) {
var store DynamicStore
var err error
path := "../../testdata/credsStore_config.json"
store.config, err = config.Load(path)
if err != nil {
t.Fatal("config.Load() error =", err)
}
got, err := store.GetConfigPath()
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.GetConfigPath()
if err == nil {
t.Error("expecting error, got nil")
}
}

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