Skip to content

Commit

Permalink
SNOW-1313544 temporal credential cache map race (#1103)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sfc-gh-dszmolka authored Apr 18, 2024
1 parent c165ed5 commit 088150c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions secure_storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
)

var (
temporaryCredCacheLock sync.RWMutex
credCacheLock sync.RWMutex
)

func createCredentialCacheDir() {
Expand Down Expand Up @@ -175,9 +175,9 @@ func deleteCredential(sc *snowflakeConn, credType string) {
// Reads temporary credential file when OS is Linux.
func readTemporaryCredential(sc *snowflakeConn, credType string) string {
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
temporaryCredCacheLock.RLock()
credCacheLock.Lock()
defer credCacheLock.Unlock()
localCredCache := readTemporaryCacheFile()
temporaryCredCacheLock.RUnlock()
cred := localCredCache[target]
if cred != "" {
logger.Debug("Successfully read token. Returning as string")
Expand All @@ -190,30 +190,32 @@ func readTemporaryCredential(sc *snowflakeConn, credType string) string {
// Writes to temporary credential file when OS is Linux.
func writeTemporaryCredential(sc *snowflakeConn, credType, token string) {
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
credCacheLock.Lock()
defer credCacheLock.Unlock()
localCredCache[target] = token

j, err := json.Marshal(localCredCache)
if err != nil {
logger.Debugf("failed to convert credential to JSON.")
logger.Warnf("failed to convert credential to JSON.")
return
}
temporaryCredCacheLock.Lock()
writeTemporaryCacheFile(j)
temporaryCredCacheLock.Unlock()
}

func deleteTemporaryCredential(sc *snowflakeConn, credType string) {
if credCacheDir == "" {
logger.Debug("Cache file doesn't exist. Skipping deleting credential file.")
} else {
credCacheLock.Lock()
defer credCacheLock.Unlock()
target := convertTarget(sc.cfg.Host, sc.cfg.User, credType)
delete(localCredCache, target)
j, err := json.Marshal(localCredCache)
if err != nil {
logger.Debugf("failed to convert credential to JSON.")
logger.Warnf("failed to convert credential to JSON.")
return
}
temporaryCredCacheLock.Lock()
writeTemporaryCacheFile(j)
temporaryCredCacheLock.Unlock()
}
}

Expand Down

0 comments on commit 088150c

Please sign in to comment.