feat: add registry:auth-status to read back login state
There was no way to ask whether Dokku was already logged in to a registry with a given credential, so tooling converging a declared state onto a server had to re-run `registry:login` on every pass purely to discover that nothing needed to change. The new `registry:auth-status` command compares a supplied credential against the stored one and reports the result through its exit code alone, distinguishing a server with no credential from one whose credential differs, and from one whose secret a docker credential helper or an identity token puts out of reach. An `auth-servers` triad on `registry:report` exposes which servers have credentials without emitting any part of one.
This commit is contained in:
11 files changed
+877
-12
No files matched your search
@@ -11,15 +11,92 @@ import (
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
// readPasswordFromStdin reads a registry password from stdin
|
||||
func readPasswordFromStdin() (string, error) {
|
||||
stdin, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimSpace(string(stdin)), nil
|
||||
}
|
||||
|
||||
// CommandAuthStatus reports whether the stored registry credential matches the
|
||||
// requested state, communicating the result via the exit code alone
|
||||
func CommandAuthStatus(appName string, server string, username string, password string, passwordStdin bool) error {
|
||||
if passwordStdin {
|
||||
if username == "" {
|
||||
return InvalidAuthStatusArguments("Missing username argument")
|
||||
}
|
||||
|
||||
stdin, err := readPasswordFromStdin()
|
||||
if err != nil {
|
||||
return InvalidAuthStatusArguments(err.Error())
|
||||
}
|
||||
|
||||
password = stdin
|
||||
}
|
||||
|
||||
if server == "" {
|
||||
return InvalidAuthStatusArguments("Missing server argument")
|
||||
}
|
||||
if username != "" && password == "" {
|
||||
return InvalidAuthStatusArguments("Missing password argument")
|
||||
}
|
||||
if username == "" && password != "" {
|
||||
return InvalidAuthStatusArguments("Missing username argument")
|
||||
}
|
||||
|
||||
configPath := GetGlobalRegistryConfigPath()
|
||||
if appName != "" {
|
||||
if err := common.VerifyAppName(appName); err != nil {
|
||||
// a missing app carries its own exit code, while an invalid name is
|
||||
// a malformed call and must not be reported as a missing credential
|
||||
if _, ok := err.(common.ErrWithExitCode); ok {
|
||||
return err
|
||||
}
|
||||
|
||||
return InvalidAuthStatusArguments(err.Error())
|
||||
}
|
||||
|
||||
configPath = GetAppRegistryConfigPath(appName)
|
||||
}
|
||||
|
||||
contents, err := os.ReadFile(configPath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return &common.ExitCodeError{
|
||||
Code: authStatusUnknown,
|
||||
Message: fmt.Sprintf("Unable to read the docker config: %s", err.Error()),
|
||||
}
|
||||
}
|
||||
|
||||
code, message := checkAuthStatus(authStatusInput{
|
||||
ConfigBytes: contents,
|
||||
Server: server,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if code == authStatusMatch {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &common.ExitCodeError{Code: code, Message: message}
|
||||
}
|
||||
|
||||
// InvalidAuthStatusArguments reports a registry:auth-status call that could not be checked
|
||||
func InvalidAuthStatusArguments(message string) error {
|
||||
return &common.ExitCodeError{Code: authStatusInvalidArguments, Message: message}
|
||||
}
|
||||
|
||||
// CommandLogin logs a user into the specified server
|
||||
func CommandLogin(appName string, server string, username string, password string, passwordStdin bool) error {
|
||||
if passwordStdin {
|
||||
stdin, err := io.ReadAll(os.Stdin)
|
||||
stdin, err := readPasswordFromStdin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
password = strings.TrimSpace(string(stdin))
|
||||
password = stdin
|
||||
}
|
||||
|
||||
if server == "" {
|
||||
|
||||
Reference in new issue
Block a user