Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional authentication flags for Docker client #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 = 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 {
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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down