Skip to content

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

[ISSUE-1206] improvement: Use the real user for audit information (#1258)

qqqttt123131575584b65
diff --git a/common/src/main/java/com/datastrato/gravitino/auth/AuthConstants.java b/common/src/main/java/com/datastrato/gravitino/auth/AuthConstants.java
index 34eb81bc18..bda20c47aa 100644
--- a/common/src/main/java/com/datastrato/gravitino/auth/AuthConstants.java
+++ b/common/src/main/java/com/datastrato/gravitino/auth/AuthConstants.java
@@ -13,4 +13,7 @@ public interface AuthConstants {
String AUTHORIZATION_BASIC_HEADER = "Basic ";
String ANONYMOUS_USER = "anonymous";
+
+ // Refer to the style of `AuthenticationFilter#AuthenticatedRoleAttributeName` of Apache Pulsar
+ String AUTHENTICATED_PRINCIPAL_ATTRIBUTE_NAME = AuthConstants.class.getName() + "-principal";
}
diff --git a/server-common/src/main/java/com/datastrato/gravitino/server/auth/UserPrincipal.java b/core/src/main/java/com/datastrato/gravitino/UserPrincipal.java
similarity index 95%
rename from server-common/src/main/java/com/datastrato/gravitino/server/auth/UserPrincipal.java
rename to core/src/main/java/com/datastrato/gravitino/UserPrincipal.java
index db7fbe321f..a869c1de36 100644
--- a/server-common/src/main/java/com/datastrato/gravitino/server/auth/UserPrincipal.java
+++ b/core/src/main/java/com/datastrato/gravitino/UserPrincipal.java
@@ -3,7 +3,7 @@
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino.server.auth;
+package com.datastrato.gravitino;
import com.google.common.base.Preconditions;
import java.security.Principal;
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
index 62b6b5dcfc..5c75d57aa5 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
@@ -30,6 +30,7 @@ import com.datastrato.gravitino.rel.SupportsSchemas;
import com.datastrato.gravitino.rel.TableCatalog;
import com.datastrato.gravitino.storage.IdGenerator;
import com.datastrato.gravitino.utils.IsolatedClassLoader;
+import com.datastrato.gravitino.utils.PrincipalUtils;
import com.datastrato.gravitino.utils.ThrowableFunction;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
@@ -261,7 +262,7 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
.withProperties(StringIdentifier.newPropertiesWithId(stringId, mergedConfig))
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino") /* TODO. Should change to real user */
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
.withCreateTime(Instant.now())
.build())
.build();
@@ -366,8 +367,7 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
new AuditInfo.Builder()
.withCreator(catalog.auditInfo().creator())
.withCreateTime(catalog.auditInfo().createTime())
- .withLastModifier(
- catalog.auditInfo().creator()) /* TODO. We should use real user */
+ .withLastModifier(PrincipalUtils.getCurrentPrincipal().getName())
.withLastModifiedTime(Instant.now())
.build();
newCatalogBuilder.withAuditInfo(newInfo);
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
index 6fb28d9f54..76ed6ff620 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
@@ -40,6 +40,7 @@ import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.storage.IdGenerator;
+import com.datastrato.gravitino.utils.PrincipalUtils;
import com.datastrato.gravitino.utils.ThrowableFunction;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
@@ -150,8 +151,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
.withNamespace(ident.namespace())
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino") // TODO. hardcoded as user "gravitino" for now, will
- // change to real user once user system is ready.
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
.withCreateTime(Instant.now())
.build())
.build();
@@ -262,8 +262,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
.withCreator(schemaEntity.auditInfo().creator())
.withCreateTime(schemaEntity.auditInfo().createTime())
.withLastModifier(
- "gravitino") // TODO. hardcoded as user "gravitino"
- // for now, will change to real user once user system is ready.
+ PrincipalUtils.getCurrentPrincipal().getName())
.withLastModifiedTime(Instant.now())
.build())
.build()),
@@ -455,7 +454,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
.withNamespace(ident.namespace())
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino")
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
.withCreateTime(Instant.now())
.build())
.build();
@@ -537,10 +536,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
new AuditInfo.Builder()
.withCreator(tableEntity.auditInfo().creator())
.withCreateTime(tableEntity.auditInfo().createTime())
- .withLastModifier(
- "gravitino") // hardcoded as user "gravitino" for now, will
- // change
- // to real user once user system is ready.
+ .withLastModifier(PrincipalUtils.getCurrentPrincipal().getName())
.withLastModifiedTime(Instant.now())
.build())
.build();
diff --git a/core/src/main/java/com/datastrato/gravitino/meta/MetalakeManager.java b/core/src/main/java/com/datastrato/gravitino/meta/MetalakeManager.java
index 2ff1ab8c7d..8c36811a5e 100644
--- a/core/src/main/java/com/datastrato/gravitino/meta/MetalakeManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/meta/MetalakeManager.java
@@ -16,6 +16,7 @@ import com.datastrato.gravitino.exceptions.MetalakeAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;
import com.datastrato.gravitino.storage.IdGenerator;
+import com.datastrato.gravitino.utils.PrincipalUtils;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.time.Instant;
@@ -107,7 +108,7 @@ public class MetalakeManager implements SupportsMetalakes {
.withVersion(SchemaVersion.V_0_1)
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino") /*TODO: Use real user later on. */
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
.withCreateTime(Instant.now())
.build())
.build();
diff --git a/core/src/main/java/com/datastrato/gravitino/utils/PrincipalUtils.java b/core/src/main/java/com/datastrato/gravitino/utils/PrincipalUtils.java
new file mode 100644
index 0000000000..cbaba064ec
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/utils/PrincipalUtils.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.utils;
+
+import com.datastrato.gravitino.UserPrincipal;
+import com.datastrato.gravitino.auth.AuthConstants;
+import com.google.common.base.Throwables;
+import java.security.Principal;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import javax.security.auth.Subject;
+
+@SuppressWarnings("removal")
+public class PrincipalUtils {
+ private PrincipalUtils() {}
+
+ public static <T> T doAs(Principal principal, PrivilegedExceptionAction<T> action)
+ throws Exception {
+ try {
+ Subject subject = new Subject();
+ subject.getPrincipals().add(principal);
+ return Subject.doAs(subject, action);
+ } catch (PrivilegedActionException pae) {
+ Throwable cause = pae.getCause();
+ Throwables.propagateIfPossible(cause, Exception.class);
+ throw new RuntimeException("doAs method occurs an unexpected exception", pae);
+ }
+ }
+
+ public static Principal getCurrentPrincipal() {
+ java.security.AccessControlContext context = java.security.AccessController.getContext();
+ Subject subject = Subject.getSubject(context);
+ if (subject == null || subject.getPrincipals(UserPrincipal.class).isEmpty()) {
+ return new UserPrincipal(AuthConstants.ANONYMOUS_USER);
+ }
+ return subject.getPrincipals(UserPrincipal.class).iterator().next();
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
index 4320ded444..6cd9e7fec1 100644
--- a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
+++ b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
@@ -26,6 +26,7 @@ import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.StringIdentifier;
import com.datastrato.gravitino.TestColumn;
import com.datastrato.gravitino.TestEntityStore;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.exceptions.IllegalNamespaceException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.meta.AuditInfo;
@@ -194,7 +195,7 @@ public class TestCatalogOperationDispatcher {
Assertions.assertEquals(schema.comment(), loadedSchema.comment());
testProperties(schema.properties(), loadedSchema.properties());
// Audit info is gotten from entity store
- Assertions.assertEquals("gravitino", loadedSchema.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, loadedSchema.auditInfo().creator());
// Case 2: Test if the schema is not found in entity store
doThrow(new NoSuchEntityException("mock error")).when(entityStore).get(any(), any(), any());
@@ -218,7 +219,7 @@ public class TestCatalogOperationDispatcher {
.withNamespace(Namespace.of(metalake, catalog))
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino")
+ .withCreator(AuthConstants.ANONYMOUS_USER)
.withCreateTime(Instant.now())
.build())
.build();
@@ -252,8 +253,8 @@ public class TestCatalogOperationDispatcher {
Map<String, String> expectedProps = ImmutableMap.of("k2", "v2", "k3", "v3");
testProperties(expectedProps, alteredSchema.properties());
// Audit info is gotten from gravitino entity store.
- Assertions.assertEquals("gravitino", alteredSchema.auditInfo().creator());
- Assertions.assertEquals("gravitino", alteredSchema.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredSchema.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredSchema.auditInfo().lastModifier());
// Case 2: Test if the schema is not found in entity store
doThrow(new NoSuchEntityException("mock error"))
@@ -287,7 +288,7 @@ public class TestCatalogOperationDispatcher {
.withNamespace(Namespace.of(metalake, catalog))
.withAuditInfo(
new AuditInfo.Builder()
- .withCreator("gravitino")
+ .withCreator(AuthConstants.ANONYMOUS_USER)
.withCreateTime(Instant.now())
.build())
.build();
@@ -414,7 +415,7 @@ public class TestCatalogOperationDispatcher {
Assertions.assertEquals(0, loadedTable1.partitioning().length);
Assertions.assertArrayEquals(table1.columns(), loadedTable1.columns());
// Audit info is gotten from the entity store
- Assertions.assertEquals("gravitino", loadedTable1.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, loadedTable1.auditInfo().creator());
// Case 2: Test if the table entity is not found in the entity store
reset(entityStore);
@@ -480,8 +481,8 @@ public class TestCatalogOperationDispatcher {
Map<String, String> expectedProps = ImmutableMap.of("k2", "v2", "k3", "v3");
testProperties(expectedProps, alteredTable.properties());
// Audit info is gotten from gravitino entity store
- Assertions.assertEquals("gravitino", alteredTable.auditInfo().creator());
- Assertions.assertEquals("gravitino", alteredTable.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredTable.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredTable.auditInfo().lastModifier());
// Case 2: Test if the table entity is not found in the entity store
reset(entityStore);
diff --git a/core/src/test/java/com/datastrato/gravitino/utils/TestPrincipalUtils.java b/core/src/test/java/com/datastrato/gravitino/utils/TestPrincipalUtils.java
new file mode 100644
index 0000000000..0394ea9465
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/utils/TestPrincipalUtils.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.utils;
+
+import com.datastrato.gravitino.UserPrincipal;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestPrincipalUtils {
+
+ @Test
+ public void testNormal() throws Exception {
+ UserPrincipal principal = new UserPrincipal("testNormal");
+ PrincipalUtils.doAs(
+ principal,
+ () -> {
+ Assertions.assertEquals("testNormal", PrincipalUtils.getCurrentPrincipal().getName());
+ return null;
+ });
+ }
+
+ @Test
+ public void testThread() throws Exception {
+ UserPrincipal principal = new UserPrincipal("testThread");
+ PrincipalUtils.doAs(
+ principal,
+ () -> {
+ Thread thread =
+ new Thread(
+ () ->
+ Assertions.assertEquals(
+ "testThread", PrincipalUtils.getCurrentPrincipal().getName()));
+ thread.start();
+ thread.join();
+ return null;
+ });
+ }
+}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
index c0168f73b6..06ae050989 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
@@ -30,6 +30,7 @@ import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.CatalogChange;
import com.datastrato.gravitino.MetalakeChange;
import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.catalog.hive.HiveClientPool;
import com.datastrato.gravitino.catalog.hive.HiveSchemaPropertiesMetadata;
import com.datastrato.gravitino.catalog.hive.HiveTablePropertiesMetadata;
@@ -728,14 +729,17 @@ public class CatalogHiveIT extends AbstractIT {
@Test
public void testAlterHiveTable() throws TException, InterruptedException {
ColumnDTO[] columns = createColumns();
- catalog
- .asTableCatalog()
- .createTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- columns,
- TABLE_COMMENT,
- createProperties(),
- new Partitioning[] {IdentityPartitioningDTO.of(columns[2].name())});
+ Table createdTable =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ TABLE_COMMENT,
+ createProperties(),
+ new Partitioning[] {IdentityPartitioningDTO.of(columns[2].name())});
+ Assertions.assertNull(createdTable.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, createdTable.auditInfo().creator());
Table alteredTable =
catalog
.asTableCatalog()
@@ -754,7 +758,8 @@ public class CatalogHiveIT extends AbstractIT {
TableChange.updateColumnComment(new String[] {HIVE_COL_NAME1}, "comment_new"),
TableChange.updateColumnType(
new String[] {HIVE_COL_NAME1}, Types.IntegerType.get()));
-
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredTable.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, alteredTable.auditInfo().lastModifier());
// Direct get table from hive metastore to check if the table is altered successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTab =
hiveClientPool.run(client -> client.getTable(schemaName, ALTER_TABLE_NAME));
@@ -897,12 +902,19 @@ public class CatalogHiveIT extends AbstractIT {
GravitinoMetaLake metalake = client.loadMetalake(NameIdentifier.of(metalakeName));
Catalog catalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));
- catalog
- .asSchemas()
- .alterSchema(
- ident,
- SchemaChange.removeProperty("key1"),
- SchemaChange.setProperty("key2", "val2-alter"));
+ Schema schema = catalog.asSchemas().loadSchema(ident);
+ Assertions.assertNull(schema.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, schema.auditInfo().creator());
+ schema =
+ catalog
+ .asSchemas()
+ .alterSchema(
+ ident,
+ SchemaChange.removeProperty("key1"),
+ SchemaChange.setProperty("key2", "val2-alter"));
+
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, schema.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, schema.auditInfo().creator());
Map<String, String> properties2 = catalog.asSchemas().loadSchema(ident).properties();
Assertions.assertFalse(properties2.containsKey("key1"));
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/AuditCatalogMysqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/AuditCatalogMysqlIT.java
new file mode 100644
index 0000000000..30587e8229
--- /dev/null
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/AuditCatalogMysqlIT.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.integration.test.catalog.jdbc.mysql;
+
+import com.datastrato.gravitino.Catalog;
+import com.datastrato.gravitino.CatalogChange;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.auth.AuthenticatorType;
+import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
+import com.datastrato.gravitino.client.GravitinoMetaLake;
+import com.datastrato.gravitino.dto.rel.ColumnDTO;
+import com.datastrato.gravitino.integration.test.catalog.jdbc.mysql.service.MysqlService;
+import com.datastrato.gravitino.integration.test.catalog.jdbc.utils.JdbcDriverDownloader;
+import com.datastrato.gravitino.integration.test.util.AbstractIT;
+import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
+import com.datastrato.gravitino.integration.test.util.ITUtils;
+import com.datastrato.gravitino.rel.Schema;
+import com.datastrato.gravitino.rel.Table;
+import com.datastrato.gravitino.rel.TableChange;
+import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.server.auth.OAuthConfig;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.Map;
+import org.apache.commons.lang.math.RandomUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.MySQLContainer;
+
+@Tag("gravitino-docker-it")
+public class AuditCatalogMysqlIT extends AbstractIT {
+
+ public static final String metalakeName = GravitinoITUtils.genRandomName("audit_mysql_metalake");
+ private static final String expectUser = System.getProperty("user.name");
+ public static final String DOWNLOAD_JDBC_DRIVER_URL =
+ "https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar";
+ public static final String mysqlImageName = "mysql:8.0";
+ protected static final String TEST_DB_NAME = RandomUtils.nextInt(10000) + "_test_db";
+ private static final String provider = "jdbc-mysql";
+
+ private static MysqlService mysqlService;
+ private static MySQLContainer<?> MYSQL_CONTAINER;
+ private static GravitinoMetaLake metalake;
+
+ @BeforeAll
+ public static void startIntegrationTest() throws Exception {
+ Map<String, String> configs = Maps.newHashMap();
+ configs.put(OAuthConfig.AUTHENTICATOR.getKey(), AuthenticatorType.SIMPLE.name().toLowerCase());
+ registerCustomConfigs(configs);
+ AbstractIT.startIntegrationTest();
+
+ if (!ITUtils.EMBEDDED_TEST_MODE.equals(testMode)) {
+ String gravitinoHome = System.getenv("GRAVITINO_HOME");
+ Path tmpPath = Paths.get(gravitinoHome, "/catalogs/jdbc-mysql/libs");
+ JdbcDriverDownloader.downloadJdbcDriver(DOWNLOAD_JDBC_DRIVER_URL, tmpPath.toString());
+ }
+
+ MYSQL_CONTAINER =
+ new MySQLContainer<>(mysqlImageName)
+ .withDatabaseName(TEST_DB_NAME)
+ .withUsername("root")
+ .withPassword("root");
+ MYSQL_CONTAINER.start();
+ mysqlService = new MysqlService(MYSQL_CONTAINER);
+ createMetalake();
+ }
+
+ @AfterAll
+ public static void stopIntegrationTest() throws IOException, InterruptedException {
+ AbstractIT.stopIntegrationTest();
+ client.dropMetalake(NameIdentifier.of(metalakeName));
+ mysqlService.close();
+ MYSQL_CONTAINER.stop();
+ }
+
+ @Test
+ public void testAuditCatalog() throws Exception {
+ String catalogName = GravitinoITUtils.genRandomName("audit_mysql_catalog");
+ Catalog catalog = createCatalog(catalogName);
+ Assertions.assertEquals(expectUser, catalog.auditInfo().creator());
+ Assertions.assertNull(catalog.auditInfo().lastModifier());
+ catalog =
+ metalake.alterCatalog(
+ NameIdentifier.of(metalakeName, catalogName),
+ CatalogChange.setProperty("key1", "value1"));
+ Assertions.assertEquals(expectUser, catalog.auditInfo().creator());
+ Assertions.assertEquals(expectUser, catalog.auditInfo().lastModifier());
+ }
+
+ @Test
+ public void testAuditSchema() throws Exception {
+ String catalogName = GravitinoITUtils.genRandomName("audit_mysql_schema_catalog");
+ String schemaName = GravitinoITUtils.genRandomName("audit_mysql_schema");
+ Catalog catalog = createCatalog(catalogName);
+ NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName, schemaName);
+ Map<String, String> prop = Maps.newHashMap();
+ Schema schema = catalog.asSchemas().createSchema(ident, "comment", prop);
+ Assertions.assertEquals(expectUser, schema.auditInfo().creator());
+ Assertions.assertNull(schema.auditInfo().lastModifier());
+ }
+
+ @Test
+ public void testAuditTable() throws Exception {
+ String catalogName = GravitinoITUtils.genRandomName("audit_mysql_table_catalog");
+ String schemaName = GravitinoITUtils.genRandomName("audit_mysql_table_schma");
+ String tableName = GravitinoITUtils.genRandomName("audit_mysql_table");
+ Catalog catalog = createCatalog(catalogName);
+ Map<String, String> properties = Maps.newHashMap();
+
+ ColumnDTO col1 =
+ new ColumnDTO.Builder()
+ .withName("col_1")
+ .withDataType(Types.IntegerType.get())
+ .withComment("col_1_comment")
+ .build();
+
+ catalog
+ .asSchemas()
+ .createSchema(
+ NameIdentifier.of(metalakeName, catalogName, schemaName), "comment", properties);
+ Table table =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ new ColumnDTO[] {col1},
+ "comment",
+ properties);
+ Assertions.assertEquals(expectUser, table.auditInfo().creator());
+ Assertions.assertNull(table.auditInfo().lastModifier());
+ table =
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.addColumn(new String[] {"col_4"}, Types.StringType.get()));
+ Assertions.assertEquals(expectUser, table.auditInfo().creator());
+ Assertions.assertEquals(expectUser, table.auditInfo().lastModifier());
+ }
+
+ private static Catalog createCatalog(String catalogName) {
+ Map<String, String> catalogProperties = Maps.newHashMap();
+
+ catalogProperties.put(
+ JdbcConfig.JDBC_URL.getKey(),
+ StringUtils.substring(
+ MYSQL_CONTAINER.getJdbcUrl(), 0, MYSQL_CONTAINER.getJdbcUrl().lastIndexOf("/")));
+ catalogProperties.put(JdbcConfig.JDBC_DRIVER.getKey(), MYSQL_CONTAINER.getDriverClassName());
+ catalogProperties.put(JdbcConfig.USERNAME.getKey(), MYSQL_CONTAINER.getUsername());
+ catalogProperties.put(JdbcConfig.PASSWORD.getKey(), MYSQL_CONTAINER.getPassword());
+
+ return metalake.createCatalog(
+ NameIdentifier.of(metalakeName, catalogName),
+ Catalog.Type.RELATIONAL,
+ provider,
+ "comment",
+ catalogProperties);
+ }
+
+ private static void createMetalake() {
+ GravitinoMetaLake[] gravitinoMetaLakes = client.listMetalakes();
+ Assertions.assertEquals(0, gravitinoMetaLakes.length);
+
+ GravitinoMetaLake createdMetalake =
+ client.createMetalake(NameIdentifier.of(metalakeName), "comment", Collections.emptyMap());
+ GravitinoMetaLake loadMetalake = client.loadMetalake(NameIdentifier.of(metalakeName));
+ Assertions.assertEquals(createdMetalake, loadMetalake);
+ metalake = loadMetalake;
+ }
+}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java
index fe6c641644..7459882da1 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java
@@ -7,6 +7,7 @@ package com.datastrato.gravitino.integration.test.catalog.jdbc.postgresql;
import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
import com.datastrato.gravitino.client.GravitinoMetaLake;
import com.datastrato.gravitino.dto.rel.ColumnDTO;
@@ -324,13 +325,16 @@ public class CatalogPostgreSqlIT extends AbstractIT {
@Test
void testAlterAndDropPostgreSqlTable() {
ColumnDTO[] columns = createColumns();
- catalog
- .asTableCatalog()
- .createTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- columns,
- table_comment,
- createProperties());
+ Table table =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ table_comment,
+ createProperties());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().creator());
+ Assertions.assertNull(table.auditInfo().lastModifier());
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
@@ -343,11 +347,14 @@ public class CatalogPostgreSqlIT extends AbstractIT {
});
// rename table
- catalog
- .asTableCatalog()
- .alterTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- TableChange.rename(alertTableName));
+ table =
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.rename(alertTableName));
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().lastModifier());
// update table
catalog
@@ -360,7 +367,7 @@ public class CatalogPostgreSqlIT extends AbstractIT {
TableChange.updateColumnType(
new String[] {POSTGRESQL_COL_NAME1}, Types.IntegerType.get()));
- Table table =
+ table =
catalog
.asTableCatalog()
.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName));
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/lakehouse/iceberg/CatalogIcebergIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/lakehouse/iceberg/CatalogIcebergIT.java
index b679eecf75..1f9ad9058e 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/lakehouse/iceberg/CatalogIcebergIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/lakehouse/iceberg/CatalogIcebergIT.java
@@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergCatalogBackend;
import com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergConfig;
import com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergSchemaPropertiesMetadata;
@@ -487,14 +488,17 @@ public class CatalogIcebergIT extends AbstractIT {
@Test
public void testAlterIcebergTable() {
ColumnDTO[] columns = createColumns();
- catalog
- .asTableCatalog()
- .createTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- columns,
- table_comment,
- createProperties(),
- new Partitioning[] {IdentityPartitioningDTO.of(columns[0].name())});
+ Table table =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ table_comment,
+ createProperties(),
+ new Partitioning[] {IdentityPartitioningDTO.of(columns[0].name())});
+ Assertions.assertNull(table.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().creator());
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
@@ -506,11 +510,14 @@ public class CatalogIcebergIT extends AbstractIT {
TableChange.updateComment(table_comment + "_new"));
});
- catalog
- .asTableCatalog()
- .alterTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- TableChange.rename(alertTableName));
+ table =
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.rename(alertTableName));
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().lastModifier());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, table.auditInfo().creator());
catalog
.asTableCatalog()
@@ -525,7 +532,7 @@ public class CatalogIcebergIT extends AbstractIT {
TableChange.updateColumnType(
new String[] {ICEBERG_COL_NAME1}, Types.IntegerType.get()));
- Table table =
+ table =
catalog
.asTableCatalog()
.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName));
@@ -787,6 +794,8 @@ public class CatalogIcebergIT extends AbstractIT {
prop.remove(IcebergSchemaPropertiesMetadata.COMMENT);
catalog.asSchemas().createSchema(ident, schema_comment, prop);
Schema loadSchema = catalog.asSchemas().loadSchema(ident);
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, loadSchema.auditInfo().creator());
+ Assertions.assertNull(loadSchema.auditInfo().lastModifier());
Assertions.assertFalse(
loadSchema.properties().containsKey(IcebergSchemaPropertiesMetadata.COMMENT));
prop.forEach((key, value) -> Assertions.assertEquals(loadSchema.properties().get(key), value));
@@ -800,6 +809,8 @@ public class CatalogIcebergIT extends AbstractIT {
() ->
catalog.asSchemas().alterSchema(ident, SchemaChange.setProperty("comment-test", "v1")));
Schema schema = catalog.asSchemas().loadSchema(ident);
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, schema.auditInfo().creator());
+ Assertions.assertEquals(AuthConstants.ANONYMOUS_USER, schema.auditInfo().lastModifier());
Assertions.assertEquals("v1", schema.properties().get("comment-test"));
// drop
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/AuditIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/AuditIT.java
new file mode 100644
index 0000000000..95cf56b26f
--- /dev/null
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/AuditIT.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.integration.test.client;
+
+import com.datastrato.gravitino.MetalakeChange;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.auth.AuthenticatorType;
+import com.datastrato.gravitino.client.GravitinoMetaLake;
+import com.datastrato.gravitino.integration.test.util.AbstractIT;
+import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
+import com.datastrato.gravitino.server.auth.OAuthConfig;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+public class AuditIT extends AbstractIT {
+
+ private static final String expectUser = System.getProperty("user.name");
+
+ @BeforeAll
+ public static void startIntegrationTest() throws Exception {
+ Map<String, String> configs = Maps.newHashMap();
+ configs.put(OAuthConfig.AUTHENTICATOR.getKey(), AuthenticatorType.SIMPLE.name().toLowerCase());
+ registerCustomConfigs(configs);
+ AbstractIT.startIntegrationTest();
+ }
+
+ @Test
+ public void testAuditMetalake() throws Exception {
+ String metalakeAuditName = GravitinoITUtils.genRandomName("metalakeAudit");
+ String newName = GravitinoITUtils.genRandomName("newmetaname");
+
+ GravitinoMetaLake metaLake =
+ client.createMetalake(
+ NameIdentifier.parse(metalakeAuditName), "metalake A comment", Collections.emptyMap());
+ Assertions.assertEquals(expectUser, metaLake.auditInfo().creator());
+ Assertions.assertNull(metaLake.auditInfo().lastModifier());
+ MetalakeChange[] changes =
+ new MetalakeChange[] {
+ MetalakeChange.rename(newName), MetalakeChange.updateComment("new metalake comment")
+ };
+ metaLake = client.alterMetalake(NameIdentifier.of(metalakeAuditName), changes);
+ Assertions.assertEquals(expectUser, metaLake.auditInfo().creator());
+ Assertions.assertEquals(expectUser, metaLake.auditInfo().lastModifier());
+ client.dropMetalake(NameIdentifier.parse(newName));
+ }
+}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/MetalakeIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/MetalakeIT.java
index e221b3725d..839496b472 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/MetalakeIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/client/MetalakeIT.java
@@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import com.datastrato.gravitino.MetalakeChange;
import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.client.GravitinoMetaLake;
import com.datastrato.gravitino.exceptions.IllegalNameIdentifierException;
import com.datastrato.gravitino.exceptions.IllegalNamespaceException;
@@ -104,7 +105,7 @@ public class MetalakeIT extends AbstractIT {
GravitinoMetaLake metaLake = client.alterMetalake(NameIdentifier.of(metalakeNameA), changes);
assertEquals(newName, metaLake.name());
assertEquals("new metalake comment", metaLake.comment());
- assertEquals("gravitino", metaLake.auditInfo().creator());
+ assertEquals(AuthConstants.ANONYMOUS_USER, metaLake.auditInfo().creator());
// Reload metadata via new name to check if the changes are applied
GravitinoMetaLake newMetalake = client.loadMetalake(NameIdentifier.of(newName));
@@ -142,7 +143,7 @@ public class MetalakeIT extends AbstractIT {
GravitinoMetaLake metalake = client.loadMetalake(NameIdentifier.of(metalakeNameA));
assertEquals(metalakeNameA, metalake.name());
assertEquals("metalake A comment", metalake.comment());
- assertEquals("gravitino", metalake.auditInfo().creator());
+ assertEquals(AuthConstants.ANONYMOUS_USER, metalake.auditInfo().creator());
// Test metalake name already exists
assertThrows(
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
index 2cbf71c96b..b9651be9ce 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
@@ -125,6 +125,11 @@ public class AbstractIT {
.toLowerCase()
.equals(customConfigs.get(OAuthConfig.AUTHENTICATOR.getKey()))) {
client = GravitinoClient.builder(uri).withOAuth(mockDataProvider).build();
+ } else if (AuthenticatorType.SIMPLE
+ .name()
+ .toLowerCase()
+ .equals(customConfigs.get(OAuthConfig.AUTHENTICATOR.getKey()))) {
+ client = GravitinoClient.builder(uri).withSimpleAuth().build();
} else {
client = GravitinoClient.builder(uri).build();
}
diff --git a/server-common/src/main/java/com/datastrato/gravitino/server/auth/AuthenticationFilter.java b/server-common/src/main/java/com/datastrato/gravitino/server/auth/AuthenticationFilter.java
index 609d09279a..c2498fed7a 100644
--- a/server-common/src/main/java/com/datastrato/gravitino/server/auth/AuthenticationFilter.java
+++ b/server-common/src/main/java/com/datastrato/gravitino/server/auth/AuthenticationFilter.java
@@ -9,6 +9,7 @@ import com.datastrato.gravitino.exceptions.UnauthorizedException;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.security.Principal;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -52,7 +53,8 @@ public class AuthenticationFilter implements Filter {
authData = headerData.nextElement().getBytes(StandardCharsets.UTF_8);
}
if (authenticator.isDataFromToken()) {
- authenticator.authenticateToken(authData);
+ Principal principal = authenticator.authenticateToken(authData);
+ request.setAttribute(AuthConstants.AUTHENTICATED_PRINCIPAL_ATTRIBUTE_NAME, principal);
}
chain.doFilter(request, response);
} catch (UnauthorizedException ue) {
diff --git a/server-common/src/main/java/com/datastrato/gravitino/server/auth/OAuth2TokenAuthenticator.java b/server-common/src/main/java/com/datastrato/gravitino/server/auth/OAuth2TokenAuthenticator.java
index 7ea53aff7c..f2dfb0288d 100644
--- a/server-common/src/main/java/com/datastrato/gravitino/server/auth/OAuth2TokenAuthenticator.java
+++ b/server-common/src/main/java/com/datastrato/gravitino/server/auth/OAuth2TokenAuthenticator.java
@@ -5,6 +5,7 @@
package com.datastrato.gravitino.server.auth;
import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.UserPrincipal;
import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.auth.SignatureAlgorithmFamilyType;
import com.datastrato.gravitino.exceptions.UnauthorizedException;
diff --git a/server-common/src/main/java/com/datastrato/gravitino/server/auth/SimpleAuthenticator.java b/server-common/src/main/java/com/datastrato/gravitino/server/auth/SimpleAuthenticator.java
index 15be05bd1d..6de1d2b861 100644
--- a/server-common/src/main/java/com/datastrato/gravitino/server/auth/SimpleAuthenticator.java
+++ b/server-common/src/main/java/com/datastrato/gravitino/server/auth/SimpleAuthenticator.java
@@ -6,6 +6,7 @@
package com.datastrato.gravitino.server.auth;
import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.UserPrincipal;
import com.datastrato.gravitino.auth.AuthConstants;
import java.security.Principal;
import java.util.Base64;
diff --git a/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestAuthenticationFilter.java b/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestAuthenticationFilter.java
index 8b398b9c2b..60f02e37c7 100644
--- a/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestAuthenticationFilter.java
+++ b/server-common/src/test/java/com/datastrato/gravitino/server/auth/TestAuthenticationFilter.java
@@ -13,6 +13,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import com.datastrato.gravitino.UserPrincipal;
import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.exceptions.UnauthorizedException;
import java.io.IOException;
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/Utils.java b/server/src/main/java/com/datastrato/gravitino/server/web/Utils.java
index 8921e511e6..91f0854178 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/Utils.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/Utils.java
@@ -4,7 +4,11 @@
*/
package com.datastrato.gravitino.server.web;
+import com.datastrato.gravitino.UserPrincipal;
+import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.dto.responses.ErrorResponse;
+import com.datastrato.gravitino.utils.PrincipalUtils;
+import java.security.PrivilegedExceptionAction;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
@@ -94,4 +98,15 @@ public class Utils {
.type(MediaType.APPLICATION_JSON)
.build();
}
+
+ public static Response doAs(
+ HttpServletRequest httpRequest, PrivilegedExceptionAction<Response> action) throws Exception {
+ UserPrincipal principal =
+ (UserPrincipal)
+ httpRequest.getAttribute(AuthConstants.AUTHENTICATED_PRINCIPAL_ATTRIBUTE_NAME);
+ if (principal == null) {
+ principal = new UserPrincipal(AuthConstants.ANONYMOUS_USER);
+ }
+ return PrincipalUtils.doAs(principal, action);
+ }
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
index 32d9df230a..696a93b6e2 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
@@ -53,10 +53,13 @@ public class CatalogOperations {
@Produces("application/vnd.gravitino.v1+json")
public Response listCatalogs(@PathParam("metalake") String metalake) {
try {
- Namespace catalogNS = Namespace.ofCatalog(metalake);
- NameIdentifier[] idents = manager.listCatalogs(catalogNS);
- return Utils.ok(new EntityListResponse(idents));
-
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ Namespace catalogNS = Namespace.ofCatalog(metalake);
+ NameIdentifier[] idents = manager.listCatalogs(catalogNS);
+ return Utils.ok(new EntityListResponse(idents));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleCatalogException(OperationType.LIST, "", metalake, e);
}
@@ -67,16 +70,20 @@ public class CatalogOperations {
public Response createCatalog(
@PathParam("metalake") String metalake, CatalogCreateRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofCatalog(metalake, request.getName());
- Catalog catalog =
- manager.createCatalog(
- ident,
- request.getType(),
- request.getProvider(),
- request.getComment(),
- request.getProperties());
- return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalake, request.getName());
+ Catalog catalog =
+ manager.createCatalog(
+ ident,
+ request.getType(),
+ request.getProvider(),
+ request.getComment(),
+ request.getProperties());
+ return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleCatalogException(
@@ -108,15 +115,19 @@ public class CatalogOperations {
@PathParam("catalog") String catalogName,
CatalogUpdatesRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
- CatalogChange[] changes =
- request.getUpdates().stream()
- .map(CatalogUpdateRequest::catalogChange)
- .toArray(CatalogChange[]::new);
-
- Catalog catalog = manager.alterCatalog(ident, changes);
- return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
+ CatalogChange[] changes =
+ request.getUpdates().stream()
+ .map(CatalogUpdateRequest::catalogChange)
+ .toArray(CatalogChange[]::new);
+
+ Catalog catalog = manager.alterCatalog(ident, changes);
+ return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleCatalogException(
@@ -130,14 +141,17 @@ public class CatalogOperations {
public Response dropCatalog(
@PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
try {
- NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
- boolean dropped = manager.dropCatalog(ident);
- if (!dropped) {
- LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);
- }
-
- return Utils.ok(new DropResponse(dropped));
-
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
+ boolean dropped = manager.dropCatalog(ident);
+ if (!dropped) {
+ LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);
+ }
+
+ return Utils.ok(new DropResponse(dropped));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleCatalogException(
OperationType.DROP, catalogName, metalakeName, e);
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
index cb15d683d7..db3dfd1cd6 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
@@ -60,10 +60,14 @@ public class MetalakeOperations {
@ResponseMetered(name = "list-metalake", absolute = true)
public Response listMetalakes() {
try {
- BaseMetalake[] metalakes = manager.listMetalakes();
- MetalakeDTO[] metalakeDTOS =
- Arrays.stream(metalakes).map(DTOConverters::toDTO).toArray(MetalakeDTO[]::new);
- return Utils.ok(new MetalakeListResponse(metalakeDTOS));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ BaseMetalake[] metalakes = manager.listMetalakes();
+ MetalakeDTO[] metalakeDTOS =
+ Arrays.stream(metalakes).map(DTOConverters::toDTO).toArray(MetalakeDTO[]::new);
+ return Utils.ok(new MetalakeListResponse(metalakeDTOS));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleMetalakeException(
@@ -77,11 +81,15 @@ public class MetalakeOperations {
@ResponseMetered(name = "create-metalake", absolute = true)
public Response createMetalake(MetalakeCreateRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());
- BaseMetalake metalake =
- manager.createMetalake(ident, request.getComment(), request.getProperties());
- return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());
+ BaseMetalake metalake =
+ manager.createMetalake(ident, request.getComment(), request.getProperties());
+ return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleMetalakeException(OperationType.CREATE, request.getName(), e);
@@ -95,9 +103,13 @@ public class MetalakeOperations {
@ResponseMetered(name = "load-metalake", absolute = true)
public Response loadMetalake(@PathParam("name") String metalakeName) {
try {
- NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
- BaseMetalake metalake = manager.loadMetalake(identifier);
- return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
+ BaseMetalake metalake = manager.loadMetalake(identifier);
+ return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleMetalakeException(OperationType.LOAD, metalakeName, e);
@@ -112,15 +124,19 @@ public class MetalakeOperations {
public Response alterMetalake(
@PathParam("name") String metalakeName, MetalakeUpdatesRequest updatesRequest) {
try {
- updatesRequest.validate();
- NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
- MetalakeChange[] changes =
- updatesRequest.getUpdates().stream()
- .map(MetalakeUpdateRequest::metalakeChange)
- .toArray(MetalakeChange[]::new);
-
- BaseMetalake updatedMetalake = manager.alterMetalake(identifier, changes);
- return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ updatesRequest.validate();
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
+ MetalakeChange[] changes =
+ updatesRequest.getUpdates().stream()
+ .map(MetalakeUpdateRequest::metalakeChange)
+ .toArray(MetalakeChange[]::new);
+
+ BaseMetalake updatedMetalake = manager.alterMetalake(identifier, changes);
+ return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleMetalakeException(OperationType.ALTER, metalakeName, e);
@@ -134,13 +150,17 @@ public class MetalakeOperations {
@ResponseMetered(name = "drop-metalake", absolute = true)
public Response dropMetalake(@PathParam("name") String metalakeName) {
try {
- NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
- boolean dropped = manager.dropMetalake(identifier);
- if (!dropped) {
- LOG.warn("Failed to drop metalake by name {}", metalakeName);
- }
-
- return Utils.ok(new DropResponse(dropped));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
+ boolean dropped = manager.dropMetalake(identifier);
+ if (!dropped) {
+ LOG.warn("Failed to drop metalake by name {}", metalakeName);
+ }
+
+ return Utils.ok(new DropResponse(dropped));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleMetalakeException(OperationType.DROP, metalakeName, e);
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
index 530d55517f..0342668d7e 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
@@ -61,10 +61,13 @@ public class SchemaOperations {
public Response listSchemas(
@PathParam("metalake") String metalake, @PathParam("catalog") String catalog) {
try {
- Namespace schemaNS = Namespace.ofSchema(metalake, catalog);
- NameIdentifier[] idents = dispatcher.listSchemas(schemaNS);
- return Utils.ok(new EntityListResponse(idents));
-
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ Namespace schemaNS = Namespace.ofSchema(metalake, catalog);
+ NameIdentifier[] idents = dispatcher.listSchemas(schemaNS);
+ return Utils.ok(new EntityListResponse(idents));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleSchemaException(OperationType.LIST, "", catalog, e);
}
@@ -79,10 +82,15 @@ public class SchemaOperations {
@PathParam("catalog") String catalog,
SchemaCreateRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, request.getName());
- Schema schema = dispatcher.createSchema(ident, request.getComment(), request.getProperties());
- return Utils.ok(new SchemaResponse(DTOConverters.toDTO(schema)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, request.getName());
+ Schema schema =
+ dispatcher.createSchema(ident, request.getComment(), request.getProperties());
+ return Utils.ok(new SchemaResponse(DTOConverters.toDTO(schema)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleSchemaException(
@@ -100,9 +108,13 @@ public class SchemaOperations {
@PathParam("catalog") String catalog,
@PathParam("schema") String schema) {
try {
- NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
- Schema s = dispatcher.loadSchema(ident);
- return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
+ Schema s = dispatcher.loadSchema(ident);
+ return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleSchemaException(OperationType.LOAD, schema, catalog, e);
@@ -120,14 +132,18 @@ public class SchemaOperations {
@PathParam("schema") String schema,
SchemaUpdatesRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
- SchemaChange[] changes =
- request.getUpdates().stream()
- .map(SchemaUpdateRequest::schemaChange)
- .toArray(SchemaChange[]::new);
- Schema s = dispatcher.alterSchema(ident, changes);
- return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
+ SchemaChange[] changes =
+ request.getUpdates().stream()
+ .map(SchemaUpdateRequest::schemaChange)
+ .toArray(SchemaChange[]::new);
+ Schema s = dispatcher.alterSchema(ident, changes);
+ return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleSchemaException(OperationType.ALTER, schema, catalog, e);
@@ -145,14 +161,17 @@ public class SchemaOperations {
@PathParam("schema") String schema,
@DefaultValue("false") @QueryParam("cascade") boolean cascade) {
try {
- NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
- boolean dropped = dispatcher.dropSchema(ident, cascade);
- if (!dropped) {
- LOG.warn("Fail to drop schema {} under namespace {}", schema, ident.namespace());
- }
-
- return Utils.ok(new DropResponse(dropped));
-
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
+ boolean dropped = dispatcher.dropSchema(ident, cascade);
+ if (!dropped) {
+ LOG.warn("Fail to drop schema {} under namespace {}", schema, ident.namespace());
+ }
+
+ return Utils.ok(new DropResponse(dropped));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleSchemaException(OperationType.DROP, schema, catalog, e);
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
index 1388613b6d..9d9114ee58 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
@@ -62,9 +62,13 @@ public class TableOperations {
@PathParam("catalog") String catalog,
@PathParam("schema") String schema) {
try {
- Namespace tableNS = Namespace.ofTable(metalake, catalog, schema);
- NameIdentifier[] idents = dispatcher.listTables(tableNS);
- return Utils.ok(new EntityListResponse(idents));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ Namespace tableNS = Namespace.ofTable(metalake, catalog, schema);
+ NameIdentifier[] idents = dispatcher.listTables(tableNS);
+ return Utils.ok(new EntityListResponse(idents));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleTableException(OperationType.LIST, "", schema, e);
@@ -81,19 +85,24 @@ public class TableOperations {
@PathParam("schema") String schema,
TableCreateRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, request.getName());
-
- Table table =
- dispatcher.createTable(
- ident,
- request.getColumns(),
- request.getComment(),
- request.getProperties(),
- fromDTOs(request.getPartitioning()),
- fromDTO(request.getDistribution()),
- fromDTOs(request.getSortOrders()));
- return Utils.ok(new TableResponse(DTOConverters.toDTO(table)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident =
+ NameIdentifier.ofTable(metalake, catalog, schema, request.getName());
+
+ Table table =
+ dispatcher.createTable(
+ ident,
+ request.getColumns(),
+ request.getComment(),
+ request.getProperties(),
+ fromDTOs(request.getPartitioning()),
+ fromDTO(request.getDistribution()),
+ fromDTOs(request.getSortOrders()));
+ return Utils.ok(new TableResponse(DTOConverters.toDTO(table)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleTableException(
@@ -112,10 +121,13 @@ public class TableOperations {
@PathParam("schema") String schema,
@PathParam("table") String table) {
try {
- NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
- Table t = dispatcher.loadTable(ident);
- return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
-
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
+ Table t = dispatcher.loadTable(ident);
+ return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleTableException(OperationType.LOAD, table, schema, e);
}
@@ -133,14 +145,18 @@ public class TableOperations {
@PathParam("table") String table,
TableUpdatesRequest request) {
try {
- request.validate();
- NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
- TableChange[] changes =
- request.getUpdates().stream()
- .map(TableUpdateRequest::tableChange)
- .toArray(TableChange[]::new);
- Table t = dispatcher.alterTable(ident, changes);
- return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ request.validate();
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
+ TableChange[] changes =
+ request.getUpdates().stream()
+ .map(TableUpdateRequest::tableChange)
+ .toArray(TableChange[]::new);
+ Table t = dispatcher.alterTable(ident, changes);
+ return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleTableException(OperationType.ALTER, table, schema, e);
@@ -159,13 +175,17 @@ public class TableOperations {
@PathParam("table") String table,
@QueryParam("purge") @DefaultValue("false") boolean purge) {
try {
- NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
- boolean dropped = purge ? dispatcher.purgeTable(ident) : dispatcher.dropTable(ident);
- if (!dropped) {
- LOG.warn("Failed to drop table {} under schema {}", table, schema);
- }
-
- return Utils.ok(new DropResponse(dropped));
+ return Utils.doAs(
+ httpRequest,
+ () -> {
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
+ boolean dropped = purge ? dispatcher.purgeTable(ident) : dispatcher.dropTable(ident);
+ if (!dropped) {
+ LOG.warn("Failed to drop table {} under schema {}", table, schema);
+ }
+
+ return Utils.ok(new DropResponse(dropped));
+ });
} catch (Exception e) {
return ExceptionHandlers.handleTableException(OperationType.DROP, table, schema, e);