Skip to content

Commit 3bd31c0

Browse files
Update cache dir lookup
1 parent d8df82e commit 3bd31c0

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

secure_storage_manager.go

+60-10
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,68 @@ func (ssm *fileBasedSecureStorageManager) createCacheDir(credCacheDir string) er
109109
return err
110110
}
111111

112+
func lookupCacheDir(envVar string, pathSegments ...string) (string, error) {
113+
envVal := os.Getenv(envVar)
114+
if envVal == "" {
115+
return "", fmt.Errorf("Environment variable %s not set", envVar)
116+
}
117+
118+
fileInfo, err := os.Stat(envVal)
119+
if err != nil {
120+
return "", fmt.Errorf("Failed to stat %s=%s, due to %w", envVar, envVal, err)
121+
}
122+
123+
if !fileInfo.IsDir() {
124+
return "", fmt.Errorf("Environment variable %s=%s is not a directory", envVar, envVal)
125+
}
126+
127+
cacheDir := envVal
128+
129+
if len(pathSegments) > 0 {
130+
for _, pathSegment := range pathSegments {
131+
err := os.Mkdir(pathSegment, os.ModePerm)
132+
if err != nil {
133+
return "", fmt.Errorf("Failed to create cache directory. %v, err: %w", pathSegment, err)
134+
}
135+
cacheDir = filepath.Join(cacheDir, pathSegment)
136+
}
137+
fileInfo, err = os.Stat(cacheDir)
138+
if err != nil {
139+
return "", fmt.Errorf("Failed to stat %s=%s, due to %w", envVar, cacheDir, err)
140+
}
141+
}
142+
143+
if fileInfo.Mode()&os.ModePerm != 0o700 {
144+
err := os.Chmod(cacheDir, 0o700)
145+
if err != nil {
146+
return "", fmt.Errorf("Failed to chmod cache directory. %v, err: %w", cacheDir, err)
147+
}
148+
}
149+
150+
return cacheDir, nil
151+
}
152+
112153
func (ssm *fileBasedSecureStorageManager) buildCredCacheDirPath() string {
113-
credCacheDir := os.Getenv(credCacheDirEnv)
114-
if credCacheDir != "" {
115-
return credCacheDir
154+
type cacheDirConf struct {
155+
envVar string
156+
pathSegments []string
116157
}
117-
home := os.Getenv("HOME")
118-
if home == "" {
119-
logger.Info("HOME is blank")
120-
return ""
158+
confs := []cacheDirConf{
159+
{envVar: credCacheDirEnv, pathSegments: []string{}},
160+
{envVar: "XDG_CACHE_DIR", pathSegments: []string{"snowflake"}},
161+
{envVar: "HOME", pathSegments: []string{".cache", "snowflake"}},
121162
}
122-
credCacheDir = filepath.Join(home, ".cache", "snowflake")
123-
return credCacheDir
163+
for _, conf := range confs {
164+
path, err := lookupCacheDir(conf.envVar, conf.pathSegments...)
165+
if err != nil {
166+
logger.Debugf("Skipping %s in cache directory lookup due to %w", conf.envVar, err)
167+
} else {
168+
logger.Infof("Using %s as cache directory", path)
169+
return path
170+
}
171+
}
172+
173+
return ""
124174
}
125175

126176
func (ssm *fileBasedSecureStorageManager) setCredential(tokenSpec *secureTokenSpec, value string) {
@@ -345,7 +395,7 @@ func buildCredentialsKey(host, user string, credType tokenType) string {
345395
host = strings.ToUpper(host)
346396
user = strings.ToUpper(user)
347397
credTypeStr := strings.ToUpper(string(credType))
348-
return host + ":" + user + ":" + driverName + ":" + credTypeStr
398+
return host + ":" + user + ":" + credTypeStr
349399
}
350400

351401
type noopSecureStorageManager struct {

0 commit comments

Comments
 (0)