feat: add the ability to log into a registry on a per-app basis

Closes #5324
This commit is contained in:
Jose Diaz-Gonzalez
2026-01-09 00:12:16 -05:00
parent 2093db6e74
commit 6a83fd053c
23 changed files with 398 additions and 29 deletions

View File

@@ -12,7 +12,7 @@ import (
)
// CommandLogin logs a user into the specified server
func CommandLogin(server string, username string, password string, passwordStdin bool) error {
func CommandLogin(appName string, server string, username string, password string, passwordStdin bool) error {
if passwordStdin {
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
@@ -39,9 +39,22 @@ func CommandLogin(server string, username string, password string, passwordStdin
buffer := bytes.Buffer{}
buffer.Write([]byte(password + "\n"))
env := map[string]string{}
if appName != "" {
if err := common.VerifyAppName(appName); err != nil {
return err
}
configDir := GetAppRegistryConfigDir(appName)
if err := os.MkdirAll(configDir, 0700); err != nil {
return fmt.Errorf("Unable to create registry config directory: %w", err)
}
env["DOCKER_CONFIG"] = GetAppRegistryConfigPath(appName)
}
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"login", "--username", username, "--password-stdin", server},
Env: env,
Stdin: &buffer,
StreamStdio: true,
})
@@ -52,6 +65,7 @@ func CommandLogin(server string, username string, password string, passwordStdin
return fmt.Errorf("Unable to run docker login: %s", result.StderrContents())
}
// todo: change the signature of the trigger to include the app name
_, err = common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "post-registry-login",
Args: []string{server, username},
@@ -67,6 +81,44 @@ func CommandLogin(server string, username string, password string, passwordStdin
return nil
}
// CommandLogout logs a user out from the specified server
func CommandLogout(appName string, server string) error {
if server == "" {
return errors.New("Missing server argument")
}
if server == "hub.docker.com" || server == "docker.com" {
server = "docker.io"
}
env := map[string]string{}
if appName != "" {
if err := common.VerifyAppName(appName); err != nil {
return err
}
configDir := GetAppRegistryConfigDir(appName)
if !common.DirectoryExists(configDir) {
return fmt.Errorf("No registry credentials found for app %s", appName)
}
env["DOCKER_CONFIG"] = GetAppRegistryConfigPath(appName)
}
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"logout", server},
Env: env,
StreamStdio: true,
})
if err != nil {
return fmt.Errorf("Unable to run docker logout: %w", err)
}
if result.ExitCode != 0 {
return fmt.Errorf("Unable to run docker logout: %s", result.StderrContents())
}
return nil
}
// CommandReport displays a registry report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 {