From 34017f119c7901ef8cb9e7bb8551047c20dd6ac1 Mon Sep 17 00:00:00 2001
From: Gcolon021 <34667267+Gcolon021@users.noreply.github.com>
Date: Fri, 13 Sep 2024 14:33:16 -0400
Subject: [PATCH] [ALS-7051] BDC: Deploy Data Dictionary API (#21)
* Add dynamic environment variables for datasource
Included support for setting DATASOURCE_URL and DATASOURCE_USERNAME through --env-file. This change allows the database configuration to be dynamically modified without altering the Dockerfile, improving flexibility and deployment ease.
* Add support for additional environment variables
Introduced new ARG directives in Dockerfile to handle DATASOURCE_URL, DATASOURCE_USERNAME, and SPRING_PROFILE. This addition ensures these variables can be passed at build time, improving flexibility in configuration.
* Increase log verbosity for debugging
Enabled DEBUG log level for AWS Secrets Manager SQL and Spring framework to facilitate easier debugging. This will provide more detailed logs for datasource and application behaviors.
* Temporary datasource verification class
Remove after testing
* Update JDBC URL scheme for PostgreSQL connection
* Update database URL in application-bdc.properties
Changed the database path in the datasource URL from 'auth' to 'dict' to ensure connectivity with the correct database. This adjustment is necessary for the application's dictionary service to function properly.
* Add application properties for dictionary weights service
Introduce a new configuration file for the dictionary weights service. This file includes PostgreSQL datasource settings and a filename for weights.
* Update Dockerfile and config for AWS Secrets Manager integration
Added environment variables and updated entrypoint in Dockerfile. Modified pom.xml to include AWS Secrets Manager JDBC dependency. Updated application properties to use AWS Secrets Manager for database credentials.
* Add DataSourceVerifier to check DB connection on startup
New class `DataSourceVerifier` created to ensure that the database connection is successfully established when the application context is refreshed. It logs messages indicating the success or failure of this verification.
* Update src/main/resources/application-bdc.properties
* Update database URL and add schema property in config
* Remove unused datasource schema property
The spring.datasource.schema property was not in use and has been removed to clean up the `application-bdc.properties` file. This helps maintain the configuration file by eliminating unnecessary properties.
* Update datasource URLs to include currentSchema parameter
This change adds the `currentSchema=dict` parameter to the JDBC URLs in the `application-bdc.properties` files. This adjustment ensures proper schema usage for database connections in both the main and dictionaryweights modules.
* Update dictionaryweights/src/main/resources/application-bdc.properties
* revert
* Update src/main/resources/application-bdc.properties
---
Dockerfile | 11 +++++-
dictionaryweights/Dockerfile | 11 +++++-
dictionaryweights/pom.xml | 5 +++
.../dictionaryweights/DataSourceVerifier.java | 39 +++++++++++++++++++
.../main/resources/application-bdc.properties | 9 +++++
.../datasource/DataSourceVerifier.java | 39 +++++++++++++++++++
src/main/resources/application-bdc.properties | 4 +-
7 files changed, 114 insertions(+), 4 deletions(-)
create mode 100644 dictionaryweights/src/main/java/edu/harvard/dbmi/avillach/dictionaryweights/DataSourceVerifier.java
create mode 100644 dictionaryweights/src/main/resources/application-bdc.properties
create mode 100644 src/main/java/edu/harvard/dbmi/avillach/dictionary/datasource/DataSourceVerifier.java
diff --git a/Dockerfile b/Dockerfile
index 1a441e0..457f53e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -16,5 +16,14 @@ COPY --from=build target/dictionary-*.jar /dictionary.jar
# Time zone
ENV TZ="US/Eastern"
+ARG DATASOURCE_URL
+ARG DATASOURCE_USERNAME
+ARG SPRING_PROFILE
+
+# If a --env-file is passed in, you can override these values
+ENV DATASOURCE_URL=${DATASOURCE_URL}
+ENV DATASOURCE_USERNAME=${DATASOURCE_USERNAME}
+ENV SPRING_PROFILE=${SPRING_PROFILE}
+
# Default to no profile
-ENTRYPOINT java $DEBUG_VARS $PROXY_VARS -Xmx8192m -jar /dictionary.jar --spring.profiles.active=${SPRING_PROFILE:-}
+ENTRYPOINT java $DEBUG_VARS $PROXY_VARS -Xmx8192m -jar /dictionary.jar --spring.profiles.active=${SPRING_PROFILE:-}
\ No newline at end of file
diff --git a/dictionaryweights/Dockerfile b/dictionaryweights/Dockerfile
index 5bab457..cc88383 100644
--- a/dictionaryweights/Dockerfile
+++ b/dictionaryweights/Dockerfile
@@ -13,4 +13,13 @@ FROM amazoncorretto:22-alpine
# Copy jar and access token from maven build
COPY --from=build target/dictionaryweights-*.jar /dictionaryweights.jar
-ENTRYPOINT java -jar /dictionaryweights.jar
\ No newline at end of file
+ARG DATASOURCE_URL
+ARG DATASOURCE_USERNAME
+ARG SPRING_PROFILE
+
+# If a --env-file is passed in, you can override these values
+ENV DATASOURCE_URL=${DATASOURCE_URL}
+ENV DATASOURCE_USERNAME=${DATASOURCE_USERNAME}
+ENV SPRING_PROFILE=${SPRING_PROFILE}
+
+ENTRYPOINT java -jar /dictionaryweights.jar --spring.profiles.active=${SPRING_PROFILE:-}
\ No newline at end of file
diff --git a/dictionaryweights/pom.xml b/dictionaryweights/pom.xml
index 9bc2280..5294e33 100644
--- a/dictionaryweights/pom.xml
+++ b/dictionaryweights/pom.xml
@@ -61,6 +61,11 @@
postgresql
test
+
+ com.amazonaws.secretsmanager
+ aws-secretsmanager-jdbc
+ 2.0.2
+
diff --git a/dictionaryweights/src/main/java/edu/harvard/dbmi/avillach/dictionaryweights/DataSourceVerifier.java b/dictionaryweights/src/main/java/edu/harvard/dbmi/avillach/dictionaryweights/DataSourceVerifier.java
new file mode 100644
index 0000000..9e36308
--- /dev/null
+++ b/dictionaryweights/src/main/java/edu/harvard/dbmi/avillach/dictionaryweights/DataSourceVerifier.java
@@ -0,0 +1,39 @@
+package edu.harvard.dbmi.avillach.dictionary.datasource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Service;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+@Service
+public class DataSourceVerifier {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DataSourceVerifier.class);
+
+ private final DataSource dataSource;
+
+ @Autowired
+ public DataSourceVerifier(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ @EventListener(ContextRefreshedEvent.class)
+ public void verifyDataSourceConnection() {
+ try (Connection connection = dataSource.getConnection()) {
+ if (connection != null) {
+ LOG.info("Datasource connection verified successfully.");
+ } else {
+ LOG.info("Failed to obtain a connection from the datasource.");
+ }
+ } catch (SQLException e) {
+ LOG.info("Error verifying datasource connection: {}", e.getMessage());
+ }
+ }
+
+}
diff --git a/dictionaryweights/src/main/resources/application-bdc.properties b/dictionaryweights/src/main/resources/application-bdc.properties
new file mode 100644
index 0000000..1c97a81
--- /dev/null
+++ b/dictionaryweights/src/main/resources/application-bdc.properties
@@ -0,0 +1,9 @@
+spring.application.name=dictionaryweights
+spring.main.web-application-type=none
+
+spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
+spring.datasource.driver-class-name=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
+spring.datasource.url=jdbc-secretsmanager:postgresql://${DATASOURCE_URL}/picsure?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true¤tSchema=dict
+spring.datasource.username=${DATASOURCE_USERNAME}
+
+weights.filename=/weights.csv
\ No newline at end of file
diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/datasource/DataSourceVerifier.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/datasource/DataSourceVerifier.java
new file mode 100644
index 0000000..9e36308
--- /dev/null
+++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/datasource/DataSourceVerifier.java
@@ -0,0 +1,39 @@
+package edu.harvard.dbmi.avillach.dictionary.datasource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Service;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+@Service
+public class DataSourceVerifier {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DataSourceVerifier.class);
+
+ private final DataSource dataSource;
+
+ @Autowired
+ public DataSourceVerifier(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ @EventListener(ContextRefreshedEvent.class)
+ public void verifyDataSourceConnection() {
+ try (Connection connection = dataSource.getConnection()) {
+ if (connection != null) {
+ LOG.info("Datasource connection verified successfully.");
+ } else {
+ LOG.info("Failed to obtain a connection from the datasource.");
+ }
+ } catch (SQLException e) {
+ LOG.info("Error verifying datasource connection: {}", e.getMessage());
+ }
+ }
+
+}
diff --git a/src/main/resources/application-bdc.properties b/src/main/resources/application-bdc.properties
index 352e321..3660041 100644
--- a/src/main/resources/application-bdc.properties
+++ b/src/main/resources/application-bdc.properties
@@ -1,6 +1,6 @@
spring.application.name=dictionary
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
-spring.datasource.url=jdbc-secretsmanager:postgres://${DATASOURCE_URL}/auth?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true
+spring.datasource.url=jdbc-secretsmanager:postgresql://${DATASOURCE_URL}/picsure?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true¤tSchema=dict
spring.datasource.username=${DATASOURCE_USERNAME}
-server.port=80
\ No newline at end of file
+server.port=80