diff --git a/cmd/kaniko-acr/main.go b/cmd/kaniko-acr/main.go index 42e82f3..5327c6d 100644 --- a/cmd/kaniko-acr/main.go +++ b/cmd/kaniko-acr/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/base64" "encoding/json" "fmt" "io/ioutil" @@ -18,6 +17,7 @@ import ( kaniko "github.com/drone/drone-kaniko" "github.com/drone/drone-kaniko/pkg/artifact" + "github.com/drone/drone-kaniko/pkg/docker" ) const ( @@ -27,7 +27,6 @@ const ( tenantKeyEnv string = "AZURE_TENANT_ID" certPathEnv string = "AZURE_CLIENT_CERTIFICATE_PATH" dockerConfigPath string = "/kaniko/.docker/config.json" - kanikoVersionEnv string = "KANIKO_VERSION" defaultDigestFile string = "/kaniko/digest-file" ) @@ -276,7 +275,7 @@ func createDockerConfig(tenantId, clientId, cert, if err != nil { return errors.Wrap(err, "failed to fetch ACR Token") } - err = createDockerCfgFile(username, token, registry) + err = docker.CreateDockerCfgFile(username, token, registry, dockerConfigPath) if err != nil { return errors.Wrap(err, "failed to create docker config") } @@ -362,31 +361,6 @@ func fetchACRToken(tenantId, token, registry string) (string, error) { } } -// Create the docker config file for authentication -func createDockerCfgFile(username, password, registry string) error { - if username == "" { - return fmt.Errorf("Username must be specified") - } - if password == "" { - return fmt.Errorf("Password must be specified") - } - - err := os.MkdirAll(dockerPath, 0600) - if err != nil { - return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", dockerPath)) - } - - authBytes := []byte(fmt.Sprintf("%s:%s", username, password)) - encodedString := base64.StdEncoding.EncodeToString(authBytes) - jsonBytes := []byte(fmt.Sprintf(`{"auths": {"%s": {"auth": "%s"}}}`, "https://"+registry, encodedString)) - err = ioutil.WriteFile(dockerConfigPath, jsonBytes, 0644) - if err != nil { - return errors.Wrap(err, "failed to create docker config file") - } - fmt.Print("crated docker file at " + dockerConfigPath) - return nil -} - func setupACRCert(jsonKey string) error { err := ioutil.WriteFile(ACRCertPath, []byte(jsonKey), 0644) if err != nil { diff --git a/pkg/docker/docker_file.go b/pkg/docker/docker_file.go new file mode 100644 index 0000000..c5dc018 --- /dev/null +++ b/pkg/docker/docker_file.go @@ -0,0 +1,35 @@ +package docker + +import ( + "encoding/base64" + "fmt" + "io/ioutil" + "os" + + "github.com/pkg/errors" +) + +// Create the docker config file for authentication +func CreateDockerCfgFile(username, password, registry, path string) error { + if username == "" { + return fmt.Errorf("Username must be specified") + } + if password == "" { + return fmt.Errorf("Password must be specified") + } + + err := os.MkdirAll(path, 0600) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", path)) + } + + authBytes := []byte(fmt.Sprintf("%s:%s", username, password)) + encodedString := base64.StdEncoding.EncodeToString(authBytes) + jsonBytes := []byte(fmt.Sprintf(`{"auths": {"%s": {"auth": "%s"}}}`, "https://"+registry, encodedString)) + err = ioutil.WriteFile(path, jsonBytes, 0644) + if err != nil { + return errors.Wrap(err, "failed to create docker config file") + } + fmt.Print("crated docker file at " + path) + return nil +}