sungwy commented on code in PR #2680: URL: https://github.com/apache/polaris/pull/2680#discussion_r2417984284
########## polaris-core/src/main/java/org/apache/polaris/core/auth/OpaPolarisAuthorizer.java: ########## @@ -0,0 +1,434 @@ +/* + * 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; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.base.Strings; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyStore; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import javax.net.ssl.SSLContext; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; +import org.apache.hc.client5.http.ssl.TrustAllStrategy; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.ssl.SSLContextBuilder; +import org.apache.hc.core5.util.Timeout; +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. + * + * <p><strong>Beta Feature:</strong> This implementation is currently in Beta and is not a stable + * release. It may undergo breaking changes in future versions. Use with caution in production + * environments. + */ +public class OpaPolarisAuthorizer implements PolarisAuthorizer { + private final String opaServerUrl; + private final String opaPolicyPath; + private final BearerTokenProvider tokenProvider; + private final CloseableHttpClient httpClient; + private final ObjectMapper objectMapper; + + /** Private constructor for factory method and advanced wiring. */ + private OpaPolarisAuthorizer( + String opaServerUrl, + String opaPolicyPath, + BearerTokenProvider 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 BearerTokenProvider 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) + * @return OpaPolarisAuthorizer instance + */ + public static OpaPolarisAuthorizer create( + String opaServerUrl, + String opaPolicyPath, + BearerTokenProvider tokenProvider, + int timeoutMs, + boolean verifySsl, + String trustStorePath, + String trustStorePassword, + Object client) { + + if (Strings.isNullOrEmpty(opaServerUrl)) { + throw new IllegalArgumentException("opaServerUrl cannot be null or empty"); + } + if (Strings.isNullOrEmpty(opaPolicyPath)) { + throw new IllegalArgumentException("opaPolicyPath cannot be null or empty"); + } + + try { + // Create request configuration with timeouts + RequestConfig requestConfig = + RequestConfig.custom() + .setConnectTimeout(Timeout.ofMilliseconds(timeoutMs)) + .setResponseTimeout(Timeout.ofMilliseconds(timeoutMs)) + .setConnectionRequestTimeout(Timeout.ofMilliseconds(timeoutMs)) + .build(); + + // Configure SSL for HTTPS connections + SSLConnectionSocketFactory sslSocketFactory = + createSslSocketFactory(opaServerUrl, verifySsl, trustStorePath, trustStorePassword); Review Comment: Yes, I'm already refactoring this as per @flyrain 's comment. It looks like I'll need to introduce an `OpaHttpClientFactory` anyways within this PR to clean this up. -- 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]
