Skip to content

Commit

Permalink
fix: allow credentials discovery to be optional
Browse files Browse the repository at this point in the history
Allow the user to configure the helper such that failure to find the required environment variables results in an empty username and password being returned instead of causing the helper to fail.
  • Loading branch information
jamestelfer committed Jul 30, 2024
1 parent 802272d commit e1a3945
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
19 changes: 15 additions & 4 deletions helper/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@ var nonAlphanumericPattern = regexp.MustCompile(`[^a-zA-Z0-9]`)
var _ credentials.Helper = EnvHelper{}

type EnvHelper struct {
CredentialsOptional bool
}

// Get retrieves the credentials for the server URL from the process
// environment.
func (e EnvHelper) Get(serverURL string) (string, string, error) {
logger := slog.Default().With("action", "get", "serverURL", serverURL)

// FIXME: allow this to return empty on failure if so configured
user, password, err := credentialsForServer(serverURL)

logger.Info("Get", "user", user, "err", err)
if err != nil {
if e.CredentialsOptional {
logger.Warn("Ignoring failed credential lookup, unset DOCKER_CREDENTIALS_ENV_OPTIONAL if this should cause Docker to fail", "err", err)
return "", "", nil
}

logger.Error("Failed to retrieve credentials, set DOCKER_CREDENTIALS_ENV_OPTIONAL to ignore this error.", "err", err)
return "", "", err
}

logger.Info("Got credentials from environment", "user", user, "err", err)

return user, password, err
}
Expand All @@ -43,8 +55,7 @@ func (e EnvHelper) Delete(serverURL string) error {
}

func (e EnvHelper) List() (map[string]string, error) {
slog.Info("", "action", "list")

slog.Error("List action not implemented", "action", "list")
return nil, ErrNotImplemented
}

Expand Down
11 changes: 11 additions & 0 deletions helper/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ func TestEnvHelper_Get_Success(t *testing.T) {
assert.Equal(t, "testpassword", password)
}

func TestEnvHelper_Get_CredentialsOptional_Success(t *testing.T) {

helper := EnvHelper{CredentialsOptional: true}

user, password, err := helper.Get("example.com")

assert.NoError(t, err)
assert.Empty(t, user)
assert.Empty(t, password)
}

func TestEnvHelper_Get_Failure(t *testing.T) {
helper := EnvHelper{}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func main() {

configureLogging()

credentials.Serve(helper.EnvHelper{})
credentialsOptional := os.Getenv("DOCKER_CREDENTIALS_ENV_OPTIONAL") == "true"

Check warning on line 24 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L24

Added line #L24 was not covered by tests

credentials.Serve(helper.EnvHelper{CredentialsOptional: credentialsOptional})

Check warning on line 26 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L26

Added line #L26 was not covered by tests
}

func configureLogging() {
Expand Down

0 comments on commit e1a3945

Please sign in to comment.