Skip to content

Commit

Permalink
use env variables for oauth only
Browse files Browse the repository at this point in the history
Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>
  • Loading branch information
iliyan-velichkov committed Jan 23, 2025
1 parent 54dc97b commit 778392b
Showing 1 changed file with 46 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,42 @@ public boolean isApplicable(DatabaseSystem databaseSystem) {

@Override
public void apply(HikariConfig config) {
setCommonConfigurations(config);

boolean registeredUsernameAndPass = StringUtils.isNotBlank(config.getUsername()) && StringUtils.isNotBlank(config.getPassword());

if (registeredUsernameAndPass && userAndPassAreNotDummyValues(config)) {
logger.info("There ARE registered username and pass for config [{}] and they will be used.", config);
config.addDataSourceProperty("user", config.getUsername());
config.addDataSourceProperty("password", config.getPassword());

} else {
configureOAuth(config);
}
}

private void setCommonConfigurations(HikariConfig config) {
config.setConnectionTestQuery("SELECT 1"); // connection validation query
config.setKeepaliveTime(TimeUnit.MINUTES.toMillis(5)); // validation execution interval, must be bigger than idle timeout
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(9)); // recreate connections after specified time
config.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(5));

config.addDataSourceProperty("CLIENT_SESSION_KEEP_ALIVE", true);
config.addDataSourceProperty("CLIENT_SESSION_KEEP_ALIVE_HEARTBEAT_FREQUENCY", 900);
}

private void configureOAuth(HikariConfig config) {
if (!hasTokenFile()) {
throw new IllegalStateException("There in no username and/or password (or both are dummy values) for provided config [" + config
+ "]. Assuming it should use oauth token but there is no token file at " + TOKEN_FILE_PATH);
}

logger.info("Missing username and/or password for config [{}]. OAuth token will be used.", config);

config.setUsername(null);
config.setPassword(null);
config.addDataSourceProperty("authenticator", "OAUTH");
config.addDataSourceProperty("token", loadTokenFile());

addDataSourcePropertyIfConfigAvailable("SNOWFLAKE_WAREHOUSE", "warehouse", config);

Expand All @@ -54,59 +83,22 @@ public void apply(HikariConfig config) {
addDataSourcePropertyIfConfigAvailable("SNOWFLAKE_DATABASE", "db", config);
addDataSourcePropertyIfConfigAvailable("SNOWFLAKE_SCHEMA", "schema", config);

String url;

boolean registeredUsernameAndPass = StringUtils.isNotBlank(config.getUsername()) && StringUtils.isNotBlank(config.getPassword());
if (registeredUsernameAndPass && userAndPassAreNotDummyValues(config)) {
logger.info("There ARE registered username and pass for config [{}]. User and password will be used.", config);
config.addDataSourceProperty("user", config.getUsername());
config.addDataSourceProperty("password", config.getPassword());

url = config.getJdbcUrl();
} else {
if (!hasTokenFile()) {
throw new IllegalStateException("There in no username and/or password (or both are dummy values) for provided config ["
+ config + "]. Assuming it should use oauth token but there is no token file at " + TOKEN_FILE_PATH);
}

logger.info("Missing username and/or password for config [{}]. OAuth token will be used.", config);

config.setUsername(null);
config.setPassword(null);
config.addDataSourceProperty("authenticator", "OAUTH");
config.addDataSourceProperty("token", loadTokenFile());

url = "jdbc:snowflake://" + Configuration.get("SNOWFLAKE_HOST") + ":" + Configuration.get("SNOWFLAKE_PORT");
}
String url = "jdbc:snowflake://" + Configuration.get("SNOWFLAKE_HOST") + ":" + Configuration.get("SNOWFLAKE_PORT");

logger.info("Will be used url [{}] for config [{}]", url, config);
config.addDataSourceProperty("url", url);
config.setJdbcUrl(url);
}

private boolean userAndPassAreNotDummyValues(HikariConfig config) {
return isNotDummyValue(config.getUsername()) && isNotDummyValue(config.getPassword());
}

/**
* Note: needed for backward compatibility with Snowflake native applications until they are updated
*
* @param value
* @return
*/
private boolean isNotDummyValue(String value) {
return !Objects.equals(value, "not-used-in-snowpark-scenario");
}

private static String loadTokenFile() {
private String loadTokenFile() {
try {
return new String(Files.readAllBytes(Paths.get(TOKEN_FILE_PATH)));
} catch (IOException ex) {
throw new IllegalStateException("Failed to load token file from path " + TOKEN_FILE_PATH, ex);
}
}

private static boolean hasTokenFile() {
private boolean hasTokenFile() {
return Files.exists(Paths.get(TOKEN_FILE_PATH));
}

Expand All @@ -120,4 +112,18 @@ private void addDataSourcePropertyIfConfigAvailable(String configName, String pr
}
}

private boolean userAndPassAreNotDummyValues(HikariConfig config) {
return isNotDummyValue(config.getUsername()) && isNotDummyValue(config.getPassword());
}

/**
* Note: needed for backward compatibility with Snowflake native applications until they are updated
*
* @param value
* @return
*/
private boolean isNotDummyValue(String value) {
return !Objects.equals(value, "not-used-in-snowpark-scenario");
}

}

0 comments on commit 778392b

Please sign in to comment.