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

fix: fail silently for unsupported actions #2

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions helper/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,43 @@ 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
}

return user, password, err
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
}

func (e EnvHelper) Add(creds *credentials.Credentials) error {

slog.Info("", "action", "add", "serverURL", creds.ServerURL, "username", creds.Username)
return ErrNotImplemented
slog.Warn("Saving credentials is not supported by docker-credential-env", "action", "add", "serverURL", creds.ServerURL, "username", creds.Username)
return nil
}

func (e EnvHelper) Delete(serverURL string) error {
slog.Info("", "action", "delete", "serverURL", serverURL)
return ErrNotImplemented
slog.Warn("Deleting credentials is not supported by docker-credential-env", "action", "delete", "serverURL", serverURL)
return nil
}

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
21 changes: 16 additions & 5 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 All @@ -31,19 +42,19 @@ func TestEnvHelper_Get_Failure(t *testing.T) {
assert.Empty(t, password)
}

func TestEnvHelper_Add(t *testing.T) {
func TestEnvHelper_Add_FailsSilently(t *testing.T) {
helper := EnvHelper{}
err := helper.Add(&credentials.Credentials{})
assert.ErrorIs(t, err, ErrNotImplemented)
assert.NoError(t, err)
}

func TestEnvHelper_Delete(t *testing.T) {
func TestEnvHelper_Delete_FailsSilently(t *testing.T) {
helper := EnvHelper{}
err := helper.Delete("foo")
assert.ErrorIs(t, err, ErrNotImplemented)
assert.NoError(t, err)
}

func TestEnvHelper_List(t *testing.T) {
func TestEnvHelper_List_NotImplemented(t *testing.T) {
helper := EnvHelper{}
_, err := helper.List()
assert.ErrorIs(t, err, ErrNotImplemented)
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 @@

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
jamestelfer marked this conversation as resolved.
Show resolved Hide resolved

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
jamestelfer marked this conversation as resolved.
Show resolved Hide resolved
}

func configureLogging() {
Expand Down