Example #1
0
func TestReadDockercfg(t *testing.T) {
	content := "{\"test-server-1.tld\":{\"auth\":\"Zm9vOmJhcgo=\",\"email\":\"[email protected]\"}}"
	tempfile, err := ioutil.TempFile("", "cfgtest")
	if err != nil {
		t.Fatalf("Unable to create temp file: %v", err)
	}
	defer os.Remove(tempfile.Name())
	tempfile.WriteString(content)
	tempfile.Close()

	dockercfg, err := readDockercfg(tempfile.Name())
	if err != nil {
		t.Errorf("Received unexpected error reading dockercfg: %v", err)
		return
	}

	keyring := credentialprovider.BasicDockerKeyring{}
	keyring.Add(dockercfg)
	authConfs, found := keyring.Lookup("test-server-1.tld/foo/bar")
	if !found || len(authConfs) == 0 {
		t.Errorf("Expected lookup success, got not found")
	}
	if authConfs[0].Email != "*****@*****.**" {
		t.Errorf("Unexpected Email value: %s", authConfs[0].Email)
	}
}
Example #2
0
// GetDockerAuth returns a valid Docker AuthConfiguration entry, and whether it was read
// from the local dockercfg file
func (h *Helper) GetDockerAuth(imageName, authType string) (docker.AuthConfiguration, bool) {
	dockercfgPath := getDockercfgFile("")
	if pathForAuthType := os.Getenv(authType); len(pathForAuthType) > 0 {
		glog.V(3).Infof("%s=%s", authType, pathForAuthType)
		dockercfgPath = getDockercfgFile(pathForAuthType)
	}
	if _, err := os.Stat(dockercfgPath); err != nil {
		glog.V(3).Infof("Problem accessing %s: %v", dockercfgPath, err)
		return docker.AuthConfiguration{}, false
	}
	cfg, err := readDockercfg(dockercfgPath)
	if err != nil {
		glog.Errorf("Reading %s failed: ", dockercfgPath, err)
		return docker.AuthConfiguration{}, false
	}
	keyring := credentialprovider.BasicDockerKeyring{}
	keyring.Add(cfg)
	authConfs, found := keyring.Lookup(imageName)
	if !found || len(authConfs) == 0 {
		return docker.AuthConfiguration{}, false
	}
	glog.V(3).Infof("Using %s user for Docker authentication", authConfs[0].Username)
	return authConfs[0], true
}