Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14902] test(lance-rest): Cover overwrite authorization for create-table and register-table (#14933)

Srihari Srithare23b85f9d8ee
diff --git a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceModeParsing.java b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceModeParsing.java
index ee144208a2..5688be6085 100644
--- a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceModeParsing.java
+++ b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceModeParsing.java
@@ -185,6 +185,37 @@ class TestGravitinoLanceModeParsing {
exception.getMessage().contains("Unknown register table mode: #register$"));
}
+ @Test
+ void testClientPropertyCannotOverrideAuthorizedMode() {
+ // The REST interceptor authorizes an overwrite from the request mode alone. A caller-supplied
+ // lance.creation-mode table property must therefore never reach the catalog in place of that
+ // mode, or CREATE_TABLE plus a property would be enough to replace an existing table.
+ TableCatalog tableCatalog = Mockito.mock(TableCatalog.class);
+ Table table = Mockito.mock(Table.class);
+ when(table.properties()).thenReturn(Map.of(LANCE_LOCATION, "/tmp/table"));
+ when(tableCatalog.createTable(
+ any(NameIdentifier.class), any(Column[].class), isNull(), anyMap()))
+ .thenReturn(table);
+ GravitinoLanceTableOperations operations = newTableOperations(tableCatalog);
+ Map<String, String> smuggled = Map.of(LANCE_CREATION_MODE, "OVERWRITE");
+
+ operations.createTable("catalog.schema.table", "create", ".", null, smuggled, null);
+ operations.declareTable("catalog.schema.table", ".", null, smuggled);
+ operations.registerTable(
+ "catalog.schema.table",
+ "create",
+ ".",
+ Map.of(LANCE_LOCATION, "/tmp/table", LANCE_CREATION_MODE, "OVERWRITE"));
+
+ ArgumentCaptor<Map<String, String>> propertiesCaptor = propertiesCaptor();
+ Mockito.verify(tableCatalog, Mockito.times(3))
+ .createTable(
+ any(NameIdentifier.class), any(Column[].class), isNull(), propertiesCaptor.capture());
+ for (Map<String, String> properties : propertiesCaptor.getAllValues()) {
+ Assertions.assertEquals("CREATE", properties.get(LANCE_CREATION_MODE));
+ }
+ }
+
@Test
void testCreateTableReturnsPropertiesAndStorageOptions() {
TableCatalog tableCatalog = Mockito.mock(TableCatalog.class);
diff --git a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/authorization/TestLanceMetadataAuthorizationMethodInterceptor.java b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/authorization/TestLanceMetadataAuthorizationMethodInterceptor.java
index 5c30b511cd..d24a846783 100644
--- a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/authorization/TestLanceMetadataAuthorizationMethodInterceptor.java
+++ b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/authorization/TestLanceMetadataAuthorizationMethodInterceptor.java
@@ -374,6 +374,29 @@ class TestLanceMetadataAuthorizationMethodInterceptor {
Response.Status.FORBIDDEN);
}
+ @Test
+ void testTableOverwriteIsRecognizedWhateverSpacingTheClientSends() throws Throwable {
+ allow(Privilege.Name.USE_CATALOG, Privilege.Name.USE_SCHEMA, Privilege.Name.CREATE_TABLE);
+
+ // Create-table and register-table read their mode through CommonUtil.normalizeToken, so each
+ // of these reaches the catalog as OVERWRITE. Authorization must read it the same way, or
+ // CREATE_TABLE alone would be enough to replace a table owned by somebody else.
+ for (String mode : new String[] {" overwrite", "overwrite ", " OverWrite ", "\toverwrite\n"}) {
+ MethodInvocation create = createTableInvocation(tableId(), mode);
+ assertErrorResponse(
+ interceptor.invoke(create),
+ Response.Status.FORBIDDEN,
+ "create-table mode '" + mode + "' must be authorized as an overwrite");
+ verify(create, never()).proceed();
+ MethodInvocation register = registerTableInvocation(tableId(), mode);
+ assertErrorResponse(
+ interceptor.invoke(register),
+ Response.Status.FORBIDDEN,
+ "register-table mode '" + mode + "' must be authorized as an overwrite");
+ verify(register, never()).proceed();
+ }
+ }
+
@Test
void testTableOverwriteRejectsIdentifiersOfTheWrongDepth() throws Throwable {
when(authorizer.isOwner(any(), any(), any(), any())).thenReturn(true);