Skip to content

Commit cabb7a5

Browse files
fix: secretmanager to be an optional dep in autoconfig module (#3706)
* fix: secretmanager to be an optional dep in autoconfig module Fixes #3560 It markes the secretmanager dependency as optional. The config loader in the Secret Manager autoconfig was assuming the existence of the now optional Secret Manager integration . We now check for the class to be present before resolving the properties * Update spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java * improve comment * fix comment checkstyle
1 parent 30bb2b6 commit cabb7a5

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

spring-cloud-gcp-autoconfigure/pom.xml

+1-4
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,7 @@
275275
<dependency>
276276
<groupId>com.google.cloud</groupId>
277277
<artifactId>spring-cloud-gcp-secretmanager</artifactId>
278-
<!-- A few starters need this dependency to validate secrets -->
279-
<!-- using SecretManagerSyntaxUtils. Making it optional produces runtime failures -->
280-
<!-- of the kind ClassNotFoundError. -->
281-
<optional>false</optional>
278+
<optional>true</optional>
282279
</dependency>
283280

284281
<!-- KMS -->

spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/secretmanager/SecretManagerConfigDataLocationResolver.java

+29
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,43 @@ public class SecretManagerConfigDataLocationResolver implements
5353
*/
5454
private static SecretManagerServiceClient secretManagerServiceClient;
5555

56+
/**
57+
* Checks if the property can be resolved by the Secret Manager resolver.
58+
* For the check, we rely on the presence of the SecretManagerSyntaxUtils class, which is an
59+
* optional dependency.
60+
* Since optional dependencies may not be present at runtime, we explicitly check for its
61+
* existence before resolving the property.
62+
* If it's not present, it means this config resolver is not meant to be used.
63+
*
64+
* @return true if it contains the expected `sm@` or `sm://` prefix, false otherwise.
65+
*/
5666
@Override
5767
public boolean isResolvable(ConfigDataLocationResolverContext context,
5868
ConfigDataLocation location) {
69+
boolean secretManagerSyntaxUtilsPresent = isClassPresent("com.google.cloud.spring.secretmanager.SecretManagerSyntaxUtils");
70+
if (!secretManagerSyntaxUtilsPresent) {
71+
return false;
72+
}
5973
Optional<String> matchedPrefix = getMatchedPrefixes(location::hasPrefix);
6074
warnIfUsingDeprecatedSyntax(logger, matchedPrefix.orElse(""));
6175
return matchedPrefix.isPresent();
6276
}
6377

78+
/**
79+
* Checks if the specified class is present in this runtime.
80+
*
81+
* @param clazzFullName the full name of the class for the existence check
82+
* @return true if present
83+
*/
84+
private boolean isClassPresent(String clazzFullName) {
85+
try {
86+
Class.forName(clazzFullName);
87+
return true;
88+
} catch (ClassNotFoundException e) {
89+
return false;
90+
}
91+
}
92+
6493
@Override
6594
public List<SecretManagerConfigDataResource> resolve(ConfigDataLocationResolverContext context,
6695
ConfigDataLocation location)

0 commit comments

Comments
 (0)