Skip to content

Commit

Permalink
Merge pull request #10608 from guangxuli/openshift_use_k8s_func
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed May 4, 2017
2 parents 0f08ac1 + 4fe6455 commit fb32130
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 104 deletions.
82 changes: 35 additions & 47 deletions pkg/build/builder/cmd/dockercfg/cfg.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package dockercfg

import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"

docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
Expand All @@ -16,8 +13,6 @@ import (
"k8s.io/kubernetes/pkg/credentialprovider"
)

//TODO: Remove this code once the methods in Kubernetes kubelet/dockertools/config.go are public

const (
PushAuthType = "PUSH_DOCKERCFG_PATH"
PullAuthType = "PULL_DOCKERCFG_PATH"
Expand All @@ -42,31 +37,19 @@ func (h *Helper) InstallFlags(flags *pflag.FlagSet) {
// from the local dockercfg file
func (h *Helper) GetDockerAuth(imageName, authType string) (docker.AuthConfiguration, bool) {
glog.V(3).Infof("Locating docker auth for image %s and type %s", imageName, authType)
var dockercfgPath string
if pathForAuthType := os.Getenv(authType); len(pathForAuthType) > 0 {
dockercfgPath = GetDockercfgFile(pathForAuthType)
} else {
dockercfgPath = GetDockercfgFile("")
}
if len(dockercfgPath) == 0 {
glog.V(3).Infof("Could not locate a docker config file")
return docker.AuthConfiguration{}, false
}
if _, err := os.Stat(dockercfgPath); err != nil {
glog.V(3).Infof("Problem accessing %s: %v", dockercfgPath, err)
return docker.AuthConfiguration{}, false
}

var searchPaths []string
var cfg credentialprovider.DockerConfig
var err error
if strings.HasSuffix(dockercfgPath, kapi.DockerConfigJsonKey) || strings.HasSuffix(dockercfgPath, "config.json") {
cfg, err = readDockerConfigJson(dockercfgPath)
} else if strings.HasSuffix(dockercfgPath, kapi.DockerConfigKey) {
cfg, err = readDockercfg(dockercfgPath)
}

if pathForAuthType := os.Getenv(authType); len(pathForAuthType) > 0 {
searchPaths = []string{pathForAuthType}
} else {
searchPaths = getExtraSearchPaths()
}
glog.V(3).Infof("Getting docker auth in paths : %v", searchPaths)
cfg, err = GetDockerConfig(searchPaths)
if err != nil {
glog.Errorf("Reading %s failed: %v", dockercfgPath, err)
glog.Errorf("Reading docker config from %v failed: %v", searchPaths, err)
return docker.AuthConfiguration{}, false
}

Expand Down Expand Up @@ -113,30 +96,35 @@ func GetDockercfgFile(path string) string {
return cfgPath
}

// readDockercfg reads the contents of a .dockercfg file into a map
// with server name keys and AuthEntry values
func readDockercfg(filePath string) (cfg credentialprovider.DockerConfig, err error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return
}
if err := json.Unmarshal(content, &cfg); err != nil {
return nil, err
// GetDockerConfig return docker config info by checking given paths
func GetDockerConfig(path []string) (cfg credentialprovider.DockerConfig, err error) {
if cfg, err = credentialprovider.ReadDockerConfigJSONFile(path); err != nil {
if cfg, err = ReadDockerConfigJsonFileGeneratedFromSecret(path); err != nil {
cfg, err = credentialprovider.ReadDockercfgFile(path)
}
}
return
return cfg, err
}

// readDockerConfigJson reads the contents of a .docker/config.json file into a map
// with server name keys and AuthEntry values
func readDockerConfigJson(filePath string) (cfg credentialprovider.DockerConfig, err error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return
// ReadDockerConfigJsonFileGeneratedFromSecret return DockerConfig by reading specific file named .dockerconfigjson
// generated by secret from given paths.
func ReadDockerConfigJsonFileGeneratedFromSecret(path []string) (cfg credentialprovider.DockerConfig, err error) {
for _, filePath := range path {
cfg, err = credentialprovider.ReadSpecificDockerConfigJsonFile(filepath.Join(filePath, kapi.DockerConfigJsonKey))
if err == nil {
return cfg, nil
}
}
var config credentialprovider.DockerConfigJson
if err = json.Unmarshal(content, &config); err != nil {
return
return nil, err
}

//getExtraSearchPaths get extra paths that may contain docker-config type files.
//this invocation we do not need to handle user.Current() since upstream k8s have handled HOME path
func getExtraSearchPaths() (searchPaths []string) {
if dockerCfgPath := os.Getenv("DOCKERCFG_PATH"); dockerCfgPath != "" {
dockerCfgDir := filepath.Dir(dockerCfgPath)
searchPaths = append(searchPaths, dockerCfgDir)
}
cfg = config.Auths
return

return searchPaths
}
56 changes: 0 additions & 56 deletions pkg/build/builder/cmd/dockercfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"
"path/filepath"
"testing"

"k8s.io/kubernetes/pkg/credentialprovider"
)

func TestGetDockerAuth(t *testing.T) {
Expand Down Expand Up @@ -52,57 +50,3 @@ func TestGetDockerAuth(t *testing.T) {
return
}
}

func TestReadDockercfg(t *testing.T) {
content := "{\"test-server-1.tld\":{\"auth\":\"Zm9vOmJhcgo=\",\"email\":\"test@email.test.com\"}}"
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 != "test@email.test.com" {
t.Errorf("Unexpected Email value: %s", authConfs[0].Email)
}
}

func TestReadDockerConfigJson(t *testing.T) {
content := "{ \"auths\": { \"test-server-1.tld\":{\"auth\":\"Zm9vOmJhcgo=\",\"email\":\"test@email.test.com\"}}}"
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 := readDockerConfigJson(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 != "test@email.test.com" {
t.Errorf("Unexpected Email value: %s", authConfs[0].Email)
}
}
1 change: 0 additions & 1 deletion pkg/build/builder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ func (d *DockerBuilder) setupPullSecret() (*docker.AuthConfigurations, error) {
return nil, fmt.Errorf("'%s': %s", dockercfgPath, err)
}
return docker.NewAuthConfigurations(r)

}

// dockerBuild performs a docker build on the source that has been retrieved
Expand Down

0 comments on commit fb32130

Please sign in to comment.