-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SNOW-1895884: Token cache refactor #2044
Open
sfc-gh-dheyman
wants to merge
7
commits into
oauth-code-flow
Choose a base branch
from
SNOW-1895884-token-cache-keys
base: oauth-code-flow
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−152
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c9fdff8
SNOW-1895884: Add new token cache keys
sfc-gh-dheyman 1d0b235
Change cache json file structure
sfc-gh-dheyman 197242f
Merge branch 'oauth-code-flow' into SNOW-1895884-token-cache-keys
sfc-gh-dheyman 0e1f28f
Merge branch 'oauth-code-flow' of github.com:snowflakedb/snowflake-jd…
sfc-gh-dheyman 4bee3f1
Merge branch 'SNOW-1895884-token-cache-keys' of github.com:snowflaked…
sfc-gh-dheyman 8b7aa40
refactor convertTarget name
sfc-gh-dheyman 6575f5d
Refactor cache locks
sfc-gh-dheyman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ public class SecureStorageLinuxManager implements SecureStorageManager { | |
private static final String CACHE_FILE_NAME = "temporary_credential.json"; | ||
private static final String CACHE_DIR_PROP = "net.snowflake.jdbc.temporaryCredentialCacheDir"; | ||
private static final String CACHE_DIR_ENV = "SF_TEMPORARY_CREDENTIAL_CACHE_DIR"; | ||
private static final String CACHE_FILE_TOKENS_OBJECT_NAME = "tokens"; | ||
private static final long CACHE_EXPIRATION_IN_SECONDS = 86400L; | ||
private static final long CACHE_FILE_LOCK_EXPIRATION_IN_SECONDS = 60L; | ||
private FileCacheManager fileCacheManager; | ||
|
@@ -43,6 +44,7 @@ private SecureStorageLinuxManager() { | |
.build(); | ||
logger.debug( | ||
"Using temporary file: {} as a token cache storage", fileCacheManager.getCacheFilePath()); | ||
populateLocalCredCache(); | ||
} | ||
|
||
private static class SecureStorageLinuxManagerHolder { | ||
|
@@ -53,62 +55,60 @@ public static SecureStorageLinuxManager getInstance() { | |
return SecureStorageLinuxManagerHolder.INSTANCE; | ||
} | ||
|
||
private ObjectNode localCacheToJson() { | ||
ObjectNode res = mapper.createObjectNode(); | ||
for (Map.Entry<String, Map<String, String>> elem : localCredCache.entrySet()) { | ||
String elemHost = elem.getKey(); | ||
Map<String, String> hostMap = elem.getValue(); | ||
ObjectNode hostNode = mapper.createObjectNode(); | ||
for (Map.Entry<String, String> elem0 : hostMap.entrySet()) { | ||
hostNode.put(elem0.getKey(), elem0.getValue()); | ||
} | ||
res.set(elemHost, hostNode); | ||
} | ||
return res; | ||
} | ||
|
||
public synchronized SecureStorageStatus setCredential( | ||
String host, String user, String type, String token) { | ||
if (Strings.isNullOrEmpty(token)) { | ||
logger.warn("No token provided", false); | ||
return SecureStorageStatus.SUCCESS; | ||
} | ||
|
||
localCredCache.computeIfAbsent(host.toUpperCase(), newMap -> new HashMap<>()); | ||
|
||
Map<String, String> hostMap = localCredCache.get(host.toUpperCase()); | ||
hostMap.put(SecureStorageManager.convertTarget(host, user, type), token); | ||
|
||
localCredCache.computeIfAbsent(CACHE_FILE_TOKENS_OBJECT_NAME, tokensMap -> new HashMap<>()); | ||
Map<String, String> tokensMap = localCredCache.get(CACHE_FILE_TOKENS_OBJECT_NAME); | ||
tokensMap.put(SecureStorageManager.convertTarget(host, user, type), token); | ||
fileCacheManager.writeCacheFile(localCacheToJson()); | ||
return SecureStorageStatus.SUCCESS; | ||
} | ||
|
||
public synchronized String getCredential(String host, String user, String type) { | ||
JsonNode res = fileCacheManager.readCacheFile(); | ||
readJsonStoreCache(res); | ||
|
||
Map<String, String> hostMap = localCredCache.get(host.toUpperCase()); | ||
|
||
if (hostMap == null) { | ||
populateLocalCredCache(); | ||
Map<String, String> tokensMap = localCredCache.get(CACHE_FILE_TOKENS_OBJECT_NAME); | ||
if (tokensMap == null) { | ||
return null; | ||
} | ||
|
||
return hostMap.get(SecureStorageManager.convertTarget(host, user, type)); | ||
return tokensMap.get(SecureStorageManager.convertTarget(host, user, type)); | ||
} | ||
|
||
/** May delete credentials which doesn't belong to this process */ | ||
public synchronized SecureStorageStatus deleteCredential(String host, String user, String type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should read, delete entry and write cache here under one file lock There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored |
||
Map<String, String> hostMap = localCredCache.get(host.toUpperCase()); | ||
if (hostMap != null) { | ||
hostMap.remove(SecureStorageManager.convertTarget(host, user, type)); | ||
if (hostMap.isEmpty()) { | ||
localCredCache.remove(host.toUpperCase()); | ||
Map<String, String> tokensMap = localCredCache.get(CACHE_FILE_TOKENS_OBJECT_NAME); | ||
if (tokensMap != null) { | ||
tokensMap.remove(SecureStorageManager.convertTarget(host, user, type)); | ||
if (tokensMap.isEmpty()) { | ||
localCredCache.remove(CACHE_FILE_TOKENS_OBJECT_NAME); | ||
} | ||
} | ||
fileCacheManager.writeCacheFile(localCacheToJson()); | ||
return SecureStorageStatus.SUCCESS; | ||
} | ||
|
||
private ObjectNode localCacheToJson() { | ||
ObjectNode res = mapper.createObjectNode(); | ||
for (Map.Entry<String, Map<String, String>> elem : localCredCache.entrySet()) { | ||
String elemHost = elem.getKey(); | ||
Map<String, String> hostMap = elem.getValue(); | ||
ObjectNode hostNode = mapper.createObjectNode(); | ||
for (Map.Entry<String, String> elem0 : hostMap.entrySet()) { | ||
hostNode.put(elem0.getKey(), elem0.getValue()); | ||
} | ||
res.set(elemHost, hostNode); | ||
} | ||
return res; | ||
} | ||
|
||
private void populateLocalCredCache() { | ||
JsonNode res = fileCacheManager.readCacheFile(); | ||
readJsonStoreCache(res); | ||
} | ||
|
||
private void readJsonStoreCache(JsonNode m) { | ||
if (m == null || !m.getNodeType().equals(JsonNodeType.OBJECT)) { | ||
logger.debug("Invalid cache file format."); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should read, update and write cache here under one file lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated logic