dimas-b commented on code in PR #940:
URL: https://github.com/apache/polaris/pull/940#discussion_r1943569687


##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/bootstrap/RootCredentialsSet.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.persistence.bootstrap;
+
+import static 
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import com.google.common.base.Splitter;
+import jakarta.annotation.Nullable;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.immutables.value.Value;
+
+/**
+ * A utility to parse and provide credentials for Polaris realms and 
principals during a bootstrap
+ * phase.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableRootCredentialsSet.class)
+@JsonDeserialize(as = ImmutableRootCredentialsSet.class)
[email protected](jdkOnly = true)
+public interface RootCredentialsSet {
+
+  RootCredentialsSet EMPTY = ImmutableRootCredentialsSet.builder().build();
+
+  String SYSTEM_PROPERTY = "polaris.bootstrap.credentials";
+  String ENVIRONMENT_VARIABLE = "POLARIS_BOOTSTRAP_CREDENTIALS";
+
+  /**
+   * Parse credentials from the system property {@value #SYSTEM_PROPERTY} or 
the environment
+   * variable {@value #ENVIRONMENT_VARIABLE}, whichever is set.
+   *
+   * <p>See {@link #fromString(String)} for the expected format.
+   */
+  static RootCredentialsSet fromEnvironment() {
+    return fromString(
+        System.getProperty(SYSTEM_PROPERTY, 
System.getenv().get(ENVIRONMENT_VARIABLE)));
+  }
+
+  /**
+   * Parse a string of credentials in the format:
+   *
+   * <pre>
+   * realm1,client1,secret1;realm2,client2,secret2;...
+   * </pre>
+   */
+  static RootCredentialsSet fromString(@Nullable String credentialsString) {
+    return credentialsString != null && !credentialsString.isBlank()
+        ? 
fromList(Splitter.on(';').trimResults().splitToList(credentialsString))
+        : EMPTY;
+  }
+
+  /**
+   * Parse a list of credentials; each element should be in the format: {@code
+   * realm,clientId,clientSecret}.
+   */
+  static RootCredentialsSet fromList(List<String> credentialsList) {
+    Map<String, RootCredentials> credentials = new HashMap<>();
+    for (String triplet : credentialsList) {
+      if (!triplet.isBlank()) {
+        List<String> parts = 
Splitter.on(',').trimResults().splitToList(triplet);
+        if (parts.size() != 3) {
+          throw new IllegalArgumentException("Invalid credentials format: " + 
triplet);
+        }
+        String realm = parts.get(0);
+        RootCredentials creds = ImmutableRootCredentials.of(parts.get(1), 
parts.get(2));
+        if (credentials.containsKey(realm)) {
+          throw new IllegalArgumentException("Duplicate realm: " + realm);
+        }
+        credentials.put(realm, creds);
+      }
+    }
+    return credentials.isEmpty() ? EMPTY : 
ImmutableRootCredentialsSet.of(credentials);
+  }
+
+  /**
+   * Parse credentials set from any URL containing a valid YAML or JSON 
credentials file.
+   *
+   * <p>The expected YAML format is:
+   *
+   * <pre>
+   * realm1:
+   *   client-id: client1
+   *   client-secret: secret1
+   * realm2:
+   *   client-id: client2
+   *   client-secret: secret2
+   * # etc.
+   * </pre>
+   *
+   * Multiple YAMl documents are also supported; all documents will be merged 
into a single set of
+   * credentials.
+   *
+   * <p>The expected JSON format is:
+   *
+   * <pre>
+   * {
+   *   "realm1": {
+   *     "client-id": "client1",
+   *     "client-secret": "secret1"
+   *   },
+   *   "realm2": {
+   *     "client-id": "client2",
+   *     "client-secret": "secret2"
+   *   }
+   * }
+   * </pre>
+   */
+  static RootCredentialsSet fromUrl(URL url) {
+    YAMLFactory factory = new YAMLFactory();
+    ObjectMapper mapper = new 
ObjectMapper(factory).configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
+    try (var parser = factory.createParser(url)) {
+      var values = mapper.readValues(parser, RootCredentialsSet.class);
+      var builder = ImmutableRootCredentialsSet.builder();
+      while (values.hasNext()) {
+        builder.putAllCredentials(values.next().credentials());
+      }
+      return builder.build();
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Failed to read credentials file: " + 
url, e);
+    }
+  }
+
+  @JsonAnyGetter
+  @JsonAnySetter
+  @Value.Parameter(order = 0)
+  Map<String, RootCredentials> credentials();

Review Comment:
   nit: javadoc about map key being the realm ID



-- 
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