damccorm commented on code in PR #39806:
URL: https://github.com/apache/beam/pull/39806#discussion_r3807243415


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/GcpHsmGeneratedSecret.java:
##########
@@ -62,6 +73,47 @@ public GcpHsmGeneratedSecret(
     this.secretId = "HsmGeneratedSecret_" + jobName;
   }
 
+  /** Initialize GcpHsmGeneratedSecret from a map specification. */
+  static GcpHsmGeneratedSecret fromMap(Map<String, String> specMap) {
+    Set<String> allowedKeys =
+        new HashSet<>(
+            Arrays.asList("project_id", "location_id", "key_ring_id", 
"key_id", "job_name"));
+    Set<String> missing = new HashSet<>();
+    for (String key : allowedKeys) {
+      if (!specMap.containsKey(key) || 
Strings.isNullOrEmpty(specMap.get(key))) {
+        missing.add(key);
+      }
+    }
+    if (!missing.isEmpty()) {
+      List<String> sortedMissing = new ArrayList<>(missing);
+      Collections.sort(sortedMissing);
+      throw new IllegalArgumentException(
+          "Missing required parameter(s) for GcpHsmGeneratedSecret: " + 
sortedMissing);
+    }
+    Set<String> invalid = new HashSet<>(specMap.keySet());
+    invalid.removeAll(allowedKeys);
+    if (!invalid.isEmpty()) {
+      List<String> sortedInvalid = new ArrayList<>(invalid);
+      Collections.sort(sortedInvalid);
+      throw new IllegalArgumentException(
+          "Invalid secret parameter " + String.join(", ", sortedInvalid));
+    }
+    return new GcpHsmGeneratedSecret(
+        Preconditions.checkNotNull(

Review Comment:
   Do we need these precondition checks? Can these ever be null at this point?



##########
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:
   If we're allowing this here, should we do the same for 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]

Reply via email to