Skip to content

Commit

Permalink
SNOW-1167983: Logs - Token caching (#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pbulawa authored Apr 9, 2024
1 parent 28486a3 commit faf3a9d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/main/java/net/snowflake/client/core/CredentialManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private void initSecureStorageManager() {

/** Helper function for tests to go back to normal settings. */
void resetSecureStorageManager() {
logger.debug("Resetting the secure storage manager");
initSecureStorageManager();
}

Expand All @@ -49,6 +50,7 @@ void resetSecureStorageManager() {
* @param manager
*/
void injectSecureStorageManager(SecureStorageManager manager) {
logger.debug("Injecting secure storage manager");
secureStorageManager = manager;
}

Expand All @@ -66,6 +68,10 @@ public static CredentialManager getInstance() {
* @param loginInput login input to attach id token
*/
void fillCachedIdToken(SFLoginInput loginInput) throws SFException {
logger.debug(
"Looking for cached id token for user: {}, host: {}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
fillCachedCredential(loginInput, ID_TOKEN);
}

Expand All @@ -75,6 +81,10 @@ void fillCachedIdToken(SFLoginInput loginInput) throws SFException {
* @param loginInput login input to attach mfa token
*/
void fillCachedMfaToken(SFLoginInput loginInput) throws SFException {
logger.debug(
"Looking for cached mfa token for user: {}, host: {}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
fillCachedCredential(loginInput, MFA_TOKEN);
}

Expand Down Expand Up @@ -106,16 +116,26 @@ synchronized void fillCachedCredential(SFLoginInput loginInput, String credType)
}

if (cred == null) {
logger.debug("retrieved %s is null", credType);
logger.debug("Retrieved {} is null", credType);
}

// cred can be null
if (credType == ID_TOKEN) {
logger.debug(
"Setting {}id token for user: {}, host: {}",
cred == null ? "null " : "",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
loginInput.setIdToken(cred);
} else if (credType == MFA_TOKEN) {
logger.debug(
"Setting {}mfa token for user: {}, host: {}",
cred == null ? "null " : "",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
loginInput.setMfaToken(cred);
} else {
logger.debug("unrecognized type %s for local cached credential", credType);
logger.debug("Unrecognized type {} for local cached credential", credType);
}
return;
}
Expand All @@ -127,6 +147,10 @@ synchronized void fillCachedCredential(SFLoginInput loginInput, String credType)
* @param loginOutput loginOutput to denote to the cache
*/
void writeIdToken(SFLoginInput loginInput, SFLoginOutput loginOutput) throws SFException {
logger.debug(
"Caching id token in a secure storage for user: {}, host: {}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
writeTemporaryCredential(loginInput, loginOutput.getIdToken(), ID_TOKEN);
}

Expand All @@ -137,6 +161,10 @@ void writeIdToken(SFLoginInput loginInput, SFLoginOutput loginOutput) throws SFE
* @param loginOutput loginOutput to denote to the cache
*/
void writeMfaToken(SFLoginInput loginInput, SFLoginOutput loginOutput) throws SFException {
logger.debug(
"Caching mfa token in a secure storage for user: {}, host: {}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl());
writeTemporaryCredential(loginInput, loginOutput.getMfaToken(), MFA_TOKEN);
}

Expand All @@ -150,7 +178,7 @@ void writeMfaToken(SFLoginInput loginInput, SFLoginOutput loginOutput) throws SF
synchronized void writeTemporaryCredential(SFLoginInput loginInput, String cred, String credType)
throws SFException {
if (Strings.isNullOrEmpty(cred)) {
logger.debug("no %s is given.", credType);
logger.debug("No {} is given.", credType);
return; // no credential
}

Expand All @@ -173,11 +201,15 @@ synchronized void writeTemporaryCredential(SFLoginInput loginInput, String cred,

/** Delete the id token cache */
void deleteIdTokenCache(String host, String user) {
logger.debug(
"Removing cached id token from a secure storage for user: {}, host: {}", user, host);
deleteTemporaryCredential(host, user, ID_TOKEN);
}

/** Delete the mfa token cache */
void deleteMfaTokenCache(String host, String user) {
logger.debug(
"Removing cached mfa token from a secure storage for user: {}, host: {}", user, host);
deleteTemporaryCredential(host, user, MFA_TOKEN);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/snowflake/client/core/FileCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,8 @@ private static long fileCreationTime(File targetFile) {
}
return -1;
}

String getCacheFilePath() {
return cacheFile.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private SecureStorageAppleManager() {
}

public static SecureStorageAppleManager builder() {
logger.info("Using Apple Keychain as a token cache storage");
return new SecureStorageAppleManager();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private SecureStorageLinuxManager() {
.setCacheExpirationInSeconds(CACHE_EXPIRATION_IN_SECONDS)
.setCacheFileLockExpirationInSeconds(CACHE_FILE_LOCK_EXPIRATION_IN_SECONDS)
.build();
logger.info(
"Using temporary file: {} as a token cache storage", fileCacheManager.getCacheFilePath());
}

private static class SecureStorageLinuxManagerHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private SecureStorageWindowsManager() {
}

public static SecureStorageWindowsManager builder() {
logger.info("Using Windows Credential Manager as a token cache storage");
return new SecureStorageWindowsManager();
}

Expand Down

0 comments on commit faf3a9d

Please sign in to comment.