Skip to content
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-732360: Allow connection caching to be disabled by user #1845

Merged
21 changes: 21 additions & 0 deletions src/main/java/net/snowflake/client/core/SFLoginInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class SFLoginInput {

private boolean disableConsoleLogin = true;
private boolean disableSamlURLCheck = false;
private boolean enableClientStoreTemporaryCredential;
private boolean enableClientRequestMfaToken;

// Additional headers to add for Snowsight.
Map<String, String> additionalHttpHeadersForSnowsight;
Expand Down Expand Up @@ -437,4 +439,23 @@ String getHostFromServerUrl() throws SFException {
}
return url.getHost();
}

boolean isEnableClientStoreTemporaryCredential() {
return enableClientStoreTemporaryCredential;
}

SFLoginInput setEnableClientStoreTemporaryCredential(
boolean enableClientStoreTemporaryCredential) {
this.enableClientStoreTemporaryCredential = enableClientStoreTemporaryCredential;
return this;
}

boolean isEnableClientRequestMfaToken() {
return enableClientRequestMfaToken;
}

SFLoginInput setEnableClientRequestMfaToken(boolean enableClientRequestMfaToken) {
this.enableClientRequestMfaToken = enableClientRequestMfaToken;
return this;
}
}
19 changes: 18 additions & 1 deletion src/main/java/net/snowflake/client/core/SFSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public class SFSession extends SFBaseSession {
*/
private int retryTimeout = 300;

private boolean enableClientStoreTemporaryCredential = true;
private boolean enableClientRequestMfaToken = true;

// This constructor is used only by tests with no real connection.
// For real connections, the other constructor is always used.
@VisibleForTesting
Expand Down Expand Up @@ -501,6 +504,18 @@ public void addSFSessionProperty(String propertyName, Object propertyValue) thro
}
break;

case ENABLE_CLIENT_STORE_TEMPORARY_CREDENTIAL:
sfc-gh-dprzybysz marked this conversation as resolved.
Show resolved Hide resolved
if (propertyValue != null) {
enableClientStoreTemporaryCredential = getBooleanValue(propertyValue);
}
break;

case ENABLE_CLIENT_REQUEST_MFA_TOKEN:
if (propertyValue != null) {
enableClientRequestMfaToken = getBooleanValue(propertyValue);
}
break;

default:
break;
}
Expand Down Expand Up @@ -632,7 +647,9 @@ public synchronized void open() throws SFException, SnowflakeSQLException {
connectionPropertiesMap.get(SFSessionProperty.DISABLE_SAML_URL_CHECK) != null
? getBooleanValue(
connectionPropertiesMap.get(SFSessionProperty.DISABLE_SAML_URL_CHECK))
: false);
: false)
.setEnableClientStoreTemporaryCredential(enableClientStoreTemporaryCredential)
.setEnableClientRequestMfaToken(enableClientRequestMfaToken);

logger.info(
"Connecting to {} Snowflake domain",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public enum SFSessionProperty {
"JDBC_DEFAULT_FORMAT_DATE_WITH_TIMEZONE", false, Boolean.class),

// Used as a fix for issue SNOW-354859. Remove with snowflake-jdbc version 4.x with BCR changes.
JDBC_GET_DATE_USE_NULL_TIMEZONE("JDBC_GET_DATE_USE_NULL_TIMEZONE", false, Boolean.class);
JDBC_GET_DATE_USE_NULL_TIMEZONE("JDBC_GET_DATE_USE_NULL_TIMEZONE", false, Boolean.class),

ENABLE_CLIENT_STORE_TEMPORARY_CREDENTIAL("clientStoreTemporaryCredential", false, Boolean.class),

ENABLE_CLIENT_REQUEST_MFA_TOKEN("clientRequestMfaToken", false, Boolean.class);

// property key in string
private String propertyKey;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/snowflake/client/core/SessionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ static SFLoginOutput openSession(
"missing token or password for opening session");
}
if (authenticator.equals(ClientAuthnDTO.AuthenticatorType.EXTERNALBROWSER)) {
if (Constants.getOS() == Constants.OS.MAC || Constants.getOS() == Constants.OS.WINDOWS) {
if ((Constants.getOS() == Constants.OS.MAC || Constants.getOS() == Constants.OS.WINDOWS)
&& loginInput.isEnableClientStoreTemporaryCredential()) {
// force to set the flag for Mac/Windows users
loginInput.getSessionParameters().put(CLIENT_STORE_TEMPORARY_CREDENTIAL, true);
} else {
Expand All @@ -299,7 +300,8 @@ static SFLoginOutput openSession(
}

if (authenticator.equals(ClientAuthnDTO.AuthenticatorType.USERNAME_PASSWORD_MFA)) {
if (Constants.getOS() == Constants.OS.MAC || Constants.getOS() == Constants.OS.WINDOWS) {
if ((Constants.getOS() == Constants.OS.MAC || Constants.getOS() == Constants.OS.WINDOWS)
&& loginInput.isEnableClientRequestMfaToken()) {
sfc-gh-ext-simba-jf marked this conversation as resolved.
Show resolved Hide resolved
loginInput.getSessionParameters().put(CLIENT_REQUEST_MFA_TOKEN, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class SnowflakeBasicDataSource implements DataSource, Serializable {
private static final long serialversionUID = 1L;
private static final String AUTHENTICATOR_SNOWFLAKE_JWT = "SNOWFLAKE_JWT";
private static final String AUTHENTICATOR_OAUTH = "OAUTH";
private static final String AUTHENTICATOR_EXTERNAL_BROWSER = "EXTERNALBROWSER";
private static final String AUTHENTICATOR_USERNAME_PASSWORD_MFA = "USERNAME_PASSWORD_MFA";
private String url;

Expand Down Expand Up @@ -94,7 +95,8 @@ public Connection getConnection(String username, String password) throws SQLExce
}

// The driver needs password for OAUTH as part of SNOW-533673 feature request.
if (!AUTHENTICATOR_SNOWFLAKE_JWT.equalsIgnoreCase(authenticator)) {
if (!AUTHENTICATOR_SNOWFLAKE_JWT.equalsIgnoreCase(authenticator)
&& !AUTHENTICATOR_EXTERNAL_BROWSER.equalsIgnoreCase(authenticator)) {
properties.put(SFSessionProperty.PASSWORD.getPropertyKey(), password);
}

Expand Down Expand Up @@ -380,4 +382,15 @@ public void setJDBCDefaultFormatDateWithTimezone(Boolean jdbcDefaultFormatDateWi
public void setGetDateUseNullTimezone(Boolean getDateUseNullTimezone) {
this.properties.put("JDBC_GET_DATE_USE_NULL_TIMEZONE", getDateUseNullTimezone);
}

public void setEnableClientRequestMfaToken(boolean enableClientRequestMfaToken) {
this.setAuthenticator(AUTHENTICATOR_USERNAME_PASSWORD_MFA);
this.properties.put("clientRequestMfaToken", enableClientRequestMfaToken);
sfc-gh-ext-simba-jf marked this conversation as resolved.
Show resolved Hide resolved
}

public void setEnableClientStoreTemporaryCredential(
boolean enableClientStoreTemporaryCredential) {
this.setAuthenticator(AUTHENTICATOR_EXTERNAL_BROWSER);
this.properties.put("clientStoreTemporaryCredential", enableClientStoreTemporaryCredential);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.jdbc.SnowflakeBasicDataSource;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import net.snowflake.common.core.ClientAuthnDTO;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand Down Expand Up @@ -237,4 +244,27 @@ private SFLoginInput initMockLoginInput() {
when(loginInput.getDisableConsoleLogin()).thenReturn(true);
return loginInput;
}

// Run this test manually to test disabling storing temporary credetials with external browser
// auth. This is valid for versions after 3.18.0.
@Test
@Ignore
public void testEnableClientStoreTemporaryCredential() throws Exception {
Map<String, String> params = AbstractDriverIT.getConnectionParameters();
SnowflakeBasicDataSource ds = new SnowflakeBasicDataSource();
ds.setServerName(params.get("host"));
ds.setAccount(params.get("account"));
ds.setPortNumber(Integer.parseInt(params.get("port")));
ds.setUser(params.get("user"));
ds.setEnableClientStoreTemporaryCredential(false);

try (Connection con = ds.getConnection()) {
Statement stmt = con.createStatement();
for (int i = 0; i < 3; i++) {
try (ResultSet rs = stmt.executeQuery("SELECT CURRENT_USER()")) {
sfc-gh-ext-simba-jf marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(rs.next());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.jdbc.SnowflakeBasicDataSource;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.HttpPost;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand Down Expand Up @@ -327,4 +333,28 @@ public void testUnavailableLocalSecureStorage() throws SQLException {
CredentialManager.getInstance().resetSecureStorageManager();
}
}

// Run this test manually to test disabling the client request MFA token. Use an MFA
// authentication enabled user. This is valid for versions after 3.18.0.
@Test
@Ignore
public void testEnableClientRequestMfaToken() throws SQLException {
Map<String, String> params = AbstractDriverIT.getConnectionParameters();
SnowflakeBasicDataSource ds = new SnowflakeBasicDataSource();
ds.setServerName(params.get("host"));
ds.setAccount(params.get("account"));
ds.setPortNumber(Integer.parseInt(params.get("port")));
ds.setUser(params.get("user"));
ds.setPassword(params.get("password"));
ds.setEnableClientRequestMfaToken(false);

try (Connection con = ds.getConnection()) {
for (int i = 0; i < 3; i++) {
sfc-gh-ext-simba-jf marked this conversation as resolved.
Show resolved Hide resolved
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CURRENT_USER()")) {
assertTrue(rs.next());
}
}
}
}
}
Loading