Skip to content

Commit

Permalink
Fix snowflake communication (#4183)
Browse files Browse the repository at this point in the history
* fix snowflake communication

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* refactoring

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* rename

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

---------

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>
  • Loading branch information
iliyan-velichkov authored Jul 30, 2024
1 parent f1714ed commit b555a6a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
*/
package org.eclipse.dirigible.components.data.source.snowpark;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.components.data.sources.domain.DataSource;
import org.eclipse.dirigible.components.data.sources.manager.DataSourceInitializerContributor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
* The Class DataSourceInitializerContributorSnowpark.
*/
@Configuration
@org.springframework.context.annotation.Configuration
public class DataSourceInitializerContributorSnowpark implements DataSourceInitializerContributor {
/** The Constant logger. */
private static final Logger logger = LoggerFactory.getLogger(DataSourceInitializerContributorSnowpark.class);
public static final String TOKEN_FILE_PATH = "/snowflake/session/token";

/**
* Contribute.
Expand All @@ -38,37 +40,49 @@ public class DataSourceInitializerContributorSnowpark implements DataSourceIniti
public void contribute(DataSource dataSource, Properties properties) {
if (!dataSource.getName()
.startsWith("SNOWFLAKE")) {
logger.info("[{}] will NOT contribute to datasource [{}]", this, dataSource.getName());
return;
}
Map<String, String> env = System.getenv();
try {
String url;
if (Files.exists(Paths.get("/snowflake/session/token"))) {
properties.put("dataSource.CLIENT_SESSION_KEEP_ALIVE", true);
properties.put("dataSource.account", env.get("SNOWFLAKE_ACCOUNT"));
properties.put("dataSource.CLIENT_SESSION_KEEP_ALIVE", true);
setPropertyIfConfigAvailable("SNOWFLAKE_ACCOUNT", "dataSource.account", properties);
setPropertyIfConfigAvailable("SNOWFLAKE_WAREHOUSE", "dataSource.warehouse", properties);
setPropertyIfConfigAvailable("SNOWFLAKE_DATABASE", "dataSource.db", properties);
setPropertyIfConfigAvailable("SNOWFLAKE_SCHEMA", "dataSource.schema", properties);

if (hasTokenFile()) {
properties.put("dataSource.authenticator", "OAUTH");
properties.put("dataSource.token", new String(Files.readAllBytes(Paths.get("/snowflake/session/token"))));
properties.put("dataSource.warehouse", env.getOrDefault("SNOWFLAKE_WAREHOUSE", ""));
properties.put("dataSource.db", env.get("SNOWFLAKE_DATABASE"));
properties.put("dataSource.schema", env.get("SNOWFLAKE_SCHEMA"));
properties.put("dataSource.token", new String(Files.readAllBytes(Paths.get(TOKEN_FILE_PATH))));
properties.put("dataSource.insecureMode", true);
url = "jdbc:snowflake://" + env.get("SNOWFLAKE_HOST") + ":" + env.get("SNOWFLAKE_PORT");
properties.put("jdbcUrl", url);
properties.put("dataSource.url", url);
url = "jdbc:snowflake://" + Configuration.get("SNOWFLAKE_HOST") + ":" + Configuration.get("SNOWFLAKE_PORT");
} else {
properties.put("dataSource.CLIENT_SESSION_KEEP_ALIVE", true);
properties.put("dataSource.account", env.getOrDefault("SNOWFLAKE_ACCOUNT", ""));
properties.put("dataSource.user", env.getOrDefault("SNOWFLAKE_USERNAME", dataSource.getUsername()));
properties.put("dataSource.password", env.getOrDefault("SNOWFLAKE_PASSWORD", dataSource.getPassword()));
properties.put("dataSource.warehouse", env.getOrDefault("SNOWFLAKE_WAREHOUSE", ""));
properties.put("dataSource.db", env.getOrDefault("SNOWFLAKE_DATABASE", ""));
properties.put("dataSource.schema", env.getOrDefault("SNOWFLAKE_SCHEMA", ""));
url = env.getOrDefault("SNOWFLAKE_URL", dataSource.getUrl());
properties.put("jdbcUrl", url);
properties.put("dataSource.url", url);
setPropertyIfConfigAvailable("SNOWFLAKE_ROLE", "dataSource.role", properties);
setPropertyIfConfigAvailable("SNOWFLAKE_USERNAME", "dataSource.user", properties);
setPropertyIfConfigAvailable("SNOWFLAKE_PASSWORD", "dataSource.password", properties);

url = Configuration.get("SNOWFLAKE_URL", dataSource.getUrl());
}

properties.put("jdbcUrl", url);
properties.put("dataSource.url", url);

} catch (IOException ex) {
logger.error("Invalid configuration for the datasource: [{}]", dataSource.getName(), ex);
}
}

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

private void setPropertyIfConfigAvailable(String configName, String propertyName, Properties properties) {
String value = Configuration.get(configName);
if (StringUtils.isNotBlank(value)) {
logger.debug("Setting property [{}] from config [{}]", propertyName, configName);
properties.put(propertyName, value);
} else {
logger.debug("Will NOT set property [{}] since config [{}] value is [{}]", propertyName, configName, value);
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
<!-- JDBC -->
<mongodb.version>3.12.14</mongodb.version>
<ngdbc.version>2.21.10</ngdbc.version>
<snowflake.version>3.13.34</snowflake.version>
<snowflake.version>3.18.0</snowflake.version>
<mysql.version>8.4.0</mysql.version>

<!-- QLDB -->
Expand Down

0 comments on commit b555a6a

Please sign in to comment.