sungwy commented on code in PR #2680:
URL: https://github.com/apache/polaris/pull/2680#discussion_r2416911569


##########
polaris-core/src/main/java/org/apache/polaris/core/auth/OpaPolarisAuthorizer.java:
##########
@@ -0,0 +1,407 @@
+/*
+ * 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.auth;
+
+// Removed Quarkus/MicroProfile annotations for portability
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyStore;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import javax.net.ssl.SSLContext;
+import org.apache.http.HttpHeaders;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.TrustAllStrategy;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.ssl.SSLContextBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+
+/**
+ * OPA-based implementation of {@link PolarisAuthorizer}.
+ *
+ * <p>This authorizer delegates authorization decisions to an Open Policy 
Agent (OPA) server using a
+ * configurable REST API endpoint and policy path. The input to OPA is 
constructed from the
+ * principal, entities, operation, and resource context.
+ */
+public class OpaPolarisAuthorizer implements PolarisAuthorizer {
+  private final String opaServerUrl;
+  private final String opaPolicyPath;
+  private final TokenProvider tokenProvider;
+  private final CloseableHttpClient httpClient;
+  private final ObjectMapper objectMapper;
+
+  /** Private constructor for factory method and advanced wiring. */
+  private OpaPolarisAuthorizer(
+      String opaServerUrl,
+      String opaPolicyPath,
+      TokenProvider tokenProvider,
+      CloseableHttpClient httpClient,
+      ObjectMapper objectMapper) {
+    this.opaServerUrl = opaServerUrl;
+    this.opaPolicyPath = opaPolicyPath;
+    this.tokenProvider = tokenProvider;
+    this.httpClient = httpClient;
+    this.objectMapper = objectMapper;
+  }
+
+  /**
+   * Static factory that accepts a TokenProvider for advanced token management.
+   *
+   * @param opaServerUrl OPA server URL
+   * @param opaPolicyPath OPA policy path
+   * @param tokenProvider Token provider for authentication (optional)
+   * @param timeoutMs HTTP call timeout in milliseconds
+   * @param verifySsl Whether to verify SSL certificates for HTTPS connections
+   * @param trustStorePath Custom SSL trust store path (optional)
+   * @param trustStorePassword Custom SSL trust store password (optional)
+   * @param client Apache HttpClient (optional, can be null)
+   * @param mapper ObjectMapper (optional, can be null)
+   * @return OpaPolarisAuthorizer instance
+   */
+  public static OpaPolarisAuthorizer create(
+      String opaServerUrl,
+      String opaPolicyPath,
+      TokenProvider tokenProvider,
+      int timeoutMs,
+      boolean verifySsl,
+      String trustStorePath,
+      String trustStorePassword,
+      Object client, // Accept any client type for compatibility
+      ObjectMapper mapper) {
+
+    try {
+      // Create request configuration with timeouts
+      RequestConfig requestConfig =
+          RequestConfig.custom()
+              .setConnectTimeout(timeoutMs)
+              .setSocketTimeout(timeoutMs)
+              .setConnectionRequestTimeout(timeoutMs)
+              .build();
+
+      // Configure SSL for HTTPS connections
+      SSLConnectionSocketFactory sslSocketFactory =
+          createSslSocketFactory(opaServerUrl, verifySsl, trustStorePath, 
trustStorePassword);
+
+      // Create HTTP client with SSL configuration
+      CloseableHttpClient httpClient;
+      if (client instanceof CloseableHttpClient) {
+        httpClient = (CloseableHttpClient) client;
+      } else {
+        if (sslSocketFactory != null) {
+          httpClient =
+              HttpClients.custom()
+                  .setDefaultRequestConfig(requestConfig)
+                  .setSSLSocketFactory(sslSocketFactory)

Review Comment:
   Agreed, @flyrain — I’d prefer not to use 
`org.apache.iceberg.rest.HTTPClient`, since it assumes rest-prefixed 
configurations in the properties (as it’s tied to the REST catalog in Iceberg).
   
   If we plan to introduce server-specific configurations such as proxy 
settings into the HTTPClient (which I think we should), we’ll likely need to 
create a distinct HTTPClient per HTTPServer, each initialized with its own 
server-specific HTTP configuration properties (e.g., 
`polaris.authorization.opa.http.proxy-url`, etc.).
   
   I’ll start working on this change, but feel free to merge this PR if the 
current version looks good as an initial step. I’ll follow up soon with the 
enhanced HTTPClient integration and its corresponding configuration support.



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