fix: read docker config file from HOME directory
The path to the docker config file varies depending on location of HOME directory. When an agent was run somewhere where /root is not the HOME directory, this prevented using docker credential stores.
@@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"os"
+ "path"
hclient "github.com/docker/docker-credential-helpers/client"
"github.com/docker/docker/api/types"
@@ -11,7 +12,6 @@ import (
)
const (
- dockerConfigFile = "/root/.docker/config.json"
credentialsHelperPrefix = "docker-credential-"
tokenUsername = "<token>"
)
@@ -21,6 +21,14 @@ type credentialStore struct {
store hclient.ProgramFunc
}
+func getDockerConfigPath() (string, error) {
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ return homeDir, errors.Wrap(err, "unable to find user's HOME directory")
+ }
+ return path.Join(homeDir, ".docker", "config.json"), nil
+}
+
// getAllCredentialStores returns the credential helpers configured in the default docker
// config or an error.
func getAllCredentialStores() (map[string]*credentialStore, error) {
@@ -29,7 +37,12 @@ func getAllCredentialStores() (map[string]*credentialStore, error) {
}
credentialsStores := map[string]*credentialStore{}
- configFile, err := os.Open(dockerConfigFile)
+ dockerConfigFile, err := getDockerConfigPath()
+ if err != nil {
+ return credentialsStores, err
+ }
+
+ configFile, err := os.Open(dockerConfigFile) // #nosec: G304
if err != nil {
return credentialsStores, errors.Wrap(err, "can't open docker config")
}
