dimas-b commented on code in PR #2680:
URL: https://github.com/apache/polaris/pull/2680#discussion_r2417915432
##########
runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java:
##########
@@ -244,6 +254,59 @@ public StsClientsPool stsClientsPool(
return new StsClientsPool(config.effectiveClientsCacheMaxSize(),
httpClient, meterRegistry);
}
+ @Produces
+ @Singleton
+ @Identifier("opa-http-client")
+ public CloseableHttpClient opaHttpClient() {
+ return HttpClients.custom().build();
+ }
+
+ public void closeOpaHttpClient(
+ @Disposes @Identifier("opa-http-client") CloseableHttpClient client) {
+ try {
+ client.close();
+ } catch (Exception e) {
+ LOGGER.warn("Error closing OPA HTTP client", e);
+ }
+ }
+
+ @Produces
+ @Singleton
+ @Identifier("opa-bearer-token-provider")
+ public BearerTokenProvider opaBearerTokenProvider(
Review Comment:
I wonder it this producer could be moved into `OpaPolarisAuthorizerFactory`.
It should only be required when that factory is active.
##########
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:
This will be called for every request (REST API). I'm not sure how heavy is
the socket factory, but would it make sense to keep it in
`OpaPolarisAuthorizerFactory` (to be shared across `OpaPolarisAuthorizer`
instances)?
##########
runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java:
##########
@@ -244,6 +254,59 @@ public StsClientsPool stsClientsPool(
return new StsClientsPool(config.effectiveClientsCacheMaxSize(),
httpClient, meterRegistry);
}
+ @Produces
+ @Singleton
+ @Identifier("opa-http-client")
+ public CloseableHttpClient opaHttpClient() {
+ return HttpClients.custom().build();
+ }
+
+ public void closeOpaHttpClient(
+ @Disposes @Identifier("opa-http-client") CloseableHttpClient client) {
+ try {
+ client.close();
+ } catch (Exception e) {
+ LOGGER.warn("Error closing OPA HTTP client", e);
+ }
+ }
+
+ @Produces
+ @Singleton
+ @Identifier("opa-bearer-token-provider")
+ public BearerTokenProvider opaBearerTokenProvider(
Review Comment:
Side note: if we take OPA out or core, it would be nice to avoid direct
references to it here as well and rely only on CDI to find the right classes
via annotations.
##########
runtime/service/src/main/java/org/apache/polaris/service/config/AuthorizationConfiguration.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.service.config;
+
+import com.google.common.base.Strings;
+import io.smallrye.config.ConfigMapping;
+import io.smallrye.config.WithDefault;
+import java.util.Optional;
+
+@ConfigMapping(prefix = "polaris.authorization")
+public interface AuthorizationConfiguration {
+ @WithDefault("default")
+ String type();
+
+ OpaConfig opa();
Review Comment:
If we move OPA out of core, let's create a separate config interface for it
(with prefix `polaris.authorization.opa`). This class will only declare
`type()` (which is common for all authorizers).
##########
runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java:
##########
@@ -244,6 +254,59 @@ public StsClientsPool stsClientsPool(
return new StsClientsPool(config.effectiveClientsCacheMaxSize(),
httpClient, meterRegistry);
}
+ @Produces
+ @Singleton
+ @Identifier("opa-http-client")
+ public CloseableHttpClient opaHttpClient() {
+ return HttpClients.custom().build();
+ }
+
+ public void closeOpaHttpClient(
+ @Disposes @Identifier("opa-http-client") CloseableHttpClient client) {
+ try {
+ client.close();
+ } catch (Exception e) {
+ LOGGER.warn("Error closing OPA HTTP client", e);
+ }
+ }
+
+ @Produces
+ @Singleton
+ @Identifier("opa-bearer-token-provider")
+ public BearerTokenProvider opaBearerTokenProvider(
+ AuthorizationConfiguration authorizationConfig) {
+ AuthorizationConfiguration.OpaConfig opa = authorizationConfig.opa();
+ AuthorizationConfiguration.BearerTokenConfig bearerToken =
opa.bearerToken();
+
+ // Validate configuration before using it
+ bearerToken.validate();
+
+ // Check if bearer token authentication is enabled
+ if (!bearerToken.enabled()) {
+ return new StaticBearerTokenProvider("");
+ }
+
+ // Static token takes precedence
+ if (bearerToken.staticValue().isPresent()) {
+ return new StaticBearerTokenProvider(bearerToken.staticValue().get());
+ }
+
+ // File-based token as fallback
+ if (bearerToken.filePath().isPresent()) {
+ java.time.Duration refreshInterval =
+ java.time.Duration.ofSeconds(bearerToken.refreshInterval());
+ boolean jwtExpirationRefresh = bearerToken.jwtExpirationRefresh();
+ java.time.Duration jwtExpirationBuffer =
Review Comment:
nit: import?
##########
polaris-core/build.gradle.kts:
##########
@@ -24,6 +24,7 @@ plugins {
dependencies {
implementation(project(":polaris-api-management-model"))
+ implementation(libs.apache.httpclient5)
Review Comment:
I think it might be preferable to move OPA stuff out of "core". I propose
`extensions/opa` (include into `runtime/server`).
Rationale:
1) the new authorizer is pluggable, but not all downstream projects may want
to have it by default.
2) reducing core dependencies (IIRC, Quarkus runtime already has
`httpclient5`, but core does not have to depend on it).
##########
runtime/service/src/test/java/org/apache/polaris/service/auth/OpaFileTokenIntegrationTest.java:
##########
@@ -0,0 +1,307 @@
+/*
+ * 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.service.auth;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.QuarkusTestProfile.TestResourceEntry;
+import io.quarkus.test.junit.TestProfile;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.polaris.test.commons.OpaTestResource;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+@TestProfile(OpaFileTokenIntegrationTest.FileTokenOpaProfile.class)
+public class OpaFileTokenIntegrationTest {
+
+ public static class FileTokenOpaProfile implements QuarkusTestProfile {
+ private static volatile Path tokenFile;
+
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ Map<String, String> config = new HashMap<>();
+ config.put("polaris.authorization.type", "opa");
+ config.put("polaris.authorization.opa.policy-path",
"/v1/data/polaris/authz");
+ config.put("polaris.authorization.opa.timeout-ms", "2000");
+
+ // Create temporary token file for testing
+ try {
+ tokenFile = Files.createTempFile("opa-test-token", ".txt");
+ Files.writeString(tokenFile, "test-opa-bearer-token-from-file-67890");
+ tokenFile.toFile().deleteOnExit();
Review Comment:
nit: this is not guaranteed to delete the file, AFAIK. Plus the "JVM" may be
long running, e.g. a Gradle process.
It might be best to manage this as a `QuarkusTestResourceLifecycleManager`,
which can produce extra server config entries.
##########
runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java:
##########
@@ -244,6 +254,59 @@ public StsClientsPool stsClientsPool(
return new StsClientsPool(config.effectiveClientsCacheMaxSize(),
httpClient, meterRegistry);
}
+ @Produces
+ @Singleton
+ @Identifier("opa-http-client")
+ public CloseableHttpClient opaHttpClient() {
Review Comment:
`@Singleton` might be an overkill. It is only needed when OPA is active. It
should be a dependent bean, I think.
Also, `OpaPolarisAuthorizerFactory` might be able to produce / dispose of
this bean. Beans can produce other beans, AFAIK.
If dependent beans prove to be too hard to manage, perhaps we can keep the
client as a field in the factory and dispose of it together with the factory.
##########
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);
+
+ // Create HTTP client with SSL configuration
+ CloseableHttpClient httpClient;
+ if (client instanceof CloseableHttpClient) {
+ httpClient = (CloseableHttpClient) client;
+ } else {
+ if (sslSocketFactory != null) {
+ httpClient =
+ HttpClients.custom()
+ .setDefaultRequestConfig(requestConfig)
+ .setConnectionManager(
+ PoolingHttpClientConnectionManagerBuilder.create()
+ .setSSLSocketFactory(sslSocketFactory)
+ .build())
+ .build();
+ } else {
+ httpClient =
HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
+ }
+ }
+
+ ObjectMapper objectMapperWithDefaults = new ObjectMapper();
+ return new OpaPolarisAuthorizer(
+ opaServerUrl, opaPolicyPath, tokenProvider, httpClient,
objectMapperWithDefaults);
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to create OpaPolarisAuthorizer with
SSL configuration", e);
+ }
+ }
+
+ /**
+ * Authorizes a single target and secondary entity for the given principal
and operation.
+ *
+ * <p>Delegates to the multi-target version for consistency.
+ *
+ * @param polarisPrincipal the principal requesting authorization
+ * @param activatedEntities the set of activated entities (roles, etc.)
+ * @param authzOp the operation to authorize
+ * @param target the main target entity
+ * @param secondary the secondary entity (if any)
+ * @throws ForbiddenException if authorization is denied by OPA
+ */
+ @Override
+ public void authorizeOrThrow(
+ @Nonnull PolarisPrincipal polarisPrincipal,
+ @Nonnull Set<PolarisBaseEntity> activatedEntities,
+ @Nonnull PolarisAuthorizableOperation authzOp,
+ @Nullable PolarisResolvedPathWrapper target,
+ @Nullable PolarisResolvedPathWrapper secondary) {
+ authorizeOrThrow(
+ polarisPrincipal,
+ activatedEntities,
+ authzOp,
+ target == null ? null : List.of(target),
+ secondary == null ? null : List.of(secondary));
+ }
+
+ /**
+ * Authorizes one or more target and secondary entities for the given
principal and operation.
+ *
+ * <p>Sends the authorization context to OPA and throws if not allowed.
+ *
+ * @param polarisPrincipal the principal requesting authorization
+ * @param activatedEntities the set of activated entities (roles, etc.)
+ * @param authzOp the operation to authorize
+ * @param targets the list of main target entities
+ * @param secondaries the list of secondary entities (if any)
+ * @throws ForbiddenException if authorization is denied by OPA
+ */
+ @Override
+ public void authorizeOrThrow(
+ @Nonnull PolarisPrincipal polarisPrincipal,
+ @Nonnull Set<PolarisBaseEntity> activatedEntities,
+ @Nonnull PolarisAuthorizableOperation authzOp,
+ @Nullable List<PolarisResolvedPathWrapper> targets,
+ @Nullable List<PolarisResolvedPathWrapper> secondaries) {
+ boolean allowed = queryOpa(polarisPrincipal, activatedEntities, authzOp,
targets, secondaries);
+ if (!allowed) {
+ throw new ForbiddenException("OPA denied authorization");
+ }
+ }
+
+ /**
+ * Sends an authorization query to the OPA server and parses the response.
+ *
+ * <p>Builds the OPA input JSON, sends it via HTTP POST, and checks the
'allow' field in the
+ * response. The request format follows the OPA REST API specification for
data queries.
+ *
+ * @param principal the principal requesting authorization
+ * @param entities the set of activated entities
+ * @param op the operation to authorize
+ * @param targets the list of main target entities
+ * @param secondaries the list of secondary entities (if any)
+ * @return true if OPA allows the operation, false otherwise
+ * @throws RuntimeException if the OPA query fails
+ * @see <a href="https://www.openpolicyagent.org/docs/rest-api">OPA REST API
Documentation</a>
+ */
+ private boolean queryOpa(
+ PolarisPrincipal principal,
+ Set<PolarisBaseEntity> entities,
+ PolarisAuthorizableOperation op,
+ List<PolarisResolvedPathWrapper> targets,
+ List<PolarisResolvedPathWrapper> secondaries) {
+ try {
+ String inputJson = buildOpaInputJson(principal, entities, op, targets,
secondaries);
+
+ // Create HTTP POST request using Apache HttpComponents
+ HttpPost httpPost = new HttpPost(opaServerUrl + opaPolicyPath);
+ httpPost.setHeader("Content-Type", "application/json");
+
+ // Add bearer token authentication if provided
+ if (tokenProvider != null) {
+ String token = tokenProvider.getToken();
+ if (token != null && !token.isEmpty()) {
+ httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
+ }
+ }
+
+ httpPost.setEntity(new StringEntity(inputJson,
ContentType.APPLICATION_JSON));
+
+ // Execute request
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
+ int statusCode = response.getCode();
+ if (statusCode != 200) {
+ return false;
+ }
+
+ // Read and parse response
+ String responseBody;
+ try {
+ responseBody = EntityUtils.toString(response.getEntity());
+ } catch (ParseException e) {
+ throw new RuntimeException("Failed to parse OPA response", e);
+ }
+ ObjectNode respNode = (ObjectNode) objectMapper.readTree(responseBody);
+ return respNode.path("result").path("allow").asBoolean(false);
+ }
+ } catch (IOException e) {
+ throw new RuntimeException("OPA query failed", e);
+ }
+ }
+
+ /**
+ * Builds the OPA input JSON for the authorization query.
+ *
+ * <p>Assembles the actor, action, resource, and context sections into the
expected OPA input
+ * format.
+ *
+ * <p><strong>Note:</strong> OpaPolarisAuthorizer bypasses Polaris's
built-in role-based
+ * authorization system. This includes both principal roles and catalog
roles that would normally
+ * be processed by Polaris. Instead, authorization decisions are delegated
entirely to the
+ * configured OPA policies, which receive the raw principal information and
must implement their
+ * own role/permission logic.
+ *
+ * @param principal the principal requesting authorization
+ * @param entities the set of activated entities
+ * @param op the operation to authorize
+ * @param targets the list of main target entities
+ * @param secondaries the list of secondary entities (if any)
+ * @return the OPA input JSON string
+ * @throws IOException if JSON serialization fails
+ */
+ private String buildOpaInputJson(
+ PolarisPrincipal principal,
+ Set<PolarisBaseEntity> entities,
+ PolarisAuthorizableOperation op,
+ List<PolarisResolvedPathWrapper> targets,
+ List<PolarisResolvedPathWrapper> secondaries)
+ throws IOException {
+ ObjectNode input = objectMapper.createObjectNode();
+ input.set("actor", buildActorNode(principal));
+ input.put("action", op.name());
+ input.set("resource", buildResourceNode(targets, secondaries));
+ input.set("context", buildContextNode());
+ ObjectNode root = objectMapper.createObjectNode();
+ root.set("input", input);
+ return objectMapper.writeValueAsString(root);
+ }
+
+ /**
+ * Builds the actor section of the OPA input JSON.
+ *
+ * <p>Includes principal name, and roles as a generic field.
+ *
+ * @param principal the principal requesting authorization
+ * @return the actor node for OPA input
+ */
+ private ObjectNode buildActorNode(PolarisPrincipal principal) {
+ ObjectNode actor = objectMapper.createObjectNode();
+ actor.put("principal", principal.getName());
+ ArrayNode roles = objectMapper.createArrayNode();
+ for (String role : principal.getRoles()) roles.add(role);
+ actor.set("roles", roles);
+ return actor;
+ }
+
+ /**
+ * Builds the resource section of the OPA input JSON.
+ *
+ * <p>Includes the main target entity under 'primary' and secondary entities
under 'secondaries'.
+ *
+ * @param targets the list of main target entities
+ * @param secondaries the list of secondary entities
+ * @return the resource node for OPA input
+ */
+ private ObjectNode buildResourceNode(
+ List<PolarisResolvedPathWrapper> targets,
List<PolarisResolvedPathWrapper> secondaries) {
+ ObjectNode resource = objectMapper.createObjectNode();
+ // Main targets as 'targets' array
+ ArrayNode targetsArray = objectMapper.createArrayNode();
+ if (targets != null && !targets.isEmpty()) {
+ for (PolarisResolvedPathWrapper targetWrapper : targets) {
+ targetsArray.add(buildSingleResourceNode(targetWrapper));
+ }
+ }
+ resource.set("targets", targetsArray);
+ // Secondaries as array
+ ArrayNode secondariesArray = objectMapper.createArrayNode();
+ if (secondaries != null && !secondaries.isEmpty()) {
+ for (PolarisResolvedPathWrapper secondaryWrapper : secondaries) {
+ secondariesArray.add(buildSingleResourceNode(secondaryWrapper));
+ }
+ }
+ resource.set("secondaries", secondariesArray);
+ return resource;
+ }
+
+ /** Helper to build a resource node for a single PolarisResolvedPathWrapper.
*/
+ private ObjectNode buildSingleResourceNode(PolarisResolvedPathWrapper
wrapper) {
+ ObjectNode node = objectMapper.createObjectNode();
+ if (wrapper == null) return node;
+ var resolvedEntity = wrapper.getResolvedLeafEntity();
+ if (resolvedEntity != null) {
+ var entity = resolvedEntity.getEntity();
+ node.put("type", entity.getType().name());
+ node.put("name", entity.getName());
+ var parentPath = wrapper.getResolvedParentPath();
+ if (parentPath != null && !parentPath.isEmpty()) {
+ ArrayNode parentsArray = objectMapper.createArrayNode();
+ for (var parent : parentPath) {
+ ObjectNode parentNode = objectMapper.createObjectNode();
+ parentNode.put("type", parent.getEntity().getType().name());
+ parentNode.put("name", parent.getEntity().getName());
+ parentsArray.add(parentNode);
+ }
+ node.set("parents", parentsArray);
+ }
+ }
+ return node;
+ }
+
+ /**
+ * Builds the context section of the OPA input JSON.
+ *
+ * <p>Includes only timestamp and request ID.
+ *
+ * @return the context node for OPA input
+ */
+ private ObjectNode buildContextNode() {
+ ObjectNode context = objectMapper.createObjectNode();
+ context.put("time", java.time.ZonedDateTime.now().toString());
+ context.put("request_id", java.util.UUID.randomUUID().toString());
+ return context;
+ }
+
+ /**
+ * Creates an SSL socket factory for HTTPS connections based on the
configuration.
+ *
+ * @param opaServerUrl the OPA server URL
+ * @param verifySsl whether to verify SSL certificates
+ * @param trustStorePath custom trust store path (optional)
+ * @param trustStorePassword custom trust store password (optional)
+ * @return SSLConnectionSocketFactory for HTTPS connections, or null for HTTP
+ * @throws Exception if SSL configuration fails
+ */
+ private static SSLConnectionSocketFactory createSslSocketFactory(
Review Comment:
`SSLConnectionSocketFactory` appears to be deprecated 🤔 Should we use
`DefaultClientTlsStrategy` as suggested by its javadoc?
--
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]