-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli/config/credentials: refactor DetectDefaultStore and add tests
Refactor the DetectDefaultStore to allow testing it cross-platform, and without the actual helpers installed. This also makes a small change in the logic for detecting the preferred helper. Previously, it only detected the "helper" binary ("pass"), but would fall back to using plain-text if the pass credentials-helper was not installed. With this patch, it falls back to the platform default (secretservice), before falling back to using no credentials helper (and storing unencrypted). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- Loading branch information
Showing
6 changed files
with
147 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package credentials | ||
|
||
func defaultCredentialsStore() string { | ||
return "osxkeychain" | ||
} | ||
const ( | ||
preferredHelper = "" | ||
defaultHelper = "osxkeychain" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
package credentials | ||
|
||
import ( | ||
"os/exec" | ||
const ( | ||
preferredHelper = "pass" | ||
defaultHelper = "secretservice" | ||
) | ||
|
||
func defaultCredentialsStore() string { | ||
if _, err := exec.LookPath("pass"); err == nil { | ||
return "pass" | ||
} | ||
|
||
return "secretservice" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package credentials | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestDetectDefaultStore(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
t.Setenv("PATH", tmpDir) | ||
|
||
t.Run("none available", func(t *testing.T) { | ||
const expected = "" | ||
assert.Equal(t, expected, DetectDefaultStore("")) | ||
}) | ||
t.Run("custom helper", func(t *testing.T) { | ||
const expected = "my-custom-helper" | ||
assert.Equal(t, expected, DetectDefaultStore(expected)) | ||
|
||
// Custom helper should be used even if the actual helper exists | ||
createFakeHelper(t, path.Join(tmpDir, remoteCredentialsPrefix+defaultHelper)) | ||
assert.Equal(t, expected, DetectDefaultStore(expected)) | ||
}) | ||
t.Run("default", func(t *testing.T) { | ||
createFakeHelper(t, path.Join(tmpDir, remoteCredentialsPrefix+defaultHelper)) | ||
expected := defaultHelper | ||
assert.Equal(t, expected, DetectDefaultStore("")) | ||
}) | ||
|
||
// On Linux, the "pass" credentials helper requires both a "pass" binary | ||
// to be present and a "docker-credentials-pass" credentials helper to | ||
// be installed. | ||
t.Run("preferred helper", func(t *testing.T) { | ||
// Create the default helper as we need it for the fallback. | ||
createFakeHelper(t, path.Join(tmpDir, remoteCredentialsPrefix+defaultHelper)) | ||
|
||
const testPreferredHelper = "preferred" | ||
overridePreferred = testPreferredHelper | ||
|
||
// Use preferred helper if both binaries exist. | ||
t.Run("success", func(t *testing.T) { | ||
createFakeHelper(t, path.Join(tmpDir, testPreferredHelper)) | ||
createFakeHelper(t, path.Join(tmpDir, remoteCredentialsPrefix+testPreferredHelper)) | ||
expected := testPreferredHelper | ||
assert.Equal(t, expected, DetectDefaultStore("")) | ||
}) | ||
|
||
// Fall back to the default helper if the preferred credentials-helper isn't installed. | ||
t.Run("not installed", func(t *testing.T) { | ||
createFakeHelper(t, path.Join(tmpDir, remoteCredentialsPrefix+testPreferredHelper)) | ||
expected := defaultHelper | ||
assert.Equal(t, expected, DetectDefaultStore("")) | ||
}) | ||
|
||
// Similarly, fall back to the default helper if the preferred credentials-helper | ||
// is installed, but the helper binary isn't found. | ||
t.Run("missing helper", func(t *testing.T) { | ||
createFakeHelper(t, path.Join(tmpDir, testPreferredHelper)) | ||
expected := defaultHelper | ||
assert.Equal(t, expected, DetectDefaultStore("")) | ||
}) | ||
}) | ||
} | ||
|
||
func createFakeHelper(t *testing.T, fileName string) { | ||
t.Helper() | ||
assert.NilError(t, os.WriteFile(fileName, []byte("I'm a credentials-helper executable (really!)"), 0o700)) | ||
t.Cleanup(func() { | ||
assert.NilError(t, os.RemoveAll(fileName)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package credentials | ||
|
||
func defaultCredentialsStore() string { | ||
return "wincred" | ||
} | ||
const ( | ||
preferredHelper = "" | ||
defaultHelper = "wincred" | ||
) |