2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package credentialprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2019-08-30 18:33:25 +00:00
|
|
|
"errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
2019-08-30 18:33:25 +00:00
|
|
|
"io"
|
2019-01-12 04:58:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
const (
|
|
|
|
maxReadLength = 10 * 1 << 20 // 10MB
|
|
|
|
)
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// DockerConfigJSON represents ~/.docker/config.json file info
|
2019-01-12 04:58:27 +00:00
|
|
|
// see https://github.com/docker/docker/pull/12009
|
2020-08-10 17:43:49 +00:00
|
|
|
type DockerConfigJSON struct {
|
2019-01-12 04:58:27 +00:00
|
|
|
Auths DockerConfig `json:"auths"`
|
|
|
|
// +optional
|
2020-08-10 17:43:49 +00:00
|
|
|
HTTPHeaders map[string]string `json:"HttpHeaders,omitempty"`
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DockerConfig represents the config file used by the docker CLI.
|
|
|
|
// This config that represents the credentials that should be used
|
|
|
|
// when pulling images from specific image repositories.
|
|
|
|
type DockerConfig map[string]DockerConfigEntry
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// DockerConfigEntry wraps a docker config as a entry
|
2019-01-12 04:58:27 +00:00
|
|
|
type DockerConfigEntry struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Email string
|
|
|
|
Provider DockerConfigProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
preferredPathLock sync.Mutex
|
|
|
|
preferredPath = ""
|
|
|
|
workingDirPath = ""
|
2019-08-30 18:33:25 +00:00
|
|
|
homeDirPath, _ = os.UserHomeDir()
|
2019-01-12 04:58:27 +00:00
|
|
|
rootDirPath = "/"
|
2020-08-10 17:43:49 +00:00
|
|
|
homeJSONDirPath = filepath.Join(homeDirPath, ".docker")
|
|
|
|
rootJSONDirPath = filepath.Join(rootDirPath, ".docker")
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
configFileName = ".dockercfg"
|
2020-08-10 17:43:49 +00:00
|
|
|
configJSONFileName = "config.json"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// SetPreferredDockercfgPath set preferred docker config path
|
2019-01-12 04:58:27 +00:00
|
|
|
func SetPreferredDockercfgPath(path string) {
|
|
|
|
preferredPathLock.Lock()
|
|
|
|
defer preferredPathLock.Unlock()
|
|
|
|
preferredPath = path
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// GetPreferredDockercfgPath get preferred docker config path
|
2019-01-12 04:58:27 +00:00
|
|
|
func GetPreferredDockercfgPath() string {
|
|
|
|
preferredPathLock.Lock()
|
|
|
|
defer preferredPathLock.Unlock()
|
|
|
|
return preferredPath
|
|
|
|
}
|
|
|
|
|
|
|
|
//DefaultDockercfgPaths returns default search paths of .dockercfg
|
|
|
|
func DefaultDockercfgPaths() []string {
|
|
|
|
return []string{GetPreferredDockercfgPath(), workingDirPath, homeDirPath, rootDirPath}
|
|
|
|
}
|
|
|
|
|
|
|
|
//DefaultDockerConfigJSONPaths returns default search paths of .docker/config.json
|
|
|
|
func DefaultDockerConfigJSONPaths() []string {
|
2020-08-10 17:43:49 +00:00
|
|
|
return []string{GetPreferredDockercfgPath(), workingDirPath, homeJSONDirPath, rootJSONDirPath}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDockercfgFile attempts to read a legacy dockercfg file from the given paths.
|
|
|
|
// if searchPaths is empty, the default paths are used.
|
|
|
|
func ReadDockercfgFile(searchPaths []string) (cfg DockerConfig, err error) {
|
|
|
|
if len(searchPaths) == 0 {
|
|
|
|
searchPaths = DefaultDockercfgPaths()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, configPath := range searchPaths {
|
|
|
|
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configFileName))
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("while trying to canonicalize %s: %v", configPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
klog.V(4).Infof("looking for .dockercfg at %s", absDockerConfigFileLocation)
|
|
|
|
contents, err := ioutil.ReadFile(absDockerConfigFileLocation)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
klog.V(4).Infof("while trying to read %s: %v", absDockerConfigFileLocation, err)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
cfg, err := ReadDockerConfigFileFromBytes(contents)
|
2020-10-14 19:03:41 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.V(4).Infof("couldn't get the config from %q contents: %v", absDockerConfigFileLocation, err)
|
|
|
|
continue
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-10-14 19:03:41 +00:00
|
|
|
|
|
|
|
klog.V(4).Infof("found .dockercfg at %s", absDockerConfigFileLocation)
|
|
|
|
return cfg, nil
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("couldn't find valid .dockercfg after checking in %v", searchPaths)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDockerConfigJSONFile attempts to read a docker config.json file from the given paths.
|
|
|
|
// if searchPaths is empty, the default paths are used.
|
|
|
|
func ReadDockerConfigJSONFile(searchPaths []string) (cfg DockerConfig, err error) {
|
|
|
|
if len(searchPaths) == 0 {
|
|
|
|
searchPaths = DefaultDockerConfigJSONPaths()
|
|
|
|
}
|
|
|
|
for _, configPath := range searchPaths {
|
2020-08-10 17:43:49 +00:00
|
|
|
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configJSONFileName))
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("while trying to canonicalize %s: %v", configPath, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
klog.V(4).Infof("looking for %s at %s", configJSONFileName, absDockerConfigFileLocation)
|
|
|
|
cfg, err = ReadSpecificDockerConfigJSONFile(absDockerConfigFileLocation)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
klog.V(4).Infof("while trying to read %s: %v", absDockerConfigFileLocation, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
klog.V(4).Infof("found valid %s at %s", configJSONFileName, absDockerConfigFileLocation)
|
2019-01-12 04:58:27 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil, fmt.Errorf("couldn't find valid %s after checking in %v", configJSONFileName, searchPaths)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
//ReadSpecificDockerConfigJSONFile attempts to read docker configJSON from a given file path.
|
|
|
|
func ReadSpecificDockerConfigJSONFile(filePath string) (cfg DockerConfig, err error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
var contents []byte
|
|
|
|
|
|
|
|
if contents, err = ioutil.ReadFile(filePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return readDockerConfigJSONFileFromBytes(contents)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// ReadDockerConfigFile read a docker config file from default path
|
2019-01-12 04:58:27 +00:00
|
|
|
func ReadDockerConfigFile() (cfg DockerConfig, err error) {
|
|
|
|
if cfg, err := ReadDockerConfigJSONFile(nil); err == nil {
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
// Can't find latest config file so check for the old one
|
|
|
|
return ReadDockercfgFile(nil)
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// HTTPError wraps a non-StatusOK error code as an error.
|
|
|
|
type HTTPError struct {
|
2019-01-12 04:58:27 +00:00
|
|
|
StatusCode int
|
2020-08-10 17:43:49 +00:00
|
|
|
URL string
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error
|
2020-08-10 17:43:49 +00:00
|
|
|
func (he *HTTPError) Error() string {
|
2019-01-12 04:58:27 +00:00
|
|
|
return fmt.Sprintf("http status code: %d while fetching url %s",
|
2020-08-10 17:43:49 +00:00
|
|
|
he.StatusCode, he.URL)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// ReadURL read contents from given url
|
|
|
|
func ReadURL(url string, client *http.Client, header *http.Header) (body []byte, err error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header != nil {
|
|
|
|
req.Header = *header
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
klog.V(2).Infof("body of failing http response: %v", resp.Body)
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil, &HTTPError{
|
2019-01-12 04:58:27 +00:00
|
|
|
StatusCode: resp.StatusCode,
|
2020-08-10 17:43:49 +00:00
|
|
|
URL: url,
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
limitedReader := &io.LimitedReader{R: resp.Body, N: maxReadLength}
|
|
|
|
contents, err := ioutil.ReadAll(limitedReader)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
if limitedReader.N <= 0 {
|
|
|
|
return nil, errors.New("the read limit is reached")
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// ReadDockerConfigFileFromURL read a docker config file from the given url
|
|
|
|
func ReadDockerConfigFileFromURL(url string, client *http.Client, header *http.Header) (cfg DockerConfig, err error) {
|
|
|
|
if contents, err := ReadURL(url, client, header); err == nil {
|
2021-03-18 22:40:29 +00:00
|
|
|
return ReadDockerConfigFileFromBytes(contents)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
return nil, err
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// ReadDockerConfigFileFromBytes read a docker config file from the given bytes
|
|
|
|
func ReadDockerConfigFileFromBytes(contents []byte) (cfg DockerConfig, err error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
if err = json.Unmarshal(contents, &cfg); err != nil {
|
2020-10-14 19:03:41 +00:00
|
|
|
return nil, errors.New("error occurred while trying to unmarshal json")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func readDockerConfigJSONFileFromBytes(contents []byte) (cfg DockerConfig, err error) {
|
|
|
|
var cfgJSON DockerConfigJSON
|
|
|
|
if err = json.Unmarshal(contents, &cfgJSON); err != nil {
|
2020-10-14 19:03:41 +00:00
|
|
|
return nil, errors.New("error occurred while trying to unmarshal json")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
cfg = cfgJSON.Auths
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// dockerConfigEntryWithAuth is used solely for deserializing the Auth field
|
|
|
|
// into a dockerConfigEntry during JSON deserialization.
|
|
|
|
type dockerConfigEntryWithAuth struct {
|
|
|
|
// +optional
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
// +optional
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
// +optional
|
|
|
|
Email string `json:"email,omitempty"`
|
|
|
|
// +optional
|
|
|
|
Auth string `json:"auth,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2019-01-12 04:58:27 +00:00
|
|
|
func (ident *DockerConfigEntry) UnmarshalJSON(data []byte) error {
|
|
|
|
var tmp dockerConfigEntryWithAuth
|
|
|
|
err := json.Unmarshal(data, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ident.Username = tmp.Username
|
|
|
|
ident.Password = tmp.Password
|
|
|
|
ident.Email = tmp.Email
|
|
|
|
|
|
|
|
if len(tmp.Auth) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ident.Username, ident.Password, err = decodeDockerConfigFieldAuth(tmp.Auth)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2019-01-12 04:58:27 +00:00
|
|
|
func (ident DockerConfigEntry) MarshalJSON() ([]byte, error) {
|
|
|
|
toEncode := dockerConfigEntryWithAuth{ident.Username, ident.Password, ident.Email, ""}
|
|
|
|
toEncode.Auth = encodeDockerConfigFieldAuth(ident.Username, ident.Password)
|
|
|
|
|
|
|
|
return json.Marshal(toEncode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeDockerConfigFieldAuth deserializes the "auth" field from dockercfg into a
|
|
|
|
// username and a password. The format of the auth field is base64(<username>:<password>).
|
|
|
|
func decodeDockerConfigFieldAuth(field string) (username, password string, err error) {
|
2019-12-12 01:27:03 +00:00
|
|
|
|
|
|
|
var decoded []byte
|
|
|
|
|
|
|
|
// StdEncoding can only decode padded string
|
|
|
|
// RawStdEncoding can only decode unpadded string
|
|
|
|
if strings.HasSuffix(strings.TrimSpace(field), "=") {
|
|
|
|
// decode padded data
|
|
|
|
decoded, err = base64.StdEncoding.DecodeString(field)
|
|
|
|
} else {
|
|
|
|
// decode unpadded data
|
|
|
|
decoded, err = base64.RawStdEncoding.DecodeString(field)
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(string(decoded), ":", 2)
|
|
|
|
if len(parts) != 2 {
|
2020-08-10 17:43:49 +00:00
|
|
|
err = fmt.Errorf("unable to parse auth field, must be formatted as base64(username:password)")
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
username = parts[0]
|
|
|
|
password = parts[1]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeDockerConfigFieldAuth(username, password string) string {
|
|
|
|
fieldValue := username + ":" + password
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(fieldValue))
|
|
|
|
}
|