From dcf9fef834b71e4dcee6fd48bdb76b3c307bf30e Mon Sep 17 00:00:00 2001 From: Ossi Vaananen Date: Thu, 3 Mar 2022 16:15:06 +0200 Subject: [PATCH 1/2] Optional authentication flags for Docker client --- docker.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/docker.go b/docker.go index ba85915..1a414d8 100644 --- a/docker.go +++ b/docker.go @@ -2,11 +2,14 @@ package main import ( "bytes" + "encoding/base64" + "encoding/json" "fmt" "io/ioutil" "os" "regexp" + "github.com/docker/docker/api/types" docker "github.com/docker/docker/client" "github.com/spf13/cobra" ) @@ -36,11 +39,28 @@ var ( } dockerfile *string + username *string + password *string ) func init() { dockerfile = rootCmd.PersistentFlags().StringP("dockerfile", "f", "Dockerfile", "Path to your Dockerfile (or - for stdin)") rootCmd.MarkPersistentFlagFilename("dockerfile") + username = rootCmd.PersistentFlags().StringP("username", "u", "", "Username to authenticate with to the Docker registry") + password = rootCmd.PersistentFlags().StringP("password", "p", "", "Password to authenticate with to the Docker registry") +} + +func authStringForCommand(cmd *cobra.Command) string { + if *username != "" { // permit empty password but not username + authConfig := types.AuthConfig{ + Username: *username, + Password: *password, + } + + encodedJSON, _ := json.Marshal(authConfig) + return base64.URLEncoding.EncodeToString(encodedJSON) + } + return "" } func runDockerPin(cmd *cobra.Command, args []string) error { @@ -54,9 +74,10 @@ func runDockerPin(cmd *cobra.Command, args []string) error { } pinned := map[string]string{} images := getUsedBaseImages(b) + authStr := authStringForCommand(cmd) for _, i := range images { fmt.Fprintf(os.Stderr, "Resolving digest of %s...\n", i) - di, err := c.DistributionInspect(cmd.Context(), i, "") + di, err := c.DistributionInspect(cmd.Context(), i, authStr) if err != nil { return err } @@ -72,7 +93,9 @@ func runDockerResolve(cmd *cobra.Command, args []string) error { if err != nil { return err } - di, err := dockerClient.DistributionInspect(cmd.Context(), baseImageAndVersion, "") + + authStr := authStringForCommand(cmd) + di, err := dockerClient.DistributionInspect(cmd.Context(), baseImageAndVersion, authStr) if err != nil { return err } From 3fa56c99264c1a99f304d471385a7ae65aee6d3c Mon Sep 17 00:00:00 2001 From: Ossi Vaananen Date: Thu, 3 Mar 2022 16:20:21 +0200 Subject: [PATCH 2/2] oops added flag to the wrong command --- docker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker.go b/docker.go index 1a414d8..244e2d8 100644 --- a/docker.go +++ b/docker.go @@ -46,8 +46,8 @@ var ( func init() { dockerfile = rootCmd.PersistentFlags().StringP("dockerfile", "f", "Dockerfile", "Path to your Dockerfile (or - for stdin)") rootCmd.MarkPersistentFlagFilename("dockerfile") - username = rootCmd.PersistentFlags().StringP("username", "u", "", "Username to authenticate with to the Docker registry") - password = rootCmd.PersistentFlags().StringP("password", "p", "", "Password to authenticate with to the Docker registry") + username = dockerCmd.PersistentFlags().StringP("username", "u", "", "Username to authenticate with to the Docker registry") + password = dockerCmd.PersistentFlags().StringP("password", "p", "", "Password to authenticate with to the Docker registry") } func authStringForCommand(cmd *cobra.Command) string {