@@ -109,18 +109,68 @@ func (ssm *fileBasedSecureStorageManager) createCacheDir(credCacheDir string) er
109
109
return err
110
110
}
111
111
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
+
112
153
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
116
157
}
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" }},
121
162
}
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 ""
124
174
}
125
175
126
176
func (ssm * fileBasedSecureStorageManager ) setCredential (tokenSpec * secureTokenSpec , value string ) {
@@ -345,7 +395,7 @@ func buildCredentialsKey(host, user string, credType tokenType) string {
345
395
host = strings .ToUpper (host )
346
396
user = strings .ToUpper (user )
347
397
credTypeStr := strings .ToUpper (string (credType ))
348
- return host + ":" + user + ":" + driverName + ":" + credTypeStr
398
+ return host + ":" + user + ":" + credTypeStr
349
399
}
350
400
351
401
type noopSecureStorageManager struct {
0 commit comments