Skip to content
Closed
Show file tree
Hide file tree
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
57 changes: 1 addition & 56 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

Expand All @@ -21,10 +20,6 @@ const (
dockerRegistry = "registry-1.docker.io"
dockerAuthRegistry = "https://index.docker.io/v1/"

dockerCfg = ".docker"
dockerCfgFileName = "config.json"
dockerCfgObsolete = ".dockercfg"

baseURL = "%s://%s/v2/"
tagsURL = "%s/tags/list"
manifestURL = "%s/manifests/%s"
Expand All @@ -43,17 +38,13 @@ type dockerClient struct {
}

// newDockerClient returns a new dockerClient instance for refHostname (a host a specified in the Docker image reference, not canonicalized to dockerRegistry)
func newDockerClient(refHostname, certPath string, tlsVerify bool) (*dockerClient, error) {
func newDockerClient(refHostname, certPath string, tlsVerify bool, username, password string) (*dockerClient, error) {
var registry string
if refHostname == dockerHostname {
registry = dockerRegistry
} else {
registry = refHostname
}
username, password, err := getAuth(refHostname)
if err != nil {
return nil, err
}
var tr *http.Transport
if certPath != "" || !tlsVerify {
tlsc := &tls.Config{}
Expand Down Expand Up @@ -234,52 +225,6 @@ func (c *dockerClient) getBearerToken(realm, service, scope string) (string, err
return tokenStruct.Token, nil
}

func getAuth(hostname string) (string, string, error) {
// TODO(runcom): get this from *cli.Context somehow
//if username != "" && password != "" {
//return username, password, nil
//}
if hostname == dockerHostname {
hostname = dockerAuthRegistry
}
dockerCfgPath := filepath.Join(getDefaultConfigDir(".docker"), dockerCfgFileName)
if _, err := os.Stat(dockerCfgPath); err == nil {
j, err := ioutil.ReadFile(dockerCfgPath)
if err != nil {
return "", "", err
}
var dockerAuth dockerConfigFile
if err := json.Unmarshal(j, &dockerAuth); err != nil {
return "", "", err
}
// try the normal case
if c, ok := dockerAuth.AuthConfigs[hostname]; ok {
return decodeDockerAuth(c.Auth)
}
} else if os.IsNotExist(err) {
oldDockerCfgPath := filepath.Join(getDefaultConfigDir(dockerCfgObsolete))
if _, err := os.Stat(oldDockerCfgPath); err != nil {
return "", "", nil //missing file is not an error
}
j, err := ioutil.ReadFile(oldDockerCfgPath)
if err != nil {
return "", "", err
}
var dockerAuthOld map[string]dockerAuthConfigObsolete
if err := json.Unmarshal(j, &dockerAuthOld); err != nil {
return "", "", err
}
if c, ok := dockerAuthOld[hostname]; ok {
return decodeDockerAuth(c.Auth)
}
} else {
// if file is there but we can't stat it for any reason other
// than it doesn't exist then stop
return "", "", fmt.Errorf("%s - %v", dockerCfgPath, err)
}
return "", "", nil
}

type apiErr struct {
Code string
Message string
Expand Down
4 changes: 2 additions & 2 deletions docker/docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Image struct {

// NewImage returns a new Image interface type after setting up
// a client to the registry hosting the given image.
func NewImage(img, certPath string, tlsVerify bool) (types.Image, error) {
s, err := newDockerImageSource(img, certPath, tlsVerify)
func NewImage(img, certPath string, tlsVerify bool, username, password string) (types.Image, error) {
s, err := newDockerImageSource(img, certPath, tlsVerify, username, password)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions docker/docker_image_dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type dockerImageDestination struct {
}

// NewImageDestination creates a new ImageDestination for the specified image and connection specification.
func NewImageDestination(img, certPath string, tlsVerify bool) (types.ImageDestination, error) {
func NewImageDestination(img, certPath string, tlsVerify bool, username, password string) (types.ImageDestination, error) {
ref, err := parseImageName(img)
if err != nil {
return nil, err
}
c, err := newDockerClient(ref.Hostname(), certPath, tlsVerify)
c, err := newDockerClient(ref.Hostname(), certPath, tlsVerify, username, password)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions docker/docker_image_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type dockerImageSource struct {
}

// newDockerImageSource is the same as NewImageSource, only it returns the more specific *dockerImageSource type.
func newDockerImageSource(img, certPath string, tlsVerify bool) (*dockerImageSource, error) {
func newDockerImageSource(img, certPath string, tlsVerify bool, username, password string) (*dockerImageSource, error) {
ref, err := parseImageName(img)
if err != nil {
return nil, err
}
c, err := newDockerClient(ref.Hostname(), certPath, tlsVerify)
c, err := newDockerClient(ref.Hostname(), certPath, tlsVerify, username, password)
if err != nil {
return nil, err
}
Expand All @@ -45,8 +45,8 @@ func newDockerImageSource(img, certPath string, tlsVerify bool) (*dockerImageSou
}

// NewImageSource creates a new ImageSource for the specified image and connection specification.
func NewImageSource(img, certPath string, tlsVerify bool) (types.ImageSource, error) {
return newDockerImageSource(img, certPath, tlsVerify)
func NewImageSource(img, certPath string, tlsVerify bool, username, password string) (types.ImageSource, error) {
return newDockerImageSource(img, certPath, tlsVerify, username, password)
}

// IntendedDockerReference returns the Docker reference for this image, _as specified by the user_
Expand Down
12 changes: 8 additions & 4 deletions openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ type openshiftImageSource struct {
// Values specific to this image
certPath string // Only for parseDockerImageSource
tlsVerify bool // Only for parseDockerImageSource
username string // Only for parseDockerImageSource
password string // Only for parseDockerImageSource
// State
docker types.ImageSource // The Docker Registry endpoint, or nil if not resolved yet
imageStreamImageName string // Resolved image identifier, or "" if not known yet
}

// NewImageSource creates a new ImageSource for the specified image and connection specification.
func NewImageSource(imageName, certPath string, tlsVerify bool) (types.ImageSource, error) {
func NewImageSource(imageName, certPath string, tlsVerify bool, username, password string) (types.ImageSource, error) {
client, err := newOpenshiftClient(imageName)
if err != nil {
return nil, err
Expand All @@ -192,6 +194,8 @@ func NewImageSource(imageName, certPath string, tlsVerify bool) (types.ImageSour
client: client,
certPath: certPath,
tlsVerify: tlsVerify,
username: username,
password: password,
}, nil
}

Expand Down Expand Up @@ -257,7 +261,7 @@ func (s *openshiftImageSource) ensureImageIsResolved() error {
return err
}
logrus.Debugf("Resolved reference %#v", dockerRef)
d, err := docker.NewImageSource(dockerRef, s.certPath, s.tlsVerify)
d, err := docker.NewImageSource(dockerRef, s.certPath, s.tlsVerify, s.username, s.password)
if err != nil {
return err
}
Expand All @@ -272,7 +276,7 @@ type openshiftImageDestination struct {
}

// NewImageDestination creates a new ImageDestination for the specified image and connection specification.
func NewImageDestination(imageName, certPath string, tlsVerify bool) (types.ImageDestination, error) {
func NewImageDestination(imageName, certPath string, tlsVerify bool, username, password string) (types.ImageDestination, error) {
client, err := newOpenshiftClient(imageName)
if err != nil {
return nil, err
Expand All @@ -282,7 +286,7 @@ func NewImageDestination(imageName, certPath string, tlsVerify bool) (types.Imag
// i.e. a single signed image cannot be available under multiple tags. But with types.ImageDestination, we don't know
// the manifest digest at this point.
dockerRef := fmt.Sprintf("%s/%s/%s:%s", client.dockerRegistryHostPart(), client.namespace, client.stream, client.tag)
docker, err := docker.NewImageDestination(dockerRef, certPath, tlsVerify)
docker, err := docker.NewImageDestination(dockerRef, certPath, tlsVerify, username, password)
if err != nil {
return nil, err
}
Expand Down