Skip to content

Commit

Permalink
Auto configure authentication helper for gcr.io
Browse files Browse the repository at this point in the history
Fixes #966

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Sep 17, 2018
1 parent 3f96779 commit 0539f2f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/skaffold/docker/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/gcp"
"github.com/docker/cli/cli/config"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -64,6 +65,9 @@ func (credsHelper) GetAuthConfig(registry string) (types.AuthConfig, error) {
if err != nil {
return types.AuthConfig{}, errors.Wrap(err, "docker config")
}

gcp.AutoConfigureGCRCredentialHelper(cf, registry)

return cf.GetAuthConfig(registry)
}

Expand Down
49 changes: 49 additions & 0 deletions pkg/skaffold/gcp/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2018 The Skaffold 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 gcp

import (
"os/exec"
"strings"

"github.com/docker/cli/cli/config/configfile"
"github.com/sirupsen/logrus"
)

// AutoConfigureGCRCredentialHelper automatically adds the `gcloud` credential helper
// to docker's configuration.
// This doesn't modify the ~/.docker/config.json. It's only in-memory
func AutoConfigureGCRCredentialHelper(cf *configfile.ConfigFile, registry string) {
if registry != "gcr.io" && !strings.HasSuffix(registry, ".gcr.io") {
return
}

if cf.CredentialHelpers != nil && cf.CredentialHelpers[registry] != "" {
return
}

if path, _ := exec.LookPath("docker-credential-gcloud"); path == "" {
return
}

logrus.Debugf("Adding `gcloud` as a docker credential helper for [%s]", registry)

if cf.CredentialHelpers == nil {
cf.CredentialHelpers = make(map[string]string)
}
cf.CredentialHelpers[registry] = "gcloud"
}
117 changes: 117 additions & 0 deletions pkg/skaffold/gcp/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2018 The Skaffold 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 gcp

import (
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/docker/cli/cli/config/configfile"
)

func TestAutoConfigureGCRCredentialHelper(t *testing.T) {
var tests = []struct {
description string
registry string
helperInPath bool
config *configfile.ConfigFile
expected *configfile.ConfigFile
}{
{
description: "add to nil map",
registry: "gcr.io",
helperInPath: true,
config: &configfile.ConfigFile{},
expected: &configfile.ConfigFile{
CredentialHelpers: map[string]string{
"gcr.io": "gcloud",
},
},
},
{
description: "add to empty map",
registry: "gcr.io",
helperInPath: true,
config: &configfile.ConfigFile{
CredentialHelpers: map[string]string{},
},
expected: &configfile.ConfigFile{
CredentialHelpers: map[string]string{
"gcr.io": "gcloud",
},
},
},
{
description: "leave existing helper",
registry: "gcr.io",
config: &configfile.ConfigFile{
CredentialHelpers: map[string]string{
"gcr.io": "existing",
},
},
expected: &configfile.ConfigFile{
CredentialHelpers: map[string]string{
"gcr.io": "existing",
},
},
},
{
description: "any.gcr.io",
registry: "any.gcr.io",
helperInPath: true,
config: &configfile.ConfigFile{},
expected: &configfile.ConfigFile{
CredentialHelpers: map[string]string{
"any.gcr.io": "gcloud",
},
},
},
{
description: "case is important",
registry: "GCR.io",
helperInPath: true,
config: &configfile.ConfigFile{},
expected: &configfile.ConfigFile{},
},
{
description: "ignore if gcloud is not in PATH",
registry: "gcr.io",
helperInPath: false,
config: &configfile.ConfigFile{},
expected: &configfile.ConfigFile{},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
tmp, cleanup := testutil.NewTempDir(t)
defer cleanup()

reset := testutil.SetEnvs(t, map[string]string{
"PATH": tmp.Root(),
})
defer reset(t)

if test.helperInPath {
tmp.Write("docker-credential-gcloud", "")
}

AutoConfigureGCRCredentialHelper(test.config, test.registry)

testutil.CheckDeepEqual(t, test.expected, test.config)
})
}
}

0 comments on commit 0539f2f

Please sign in to comment.