Skip to content

Pass a context to DefaultAuthHelper.GetAllConfigs() #4760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2020
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
8 changes: 6 additions & 2 deletions pkg/skaffold/build/cache/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ func (b *mockBuilder) BuildAndTest(ctx context.Context, out io.Writer, tags tag.

type stubAuth struct{}

func (t stubAuth) GetAuthConfig(string) (types.AuthConfig, error) { return types.AuthConfig{}, nil }
func (t stubAuth) GetAllAuthConfigs() (map[string]types.AuthConfig, error) { return nil, nil }
func (t stubAuth) GetAuthConfig(string) (types.AuthConfig, error) {
return types.AuthConfig{}, nil
}
func (t stubAuth) GetAllAuthConfigs(context.Context) (map[string]types.AuthConfig, error) {
return nil, nil
}

func TestCacheBuildLocal(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ type testAuthHelper struct{}
func (t testAuthHelper) GetAuthConfig(string) (types.AuthConfig, error) {
return types.AuthConfig{}, nil
}
func (t testAuthHelper) GetAllAuthConfigs() (map[string]types.AuthConfig, error) { return nil, nil }
func (t testAuthHelper) GetAllAuthConfigs(context.Context) (map[string]types.AuthConfig, error) {
return nil, nil
}

func TestLocalRun(t *testing.T) {
tests := []struct {
Expand Down
29 changes: 26 additions & 3 deletions pkg/skaffold/docker/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func init() {
// Ideally this shouldn't be public, but the LocalBuilder needs to use it.
type AuthConfigHelper interface {
GetAuthConfig(registry string) (types.AuthConfig, error)
GetAllAuthConfigs() (map[string]types.AuthConfig, error)
GetAllAuthConfigs(ctx context.Context) (map[string]types.AuthConfig, error)
}

type credsHelper struct{}
Expand All @@ -73,7 +73,7 @@ func loadDockerConfig() (*configfile.ConfigFile, error) {
return cf, nil
}

func (credsHelper) GetAuthConfig(registry string) (types.AuthConfig, error) {
func (h credsHelper) GetAuthConfig(registry string) (types.AuthConfig, error) {
cf, err := loadDockerConfig()
if err != nil {
return types.AuthConfig{}, err
Expand All @@ -87,7 +87,30 @@ func (credsHelper) GetAuthConfig(registry string) (types.AuthConfig, error) {
return types.AuthConfig(auth), nil
}

func (credsHelper) GetAllAuthConfigs() (map[string]types.AuthConfig, error) {
// GetAllAuthConfigs retrieves all the auth configs.
// Because this can take a long time, we make sure it can be interrupted by the user.
func (h credsHelper) GetAllAuthConfigs(ctx context.Context) (map[string]types.AuthConfig, error) {
type result struct {
configs map[string]types.AuthConfig
err error
}

auth := make(chan result)

go func() {
configs, err := h.doGetAllAuthConfigs()
auth <- result{configs, err}
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case r := <-auth:
return r.configs, r.err
}
}

func (h credsHelper) doGetAllAuthConfigs() (map[string]types.AuthConfig, error) {
cf, err := loadDockerConfig()
if err != nil {
return nil, err
Expand Down
46 changes: 44 additions & 2 deletions pkg/skaffold/docker/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package docker
import (
"context"
"fmt"
"runtime"
"testing"

"github.com/docker/docker/api/types"
Expand All @@ -44,11 +45,52 @@ var allAuthConfig = map[string]types.AuthConfig{
func (t testAuthHelper) GetAuthConfig(string) (types.AuthConfig, error) {
return gcrAuthConfig, t.getAuthConfigErr
}

func (t testAuthHelper) GetAllAuthConfigs() (map[string]types.AuthConfig, error) {
func (t testAuthHelper) GetAllAuthConfigs(context.Context) (map[string]types.AuthConfig, error) {
return allAuthConfig, t.getAllAuthConfigsErr
}

func TestGetAllAuthConfigs(t *testing.T) {
testutil.Run(t, "auto-configure gcr.io", func(t *testutil.T) {
if runtime.GOOS == "windows" {
t.Skip("test doesn't work on windows")
}

tmpDir := t.NewTempDir().
Write("config.json", `{"credHelpers":{"my.registry":"helper"}}`).
Write("docker-credential-gcloud", `#!/bin/sh
read server
echo "{\"Username\":\"<token>\",\"Secret\":\"TOKEN_$server\"}"`).
Write("docker-credential-helper", `#!/bin/sh
read server
echo "{\"Username\":\"<token>\",\"Secret\":\"TOKEN_$server\"}"`)
t.Override(&configDir, tmpDir.Root())
t.SetEnvs(map[string]string{"PATH": tmpDir.Root()})

auth, err := DefaultAuthHelper.GetAllAuthConfigs(context.Background())

t.CheckNoError(err)
t.CheckDeepEqual(map[string]types.AuthConfig{
"asia.gcr.io": {IdentityToken: "TOKEN_asia.gcr.io"},
"eu.gcr.io": {IdentityToken: "TOKEN_eu.gcr.io"},
"gcr.io": {IdentityToken: "TOKEN_gcr.io"},
"my.registry": {IdentityToken: "TOKEN_my.registry"},
"marketplace.gcr.io": {IdentityToken: "TOKEN_marketplace.gcr.io"},
"staging-k8s.gcr.io": {IdentityToken: "TOKEN_staging-k8s.gcr.io"},
"us.gcr.io": {IdentityToken: "TOKEN_us.gcr.io"},
}, auth)
})

testutil.Run(t, "invalid config.json", func(t *testutil.T) {
tmpDir := t.NewTempDir().Write("config.json", "invalid json")
t.Override(&configDir, tmpDir.Root())

auth, err := DefaultAuthHelper.GetAllAuthConfigs(context.Background())

t.CheckError(true, err)
t.CheckEmpty(auth)
})
}

func TestGetEncodedRegistryAuth(t *testing.T) {
tests := []struct {
description string
Expand Down
23 changes: 3 additions & 20 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,6 @@ func (l *localDaemon) ConfigFile(ctx context.Context, image string) (*v1.ConfigF
return cfg, nil
}

func authConfig(ctx context.Context) map[string]types.AuthConfig {
auth := make(chan map[string]types.AuthConfig)

go func() {
// Like `docker build`, we ignore the errors
// See https://github.com/docker/cli/blob/75c1bb1f33d7cedbaf48404597d5bf9818199480/cli/command/image/build.go#L364
authConfigs, _ := DefaultAuthHelper.GetAllAuthConfigs()
auth <- authConfigs
}()

// Because this can take a long time, we make sure it can be interrupted by the user.
select {
case <-ctx.Done():
return nil
case r := <-auth:
return r
}
}

// Build performs a docker build and returns the imageID.
func (l *localDaemon) Build(ctx context.Context, out io.Writer, workspace string, a *latest.DockerArtifact, ref string, mode config.RunMode) (string, error) {
logrus.Debugf("Running docker build: context: %s, dockerfile: %s", workspace, a.DockerfilePath)
Expand All @@ -184,7 +165,9 @@ func (l *localDaemon) Build(ctx context.Context, out io.Writer, workspace string
return "", fmt.Errorf("unable to evaluate build args: %w", err)
}

authConfigs := authConfig(ctx)
// Like `docker build`, we ignore the errors
// See https://github.com/docker/cli/blob/75c1bb1f33d7cedbaf48404597d5bf9818199480/cli/command/image/build.go#L364
authConfigs, _ := DefaultAuthHelper.GetAllAuthConfigs(ctx)

buildCtx, buildCtxWriter := io.Pipe()
go func() {
Expand Down