Skip to content

Commit 088150c

Browse files
SNOW-1313544 temporal credential cache map race (#1103)
* SNOW-1313544 Parallel operations on different roles cause program to crash (race condition on temporal credential cache) * moving locks a bit ahead (delete on map happened before lock) * write-lock instead of read-lock when reading the credentials
1 parent c165ed5 commit 088150c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

secure_storage_manager.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
)
2828

2929
var (
30-
temporaryCredCacheLock sync.RWMutex
30+
credCacheLock sync.RWMutex
3131
)
3232

3333
func createCredentialCacheDir() {
@@ -175,9 +175,9 @@ func deleteCredential(sc *snowflakeConn, credType string) {
175175
// Reads temporary credential file when OS is Linux.
176176
func readTemporaryCredential(sc *snowflakeConn, credType string) string {
177177
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
178-
temporaryCredCacheLock.RLock()
178+
credCacheLock.Lock()
179+
defer credCacheLock.Unlock()
179180
localCredCache := readTemporaryCacheFile()
180-
temporaryCredCacheLock.RUnlock()
181181
cred := localCredCache[target]
182182
if cred != "" {
183183
logger.Debug("Successfully read token. Returning as string")
@@ -190,30 +190,32 @@ func readTemporaryCredential(sc *snowflakeConn, credType string) string {
190190
// Writes to temporary credential file when OS is Linux.
191191
func writeTemporaryCredential(sc *snowflakeConn, credType, token string) {
192192
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
193+
credCacheLock.Lock()
194+
defer credCacheLock.Unlock()
193195
localCredCache[target] = token
194196

195197
j, err := json.Marshal(localCredCache)
196198
if err != nil {
197-
logger.Debugf("failed to convert credential to JSON.")
199+
logger.Warnf("failed to convert credential to JSON.")
200+
return
198201
}
199-
temporaryCredCacheLock.Lock()
200202
writeTemporaryCacheFile(j)
201-
temporaryCredCacheLock.Unlock()
202203
}
203204

204205
func deleteTemporaryCredential(sc *snowflakeConn, credType string) {
205206
if credCacheDir == "" {
206207
logger.Debug("Cache file doesn't exist. Skipping deleting credential file.")
207208
} else {
209+
credCacheLock.Lock()
210+
defer credCacheLock.Unlock()
208211
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
209212
delete(localCredCache, target)
210213
j, err := json.Marshal(localCredCache)
211214
if err != nil {
212-
logger.Debugf("failed to convert credential to JSON.")
215+
logger.Warnf("failed to convert credential to JSON.")
216+
return
213217
}
214-
temporaryCredCacheLock.Lock()
215218
writeTemporaryCacheFile(j)
216-
temporaryCredCacheLock.Unlock()
217219
}
218220
}
219221

0 commit comments

Comments
 (0)