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
@@ -1,5 +1,6 @@
package io.unitycatalog.server.auth;

import io.unitycatalog.server.model.SecurableType;
import io.unitycatalog.server.persist.model.Privileges;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -54,17 +55,20 @@ public UUID getHierarchyParent(UUID resource) {
}

@Override
public boolean authorize(UUID principal, UUID resource, Privileges action) {
public boolean authorize(
UUID principal, SecurableType resourceType, UUID resource, Privileges action) {
return true;
}

@Override
public boolean authorizeAny(UUID principal, UUID resource, Privileges... actions) {
public boolean authorizeAny(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions) {
return true;
}

@Override
public boolean authorizeAll(UUID principal, UUID resource, Privileges... actions) {
public boolean authorizeAll(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.unitycatalog.server.auth;

import io.unitycatalog.server.model.SecurableType;
import io.unitycatalog.server.persist.model.Privileges;
import io.unitycatalog.server.persist.utils.HibernateConfigurator;
import io.unitycatalog.server.utils.ServerProperties;
Expand Down Expand Up @@ -213,14 +214,16 @@ public UUID getHierarchyParent(UUID resource) {
}

@Override
public boolean authorize(UUID principal, UUID resource, Privileges action) {
public boolean authorize(
UUID principal, SecurableType resourceType, UUID resource, Privileges action) {
return currentEnforcer
.get()
.enforce(principal.toString(), resource.toString(), action.toString());
}

@Override
public boolean authorizeAny(UUID principal, UUID resource, Privileges... actions) {
public boolean authorizeAny(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions) {
SyncedEnforcer enforcer = currentEnforcer.get();
return Arrays.stream(actions)
.anyMatch(
Expand All @@ -229,7 +232,8 @@ public boolean authorizeAny(UUID principal, UUID resource, Privileges... actions
}

@Override
public boolean authorizeAll(UUID principal, UUID resource, Privileges... actions) {
public boolean authorizeAll(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions) {
SyncedEnforcer enforcer = currentEnforcer.get();
return Arrays.stream(actions)
.allMatch(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.unitycatalog.server.auth;

import io.unitycatalog.server.model.SecurableType;
import io.unitycatalog.server.persist.model.Privileges;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -32,11 +33,13 @@ public interface UnityCatalogAuthorizer {

UUID getHierarchyParent(UUID resource);

boolean authorize(UUID principal, UUID resource, Privileges action);
boolean authorize(UUID principal, SecurableType resourceType, UUID resource, Privileges action);

boolean authorizeAny(UUID principal, UUID resource, Privileges... actions);
boolean authorizeAny(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions);

boolean authorizeAll(UUID principal, UUID resource, Privileges... actions);
boolean authorizeAll(
UUID principal, SecurableType resourceType, UUID resource, Privileges... actions);

List<Privileges> listAuthorizations(UUID principal, UUID resource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
* <li>#principal - the (UUID) of the principal making the request
* <li>#deny - constant evaluating to false
* <li>#permit - constant evaluating to true - always allow access
* <li>#catalog - the ID of the catalog associated with the request (if applicable)
* <li>#schema - the ID of the schema associated with the request (if applicable)
* <li>#table - the ID of the table associated with the request (if applicable)
* <li>#metastore - the ID of the metastore/server (if applicable)
* <li>#catalog - the catalog resource associated with the request (if applicable)
* <li>#schema - the schema resource associated with the request (if applicable)
* <li>#table - the table resource associated with the request (if applicable)
* <li>#metastore - the metastore resource (if applicable)
* </ul>
*
* <p>Resource variables contain a resource type and UUID. Pass them directly to the authorization
* functions. Missing resources are null; compare them with null, not with UUIDs.
*
* <p>Example: #authorize(#principal, #schema, 'USE_SCHEMA')
*/
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
/**
* Spring Expression Language access control evaluator.
*
* <p>A simple class to allow evaluating an SpEL authorization expression. This class exposes all
* the principal and resource ids provided as SpEL context variables. It also exposes functions
* authorization functions to call the configured authorization backend implementation which can be
* used with SpEL logical operators to define compound authorization rules.
* <p>Exposes the principal UUID and resource values (type and UUID) as SpEL context variables.
* Authorization functions call the configured backend and can be combined with SpEL logical
* operators to define compound authorization rules.
*
* <p>{@code #strict_privilege_checks} is a boolean from server configuration. Request values cannot
* override it.
Expand All @@ -47,35 +46,42 @@ public UnityAccessEvaluator(UnityCatalogAuthorizer authorizer, boolean strictPri
this.parser = new SpelExpressionParser();

MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(boolean.class, UUID.class, UUID.class, Privileges.class);
MethodHandle mh = lookup.findVirtual(authorizer.getClass(), "authorize", mt);
authorizeHandle = mh.bindTo(this.authorizer);
MethodType mt =
MethodType.methodType(boolean.class, UUID.class, Resource.class, Privileges.class);
MethodHandle mh = lookup.findVirtual(UnityAccessEvaluator.class, "authorize", mt);
authorizeHandle = mh.bindTo(this);

mt = MethodType.methodType(boolean.class, Object[].class);
mh = lookup.findVirtual(this.getClass(), "authorizeAny", mt);
mh = lookup.findVirtual(UnityAccessEvaluator.class, "authorizeAny", mt);
authorizeAnyHandle = mh.bindTo(this);

mt = MethodType.methodType(boolean.class, Object[].class);
mh = lookup.findVirtual(this.getClass(), "authorizeAll", mt);
mh = lookup.findVirtual(UnityAccessEvaluator.class, "authorizeAll", mt);
authorizeAllHandle = mh.bindTo(this);
}

protected boolean authorizeAny(Object... parameters) {
private record Resource(SecurableType type, UUID id) {}

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.

[AI Assisted] nit: The Javadoc on AuthorizeExpression still describes #catalog, #schema, #table and #metastore as "the ID of the ...". With this change they are a typed Resource wrapper, not bare IDs. Can you update that doc so expression authors don't compare them against UUIDs?

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.

Updated the docs to explain that these contain a type and UUID, and that missing resources are null.


private boolean authorize(UUID principal, Resource resource, Privileges privilege) {
return authorizer.authorize(principal, resource.type(), resource.id(), privilege);
}

private boolean authorizeAny(Object... parameters) {
// TODO: Find a better way to deal with the varargs authorizeAny() method.
UUID principalId = (UUID) parameters[0];
UUID resource = (UUID) parameters[1];
Resource resource = (Resource) parameters[1];
Privileges[] privileges = new Privileges[parameters.length - 2];
System.arraycopy(parameters, 2, privileges, 0, privileges.length);
return authorizer.authorizeAny(principalId, resource, privileges);
return authorizer.authorizeAny(principalId, resource.type(), resource.id(), privileges);
}

protected boolean authorizeAll(Object... parameters) {
private boolean authorizeAll(Object... parameters) {
// TODO: Find a better way to deal with the varargs authorizeAll() method.
UUID principalId = (UUID) parameters[0];
UUID resource = (UUID) parameters[1];
Resource resource = (Resource) parameters[1];
Privileges[] privileges = new Privileges[parameters.length - 2];
System.arraycopy(parameters, 2, privileges, 0, privileges.length);
return authorizer.authorizeAll(principalId, resource, privileges);
return authorizer.authorizeAll(principalId, resource.type(), resource.id(), privileges);
}

/**
Expand Down Expand Up @@ -123,7 +129,10 @@ public boolean evaluate(
context.setVariable("permit", Boolean.TRUE);
context.setVariable("principal", principal);

resourceIds.forEach((k, v) -> context.setVariable(k.name().toLowerCase(), v));
resourceIds.forEach(
(type, id) ->
context.setVariable(
type.name().toLowerCase(), id == null ? null : new Resource(type, id)));
nonResourceValues.forEach(context::setVariable);
context.setVariable("strict_privilege_checks", strictPrivilegeChecks);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.unitycatalog.server.auth.UnityCatalogAuthorizer;
import io.unitycatalog.server.exception.BaseException;
import io.unitycatalog.server.exception.ErrorCode;
import io.unitycatalog.server.model.SecurableType;
import io.unitycatalog.server.persist.MetastoreRepository;
import io.unitycatalog.server.persist.Repositories;
import io.unitycatalog.server.persist.UserRepository;
Expand All @@ -21,6 +22,20 @@ public UnityAccessUtil(Repositories repositories) {
this.metastoreRepository = repositories.getMetastoreRepository();
}

/**
* Returns the parent type in the stored authorization hierarchy, or null at a root.
*
* <p>Metastore ownership is checked separately; catalogs, external locations, and credentials
* have no stored parent.
*/
public static SecurableType getHierarchyParentType(SecurableType type) {
return switch (type) {
case TABLE, VOLUME, FUNCTION, REGISTERED_MODEL -> SecurableType.SCHEMA;
case SCHEMA -> SecurableType.CATALOG;
case METASTORE, CATALOG, EXTERNAL_LOCATION, CREDENTIAL -> null;
};
}

public void initializeAdmin(UnityCatalogAuthorizer authorizer) {

// If no admin user exists, lets create one and grant the admin as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,19 @@ Set<CredentialContext.Privilege> getLoadCredentialPrivileges(
// Check cached permissions directly to avoid UnityAccessEvaluator's policy refresh.
boolean canRead =
authorizer.authorizeAny(
principalId, resourceIds.get(CATALOG), Privileges.OWNER, Privileges.USE_CATALOG)
principalId,
CATALOG,
resourceIds.get(CATALOG),
Privileges.OWNER,
Privileges.USE_CATALOG)
&& authorizer.authorizeAny(
principalId, resourceIds.get(SCHEMA), Privileges.OWNER, Privileges.USE_SCHEMA)
principalId,
SCHEMA,
resourceIds.get(SCHEMA),
Privileges.OWNER,
Privileges.USE_SCHEMA)
&& authorizer.authorizeAny(
principalId, state.tableId(), Privileges.OWNER, Privileges.SELECT);
principalId, TABLE, state.tableId(), Privileges.OWNER, Privileges.SELECT);
if (!canRead) {
return Set.of();
}
Expand All @@ -490,9 +498,9 @@ Set<CredentialContext.Privilege> getLoadCredentialPrivileges(
// 1. with auth off: AllowingAuthorizer allows READ_WRITE anyway even if it's null
// 2. with auth on and principalId==null: It should have failed earlier before hitting here.
boolean canWrite =
authorizer.authorize(principalId, state.tableId(), Privileges.OWNER)
authorizer.authorize(principalId, TABLE, state.tableId(), Privileges.OWNER)
|| authorizer.authorizeAll(
principalId, state.tableId(), Privileges.SELECT, Privileges.MODIFY);
principalId, TABLE, state.tableId(), Privileges.SELECT, Privileges.MODIFY);
return canWrite ? READ_WRITE : READ_ONLY;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.unitycatalog.server.service;

import static io.unitycatalog.server.auth.decorator.UnityAccessUtil.getHierarchyParentType;
import static io.unitycatalog.server.model.SecurableType.CATALOG;
import static io.unitycatalog.server.model.SecurableType.CREDENTIAL;
import static io.unitycatalog.server.model.SecurableType.EXTERNAL_LOCATION;
Expand Down Expand Up @@ -150,13 +151,20 @@ private HttpResponse getAuthorization(SecurableType securableType, String name)
// For now this is sufficient in that it covers owner on resources parentage.
UUID parentId = authorizer.getHierarchyParent(resourceId);
UUID grandparentId = (parentId != null) ? authorizer.getHierarchyParent(parentId) : null;
SecurableType parentType = getHierarchyParentType(securableType);
SecurableType grandparentType = parentType != null ? getHierarchyParentType(parentType) : null;

boolean isOwner =
authorizer.authorize(principalId, metastoreRepository.getMetastoreId(), Privileges.OWNER)
|| authorizer.authorize(principalId, resourceId, Privileges.OWNER)
|| (parentId != null && authorizer.authorize(principalId, parentId, Privileges.OWNER))
|| (grandparentId != null
&& authorizer.authorize(principalId, grandparentId, Privileges.OWNER));
authorizer.authorize(
principalId, METASTORE, metastoreRepository.getMetastoreId(), Privileges.OWNER)
|| authorizer.authorize(principalId, securableType, resourceId, Privileges.OWNER)
|| (parentType != null
&& parentId != null
&& authorizer.authorize(principalId, parentType, parentId, Privileges.OWNER))
|| (grandparentType != null
&& grandparentId != null
&& authorizer.authorize(
principalId, grandparentType, grandparentId, Privileges.OWNER));

Map<UUID, List<Privileges>> authorizations =
isOwner
Expand Down
Loading
Loading