sungwy commented on code in PR #3332: URL: https://github.com/apache/polaris/pull/3332#discussion_r2646315504
########## 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: That's a great suggestion - I'll adopt this in my next iteration -- 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]
