Skip to content

Commit 2fb78c4

Browse files
committed
build(secrets): enforce valid API keys in secrets.properties
Introduce a Gradle build-time check to enforce that required API keys are present and valid in the secrets.properties file before debug build or installation tasks proceed. This avoids silent build successes that inevitably lead to immediate crashes at runtime when required keys (such as MAPS_API_KEY) are missing or left as default placeholder values. - Add gradle/secrets-enforcement.gradle.kts to parse and validate keys in secrets.properties against the standard Google API key signature pattern (AIza...). - Apply the enforcement script globally in the root build.gradle.kts for non-CI environments. - Support IDE synchronization and test task bypasses to preserve developer workflows.
1 parent e6ead25 commit 2fb78c4

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

build.gradle.kts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ plugins {
2222
alias(libs.plugins.secrets.gradle.plugin) apply false
2323
alias(libs.plugins.hilt.android) apply false
2424
alias(libs.plugins.ksp) apply false
25-
}
25+
}
26+
27+
// Share the isCI flag with all subprojects (optional, the script handles it if omitted)
28+
val isCI = System.getenv("CI")?.toBoolean() ?: false
29+
extra["isCI"] = isCI
30+
31+
// Enforce that required API keys are present in secrets.properties
32+
extra["requiredSecrets"] = listOf("MAPS_API_KEY")
33+
34+
// Apply the enforcement script
35+
apply(from = "gradle/secrets-enforcement.gradle.kts")
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.util.Properties
18+
19+
/**
20+
* This script enforces that required API keys are present in a `secrets.properties` file
21+
* before allowing build or install tasks to proceed. It ignores syncs and test tasks.
22+
*
23+
* Usage in build.gradle.kts:
24+
*
25+
* extra["requiredSecrets"] = listOf("MAPS3D_API_KEY", "PLACES_API_KEY")
26+
* apply(from = "gradle/secrets-enforcement.gradle.kts")
27+
*/
28+
29+
// Use the provided isCI flag if available, otherwise detect it from environment variables
30+
val isCI = if (project.extra.has("isCI")) {
31+
project.extra["isCI"] as? Boolean ?: false
32+
} else {
33+
System.getenv("CI")?.toBoolean() ?: false
34+
}
35+
36+
if (!isCI) {
37+
val secretsFile = rootProject.file("secrets.properties")
38+
val requestedTasks = gradle.startParameter.taskNames
39+
@Suppress("UNCHECKED_CAST")
40+
val requiredKeys = (project.extra["requiredSecrets"] as? List<String>) ?: emptyList()
41+
42+
if (requestedTasks.isEmpty() && !secretsFile.exists()) {
43+
// It's likely an IDE sync if no tasks are specified, so just issue a warning.
44+
println("Warning: secrets.properties not found. Gradle sync may succeed, but building/running the app will fail.")
45+
} else if (requestedTasks.isNotEmpty()) {
46+
val buildTaskKeywords = setOf("build", "install", "assemble")
47+
val testTaskKeywords = setOf("test", "report", "lint")
48+
49+
val isBuildTask = requestedTasks.any { name ->
50+
buildTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) }
51+
}
52+
val isTestTask = requestedTasks.any { name ->
53+
testTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) }
54+
}
55+
val isDebugTask = requestedTasks.any { task ->
56+
task.contains("Debug", ignoreCase = true) || task.contains("installAndLaunch", ignoreCase = true)
57+
}
58+
59+
if (isBuildTask && !isTestTask && isDebugTask) {
60+
val defaultsFile = rootProject.file("local.defaults.properties")
61+
val requiredKeysMessage = if (defaultsFile.exists()) {
62+
defaultsFile.readText()
63+
} else {
64+
requiredKeys.joinToString("\n") { "$it=<YOUR_API_KEY>" }
65+
}
66+
67+
if (!secretsFile.exists()) {
68+
throw GradleException("secrets.properties file not found. Please create a 'secrets.properties' file in the root project directory with the following content:\n\n$requiredKeysMessage")
69+
}
70+
71+
val secrets = Properties()
72+
secretsFile.inputStream().use { secrets.load(it) }
73+
74+
requiredKeys.forEach { key ->
75+
val value = secrets.getProperty(key)
76+
if (value.isNullOrBlank() || !value.matches(Regex("^AIza[a-zA-Z0-9_-]{35}$"))) {
77+
throw GradleException("Invalid or missing $key in secrets.properties. Please provide a valid Google API key (starts with 'AIza').")
78+
}
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)