diff --git a/components/data/data-source-snowpark/src/main/java/org/eclipse/dirigible/components/data/source/snowpark/DataSourceInitializerContributorSnowpark.java b/components/data/data-source-snowpark/src/main/java/org/eclipse/dirigible/components/data/source/snowpark/DataSourceInitializerContributorSnowpark.java index b25fc8df878..de92c38534a 100644 --- a/components/data/data-source-snowpark/src/main/java/org/eclipse/dirigible/components/data/source/snowpark/DataSourceInitializerContributorSnowpark.java +++ b/components/data/data-source-snowpark/src/main/java/org/eclipse/dirigible/components/data/source/snowpark/DataSourceInitializerContributorSnowpark.java @@ -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. @@ -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 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); + } + } } diff --git a/pom.xml b/pom.xml index 84b4dca235e..4cd64d28efa 100644 --- a/pom.xml +++ b/pom.xml @@ -173,7 +173,7 @@ 3.12.14 2.21.10 - 3.13.34 + 3.18.0 8.4.0