Skip to content

[Server] Add a privilege-level OPA authorizer - #2077

Merged
timothyw553 merged 4 commits into
unitycatalog:mainfrom
timothyw553:stack/opa-authorizer
Nov 3, 2026
Merged

timothyw553 merged 4 commits into
unitycatalog:mainfrom
timothyw553:stack/opa-authorizer

Conversation

@timothyw553

@timothyw553 timothyw553 commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

🥞 Stacked PR

Use this link to review incremental changes.


Signed-off-by: Timothy Wang [email protected]

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated (not applicable to this prerequisite; documentation follows in the integration PR)

Description of changes

Add an OPA authorizer that asks about each evaluated privilege, such as USE_CATALOG on a catalog or CREATE_TABLE on a schema. UC still combines the answers through its existing expressions. Any/all checks preserve their order and short-circuit behavior.

OpaInputBuilder reads the user's UUID, email and optional external ID, plus resource identity and parents. It supports every securable type and staging tables. No native grants, owner fields, tokens, or cloud credentials are sent. OPA replies must contain a boolean result.allow; unusable replies and connection failures return a generic UNAVAILABLE error, with no native fallback or decision cache. Missing users, resources, or parents deny the check without contacting OPA.

Native grant and hierarchy methods delegate to the composed Casbin authorizer for bookkeeping. Server selection and user documentation follow in the child PR.

Validation: Java formatting and all 30 backend/metadata tests passed:

build/sbt javafmtAll 'server/testOnly io.unitycatalog.server.auth.OpaAuthorizerTest io.unitycatalog.server.auth.OpaInputBuilderTest'

yili-db pushed a commit that referenced this pull request Oct 30, 2026
## 🥞 Stacked PR
Use this
[link](https://github.com/unitycatalog/unitycatalog/pull/2076/files) to
review incremental changes.
-
[**stack/auth-resource-types**](#2076)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2076/files)]
← _this PR_
-
[stack/opa-authorizer](#2077)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2077/files/2912ae45366c1a9e3c3b849ac8c78a6e0ad7f7b8..11a01ea1d1964eb1292dc6bc5dabb7786fce7e2d)]
-
[stack/opa-server-config](#2078)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2078/files/11a01ea1d1964eb1292dc6bc5dabb7786fce7e2d..3794f17fbd2e0f69148f9369ed972733fc389fca)]

---------
Signed-off-by: Timothy Wang <[email protected]>

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated (not applicable to this prerequisite; documentation follows in
the integration PR)

**Description of changes**

The evaluator currently drops the resource type before calling the
authorizer. Preserve the resolved `SecurableType` alongside each UUID so
a backend can identify the resource it must authorize.

Pass that type through `authorize`, `authorizeAny`, and `authorizeAll`,
including direct calls from the permission and Iceberg services.
Existing SpEL expressions, null guards, and Casbin decision logic keep
their behavior. There is no REST API change.

Validation: Java formatting and 83 focused server tests passed:

```sh
build/sbt javafmtAll 'server/testOnly io.unitycatalog.server.auth.JCasbinAuthorizerTest io.unitycatalog.server.auth.JCasbinAuthorizerMultiInstanceTest io.unitycatalog.server.auth.decorator.UnityAccessEvaluatorTest io.unitycatalog.server.service.PermissionServiceTest io.unitycatalog.server.service.IcebergRestCatalogServiceTest io.unitycatalog.server.sdk.access.TemporaryPathCredentialAccessControlTest'
```

---------

Signed-off-by: Timothy Wang <[email protected]>
@timothyw553
timothyw553 marked this pull request as ready for review October 30, 2026 23:51
@timothyw553
timothyw553 requested a review from tdas as a code owner October 30, 2026 23:51
@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db yili-db left a comment

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] The fail-closed handling looks solid: no redirects, a bounded response size, trailing JSON rejected, a strict boolean result.allow, no native fallback and no cache. I checked this together with #2078, which already unwraps UNAVAILABLE from SpEL and propagates it out of ResultFilter, so those are not raised here.

Follow-up (not a blocker): each privilege is one OPA call plus one transaction that reloads the user, resource and parents, and list endpoints multiply that by the number of items. Building the input once per any/all call (or per request) and varying only privilege would keep the per-privilege contract. Can you open a tracking issue and cross-link it here?

.join();
if (!response.status().isSuccess()) {
throw new BaseException(
ErrorCode.UNAVAILABLE, "OPA returned HTTP " + response.status().code());

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] These messages reach API clients, and #2078's tests pin them on the UC, Iceberg and Delta APIs. The OPA HTTP status and the expected response shape are server-side details. Can we log the detail and return a generic message, like the metadata-failure path already does? Same for line 84.

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.

done, clients now get the generic message. the original details stay in the cause and get logged by the existing handler.

private <T> T required(Session session, Class<T> type, UUID id) {
T value = session.get(type, id);
if (value == null) {
throw new BaseException(ErrorCode.UNAVAILABLE, "Could not resolve authorization metadata");

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] A resource deleted between key mapping and evaluation (or a stale parent) ends up here as UNAVAILABLE, i.e. a 503 that tells the client to retry. Is that intended? Not found, or a deny, seems closer to what actually happened.

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.

changed this to return false when a record is missing. database and OPA failures still return 503.

.reader()
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readTree(response.contentUtf8());
JsonNode allow = body == null ? null : body.path("result").path("allow");

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: path() never returns null, so body == null and allow == null cannot fire; body.path("result").path("allow").isBoolean() is enough. The two catch blocks below can also merge into one catch (Exception e) that rethrows an UNAVAILABLE BaseException as is.

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.

yep, removed the null checks and combined the catch blocks.

@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db yili-db left a comment

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.

Thanks for addressing the earlier comments. Requesting changes for one blocker: the new missing-metadata deny needs to be logged (inline below). The other two inline comments are nits.

return allow.booleanValue();
} catch (Exception e) {
if (e instanceof BaseException failure && failure.getErrorCode() == ErrorCode.NOT_FOUND) {
return false;

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.

blocker: This deny is silent. When a user, resource or parent record is missing, the request gets a 403, OPA's decision log shows no request at all, and UC logs nothing, so there is nothing to start debugging from. Please log it, at least at WARN, with the principal, privilege, and resource type and id. Better still, name which record was missing: required() knows the type and id, so its exception message could carry them and this line could log it. The log stays server-side, so the client still gets a plain 403.

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.

added a WARN log with the principal, privilege, resource type and ID, and which record was missing. added a test for the log too. these details stay server-side.

try {
String request =
JsonUtils.getInstance()
.writeValueAsString(inputBuilder.build(principal, resourceType, resource, action));

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 NOT_FOUND handling in the catch below covers the whole try, not just this inputBuilder.build(...) call. Nothing else in the block throws NOT_FOUND today, but limiting it to the build call would keep a future NOT_FOUND from elsewhere from turning into a silent deny.

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.

scoped this to inputBuilder.build(...), so a NOT_FOUND from elsewhere won’t turn into a deny.

void metadataFailureStopsBeforeHttpAndDoesNotExposeDatabaseDetails() {
when(session.get(UserDAO.class, principal))
.thenThrow(new IllegalStateException("private-db-detail"));
assertThat(unavailableDecision()).hasRootCauseMessage("private-db-detail");

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 test name says the database detail is not exposed, but the visible assertion is that the root cause is private-db-detail; the generic top-level message is only checked inside unavailableDecision(). Can we assert the generic message here too, or rename the test, so a reader doesn't take it the wrong way?

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.

added the generic message assertion directly in this test. it now checks both the client message and the database detail preserved in the cause.

@timothyw553

Copy link
Copy Markdown
Collaborator Author

the AI review flagged two blockers:

  • the expression evaluator could turn OPA failures into HTTP 500 errors.
  • list filtering could hide OPA failures as empty or partial results.

both fixes and their tests are already in #2078. this PR adds the OPA backend without enabling it in the server. i’ve kept those fixes in #2078 so OPA activation and the required API error handling land together.

@timothyw553
timothyw553 requested a review from yili-db November 3, 2026 22:45
@timothyw553

Copy link
Copy Markdown
Collaborator Author

/review

@timothyw553
timothyw553 merged commit 4b2c35e into unitycatalog:main Nov 3, 2026
19 of 20 checks passed
yili-db pushed a commit that referenced this pull request Nov 11, 2026
## 🥞 Stacked PR
Use this
[link](https://github.com/unitycatalog/unitycatalog/pull/2078/files) to
review incremental changes.
-
[stack/auth-resource-types](#2076)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2076/files)]
[MERGED]
-
[stack/opa-authorizer](#2077)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2077/files)]
[MERGED]
-
[**stack/opa-server-config**](#2078)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/2078/files)]
← _this PR_

---------
Signed-off-by: Timothy Wang <[email protected]>

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [x] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Make OPA selectable with `server.authorization.provider=opa`, a decision
URL, and a response timeout for each OPA call. Casbin remains the
default. Startup rejects OPA with authorization disabled or a
missing/invalid decision URL.

Pending OPA calls are cancelled when the UC request is cancelled or
times out.

OPA failures now reach callers as HTTP 503 through UC, Iceberg, and
Delta error handling. UC database failures keep their UC error codes.

Single authorization checks also preserve UC server error codes (HTTP
5xx) for both Casbin and OPA.

Behavior change for both Casbin and OPA: unexpected errors during list
authorization or resource resolution fail the whole request instead of
silently omitting affected items. Permission denials and client errors
(HTTP 4xx) still filter out individual items.

Add configuration documentation and a runnable Rego example. Native
grant/revoke/list APIs continue to manage native records; those grants
do not affect OPA decisions. UC still enforces local path and
staging-creator checks.

Follow-up:
[#2177](#2177) tracks
consistent masking of internal errors in the outer list-filter catch.

Validation:

- `build/sbt javafmtAll`.
- `server/testOnly` for `UnityCatalogServerTest`,
`ServerPropertiesTest`, `IcebergRestCatalogServiceTest`,
`ResultFilterTest`, `UnityAccessEvaluatorTest`, `OpaAuthorizerTest`, and
`SdkOpaAccessControlTest` on `6fb1cf5d1`: 138 tests passed, none
skipped. All four `SdkOpaAccessControlTest` cases ran against OPA in
Docker.

---------

Signed-off-by: Timothy Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants