Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public final class AuthorizeExpressions {
private AuthorizeExpressions() {}

/**
* Authorization policy for reading table metadata (UC REST {@code GET /tables/{name}} and Delta
* REST Catalog {@code loadTable}). Metastore admin and catalog owner pass unconditionally; schema
* owner passes with catalog {@code USE_CATALOG}; regular callers need {@code USE_SCHEMA} + {@code
* USE_CATALOG} plus any of {@code OWNER} / {@code SELECT} / {@code MODIFY} on the table itself.
* Authorization policy for reading table metadata (UC REST {@code GET /tables/{name}}, UC Delta
* API, and the Iceberg REST {@code loadTable}). Metastore admin and catalog owner pass
* unconditionally; schema owner passes with catalog {@code USE_CATALOG}; regular callers need
* {@code USE_SCHEMA} + {@code USE_CATALOG} plus any of {@code OWNER} / {@code SELECT} / {@code
* MODIFY} on the table itself.
*/
public static final String GET_TABLE =
"""
Expand All @@ -36,6 +37,72 @@ private AuthorizeExpressions() {}
#authorizeAny(#principal, #table, OWNER, SELECT, MODIFY))
""";

/**
* Catalog-level read access: metastore admin, or {@code OWNER}/{@code USE_CATALOG} on the
* catalog. Shared by the catalog get/list endpoints and the Delta/Iceberg config endpoints.
*/
public static final String GET_CATALOG =
"""
#authorize(#principal, #metastore, OWNER) ||
#authorizeAny(#principal, #catalog, OWNER, USE_CATALOG)
""";

/**
* Schema-level read access: metastore admin, catalog {@code OWNER}, or schema {@code
* OWNER}/{@code USE_SCHEMA} with catalog {@code USE_CATALOG}. Used as the get-schema gate and the
* list-schemas/namespaces response filter (listing and reading grant the same access).
*/
public static final String GET_SCHEMA =
"""
#authorize(#principal, #metastore, OWNER) ||
#authorize(#principal, #catalog, OWNER) ||
(#authorizeAny(#principal, #catalog, USE_CATALOG) &&
#authorizeAny(#principal, #schema, OWNER, USE_SCHEMA))
""";

/**
* Catalog tier for creating a schema: catalog {@code OWNER}, or {@code USE_CATALOG} + {@code
* CREATE_SCHEMA}. Used directly by Iceberg {@code createNamespace} (no storage root); {@link
* #CREATE_SCHEMA_WITH_STORAGE_ROOT} wraps it for the UC REST endpoint.
*/
public static final String CREATE_SCHEMA =
"""
#authorize(#principal, #catalog, OWNER) ||
#authorizeAll(#principal, #catalog, USE_CATALOG, CREATE_SCHEMA)
""";

/**
* UC REST create-schema policy: the {@link #CREATE_SCHEMA} catalog tier plus, when a {@code
* storage_root} is supplied, {@code OWNER}/{@code CREATE_MANAGED_STORAGE} on the covering
* (non-overlapping) external location. Mirrors the createCatalog storage-root gate.
*/
public static final String CREATE_SCHEMA_WITH_STORAGE_ROOT =
"("
+ CREATE_SCHEMA
+ """
) &&
(#storage_root == null ||
(#no_overlap_with_data_securable &&
#external_location != null &&
#authorizeAny(#principal, #external_location, OWNER, CREATE_MANAGED_STORAGE)))
""";

/**
* Schema-level mutation policy (update or delete a schema): catalog {@code OWNER}, or {@code
* USE_CATALOG} + schema {@code OWNER}. Metastore admin alone is intentionally not sufficient.
* Shared by the UC REST {@code updateSchema}/{@code deleteSchema} and the Iceberg REST {@code
* updateNamespaceProperties}/{@code dropNamespace} endpoints (updating and dropping a schema
* require the same privilege).
*/
public static final String UPDATE_SCHEMA =
"""
#authorize(#principal, #catalog, OWNER) ||
(#authorize(#principal, #catalog, USE_CATALOG) && #authorize(#principal, #schema, OWNER))
""";

/** Deleting a schema requires the same privilege as {@link #UPDATE_SCHEMA}. */
public static final String DELETE_SCHEMA = UPDATE_SCHEMA;

/**
* Authorization policy for creating a staging table (UC REST {@code POST /staging-tables} and UC
* Delta API {@code createStagingTable}). Catalog {@code USE_CATALOG}/{@code OWNER} plus either
Expand Down Expand Up @@ -72,6 +139,23 @@ private AuthorizeExpressions() {}
#authorizeAny(#principal, #external_location, OWNER, CREATE_EXTERNAL_TABLE))))
""";

/**
* Iceberg {@code createTable} policy: same as {@link #CREATE_TABLE}, but discriminates MANAGED vs
* EXTERNAL on the request's {@code location} (Iceberg has no table_type). Keying on location (not
* the resolved external location) keeps the no-overlap guard running when a path resolves to no
* external location.
*/
public static final String CREATE_ICEBERG_TABLE =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: CREATE_ICEBERG_TABLE is identical to CREATE_TABLE except for the MANAGED/EXTERNAL discriminator (#location == null vs #table_type != 'EXTERNAL'). Consider factoring the shared catalog/schema tier + external-location tail so the two policies can't drift.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CREATE_ICEBERG_TABLE will be removed in the next PR in stack once it's able to extract table_type for IRC.

"""
#authorizeAny(#principal, #catalog, OWNER, USE_CATALOG) &&
(#authorize(#principal, #schema, OWNER) ||
#authorizeAll(#principal, #schema, USE_SCHEMA, CREATE_TABLE)) &&
(#location == null ||
(#no_overlap_with_data_securable &&
(#external_location == null ||
#authorizeAny(#principal, #external_location, OWNER, CREATE_EXTERNAL_TABLE))))
""";

/**
* Authorization policy for updating / committing to a table (UC REST {@code
* DeltaCommitsService.postCommit} and the Delta {@code updateTable}). The Delta {@code POST
Expand All @@ -90,9 +174,9 @@ private AuthorizeExpressions() {}
""";

/**
* Authorization policy for deleting a table, shared by the UC REST and Delta REST Catalog delete
* endpoints. Metastore admin alone is intentionally not sufficient -- the caller must hold {@code
* OWNER} somewhere in the catalog / schema / table hierarchy.
* Authorization policy for deleting a table, shared by the UC REST, UC Delta API, and Iceberg
* REST delete endpoints. Metastore admin alone is intentionally not sufficient -- the caller must
* hold {@code OWNER} somewhere in the catalog / schema / table hierarchy.
*/
public static final String DELETE_TABLE =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.unitycatalog.server.model.SecurableType;
import io.unitycatalog.server.model.TableInfo;
import io.unitycatalog.server.model.VolumeInfo;
import io.unitycatalog.server.persist.dao.TableInfoDAO;
import io.unitycatalog.server.service.AuthorizedService;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -174,7 +175,12 @@ private Map<SecurableType, UUID> resolveResourceIdsForItem(
private UUID resolveResourceId(SecurableType securableType, Object item) {
String id =
switch (securableType) {
case TABLE -> ((TableInfo) item).getTableId();
// Table listings may filter over either the API model or the raw DAO (the Iceberg REST
// listTables path passes DAOs straight through to avoid materializing a TableInfo).
case TABLE ->
item instanceof TableInfoDAO dao
? dao.getId().toString()
: ((TableInfo) item).getTableId();
case VOLUME -> ((VolumeInfo) item).getVolumeId();
case FUNCTION -> ((FunctionInfo) item).getFunctionId();
case REGISTERED_MODEL -> ((RegisteredModelInfo) item).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,10 +1067,13 @@ public ListTablesResponse listTables(

/**
* One page of the tables in a schema that carry an Iceberg metadata pointer, and the token for
* the page after it. An empty {@code nextPageToken} means the listing is complete. The names are
* only the tables that carry one, so a page can be empty while more pages remain.
* the page after it. An empty {@code nextPageToken} means the listing is complete. The DAOs are
* only the ones that carry one, so a page can be empty while more pages remain. The raw DAOs are
* returned (rather than converted {@code TableInfo}s) so a caller can authorize the listing per
* table by id and read each name without materializing an API model; callers must read only eager
* fields ({@code id}, {@code name}), since the rows are detached once the page returns.
*/
public record IcebergTablePage(List<String> tableNames, Optional<String> nextPageToken) {}
public record IcebergTablePage(List<TableInfoDAO> tableDaos, Optional<String> nextPageToken) {}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: returning detached TableInfoDAOs (safe only for eager id/name, per the doc) leaks a persistence entity into the service and ResultFilter, and is what forces the instanceof TableInfoDAO branch in ResultFilter (L173-176). A minimal projection record (UUID id, String name) would drop both the "eager fields only" caveat and the instanceof. Non-blocking.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a follow up PR, I can make a minimum interface and have TableInfoDAO implements it.


/**
* Lists the tables in a schema that carry an Iceberg metadata pointer -- a Delta UniForm
Expand All @@ -1095,13 +1098,10 @@ public IcebergTablePage listIcebergTables(
List<TableInfoDAO> page =
LISTING_HELPER.listEntity(session, Optional.empty(), pageToken, schemaId);
String nextPageToken = LISTING_HELPER.getNextPageToken(page, Optional.empty());
List<String> tableNames =
page.stream()
.filter(dao -> dao.getIcebergMetadataLocation() != null)
.map(TableInfoDAO::getName)
.toList();
List<TableInfoDAO> tableDaos =
page.stream().filter(dao -> dao.getIcebergMetadataLocation() != null).toList();
return new IcebergTablePage(
tableNames, Optional.ofNullable(nextPageToken).filter(token -> !token.isEmpty()));
tableDaos, Optional.ofNullable(nextPageToken).filter(token -> !token.isEmpty()));
},
"Failed to list tables",
/* readOnly= */ true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.linecorp.armeria.server.annotation.Param;
import com.linecorp.armeria.server.annotation.Patch;
import com.linecorp.armeria.server.annotation.Post;
import io.unitycatalog.server.auth.AuthorizeExpressions;
import io.unitycatalog.server.auth.UnityCatalogAuthorizer;
import io.unitycatalog.server.auth.annotation.AuthorizeExpression;
import io.unitycatalog.server.auth.annotation.AuthorizeKey;
Expand Down Expand Up @@ -78,14 +79,8 @@ public HttpResponse createCatalog(
return HttpResponse.ofJson(catalogInfo);
}

private static final String LIST_AND_GET_AUTH_EXPRESSION =
"""
#authorize(#principal, #metastore, OWNER) ||
#authorizeAny(#principal, #catalog, OWNER, USE_CATALOG)
""";

@Get("")
@AuthorizeExpression(LIST_AND_GET_AUTH_EXPRESSION)
@AuthorizeExpression(AuthorizeExpressions.GET_CATALOG)
@ResponseAuthorizeFilter
@AuthorizeResourceKey(METASTORE)
public HttpResponse listCatalogs(
Expand All @@ -98,7 +93,7 @@ public HttpResponse listCatalogs(
}

@Get("/{name}")
@AuthorizeExpression(LIST_AND_GET_AUTH_EXPRESSION)
@AuthorizeExpression(AuthorizeExpressions.GET_CATALOG)
@AuthorizeResourceKey(METASTORE)
public HttpResponse getCatalog(@Param("name") @AuthorizeResourceKey(CATALOG) String name) {
return HttpResponse.ofJson(catalogRepository.getCatalog(name));
Expand Down
Loading
Loading