Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Fix credential search to work properly for file paths and .yml extens…
Browse files Browse the repository at this point in the history
…ions (#802)

Fixes #718
  • Loading branch information
benpatt authored and jeremyrickard committed Jul 23, 2019
1 parent 63d32c2 commit f8f1196
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
27 changes: 20 additions & 7 deletions cmd/duffle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ func loadCredentials(files []string, b *bundle.Bundle) (map[string]string, error
return creds, credentials.Validate(creds, b.Credentials)
}

credDir := home.Home(homePath()).Credentials() // Credentials directory should exist from duffle init

// The strategy here is "last one wins". We loop through each credential file and
// calculate its credentials. Then we insert them into the creds map in the order
// in which they were supplied on the CLI.
for _, file := range files {
if !isPathy(file) {
file = filepath.Join(home.Home(homePath()).Credentials(), file+".yaml")
}
cset, err := credentials.Load(file)
cset, err := credentials.Load(findCreds(credDir, file))
if err != nil {
return creds, err
}
Expand All @@ -86,9 +85,23 @@ func loadCredentials(files []string, b *bundle.Bundle) (map[string]string, error
return creds, credentials.Validate(creds, b.Credentials)
}

// isPathy checks to see if a name looks like a path.
func isPathy(name string) bool {
return strings.Contains(name, string(filepath.Separator))
func findCreds(credDir string, file string) string {
if !fileExists(file) {
testPath := filepath.Join(credDir, file+".yaml")
if fileExists(testPath) {
file = testPath
} else {
file = filepath.Join(credDir, file+".yml") // Don't bother checking existence because it fails later
}
}
return file
}

func fileExists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}

func must(err error) {
Expand Down
62 changes: 48 additions & 14 deletions cmd/duffle/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ func CreateTestHome(t *testing.T) home.Home {
return testHome
}

func TestIsPathy(t *testing.T) {
is := assert.New(t)
thispath := filepath.Join("this", "is", "a", "path")
fooya := filepath.Join("..", "foo.yaml")
for path, expect := range map[string]bool{
"foo": false,
thispath: true,
"foo.yaml": false,
fooya: true,
} {
is.Equal(expect, isPathy(path), "Expected %t, for %s", expect, path)
}
}

func TestLoadCredentials(t *testing.T) {
cred1 := credentials.CredentialSet{
Name: "first",
Expand Down Expand Up @@ -128,3 +114,51 @@ func TestLoadCredentials(t *testing.T) {
is.Equal("cred3", creds["haversack"])
is.Equal("cred1", creds["gym-bag"])
}

func TestFindCreds(t *testing.T) {
credDir, err := ioutil.TempDir("", "credTest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(credDir)
is := assert.New(t)

tests := map[string]struct {
input string
expectedFilePath func() string
}{
"no path yaml": {
input: "creds1",
expectedFilePath: func() string {
credPath := filepath.Join(credDir, "creds1.yaml")
err = ioutil.WriteFile(credPath, []byte("test"), 0644)
if err != nil {
t.Fatal(err)
}
return credPath
},
},
"no path yml": {
input: "creds2",
expectedFilePath: func() string {
credPath := filepath.Join(credDir, "creds2.yml")
err = ioutil.WriteFile(credPath, []byte("test"), 0644)
if err != nil {
t.Fatal(err)
}
return credPath
},
},
"path": {
input: "testdata/dufflehome/credentials/testing.yaml",
expectedFilePath: func() string {
return "testdata/dufflehome/credentials/testing.yaml"
},
},
}

for name, testCase := range tests {
is.Equal(testCase.expectedFilePath(), findCreds(credDir, testCase.input), "Fail on test: "+name)
os.RemoveAll(filepath.Join(credDir, "*"))
}
}

0 comments on commit f8f1196

Please sign in to comment.