Skip to content

Commit

Permalink
client/driver: use correct repo address when using docker-credential …
Browse files Browse the repository at this point in the history
…helper (#4266)
  • Loading branch information
nickethier committed May 15, 2018
2 parents 126e049 + 6e86c4e commit 504a910
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ BUG FIXES:
* api/client: Fix potentially out of order logs and streamed file contents
[[GH-4234](https://github.com/hashicorp/nomad/issues/4234)]

BUG FIXES:
* driver/docker: Fix docker credential helper support [[GH-4266](https://github.com/hashicorp/nomad/issues/4266)]

## 0.8.3 (April 27, 2018)

BUG FIXES:
Expand Down
10 changes: 6 additions & 4 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,13 +2130,15 @@ func authFromHelper(helperName string) authBackend {
helper := dockerAuthHelperPrefix + helperName
cmd := exec.Command(helper, "get")

// Ensure that the HTTPs prefix exists
if !strings.HasPrefix(repo, "https://") {
repo = fmt.Sprintf("https://%s", repo)
repoParsed, err := reference.ParseNamed(repo)
if err != nil {
return nil, err
}

cmd.Stdin = strings.NewReader(repo)
// Ensure that the HTTPs prefix exists
repoAddr := fmt.Sprintf("https://%s", repoParsed.Hostname())

cmd.Stdin = strings.NewReader(repoAddr)
output, err := cmd.Output()
if err != nil {
switch err.(type) {
Expand Down
41 changes: 41 additions & 0 deletions client/driver/docker_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package driver

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

"github.com/stretchr/testify/require"
)

func TestDockerDriver_authFromHelper(t *testing.T) {
dir, err := ioutil.TempDir("", "test-docker-driver_authfromhelper")
require.NoError(t, err)
defer os.RemoveAll(dir)
helperPayload := "{\"Username\":\"hashi\",\"Secret\":\"nomad\"}"
helperContent := []byte(fmt.Sprintf("#!/bin/sh\ncat > %s/helper-$1.out;echo '%s'", dir, helperPayload))

helperFile := filepath.Join(dir, "docker-credential-testnomad")
err = ioutil.WriteFile(helperFile, helperContent, 0777)
require.NoError(t, err)

path := os.Getenv("PATH")
os.Setenv("PATH", fmt.Sprintf("%s:%s", path, dir))
defer os.Setenv("PATH", path)

helper := authFromHelper("testnomad")
creds, err := helper("registry.local:5000/repo/image")
require.NoError(t, err)
require.NotNil(t, creds)
require.Equal(t, "hashi", creds.Username)
require.Equal(t, "nomad", creds.Password)

if _, err := os.Stat(filepath.Join(dir, "helper-get.out")); os.IsNotExist(err) {
t.Fatalf("Expected helper-get.out to exist")
}
content, err := ioutil.ReadFile(filepath.Join(dir, "helper-get.out"))
require.NoError(t, err)
require.Equal(t, []byte("https://registry.local:5000"), content)
}

0 comments on commit 504a910

Please sign in to comment.