Skip to content

Commit

Permalink
SNOW-1167982: Logs - Authentication (#1664)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pbulawa authored Mar 22, 2024
1 parent 7d4eee6 commit 28486a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/snowflake/client/core/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public static CloseableHttpClient buildHttpClient(
AuthScope authScope = new AuthScope(key.getProxyHost(), key.getProxyPort());
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
logger.debug(
"Using user: {} with password {} for proxy host: {}, port: {}",
"Using user: {}, password is {} for proxy host: {}, port: {}",
key.getProxyUser(),
key.getProxyPassword().isEmpty() ? "not provided" : "provided",
key.getProxyHost(),
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/net/snowflake/client/core/SessionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import net.snowflake.client.util.SecretDetector;
import net.snowflake.client.util.Stopwatch;
import net.snowflake.common.core.ClientAuthnDTO;
import net.snowflake.common.core.ClientAuthnParameter;
import net.snowflake.common.core.SqlState;
Expand Down Expand Up @@ -343,6 +344,8 @@ private static SFLoginOutput newSession(
Map<SFSessionProperty, Object> connectionPropertiesMap,
String tracingLevel)
throws SFException, SnowflakeSQLException {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
// build URL for login request
URIBuilder uriBuilder;
URI loginURI;
Expand All @@ -369,6 +372,18 @@ private static SFLoginOutput newSession(
final ClientAuthnDTO.AuthenticatorType authenticatorType = getAuthenticator(loginInput);
Map<String, Object> commonParams;

String oktaUsername = loginInput.getOKTAUserName();
logger.info(
"Opening new session. Authenticating user: {}, host: {} with authentication method: {}."
+ " Login timeout: {} s, auth timeout: {} s, OCSP mode: {}{}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl(),
authenticatorType,
loginInput.getLoginTimeout(),
loginInput.getAuthTimeout(),
loginInput.getOCSPMode(),
Strings.isNullOrEmpty(oktaUsername) ? "" : ", okta username: " + oktaUsername);

try {

uriBuilder = new URIBuilder(loginInput.getServerUrl());
Expand Down Expand Up @@ -725,9 +740,16 @@ private static SFLoginOutput newSession(
deleteMfaTokenCache(loginInput.getHostFromServerUrl(), loginInput.getUserName());
}

String errorMessage = jsonNode.path("message").asText();

logger.error(
"Failed to open new session for user: {}, host: {}. Error: {}",
loginInput.getUserName(),
loginInput.getHostFromServerUrl(),
errorMessage);
throw new SnowflakeSQLException(
NO_QUERY_ID,
jsonNode.path("message").asText(),
errorMessage,
SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,
errorCode);
}
Expand Down Expand Up @@ -857,6 +879,13 @@ && asBoolean(loginInput.getSessionParameters().get(CLIENT_STORE_TEMPORARY_CREDEN
CredentialManager.getInstance().writeMfaToken(loginInput, ret);
}

stopwatch.stop();
logger.info(
"Session opened in {} ms. User: {}, host: {} with authentication method: {} authenticated successfully.",
stopwatch.elapsedMillis(),
loginInput.getUserName(),
loginInput.getHostFromServerUrl(),
authenticatorType);
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration
logMessage +=
", user: "
+ key.getProxyUser()
+ "with password "
+ ", password is "
+ (key.getProxyPassword().isEmpty() ? "not provided" : "provided");
clientConfig.setProxyUsername(key.getProxyUser());
clientConfig.setProxyPassword(key.getProxyPassword());
Expand Down

0 comments on commit 28486a3

Please sign in to comment.