Skip to content

Commit

Permalink
test credential-helper configs
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Wang <[email protected]>
  • Loading branch information
QiWang19 committed Jul 10, 2020
1 parent 49b1e38 commit 903a079
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
46 changes: 45 additions & 1 deletion pkg/docker/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ func TestGetAuth(t *testing.T) {
os.Setenv("XDG_RUNTIME_DIR", origXDG)
}()

// override PATH for executing credHelper
path, err := os.Getwd()
require.NoError(t, err)
origPath := os.Getenv("PATH")
newPath := fmt.Sprintf("%s:%s", filepath.Join(path, "testdata"), origPath)
os.Setenv("PATH", newPath)
t.Logf("using PATH: %q", newPath)
defer func() {
os.Setenv("PATH", origPath)
}()
err = os.Chmod(filepath.Join(path, "testdata", "docker-credential-helper-registry"), os.ModePerm)
require.NoError(t, err)

origHomeDir := homedir.Get()
tmpDir2, err := ioutil.TempDir("", "test_docker_client_get_auth")
if err != nil {
Expand Down Expand Up @@ -224,6 +237,17 @@ func TestGetAuth(t *testing.T) {
hostname: "https://localhost:5000",
path: filepath.Join("testdata", "empty.json"),
},
{
name: "credhelper from registries.conf",
hostname: "registry-a.com",
sys: &types.SystemContext{
SystemRegistriesConfPath: filepath.Join("testdata", "cred-helper.conf"),
},
expected: types.DockerAuthConfig{
Username: "foo",
Password: "bar",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
if err := os.RemoveAll(configPath); err != nil {
Expand Down Expand Up @@ -470,7 +494,22 @@ func TestGetAllCredentials(t *testing.T) {
err = tmpFile.Close()
require.NoError(t, err)
authFilePath := tmpFile.Name()
sys := types.SystemContext{AuthFilePath: authFilePath}
// override PATH for executing credHelper
path, err := os.Getwd()
require.NoError(t, err)
origPath := os.Getenv("PATH")
newPath := fmt.Sprintf("%s:%s", filepath.Join(path, "testdata"), origPath)
os.Setenv("PATH", newPath)
t.Logf("using PATH: %q", newPath)
defer func() {
os.Setenv("PATH", origPath)
}()
err = os.Chmod(filepath.Join(path, "testdata", "docker-credential-helper-registry"), os.ModePerm)
require.NoError(t, err)
sys := types.SystemContext{
AuthFilePath: authFilePath,
SystemRegistriesConfPath: filepath.Join("testdata", "cred-helper.conf"),
}

data := []struct {
server string
Expand All @@ -492,6 +531,11 @@ func TestGetAllCredentials(t *testing.T) {
username: "local-user",
password: "local-password",
},
{
server: "registry-a.com",
username: "foo",
password: "bar",
},
}

// Write the credentials to the authfile.
Expand Down
3 changes: 3 additions & 0 deletions pkg/docker/config/testdata/cred-helper.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[registry]]
credential-helper = "helper-registry"
location = "registry-a.com"
15 changes: 15 additions & 0 deletions pkg/docker/config/testdata/docker-credential-helper-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

ACTION="${1}"
shift

case "${ACTION}" in
get)
read REGISTRY
echo "{\"ServerURL\":\"${REGISTRY}\",\"Username\":\"foo\",\"Secret\":\"bar\"}"
exit 0
;;
*)
echo "not implemented"
exit 1
;;
31 changes: 31 additions & 0 deletions pkg/sysregistriesv2/system_registries_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,34 @@ func TestRegistriesConfDirectory(t *testing.T) {
require.NoError(t, err)
assert.True(t, reg.Blocked)
}

func TestCredentialHelpersForRegistry(t *testing.T) {
ctx := &types.SystemContext{
SystemRegistriesConfPath: "testdata/cred-helper.conf",
SystemRegistriesConfDirPath: "testdata/registries.conf.d",
}
for _, c := range []struct {
registry string
helper []string
}{
{"registry-a.com", []string{"helper-a"}},
{"registry-b.com", []string{"helper-b", "helper-c"}},
{"registry-c.com/foo", []string{"helper-b", "helper-c"}},
{"registry-not-conf.com", []string{"helper-b", "helper-c"}},
} {
ret, err := CredentialHelpersForRegistry(ctx, c.registry)
require.NoError(t, err)
assert.ElementsMatch(t, c.helper, ret)
}
}

func TestAllConfiguredCredentialHelpers(t *testing.T) {
ctx := &types.SystemContext{
SystemRegistriesConfPath: "testdata/cred-helper.conf",
SystemRegistriesConfDirPath: "testdata/registries.conf.d",
}
expect := []string{"helper-b", "helper-c", "helper-a"}
ret, err := AllConfiguredCredentialHelpers(ctx)
require.NoError(t, err)
assert.ElementsMatch(t, expect, ret)
}
11 changes: 11 additions & 0 deletions pkg/sysregistriesv2/testdata/cred-helper.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
credential-helpers = ["helper-b", "helper-c"]
[[registry]]
credential-helper = "helper-a"
location = "registry-a.com"

[[registry]]
location = "registry-b.com"

[[registry]]
credential-helper = "helper-"
location = "registry-c.com/foo"

0 comments on commit 903a079

Please sign in to comment.