Repository navigation
feat(semantic-layer): support per-user OAuth2 authentication - #45894
betodealmeida wants to merge 19 commits into
Conversation
9a5af7e to
76ee7e9
Compare
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #45894 +/- ##
==========================================
+ Coverage 85.44% 85.50% +0.06%
==========================================
Files 3100 3104 +4
Lines 213634 214402 +768
Branches 50430 50607 +177
==========================================
+ Hits 182533 183320 +787
+ Misses 27675 27650 -25
- Partials 3426 3432 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
61fde49 to
088cf58
Compare
|
@michael-s-molina can you take a look when you have the chance? Thanks! |
This comment was marked as resolved.
This comment was marked as resolved.
088cf58 to
ecb9fb8
Compare
6728a7f to
1711506
Compare
|
cc: @mikebridge |
mikebridge
left a comment
There was a problem hiding this comment.
Thanks for this, Beto. I went through it at 1711506b4e by reading only: nothing was run, so please treat anything below as a question where I may have missed context. The earlier threads from Joe and Ville look addressed at this head, apart from the two I note below.
The one thing I'd like to settle before this merges: who owns token scoping when a layer's configuration changes? SecretsState scopes by extension and user (superset/extensions/storage/secrets.py:43), and the action hooks don't receive the layer's identity, so a provider has nothing stable to key on and the host can't purge. If a provider uses a fixed key and someone repoints a layer's host, connected users' tokens would be sent to the new host; two layers of the same type would also share them. Could the host namespace the key by layer UUID and clear it on configuration change or delete? If I've missed where this is handled, please point me at it.
Two smaller ones that look like quick fixes:
- Guests get an authorization link that can't succeed.
check_for_oauth2(superset/semantic_layers/oauth2.py:43) builds the redirect error for any caller, but the start route refuses a missing user (superset/semantic_layers/api.py:1281). An embedded viewer sees "provide authorization" and lands on a 401. A non-interactive refusal for guest and anonymous principals would avoid that. - The view-discovery retry can replay stale form data. In
AddSemanticViewModal.tsx, the retry stored at line 318 closes over the oldrData. On re-entryfetchViewsreads the currentviewsGenRef, andfetchGenRefisn't advanced by runtime-form edits, so the stale response passesisCurrent(). This is the sibling of the runtime-schema thread you already fixed withruntimeDataRef.
Two tradeoffs I think are fine but worth a line in UPDATING.md:
- Caches are per-user for every semantic view, including shared-credential layers (
superset/semantic_layers/models.py:931,superset/datasource/api.py:101,:746). Results are correct either way and this is the safe default. Operators will see a one-time cold cache on deploy, and viewers of the same dashboard no longer share results or warm-up entries. Is that meant to be permanent, or would you want a way for a provider to declare its results user-independent later? - First rollout only: secrets share the
extension_storagetable, and the previous release's read and list routes have nostorepredicate. While old and new processes overlap on the upgrade that introduces this, or after an application rollback without the database downgrade, an old process could return a user's own token to that user's browser through the persistent-storage API. A note saying not to enable an OAuth-using extension until the rollout is complete would cover it.
Some that may be fine as documented provider obligations, but I wanted to ask:
secrets.getreads throughdb.sessionwhilesetuses a new session, so a read after a refresh in the same request can return the pre-refresh token (secrets.py:45). With rotating refresh tokens that could trigger reuse detection.ExtensionStorageDAO.settakesDistributedLockbefore writing; with the default key-value lock that goes throughdb.session, so the "independent of the caller's transaction" guarantee doesn't seem to hold, andtest_secret_changes_survive_outer_rollbackpatches the lock out.- There's no host helper to serialize token refresh across a dashboard's parallel requests.
- Personal credentials run under
ExecutionMethod.CONNECTION, whose docstring still says results must not depend on the context. A provider declaringGLOBALcontainment scope, or caching catalogs oncache_tokenalone, wouldn't be user-partitioned. - The action hooks don't receive the host transport, so OAuth traffic sits outside the egress policy the other provider calls get.
- State, PKCE and callback-to-user binding are left to each provider. Would a small helper in superset-core, or a host-minted signed state as the database OAuth2 flow has, be worth it?
Minor notes: str(self.uuid) gives "None" for a transient layer (models.py:336); ENCRYPTED_VALUE_PREFIX is stripped on read but never written; unmask_configuration_for_update reads the schema outside the extension context; the migration file is dated before its parent, and its test covers SQLite only.
On ordering: the branch currently conflicts with master in superset/semantic_layers/api.py and tests/unit_tests/semantic_layers/models_test.py. There are several small semantic-layer fixes open that touch neighbouring code (#45995, #46330, #46329, #46333, #46338, #46339, #46126). The overlaps are light, mostly different hunks. If it's all right with you, I'd suggest those land first and this rebases after, since the migration parent needs re-checking then anyway.
The migration itself reads correctly to me, and down_revision is the single head on master today.
|
Thanks for this, Beto. The OAuth2 flow is a good use of the extension storage work, and reusing the existing encryption and the Main question: discriminator column vs. separate tableRight now the only thing keeping browser-readable code away from secrets is that every query against A separate table (roughly
Duplication should be small. Secrets don't need If you'd rather keep one table, can you:
|
@michael-s-molina I agree with the separate table---that would address the rollout concern that I had below (or above, depending on which PR ui version we're using, I guess). With secrets in their own table, the previous release's read and list routes can't reach them during a rolling deploy, so the mixed-version window wouldn't be an issue. |
|
1 similar comment
|
|
On @michael-s-molina concern about shared vs dedicated table, my thoughts:
Regarding the risk of leaking data, I think we can control that easily by encrypting backend secrets with a key that only the secrets store has access to. This should completely eliminate any confused deputy problem, as leaking a row to the wrong audience would just expose garbled data. |
|
rusackas
left a comment
There was a problem hiding this comment.
Thanks @betodealmeida, nice to see OAuth2 make its way to semantic layers.
Not approving just yet, since a couple of open questions seem worth settling first: the separate table vs. shared table discussion between @michael-s-molina and @villebro, and @mikebridge's question about scoping tokens per layer when a layer gets repointed. FWIW I'm inclined toward the separate table, mostly because it keeps the migration additive and the downgrade non-destructive, but happy to defer to you all on that one.
It'll also need a rebase, as GitHub shows conflicts. Mike's idea of landing the smaller semantic-layer fixes first and rebasing once after seems sensible to me.
|
My comment about column vs. separate table is not a blocker from me. @villebro suggestion below might address my concerns:
I'll leave the call to @betodealmeida and I'm fine with a column or separate table. |
|
…ext-mcp-tasks The helper this branch used to carry now lands once, on geido/extension-context-user, shaped as #45894 shapes the extension context and passing the user the call runs for; this branch keeps only what runs an extension's MCP tools, prompts and tasks in its context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vents The extension context helper this branch carried now lands once, on geido/extension-context-user, shaped as #45894 shapes the extension context and passing the user the call runs for. Draft listeners, step handlers and commit participants keep running in their extension's context, for the caller's user. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The extension context helper lands once, from its own branch, shaped as #45894 shapes the context and passing the user the call runs for; the copies three branches carried resolve to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Persistent storage's set and compare_and_set committed in a Session of their own, without @transaction(), for every extension, under a storage lock, and had no clean branch or changelog line. Their only consumer, the artifacts extension's default store, refuses to run until extension storage can scope rows and list keys, and #45894 rewrites this DAO meanwhile. Storage, its tests and its docs are master's again: set runs under @transaction() on the request's session, and there is no compare-and-swap write until something can use one, shaped on #45894's DAO with a changelog line and a test that reads after a write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
SUMMARY
Adds support for per-user OAuth2 authentication in Semantic Layer extensions.
With this PR, extensions can now store access and refresh tokens in encrypted, user-scoped storage that is unavailable through browser-facing extension storage APIs. Tokens remain scoped to the user who authorized access.
Semantic Layer extensions reuse Superset’s existing OAuth2 error handling and authorization UI. Authorization can be triggered from configuration dialogs, Explore, and dashboard queries. Completing authorization retries the interrupted operation. Semantic view metadata loading can require authentication before Explore renders, so that path displays the authorization prompt before loading the editor.
The change also preserves user identity in asynchronous query execution and partitions authenticated semantic-view caches by user.
A database migration adds the storage access classification. Downgrading permanently deletes backend-only secrets to prevent them from becoming browser-accessible; this behavior is documented in
UPDATING.md.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Before: opening a semantic-view chart without credentials could show a permission error and “Missing datasource,” with no way to authorize access.
After: Superset displays an OAuth2 authorization link and reloads the chart after authorization completes.
Note that the behavior in Explore is slightly different with semantic views compared to datasets: with a dataset that needs OAuth2, Explore will render with metrics/columns/controls, but the dataset component will ask for auth. For semantic views, Explore doesn't know what metrics and columns exist until the user authenticates, since that information lives in the semantic layer, not in Superset. Because of this, the whole Explore UI is replaced by the authentication prompt.
TESTING INSTRUCTIONS
I wrote the specification for a generic RESTful semantic layer API at https://github.com/betodealmeida/sl-rest-api. The API is a 1:1 mapping of the Superset semantic layer Python API to JSON requests. In addition to the spec itself, the repo has:
I'm also in the process of implementing the API in DataJunction, so that Superset can be used with it: DataJunction/dj#2661
A simple way of testing this PR:
demo-clientdemo-secretADDITIONAL INFORMATION