From 2ff6013b9db7617fda9a8b2e6547b4aa8f77cb2f Mon Sep 17 00:00:00 2001 From: GKAISER9 Date: Thu, 26 Oct 2023 14:25:29 -0400 Subject: [PATCH] added DatabaseConfiguration.java to point to GCP db --- api/.gitignore | 6 +- api/build.gradle | 2 + .../DatabaseConfiguration.java | 60 +++++++++++++++++++ .../main/resources/application-gcp.properties | 5 ++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/com/ford/labs/retroquest/database_configuration/DatabaseConfiguration.java create mode 100644 api/src/main/resources/application-gcp.properties diff --git a/api/.gitignore b/api/.gitignore index e5636f792..33ab43d1f 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -2,7 +2,7 @@ /build/ !gradle/wrapper/gradle-wrapper.jar /out/ - /src/main/resources/static/ - -bin \ No newline at end of file +bin +*.pem +*.pkcs8 diff --git a/api/build.gradle b/api/build.gradle index 698667b13..8bd8e1645 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -81,6 +81,8 @@ dependencies { testImplementation 'org.springframework.security:spring-security-test' developmentOnly 'org.springframework.boot:spring-boot-devtools' + + runtimeOnly 'org.postgresql:postgresql:42.4.0' } static def noProfileOrDbDefined(tasks) { diff --git a/api/src/main/java/com/ford/labs/retroquest/database_configuration/DatabaseConfiguration.java b/api/src/main/java/com/ford/labs/retroquest/database_configuration/DatabaseConfiguration.java new file mode 100644 index 000000000..fd05484af --- /dev/null +++ b/api/src/main/java/com/ford/labs/retroquest/database_configuration/DatabaseConfiguration.java @@ -0,0 +1,60 @@ +package com.ford.labs.retroquest.database_configuration; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import javax.sql.DataSource; +import java.io.File; +import java.io.IOException; + +@Configuration +@Profile("gcp") +public class DatabaseConfiguration { + + private final String dbHost; + private final String dbPort; + private final String dbName; + private final String username; + private final String password; + + + public DatabaseConfiguration( + @Value("${database.dbHost}") final String dbHost, + @Value("${database.dbPort}") final String dbPort, + @Value("${database.dbName}") final String dbName, + @Value("${database.username}") final String username, + @Value("${database.password}") final String password + ) { + this.dbHost = dbHost; + this.dbPort = dbPort; + this.dbName = dbName; + this.username = username; + this.password = password; + } + + @Bean + public DataSource getDataSource() throws IOException { + + Resource certResource = new ClassPathResource("client_cert.pem"); + File certFile = certResource.getFile(); + Resource keyResource = new ClassPathResource("private_key.pkcs8"); + File keyFile = keyResource.getFile(); + + String url = "jdbc:postgresql://" + dbHost + ":" + dbPort + "/" + dbName + + "?sslMode=require" + + "&sslcert=" + certFile.getPath() + + "&sslkey=" + keyFile.getPath(); + + return DataSourceBuilder.create() + .url(url) + .username(username) + .password(password) + .driverClassName("org.postgresql.Driver") + .build(); + } +} diff --git a/api/src/main/resources/application-gcp.properties b/api/src/main/resources/application-gcp.properties new file mode 100644 index 000000000..15c197f29 --- /dev/null +++ b/api/src/main/resources/application-gcp.properties @@ -0,0 +1,5 @@ +database.dbHost=${vcap.services.database.credentials.dbHost} +database.dbPort=${vcap.services.database.credentials.dbPort} +database.dbName=${vcap.services.database.credentials.dbName} +database.username=${vcap.services.database.credentials.username} +database.password=${vcap.services.database.credentials.password}