dimas-b commented on code in PR #3332: URL: https://github.com/apache/polaris/pull/3332#discussion_r2646299991
########## extensions/auth/opa/tests/src/intTest/java/org/apache/polaris/extension/auth/opa/test/OpaAdminServiceIT.java: ########## @@ -0,0 +1,428 @@ +/* + * 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.extension.auth.opa.test; + +import static io.restassured.RestAssured.given; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.http.ContentType; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * OPA authorization coverage for management endpoints: + * + * <ul> + * <li>Catalog list/create + * <li>Principal-role to catalog-role bindings + * <li>Catalog-role grant management + * <li>Catalog-role assignee listings + * <li>Catalog-role grant listings + * </ul> + */ +@QuarkusTest +@TestProfile(OpaIntegrationTest.StaticTokenOpaProfile.class) +public class OpaAdminServiceIT extends OpaIntegrationTestBase { + + private String baseCatalogName; + private String baseCatalogLocation; + private String baseRootToken; + + @BeforeEach + void setupBaseCatalog() throws Exception { + baseRootToken = getRootToken(); + baseCatalogName = "opa-base-cat-" + UUID.randomUUID().toString().replace("-", ""); + baseCatalogLocation = Files.createTempDirectory("opa-base-cat").toUri().toString(); + createFileCatalog( + baseRootToken, baseCatalogName, baseCatalogLocation, List.of(baseCatalogLocation)); + } + + @Test + void assignCatalogRoleToPrincipalRole() { + String rootToken = baseRootToken; + String strangerToken = createPrincipalAndGetToken("stranger-" + UUID.randomUUID()); + + String catalogRole = "opa-cat-role-" + UUID.randomUUID().toString().replace("-", ""); + String principalRole = "opa-pr-role-" + UUID.randomUUID().toString().replace("-", ""); + + // create catalog role + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(Map.of("name", catalogRole, "properties", Map.of()))) + .post("/api/management/v1/catalogs/{cat}/catalog-roles", baseCatalogName) + .then() + .statusCode(201); + + // create principal role + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(Map.of("name", principalRole, "properties", Map.of()))) + .post("/api/management/v1/principal-roles") + .then() + .statusCode(201); + + Map<String, Object> grantRequest = + Map.of("catalogRole", Map.of("name", catalogRole, "properties", Map.of())); + + // stranger cannot bind + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + strangerToken) + .body(toJson(grantRequest)) + .put( + "/api/management/v1/principal-roles/{pr}/catalog-roles/{cat}", + principalRole, + baseCatalogName) + .then() + .statusCode(403); + + // root binds successfully + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(grantRequest)) + .put( + "/api/management/v1/principal-roles/{pr}/catalog-roles/{cat}", Review Comment: The success of this call under the OPA authorizer makes me think whether we may actually want to forbid it 🤔 Is it meaningful to assign catalog roles to principal roles when RBAC is external? ########## extensions/auth/opa/tests/src/intTest/java/org/apache/polaris/extension/auth/opa/test/OpaIntegrationTest.java: ########## @@ -50,6 +58,11 @@ public Map<String, String> getConfigOverrides() { config.put( "polaris.authorization.opa.auth.bearer.static-token.value", "test-opa-bearer-token-12345"); + config.put( + "polaris.features.\"SUPPORTED_CATALOG_STORAGE_TYPES\"", + "[\"FILE\",\"S3\",\"GCS\",\"AZURE\"]"); Review Comment: I guess we only need `FILE` here? ########## extensions/auth/opa/tests/src/intTest/java/org/apache/polaris/extension/auth/opa/test/OpaIcebergCatalogHandlerIT.java: ########## @@ -0,0 +1,155 @@ +/* + * 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.extension.auth.opa.test; + +import static io.restassured.RestAssured.given; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.http.ContentType; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * OPA authorization coverage for Iceberg catalog endpoints: + * + * <ul> + * <li>List namespaces + * <li>Register/create table + * <li>Drop table + * </ul> + */ +@QuarkusTest +@TestProfile(OpaIntegrationTest.StaticTokenOpaProfile.class) +public class OpaIcebergCatalogHandlerIT extends OpaIntegrationTestBase { + + private String catalogName; + private String namespace; + private String baseLocation; + private String rootToken; + + @BeforeEach + void setupBaseCatalog() throws Exception { + rootToken = getRootToken(); + catalogName = "opa-iceberg-" + UUID.randomUUID().toString().replace("-", ""); + namespace = "ns_" + UUID.randomUUID().toString().replace("-", ""); + Path tempDir = Files.createTempDirectory("opa-iceberg"); Review Comment: nit: maybe use `@TempDir` params injected by JUnit5 so that the test framework would own all temporary files? ########## extensions/auth/opa/tests/src/intTest/java/org/apache/polaris/extension/auth/opa/test/OpaIntegrationTestBase.java: ########## @@ -21,12 +21,42 @@ import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.fail; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.restassured.RestAssured; +import io.restassured.config.ObjectMapperConfig; +import io.restassured.http.ContentType; +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; + /** * Base class for OPA integration tests providing common helper methods for authentication and * principal management. */ public abstract class OpaIntegrationTestBase { + private static final ObjectMapper JSON = new ObjectMapper(); Review Comment: Could you switch to builders following #3269 ? ########## extensions/auth/opa/tests/src/intTest/java/org/apache/polaris/extension/auth/opa/test/OpaAdminServiceIT.java: ########## @@ -0,0 +1,428 @@ +/* + * 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.extension.auth.opa.test; + +import static io.restassured.RestAssured.given; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.http.ContentType; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * OPA authorization coverage for management endpoints: + * + * <ul> + * <li>Catalog list/create + * <li>Principal-role to catalog-role bindings + * <li>Catalog-role grant management + * <li>Catalog-role assignee listings + * <li>Catalog-role grant listings + * </ul> + */ +@QuarkusTest +@TestProfile(OpaIntegrationTest.StaticTokenOpaProfile.class) +public class OpaAdminServiceIT extends OpaIntegrationTestBase { + + private String baseCatalogName; + private String baseCatalogLocation; + private String baseRootToken; + + @BeforeEach + void setupBaseCatalog() throws Exception { + baseRootToken = getRootToken(); + baseCatalogName = "opa-base-cat-" + UUID.randomUUID().toString().replace("-", ""); + baseCatalogLocation = Files.createTempDirectory("opa-base-cat").toUri().toString(); + createFileCatalog( + baseRootToken, baseCatalogName, baseCatalogLocation, List.of(baseCatalogLocation)); + } + + @Test + void assignCatalogRoleToPrincipalRole() { + String rootToken = baseRootToken; + String strangerToken = createPrincipalAndGetToken("stranger-" + UUID.randomUUID()); + + String catalogRole = "opa-cat-role-" + UUID.randomUUID().toString().replace("-", ""); + String principalRole = "opa-pr-role-" + UUID.randomUUID().toString().replace("-", ""); + + // create catalog role + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(Map.of("name", catalogRole, "properties", Map.of()))) + .post("/api/management/v1/catalogs/{cat}/catalog-roles", baseCatalogName) + .then() + .statusCode(201); + + // create principal role + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(Map.of("name", principalRole, "properties", Map.of()))) + .post("/api/management/v1/principal-roles") + .then() + .statusCode(201); + + Map<String, Object> grantRequest = + Map.of("catalogRole", Map.of("name", catalogRole, "properties", Map.of())); + + // stranger cannot bind + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + strangerToken) + .body(toJson(grantRequest)) + .put( + "/api/management/v1/principal-roles/{pr}/catalog-roles/{cat}", + principalRole, + baseCatalogName) + .then() + .statusCode(403); + + // root binds successfully + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body(toJson(grantRequest)) + .put( + "/api/management/v1/principal-roles/{pr}/catalog-roles/{cat}", + principalRole, + baseCatalogName) + .then() + .statusCode(201); + } + + @Test + void listCatalogsAuthorization() { + String rootToken = baseRootToken; + String strangerToken = createPrincipalAndGetToken("stranger-" + UUID.randomUUID()); + + // stranger cannot list catalogs + given() + .header("Authorization", "Bearer " + strangerToken) + .get("/api/management/v1/catalogs") + .then() + .statusCode(403); + + // root lists catalogs successfully + given() + .header("Authorization", "Bearer " + rootToken) + .get("/api/management/v1/catalogs") + .then() + .statusCode(200); + } + + @Test + void createCatalogAuthorization() throws Exception { + String rootToken = getRootToken(); + String strangerToken = createPrincipalAndGetToken("stranger-" + UUID.randomUUID()); + + String catalogName = "opa-cat-create-" + UUID.randomUUID().toString().replace("-", ""); + String baseLocation = + java.nio.file.Files.createTempDirectory("opa-cat-create").toUri().toString(); + + // Stranger cannot create catalog + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + strangerToken) + .body( + toJson( + Map.of( + "type", + "INTERNAL", + "name", + "unauth-" + catalogName, + "properties", + Map.of("default-base-location", baseLocation), + "storageConfigInfo", + Map.of("storageType", "FILE", "allowedLocations", List.of(baseLocation))))) + .post("/api/management/v1/catalogs") + .then() + .statusCode(403); + + // Root creates catalog + given() + .contentType(ContentType.JSON) + .header("Authorization", "Bearer " + rootToken) + .body( Review Comment: successful and failed requests are identical in all aspects but the auth token, right? Would you mind refactoring these tests to make it explicit (at least share the payload maybe)? -- 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]
