Skip to content

Commit

Permalink
Use default keychain to authenticate image pulls
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Biville committed Dec 11, 2018
1 parent 125c759 commit 3368be1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions docker/docker.go
Expand Up @@ -2,15 +2,18 @@ package docker

import (
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
dockercli "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"io"
"io/ioutil"
"strings"
)

type Client struct {
Expand Down Expand Up @@ -59,7 +62,18 @@ func (d *Client) RunContainer(ctx context.Context, id string, stdout io.Writer,
}

func (d *Client) PullImage(ref string) error {
rc, err := d.ImagePull(context.Background(), ref, dockertypes.ImagePullOptions{})
reference, _ := name.ParseReference(ref, name.WeakValidation)
authenticator, _ := authn.DefaultKeychain.Resolve(reference.Context().Registry)
encodedHeader, _ := authenticator.Authorization()
encodedToken := strings.Replace(encodedHeader, "Basic ", "", 1)
tokenBytes, _ := base64.StdEncoding.DecodeString(encodedToken)
tokenAtoms := strings.SplitN(string(tokenBytes), ":", 2)
rc, err := d.ImagePull(context.Background(), ref, dockertypes.ImagePullOptions{
RegistryAuth: base64.StdEncoding.EncodeToString([]byte(
fmt.Sprintf(`{"username": "%s", "password": "%s"}`,
tokenAtoms[0],
tokenAtoms[1]))),
})
if err != nil {
// Retry
rc, err = d.ImagePull(context.Background(), ref, dockertypes.ImagePullOptions{})
Expand Down

0 comments on commit 3368be1

Please sign in to comment.