Skip to content

Commit

Permalink
pkg/auth/Login: return a typed error
Browse files Browse the repository at this point in the history
To preserve the knowledge about the error without string compares.
Needed in Podman's REST API.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
  • Loading branch information
vrothberg committed Mar 21, 2023
1 parent 61a6ad5 commit e40de25
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/auth/auth.go
Expand Up @@ -19,6 +19,23 @@ import (
terminal "golang.org/x/term"
)

// ErrNewCredentialsInvalid means that the new user-provided credentials are
// not accepted by the registry.
type ErrNewCredentialsInvalid struct {
underlyingError error
message string
}

// Error returns the error message as a string.
func (e ErrNewCredentialsInvalid) Error() string {
return e.message
}

// Unwrap returns the underlying error.
func (e ErrNewCredentialsInvalid) Unwrap() error {
return e.underlyingError
}

// GetDefaultAuthFile returns env value REGISTRY_AUTH_FILE as default
// --authfile path used in multiple --authfile flag definitions
// Will fail over to DOCKER_CONFIG if REGISTRY_AUTH_FILE environment is not set
Expand Down Expand Up @@ -158,7 +175,10 @@ func Login(ctx context.Context, systemContext *types.SystemContext, opts *LoginO
}
if unauthorized, ok := err.(docker.ErrUnauthorizedForCredentials); ok {
logrus.Debugf("error logging into %q: %v", key, unauthorized)
return fmt.Errorf("logging into %q: invalid username/password", key)
return ErrNewCredentialsInvalid{
underlyingError: err,
message: fmt.Sprintf("logging into %q: invalid username/password", key),
}
}
return fmt.Errorf("authenticating creds for %q: %w", key, err)
}
Expand Down

0 comments on commit e40de25

Please sign in to comment.