shunping commented on code in PR #39806:
URL: https://github.com/apache/beam/pull/39806#discussion_r3807552996
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/GcpSecret.java:
##########
@@ -39,6 +53,68 @@ public GcpSecret(String versionName) {
this.versionName = versionName;
}
+ /** Initialize GcpSecret from a map specification. */
+ static GcpSecret fromMap(Map<String, String> specMap) {
+ Set<String> allowedKeys =
+ new HashSet<>(Arrays.asList("version_name", "name", "project",
"version"));
+ Set<String> invalidKeys = new HashSet<>(specMap.keySet());
+ invalidKeys.removeAll(allowedKeys);
+ if (!invalidKeys.isEmpty()) {
+ List<String> sortedInvalid = new ArrayList<>(invalidKeys);
+ Collections.sort(sortedInvalid);
+ throw new IllegalArgumentException(
+ "Invalid secret parameter " + String.join(", ", sortedInvalid));
+ }
+ String versionName = parseVersionName(specMap);
+ return new GcpSecret(versionName);
+ }
+
+ /** Parses the version name from a specification dictionary. */
+ private static String parseVersionName(Map<String, String> specMap) {
+ String versionNameParam = specMap.get("version_name");
+ if (!Strings.isNullOrEmpty(versionNameParam)) {
+ return Preconditions.checkNotNull(
+ versionNameParam, "version_name must contain a valid value for
versionName parameter");
+ }
+ String secretId = specMap.get("name");
+ if (Strings.isNullOrEmpty(secretId)) {
+ throw new IllegalArgumentException("Secret name must be specified in
secret spec.");
+ }
+ String projectId = specMap.get("project");
+ if (Strings.isNullOrEmpty(projectId)) {
+ projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
+ }
+ if (Strings.isNullOrEmpty(projectId)) {
+ projectId = System.getenv("GCP_PROJECT");
+ }
+ if (Strings.isNullOrEmpty(projectId)) {
+ try {
+ Class<?> clazz = Class.forName("com.google.cloud.ServiceOptions");
+ java.lang.reflect.Method method =
clazz.getMethod("getDefaultProjectId");
+ @SuppressWarnings("nullness")
+ Object result = method.invoke(null);
+ if (result != null) {
+ projectId = result.toString();
+ }
+ } catch (Throwable e) {
+ LOG.debug("Could not resolve GCP project via ServiceOptions
reflection", e);
+ }
+ }
Review Comment:
Done. I extracted that piece of code into a function and applied that in
`GcpHsmGeneratedSecret`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]