1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
// Copyright 2015 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"path"
"reflect"
"strings"
"testing"
)
func TestAuthConfigurationSearchPath(t *testing.T) {
t.Parallel()
testData := []struct {
dockerConfigEnv string
homeEnv string
expectedPaths []string
}{
{"", "", []string{}},
{"", "home", []string{path.Join("home", ".docker", "plaintext-passwords.json"), path.Join("home", ".docker", "config.json"), path.Join("home", ".dockercfg")}},
{"docker_config", "", []string{path.Join("docker_config", "plaintext-passwords.json"), path.Join("docker_config", "config.json")}},
{"a", "b", []string{path.Join("a", "plaintext-passwords.json"), path.Join("a", "config.json")}},
}
for _, tt := range testData {
tt := tt
t.Run(tt.dockerConfigEnv+tt.homeEnv, func(t *testing.T) {
t.Parallel()
paths := cfgPaths(tt.dockerConfigEnv, tt.homeEnv)
if got, want := strings.Join(paths, ","), strings.Join(tt.expectedPaths, ","); got != want {
t.Errorf("cfgPaths: wrong result. Want: %s. Got: %s", want, got)
}
})
}
}
func TestAuthConfigurationsFromFile(t *testing.T) {
t.Parallel()
tmpDir, err := os.MkdirTemp("", "go-dockerclient-auth-test")
if err != nil {
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromFile: %s", err)
}
defer os.RemoveAll(tmpDir)
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
content := fmt.Sprintf(`{"auths":{"foo": {"auth": "%s"}}}`, authString)
configFile := path.Join(tmpDir, "docker_config")
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
}
auths, err := NewAuthConfigurationsFromFile(configFile)
if err != nil {
t.Errorf("Error calling NewAuthConfigurationsFromFile: %s", err)
}
if _, hasKey := auths.Configs["foo"]; !hasKey {
t.Errorf("Returned auths did not include expected auth key foo")
}
}
func TestAuthConfigurationsFromDockerCfg(t *testing.T) {
t.Parallel()
tmpDir, err := os.MkdirTemp("", "go-dockerclient-auth-dockercfg-test")
if err != nil {
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromDockerCfg: %s", err)
}
defer os.RemoveAll(tmpDir)
keys := []string{
"docker.io",
"us.gcr.io",
}
pathsToTry := []string{"some/unknown/path"}
for i, key := range keys {
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
content := fmt.Sprintf(`{"auths":{"%s": {"auth": "%s"}}}`, key, authString)
configFile := path.Join(tmpDir, fmt.Sprintf("docker_config_%d.json", i))
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
}
pathsToTry = append(pathsToTry, configFile)
}
auths, err := newAuthConfigurationsFromDockerCfg(pathsToTry)
if err != nil {
t.Errorf("Error calling NewAuthConfigurationsFromFile: %s", err)
}
for _, key := range keys {
if _, hasKey := auths.Configs[key]; !hasKey {
t.Errorf("Returned auths did not include expected auth key %q", key)
}
}
}
func TestAuthConfigurationsFromDockerCfgError(t *testing.T) {
t.Parallel()
auths, err := newAuthConfigurationsFromDockerCfg([]string{"this/doesnt/exist.json"})
if err == nil {
t.Fatalf("unexpected <nil> error, returned auth config: %#v", auths)
}
}
func TestAuthLegacyConfig(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("user:pa:ss"))
read := strings.NewReader(fmt.Sprintf(`{"docker.io":{"auth":"%s","email":"user@example.com"}}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Error(err)
}
c, ok := ac.Configs["docker.io"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain docker.io")
}
if got, want := c.Email, "user@example.com"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Email: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Username, "user"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Password, "pa:ss"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Password: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.ServerAddress, "docker.io"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].ServerAddress: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthBadConfig(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("userpass"))
read := strings.NewReader(fmt.Sprintf(`{"docker.io":{"auth":"%s","email":"user@example.com"}}`, auth))
ac, err := NewAuthConfigurations(read)
if !errors.Is(err, ErrCannotParseDockercfg) {
t.Errorf("Incorrect error returned %v\n", err)
}
if ac != nil {
t.Errorf("Invalid auth configuration returned, should be nil %v\n", ac)
}
}
func TestAuthMixedWithKeyChain(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
read := strings.NewReader(fmt.Sprintf(`{"auths":{"docker.io":{},"localhost:5000":{"auth":"%s"}},"credsStore":"osxkeychain"}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Fatal(err)
}
c, ok := ac.Configs["localhost:5000"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain localhost:5000")
}
if got, want := c.Username, "user"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Password, "pass"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Password: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.ServerAddress, "localhost:5000"; got != want {
t.Errorf(`AuthConfigurations.Configs["localhost:5000"].ServerAddress: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthAndOtherFields(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
read := strings.NewReader(fmt.Sprintf(`{
"auths":{"docker.io":{"auth":"%s","email":"user@example.com"}},
"detachKeys": "ctrl-e,e",
"HttpHeaders": { "MyHeader": "MyValue" }}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Error(err)
}
c, ok := ac.Configs["docker.io"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain docker.io")
}
if got, want := c.Email, "user@example.com"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Email: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Username, "user"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Password, "pass"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Password: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.ServerAddress, "docker.io"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].ServerAddress: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthConfig(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
read := strings.NewReader(fmt.Sprintf(`{"auths":{"docker.io":{"auth":"%s","email":"user@example.com"}}}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Error(err)
}
c, ok := ac.Configs["docker.io"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain docker.io")
}
if got, want := c.Email, "user@example.com"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Email: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Username, "user"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.Password, "pass"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Password: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.ServerAddress, "docker.io"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].ServerAddress: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthConfigIdentityToken(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("someuser:"))
read := strings.NewReader(fmt.Sprintf(`{"auths":{"docker.io":{"auth":"%s","identitytoken":"sometoken"}}}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Fatal(err)
}
c, ok := ac.Configs["docker.io"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain docker.io")
}
if got, want := c.Username, "someuser"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.IdentityToken, "sometoken"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].IdentityToken: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthConfigRegistryToken(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("someuser:"))
read := strings.NewReader(fmt.Sprintf(`{"auths":{"docker.io":{"auth":"%s","registrytoken":"sometoken"}}}`, auth))
ac, err := NewAuthConfigurations(read)
if err != nil {
t.Fatal(err)
}
c, ok := ac.Configs["docker.io"]
if !ok {
t.Error("NewAuthConfigurations: Expected Configs to contain docker.io")
}
if got, want := c.Username, "someuser"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got)
}
if got, want := c.RegistryToken, "sometoken"; got != want {
t.Errorf(`AuthConfigurations.Configs["docker.io"].RegistryToken: wrong result. Want %q. Got %q`, want, got)
}
}
func TestAuthCheck(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{status: http.StatusOK}
client := newTestClient(fakeRT)
if _, err := client.AuthCheck(nil); err == nil {
t.Fatalf("expected error on nil auth config")
}
// test good auth
if _, err := client.AuthCheck(&AuthConfiguration{}); err != nil {
t.Fatal(err)
}
*fakeRT = FakeRoundTripper{status: http.StatusUnauthorized}
if _, err := client.AuthCheck(&AuthConfiguration{}); err == nil {
t.Fatal("expected failure from unauthorized auth")
}
}
func TestAuthConfigurationsMerge(t *testing.T) {
t.Parallel()
tests := []struct {
name string
left AuthConfigurations
right AuthConfigurations
expected AuthConfigurations
}{
{
name: "empty configs",
expected: AuthConfigurations{},
},
{
name: "empty left config",
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
},
},
},
{
name: "empty right config",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
},
},
},
{
name: "no conflicts",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
},
},
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"us.gcr.io": {Email: "user@google.com"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
"us.gcr.io": {Email: "user@google.com"},
},
},
},
{
name: "no conflicts",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
"us.gcr.io": {Email: "google-user@example.com"},
},
},
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"us.gcr.io": {Email: "user@google.com"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "user@example.com"},
"us.gcr.io": {Email: "google-user@example.com"},
},
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
test.left.merge(test.right)
if !reflect.DeepEqual(test.left, test.expected) {
t.Errorf("wrong configuration map after merge\nwant %#v\ngot %#v", test.expected, test.left)
}
})
}
}
func TestGetHelperProviderFromDockerCfg(t *testing.T) {
t.Parallel()
tmpDir, err := os.MkdirTemp("", "go-dockerclient-creds-test")
if err != nil {
t.Fatalf("Unable to create temporary directory for TestGetHelperProviderFromDockerCfg: %s", err)
}
defer os.RemoveAll(tmpDir)
expectedProvider := "ecr-login-test"
content := fmt.Sprintf(`{"credsStore": "ecr-login","credHelpers":{"docker.io":"%s"}}`, expectedProvider)
configFile := path.Join(tmpDir, "docker_config")
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
t.Errorf("Error writing auth config for TestGetHelperProviderFromDockerCfg: %s", err)
}
configFileNotExists := path.Join(tmpDir, "do_not_exists")
provider, err := getHelperProviderFromDockerCfg([]string{configFileNotExists, configFile}, "docker.io")
if err != nil {
t.Fatal(err)
}
if provider != expectedProvider {
t.Errorf("wrong provider found: \nwant %s\ngot %s", expectedProvider, provider)
}
}
func TestParseCredsDockerConfig(t *testing.T) {
t.Parallel()
tests := []struct {
config []byte
provider string
registry string
}{
{
config: []byte(`{"credsStore": "ecr-login"}`),
provider: "ecr-login",
registry: "docker.io",
},
{
config: []byte(`{"credsStore": "ecr-login","credHelpers":{"docker.io":"ecr-login-test"}}`),
provider: "ecr-login-test",
registry: "docker.io",
},
{
config: []byte(`{"credsStore": "ecr-login","credHelpers":{"docker.io":"ecr-login-test"}}`),
provider: "ecr-login",
registry: "docker.io2",
},
}
for _, test := range tests {
provider, err := parseCredsDockerConfig(test.config, test.registry)
if err != nil {
t.Fatal(err)
}
if provider != test.provider {
t.Errorf("wrong provider found: \nwant %s\ngot %s", test.provider, provider)
}
}
}
|