Skip to content

Commit

Permalink
Allow explicit docker login errors to be ignored
Browse files Browse the repository at this point in the history
* Add support for suppressing errors triggered by explicit calls to
  `docker login` and `docker logout` by setting the
  `IGNORE_DOCKER_LOGIN` environment variable.
  • Loading branch information
isometry committed Oct 26, 2021
1 parent e04a7eb commit 5f1c183
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The `docker-credential-env` binary must be installed to `$PATH`, configured via
}
```

By default, attempts to explicitly `docker {login,logout}` will generate an error. To ignore these errors, set the environment variable `IGNORE_DOCKER_LOGIN=1`.

## Example Usage

### Jenkins
Expand Down
15 changes: 13 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
envUsernameSuffix = "USR"
envPasswordSuffix = "PSW"
envSeparator = "_"
envIgnoreLogin = "IGNORE_DOCKER_LOGIN"
)

type NotSupportedError struct{}
Expand All @@ -39,12 +40,22 @@ type Env struct{}

// Add implements the set verb
func (*Env) Add(*docker_credentials.Credentials) error {
return fmt.Errorf("add: %w", &NotSupportedError{})
switch {
case os.Getenv(envIgnoreLogin) != "":
return nil
default:
return fmt.Errorf("add: %w", &NotSupportedError{})
}
}

// Delete implements the erase verb
func (*Env) Delete(string) error {
return fmt.Errorf("delete: %w", &NotSupportedError{})
switch {
case os.Getenv(envIgnoreLogin) != "":
return nil
default:
return fmt.Errorf("delete: %w", &NotSupportedError{})
}
}

// List implements the list verb
Expand Down
20 changes: 18 additions & 2 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,33 @@ func TestEnvNotSupportedMethods(t *testing.T) {
}
})

t.Run("Add is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
t.Setenv(envIgnoreLogin, "1")
actualErr := e.Add(nil)
if actualErr != nil {
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, nil)
}
})

t.Run("Delete is not supported", func(t *testing.T) {
actualErr := e.Delete("")
if !errors.Is(actualErr, &NotSupportedError{}) {
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
}
})

t.Run("Delete is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
t.Setenv(envIgnoreLogin, "1")
actualErr := e.Delete("")
if actualErr != nil {
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, nil)
}
})

t.Run("List is not supported", func(t *testing.T) {
_, actualErr := e.List()
if !errors.Is(actualErr, &NotSupportedError{}) {
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
t.Errorf("List() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
}
})
}

0 comments on commit 5f1c183

Please sign in to comment.