Skip to content

Commit d2d9b8c

Browse files
committed
Restore local mysql/postgresql DB credentials for bootRun and integration tests
application-mysql.properties/application-postgresql.properties no longer ship database.username/password/url, since those are packaged into the WAR. bootRun, the embedded integrationTest auto-start, and the CI external-Tomcat integration tests (scripts/tomcat/integration_tests_tomcat.sh) all relied on those defaults to reach the root/changeme database that lib_db_helper.sh boots locally/in CI. Supply the same values via -D system properties instead, so those flows keep working without reintroducing the credentials into the shipped artifact.
1 parent 3cacefd commit d2d9b8c

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

scripts/tomcat/integration_tests_tomcat.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ function download_and_extract_tomcat() {
3030
tar -xzf "${cache}" -C "${build_dir}"
3131
}
3232

33+
#######################################
34+
# local_database_catalina_opts
35+
# Local-dev-only DB credentials for the mysql/postgresql profiles, matching the database
36+
# booted by boot_db() in lib_db_helper.sh (root/changeme). application-mysql.properties /
37+
# application-postgresql.properties (packaged into the WAR) no longer ship these, so the
38+
# deployed WAR needs them supplied as -D system properties instead.
39+
# Arguments:
40+
# $1 - comma-separated active Spring profiles (e.g. "postgresql,default")
41+
#######################################
42+
function local_database_catalina_opts() {
43+
local profiles="$1"
44+
if [[ ",${profiles}," == *,mysql,* ]]; then
45+
echo "-Ddatabase.username=root -Ddatabase.password=changeme -Ddatabase.url=jdbc:mysql://127.0.0.1:3306/uaa?useSSL=true&trustServerCertificate=true"
46+
elif [[ ",${profiles}," == *,postgresql,* ]]; then
47+
echo "-Ddatabase.username=root -Ddatabase.password=changeme -Ddatabase.url=jdbc:postgresql:uaa"
48+
fi
49+
}
50+
3351
function wait_for_uaa_http() {
3452
local url="$1"
3553
local max_wait="${2:-300}"
@@ -152,13 +170,16 @@ function main() {
152170
export CATALINA_BASE="${tomcat_root}"
153171
export UAA_PORT="${uaa_port}"
154172

173+
local database_opts
174+
database_opts="$(local_database_catalina_opts "${test_profile}")"
175+
155176
# Do not use `set -u` in setenv.sh: Tomcat sources it, then setclasspath.sh tests unset
156177
# JRE_HOME with `[ -z "$JRE_HOME" ]`, which fails under nounset before Tomcat starts.
157178
# Match UaaBootApplication.main() and integration java -jar (uaa/build.gradle) for Spring context.
158179
cat > "${tomcat_root}/bin/setenv.sh" <<EOF
159180
#!/usr/bin/env sh
160181
export JAVA_OPTS="\${JAVA_OPTS:-}"
161-
export CATALINA_OPTS="\${CATALINA_OPTS:-} -DCLOUDFOUNDRY_CONFIG_PATH=${boot_dir} -DSECRETS_DIR=${boot_dir} -Dserver.servlet.context-path=/uaa -Dsmtp.host=localhost -Dsmtp.port=2525 -Dspring.profiles.active=${test_profile} -Djava.security.egd=file:/dev/./urandom -Dlogging.config=${log4j} -Dstatsd.enabled=true -Dzones.paths.enabled=true -Dspring.main.allow-bean-definition-overriding=true -Dspring.main.allow-circular-references=true"
182+
export CATALINA_OPTS="\${CATALINA_OPTS:-} -DCLOUDFOUNDRY_CONFIG_PATH=${boot_dir} -DSECRETS_DIR=${boot_dir} -Dserver.servlet.context-path=/uaa -Dsmtp.host=localhost -Dsmtp.port=2525 -Dspring.profiles.active=${test_profile} -Djava.security.egd=file:/dev/./urandom -Dlogging.config=${log4j} -Dstatsd.enabled=true -Dzones.paths.enabled=true -Dspring.main.allow-bean-definition-overriding=true -Dspring.main.allow-circular-references=true ${database_opts}"
162183
EOF
163184
chmod +x "${tomcat_root}/bin/setenv.sh"
164185

uaa/build.gradle.kts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@ import javax.inject.Inject
33

44
val identityServer = parent!!.subprojects.find { "cloudfoundry-identity-server" == it.name }!!
55

6+
// Local-dev-only DB credentials for the mysql/postgresql profiles, matching the database
7+
// bootstrapped by README.md / scripts/lib_db_helper.sh / scripts/docker-compose.yml.
8+
// These are intentionally not shipped in application-mysql.properties/application-postgresql.properties
9+
// (which are packaged into the WAR), so callers that boot a real mysql/postgresql locally must
10+
// supply them here instead. Override any of them with the matching -D system property.
11+
fun localDatabaseCredentialArgs(activeProfiles: String): Map<String, String> {
12+
val profiles = activeProfiles.split(",").map { it.trim() }
13+
return when {
14+
profiles.contains("mysql") -> mapOf(
15+
"database.username" to System.getProperty("database.username", "root"),
16+
"database.password" to System.getProperty("database.password", "changeme"),
17+
"database.url" to System.getProperty("database.url", "jdbc:mysql://127.0.0.1:3306/uaa?useSSL=true&trustServerCertificate=true"),
18+
)
19+
profiles.contains("postgresql") -> mapOf(
20+
"database.username" to System.getProperty("database.username", "root"),
21+
"database.password" to System.getProperty("database.password", "changeme"),
22+
"database.url" to System.getProperty("database.url", "jdbc:postgresql:uaa"),
23+
)
24+
else -> emptyMap()
25+
}
26+
}
27+
628
plugins {
729
war
830
alias(libs.plugins.springBoot)
@@ -266,6 +288,8 @@ tasks.register<Test>("integrationTest") {
266288
val springProfile = System.getProperty("spring.profiles.active", "hsqldb")
267289
val warFile = file("build/libs/cloudfoundry-identity-uaa-0.0.0.war")
268290
val bootDir = rootProject.file("scripts/boot")
291+
val databaseArgsList = localDatabaseCredentialArgs(springProfile).map { (key, value) -> "-D$key=$value" }
292+
val databaseArgs = if (databaseArgsList.isEmpty()) "" else databaseArgsList.joinToString(" \\\n ") + " \\\n "
269293

270294
logger.lifecycle("Starting UAA application for integration tests...")
271295

@@ -276,7 +300,7 @@ tasks.register<Test>("integrationTest") {
276300
-Dsmtp.host=localhost \
277301
-Dsmtp.port=2525 \
278302
-Dspring.profiles.active=$springProfile \
279-
-jar ${warFile.absolutePath} > ${bootLogFile.absolutePath} 2>&1 & echo ${'$'}!"""
303+
$databaseArgs-jar ${warFile.absolutePath} > ${bootLogFile.absolutePath} 2>&1 & echo ${'$'}!"""
280304

281305
val proc = ProcessBuilder("bash", "-c", javaCmd).start()
282306
proc.waitFor()
@@ -319,7 +343,9 @@ tasks.named<org.springframework.boot.gradle.tasks.run.BootRun>("bootRun") {
319343
systemProperty("logging.level.org.springframework.security", "TRACE")
320344
systemProperty("logging.config", file("../scripts/boot/log4j2.properties").absolutePath)
321345
systemProperty("uaa.boot.location.tomcat", System.getProperty("uaa.boot.location.tomcat", file("../scripts/boot/tomcat").absolutePath))
322-
systemProperty("spring.profiles.active", System.getProperty("spring.profiles.active", "hsqldb"))
346+
val activeProfiles = System.getProperty("spring.profiles.active", "hsqldb")
347+
systemProperty("spring.profiles.active", activeProfiles)
348+
localDatabaseCredentialArgs(activeProfiles).forEach { (key, value) -> systemProperty(key, value) }
323349
systemProperty("metrics.perRequestMetrics", System.getProperty("metrics.perRequestMetrics", "true"))
324350
systemProperty("smtp.host", "localhost")
325351
systemProperty("smtp.port", 2525)

0 commit comments

Comments
 (0)