Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
}

func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptions, copts *containerOptions) error {
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), copts.env.GetAll())
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(copts.env.GetAll()))
newEnv := []string{}
for k, v := range proxyConfig {
if v == nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
}

func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copts *containerOptions) error {
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), copts.env.GetAll())
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(copts.env.GetAll()))
newEnv := []string{}
for k, v := range proxyConfig {
if v == nil {
Expand Down
8 changes: 6 additions & 2 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,11 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
}

configFile := dockerCli.ConfigFile()
authConfigs, _ := configFile.GetAllCredentials()
creds, _ := configFile.GetAllCredentials()
authConfigs := make(map[string]types.AuthConfig, len(creds))
for k, auth := range creds {
authConfigs[k] = types.AuthConfig(auth)
}
buildOptions := imageBuildOptions(dockerCli, options)
buildOptions.Version = types.BuilderV1
buildOptions.Dockerfile = relDockerfile
Expand Down Expand Up @@ -619,7 +623,7 @@ func imageBuildOptions(dockerCli command.Cli, options buildOptions) types.ImageB
CgroupParent: options.cgroupParent,
ShmSize: options.shmSize.Value(),
Ulimits: options.ulimits.GetList(),
BuildArgs: configFile.ParseProxyConfig(dockerCli.Client().DaemonHost(), options.buildArgs.GetAll()),
BuildArgs: configFile.ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(options.buildArgs.GetAll())),
Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()),
CacheFrom: options.cacheFrom,
SecurityOpt: options.securityOpt,
Expand Down
10 changes: 6 additions & 4 deletions cli/command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"
"strings"

configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/debug"
"github.com/docker/cli/cli/streams"
"github.com/docker/distribution/reference"
Expand Down Expand Up @@ -77,7 +78,7 @@ func ResolveAuthConfig(ctx context.Context, cli Cli, index *registrytypes.IndexI
}

a, _ := cli.ConfigFile().GetAuthConfig(configKey)
return a
return types.AuthConfig(a)
}

// GetDefaultAuthConfig gets the default auth config given a serverAddress
Expand All @@ -86,16 +87,17 @@ func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, is
if !isDefaultRegistry {
serverAddress = registry.ConvertToHostname(serverAddress)
}
var authconfig types.AuthConfig
var authconfig configtypes.AuthConfig
var err error
if checkCredStore {
authconfig, err = cli.ConfigFile().GetAuthConfig(serverAddress)
} else {
authconfig = types.AuthConfig{}
authconfig = configtypes.AuthConfig{}
}
authconfig.ServerAddress = serverAddress
authconfig.IdentityToken = ""
return &authconfig, err
res := types.AuthConfig(authconfig)
return &res, err
}

// ConfigureAuth handles prompting of user's username and password if needed
Expand Down
3 changes: 2 additions & 1 deletion cli/command/registry/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -149,7 +150,7 @@ func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint: gocycl
}
}

if err := creds.Store(*authConfig); err != nil {
if err := creds.Store(configtypes.AuthConfig(*authConfig)); err != nil {
return errors.Errorf("Error saving credentials: %v", err)
}

Expand Down
11 changes: 6 additions & 5 deletions cli/command/registry/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"testing"

configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
Expand Down Expand Up @@ -79,21 +80,21 @@ func TestRunLogin(t *testing.T) {
const validPassword = "p1"
const validPassword2 = "p2"

validAuthConfig := types.AuthConfig{
validAuthConfig := configtypes.AuthConfig{
ServerAddress: storedServerAddress,
Username: validUsername,
Password: validPassword,
}
expiredAuthConfig := types.AuthConfig{
expiredAuthConfig := configtypes.AuthConfig{
ServerAddress: storedServerAddress,
Username: validUsername,
Password: expiredPassword,
}
testCases := []struct {
inputLoginOption loginOptions
inputStoredCred *types.AuthConfig
inputStoredCred *configtypes.AuthConfig
expectedErr string
expectedSavedCred types.AuthConfig
expectedSavedCred configtypes.AuthConfig
}{
{
inputLoginOption: loginOptions{
Expand All @@ -118,7 +119,7 @@ func TestRunLogin(t *testing.T) {
},
inputStoredCred: &validAuthConfig,
expectedErr: "",
expectedSavedCred: types.AuthConfig{
expectedSavedCred: configtypes.AuthConfig{
ServerAddress: storedServerAddress,
Username: validUsername,
Password: validPassword2,
Expand Down
3 changes: 2 additions & 1 deletion cli/command/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// Prevents a circular import with "github.com/docker/cli/internal/test"

. "github.com/docker/cli/cli/command"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/debug"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestGetDefaultAuthConfig(t *testing.T) {
errBuf := new(bytes.Buffer)
cli.SetErr(errBuf)
for _, authconfig := range testAuthConfigs {
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(authconfig)
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
}
for _, tc := range testCases {
serverAddress := tc.inputServerAddress
Expand Down
2 changes: 1 addition & 1 deletion cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
"github.com/docker/docker/pkg/homedir"
"github.com/pkg/errors"
)
Expand Down
10 changes: 6 additions & 4 deletions cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"strings"

"github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -196,7 +195,7 @@ func (configFile *ConfigFile) Save() error {

// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and
// then checking this against any environment variables provided to the container
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts []string) map[string]*string {
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts map[string]*string) map[string]*string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, funny, I was working on some refactoring for this a while back related to #1574 (I see I have two implementations, but trying to recall now what was the main difference; 24e1f96 and f0f3511 (master...thaJeztah:refactor_proxy_config and master...thaJeztah:refactor_proxy_config2)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, related to that; perhaps you could review #1573 😇

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they are equivalent refactors. I'm trying to remove dependency of docker/docker from cli/config. It's not pretty but this is what I have :)

var cfgKey string

if _, ok := configFile.Proxies[host]; !ok {
Expand All @@ -212,7 +211,10 @@ func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts []string) ma
"NO_PROXY": &config.NoProxy,
"FTP_PROXY": &config.FTPProxy,
}
m := opts.ConvertKVStringsToMapWithNil(runOpts)
m := runOpts
if m == nil {
m = make(map[string]*string)
}
for k := range permitted {
if *permitted[k] == "" {
continue
Expand Down
18 changes: 11 additions & 7 deletions cli/config/configfile/file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package configfile

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/docker/cli/cli/config/credentials"
"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestProxyConfig(t *testing.T) {
},
}

proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", []string{})
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", nil)
expected := map[string]*string{
"HTTP_PROXY": &httpProxy,
"http_proxy": &httpProxy,
Expand Down Expand Up @@ -75,9 +74,14 @@ func TestProxyConfigOverride(t *testing.T) {
},
}

ropts := []string{
fmt.Sprintf("HTTP_PROXY=%s", overrideHTTPProxy),
"NO_PROXY=",
clone := func(s string) *string {
s2 := s
return &s2
}

ropts := map[string]*string{
"HTTP_PROXY": clone(overrideHTTPProxy),
"NO_PROXY": clone(overrideNoProxy),
}
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
expected := map[string]*string{
Expand Down Expand Up @@ -124,7 +128,7 @@ func TestProxyConfigPerHost(t *testing.T) {
},
}

proxyConfig := cfg.ParseProxyConfig("tcp://example.docker.com:2376", []string{})
proxyConfig := cfg.ParseProxyConfig("tcp://example.docker.com:2376", nil)
expected := map[string]*string{
"HTTP_PROXY": &extHTTPProxy,
"http_proxy": &extHTTPProxy,
Expand Down
2 changes: 1 addition & 1 deletion cli/config/credentials/credentials.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package credentials

import (
"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
)

// Store is the interface that any credentials store must implement.
Expand Down
23 changes: 20 additions & 3 deletions cli/config/credentials/file_store.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package credentials

import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/registry"
"strings"

"github.com/docker/cli/cli/config/types"
)

type store interface {
Expand Down Expand Up @@ -35,7 +36,7 @@ func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) {
// Maybe they have a legacy config file, we will iterate the keys converting
// them to the new format and testing
for r, ac := range c.file.GetAuthConfigs() {
if serverAddress == registry.ConvertToHostname(r) {
if serverAddress == ConvertToHostname(r) {
return ac, nil
}
}
Expand All @@ -62,3 +63,19 @@ func (c *fileStore) GetFilename() string {
func (c *fileStore) IsFileStore() bool {
return true
}

// ConvertToHostname converts a registry url which has http|https prepended
// to just an hostname.
// Copied from github.com/docker/docker/registry.ConvertToHostname to reduce dependencies.
func ConvertToHostname(url string) string {
stripped := url
if strings.HasPrefix(url, "http://") {
stripped = strings.TrimPrefix(url, "http://")
} else if strings.HasPrefix(url, "https://") {
stripped = strings.TrimPrefix(url, "https://")
}

nameParts := strings.SplitN(stripped, "/", 2)

return nameParts[0]
}
2 changes: 1 addition & 1 deletion cli/config/credentials/file_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package credentials
import (
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
Expand Down
2 changes: 1 addition & 1 deletion cli/config/credentials/native_store.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package credentials

import (
"github.com/docker/cli/cli/config/types"
"github.com/docker/docker-credential-helpers/client"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/docker/docker/api/types"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cli/config/credentials/native_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"
"testing"

"github.com/docker/cli/cli/config/types"
"github.com/docker/docker-credential-helpers/client"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
Expand Down
22 changes: 22 additions & 0 deletions cli/config/types/authconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

// AuthConfig contains authorization information for connecting to a Registry
type AuthConfig struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Auth string `json:"auth,omitempty"`

// Email is an optional value associated with the username.
// This field is deprecated and will be removed in a later
// version of docker.
Email string `json:"email,omitempty"`

ServerAddress string `json:"serveraddress,omitempty"`

// IdentityToken is used to authenticate the user and get
// an access token for the registry.
IdentityToken string `json:"identitytoken,omitempty"`

// RegistryToken is a bearer token to be sent to a registry
RegistryToken string `json:"registrytoken,omitempty"`
}
2 changes: 1 addition & 1 deletion internal/test/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import (
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/docker/api/types"
"github.com/docker/cli/cli/config/types"
)

// FakeStore implements a credentials.Store that only acts as an in memory map
Expand Down