2017-04-17 18:08:24 -04:00
|
|
|
package credentials
|
|
|
|
|
2024-01-10 16:35:03 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
2023-07-17 10:06:44 -04:00
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
const (
|
|
|
|
// ErrCredentialsNotFound standardizes the not found error, so every helper returns
|
|
|
|
// the same message and docker can handle it properly.
|
|
|
|
errCredentialsNotFoundMessage = "credentials not found in native keychain"
|
|
|
|
|
|
|
|
// ErrCredentialsMissingServerURL and ErrCredentialsMissingUsername standardize
|
|
|
|
// invalid credentials or credentials management operations
|
|
|
|
errCredentialsMissingServerURLMessage = "no credentials server URL"
|
2017-08-16 16:02:38 -04:00
|
|
|
errCredentialsMissingUsernameMessage = "no credentials username"
|
2017-06-05 18:23:21 -04:00
|
|
|
)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// errCredentialsNotFound represents an error
|
|
|
|
// raised when credentials are not in the store.
|
|
|
|
type errCredentialsNotFound struct{}
|
|
|
|
|
|
|
|
// Error returns the standard error message
|
|
|
|
// for when the credentials are not in the store.
|
|
|
|
func (errCredentialsNotFound) Error() string {
|
|
|
|
return errCredentialsNotFoundMessage
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// NotFound implements the [ErrNotFound][errdefs.ErrNotFound] interface.
|
|
|
|
//
|
|
|
|
// [errdefs.ErrNotFound]: https://pkg.go.dev/github.com/docker/docker@v24.0.1+incompatible/errdefs#ErrNotFound
|
|
|
|
func (errCredentialsNotFound) NotFound() {}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// NewErrCredentialsNotFound creates a new error
|
|
|
|
// for when the credentials are not in the store.
|
|
|
|
func NewErrCredentialsNotFound() error {
|
|
|
|
return errCredentialsNotFound{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrCredentialsNotFound returns true if the error
|
|
|
|
// was caused by not having a set of credentials in a store.
|
|
|
|
func IsErrCredentialsNotFound(err error) bool {
|
2023-07-17 10:06:44 -04:00
|
|
|
var target errCredentialsNotFound
|
|
|
|
return errors.As(err, &target)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrCredentialsNotFoundMessage returns true if the error
|
|
|
|
// was caused by not having a set of credentials in a store.
|
|
|
|
//
|
|
|
|
// This function helps to check messages returned by an
|
|
|
|
// external program via its standard output.
|
|
|
|
func IsErrCredentialsNotFoundMessage(err string) bool {
|
2024-01-10 16:35:03 -05:00
|
|
|
return strings.TrimSpace(err) == errCredentialsNotFoundMessage
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2017-06-05 18:23:21 -04:00
|
|
|
|
|
|
|
// errCredentialsMissingServerURL represents an error raised
|
|
|
|
// when the credentials object has no server URL or when no
|
|
|
|
// server URL is provided to a credentials operation requiring
|
|
|
|
// one.
|
|
|
|
type errCredentialsMissingServerURL struct{}
|
|
|
|
|
|
|
|
func (errCredentialsMissingServerURL) Error() string {
|
|
|
|
return errCredentialsMissingServerURLMessage
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// InvalidParameter implements the [ErrInvalidParameter][errdefs.ErrInvalidParameter]
|
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
// [errdefs.ErrInvalidParameter]: https://pkg.go.dev/github.com/docker/docker@v24.0.1+incompatible/errdefs#ErrInvalidParameter
|
|
|
|
func (errCredentialsMissingServerURL) InvalidParameter() {}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
// errCredentialsMissingUsername represents an error raised
|
|
|
|
// when the credentials object has no username or when no
|
|
|
|
// username is provided to a credentials operation requiring
|
|
|
|
// one.
|
|
|
|
type errCredentialsMissingUsername struct{}
|
|
|
|
|
|
|
|
func (errCredentialsMissingUsername) Error() string {
|
|
|
|
return errCredentialsMissingUsernameMessage
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// InvalidParameter implements the [ErrInvalidParameter][errdefs.ErrInvalidParameter]
|
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
// [errdefs.ErrInvalidParameter]: https://pkg.go.dev/github.com/docker/docker@v24.0.1+incompatible/errdefs#ErrInvalidParameter
|
|
|
|
func (errCredentialsMissingUsername) InvalidParameter() {}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
// NewErrCredentialsMissingServerURL creates a new error for
|
|
|
|
// errCredentialsMissingServerURL.
|
|
|
|
func NewErrCredentialsMissingServerURL() error {
|
|
|
|
return errCredentialsMissingServerURL{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewErrCredentialsMissingUsername creates a new error for
|
|
|
|
// errCredentialsMissingUsername.
|
|
|
|
func NewErrCredentialsMissingUsername() error {
|
|
|
|
return errCredentialsMissingUsername{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCredentialsMissingServerURL returns true if the error
|
|
|
|
// was an errCredentialsMissingServerURL.
|
|
|
|
func IsCredentialsMissingServerURL(err error) bool {
|
2023-07-17 10:06:44 -04:00
|
|
|
var target errCredentialsMissingServerURL
|
|
|
|
return errors.As(err, &target)
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsCredentialsMissingServerURLMessage checks for an
|
|
|
|
// errCredentialsMissingServerURL in the error message.
|
|
|
|
func IsCredentialsMissingServerURLMessage(err string) bool {
|
2024-01-10 16:35:03 -05:00
|
|
|
return strings.TrimSpace(err) == errCredentialsMissingServerURLMessage
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsCredentialsMissingUsername returns true if the error
|
|
|
|
// was an errCredentialsMissingUsername.
|
|
|
|
func IsCredentialsMissingUsername(err error) bool {
|
2023-07-17 10:06:44 -04:00
|
|
|
var target errCredentialsMissingUsername
|
|
|
|
return errors.As(err, &target)
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsCredentialsMissingUsernameMessage checks for an
|
|
|
|
// errCredentialsMissingUsername in the error message.
|
|
|
|
func IsCredentialsMissingUsernameMessage(err string) bool {
|
2024-01-10 16:35:03 -05:00
|
|
|
return strings.TrimSpace(err) == errCredentialsMissingUsernameMessage
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|