Skip to content

Commit

Permalink
add path function for dynamic
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Jan 11, 2024
1 parent 8abeeab commit e197c00
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
8 changes: 6 additions & 2 deletions 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 @@ -215,7 +219,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
return c.fetchBasicAuth(ctx, host)
})
if err != nil {
return nil, err
return nil, fmt.Errorf("%s %q: %w", resp.Request.Method, resp.Request.URL, err)
}

req = originalReq.Clone(ctx)
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 "", ErrBasicCredNotFound
return "", ErrBasicCredentialNotFound

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

View check run for this annotation

Codecov / codecov/patch

registry/remote/auth/client.go#L287

Added line #L287 was not covered by tests
}
if cred.Username == "" || cred.Password == "" {
return "", errors.New("missing username or password for basic auth")
Expand Down
23 changes: 0 additions & 23 deletions registry/remote/auth/errors.go

This file was deleted.

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 {
errors.New("config file is nil")
}
return cfg.path, nil

Check warning on line 163 in registry/remote/credentials/internal/config/config.go

View check run for this annotation

Codecov / codecov/patch

registry/remote/credentials/internal/config/config.go#L159-L163

Added lines #L159 - L163 were not covered by tests
}

// GetAuthConfig returns an auth.Credential for serverAddress.
func (cfg *Config) GetCredential(serverAddress string) (auth.Credential, error) {
cfg.rwLock.RLock()
Expand Down
11 changes: 8 additions & 3 deletions registry/remote/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func NewStore(configPath string, opts StoreOptions) (*DynamicStore, error) {
// - https://docs.docker.com/engine/reference/commandline/cli/#configuration-files
// - https://docs.docker.com/engine/reference/commandline/cli/#change-the-docker-directory
func NewStoreFromDocker(opt StoreOptions) (*DynamicStore, error) {
configPath, err := GetDockerConfigPath()
configPath, err := getDockerConfigPath()
if err != nil {
return nil, err
}
Expand Down 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()

Check warning on line 172 in registry/remote/credentials/store.go

View check run for this annotation

Codecov / codecov/patch

registry/remote/credentials/store.go#L171-L172

Added lines #L171 - L172 were not covered by tests
}

// getHelperSuffix returns the credential helper suffix for the given server
// address.
func (ds *DynamicStore) getHelperSuffix(serverAddress string) string {
Expand All @@ -193,8 +198,8 @@ func (ds *DynamicStore) getStore(serverAddress string) Store {
return fs
}

// GetDockerConfigPath returns the path to the default docker config file.
func GetDockerConfigPath() (string, error) {
// getDockerConfigPath returns the path to the default docker config file.
func getDockerConfigPath() (string, error) {
// first try the environment variable
configDir := os.Getenv(dockerConfigDirEnv)
if configDir == "" {
Expand Down
4 changes: 2 additions & 2 deletions registry/remote/credentials/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ func Test_getDockerConfigPath_env(t *testing.T) {
}
t.Setenv("DOCKER_CONFIG", dir)

got, err := GetDockerConfigPath()
got, err := getDockerConfigPath()
if err != nil {
t.Fatal("getDockerConfigPath() error =", err)
}
Expand All @@ -890,7 +890,7 @@ func Test_getDockerConfigPath_env(t *testing.T) {
func Test_getDockerConfigPath_homeDir(t *testing.T) {
t.Setenv("DOCKER_CONFIG", "")

got, err := GetDockerConfigPath()
got, err := getDockerConfigPath()
if err != nil {
t.Fatal("getDockerConfigPath() error =", err)
}
Expand Down

0 comments on commit e197c00

Please sign in to comment.