Skip to content

feat(semantic-layer): support per-user OAuth2 authentication - #45894

Open
betodealmeida wants to merge 19 commits into
masterfrom
sl-oauth2
Open

betodealmeida wants to merge 19 commits into
masterfrom
sl-oauth2

Conversation

@betodealmeida

@betodealmeida betodealmeida commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

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.

Screenshot 2026-10-30 at 12 57 02 PM

After: Superset displays an OAuth2 authorization link and reloads the chart after authorization completes.

Screenshot 2026-10-30 at 12 53 34 PM Screenshot 2026-10-30 at 12 53 26 PM

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:

  1. A simple server implementing the API and exposing Pandas dataframes as semantic views.
  2. A Superset DB engine spec.
  3. A Superset semantic layer extension.

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:

  1. Run the server from the repo above:
% SEMANTIC_LAYER_REQUIRE_AUTH=1 semantic-api
  1. Build the extension from the repo above and install it in Superset.
  2. Add a new semantic layer connection. First fill in the values:
  1. Then click the "Connect account" in the dialog to trigger OAuth2.
  2. Add a semantic view and create a chart.
  3. Login as a different user and try to view the chart.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@github-actions github-actions Bot added risk:db-migration PRs that require a DB migration api Related to the REST API doc Namespace | Anything related to documentation labels Oct 30, 2026
Comment thread superset/semantic_layers/models.py Resolved
@netlify

netlify Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 1711506
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6afdcf6b2fb556104171fa1a
😎 Deploy Preview https://deploy-preview-45894--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the i18n Namespace | Anything related to localization label Oct 30, 2026
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.79666% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.50%. Comparing base (3a61b26) to head (1711506).
⚠️ Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
...src/features/semanticLayers/SemanticLayerModal.tsx 79.71% 14 Missing ⚠️
superset/extensions/storage/persistent_dao.py 87.50% 2 Missing and 2 partials ⚠️
.../components/ErrorMessage/OAuth2RedirectMessage.tsx 95.83% 1 Missing ⚠️
superset-frontend/src/pages/Chart/index.tsx 94.73% 1 Missing ⚠️
superset/core/api/core_api_injection.py 95.23% 1 Missing ⚠️
superset/datasource/api.py 87.50% 0 Missing and 1 partial ⚠️
superset/utils/encrypt.py 93.75% 0 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
hive 33.99% <32.15%> (-0.03%) ⬇️
javascript 81.98% <93.93%> (+0.05%) ⬆️
mysql 51.60% <32.81%> (-0.11%) ⬇️
postgres 51.63% <32.81%> (-0.11%) ⬇️
presto 35.68% <32.15%> (-0.04%) ⬇️
python 88.26% <98.45%> (+0.06%) ⬆️
sqlite 51.31% <32.81%> (-0.10%) ⬇️
unit 83.49% <98.45%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added the requires:rebase Requires rebasing on top of current master label Oct 31, 2026
@betodealmeida

Copy link
Copy Markdown
Member Author

@michael-s-molina can you take a look when you have the chance? Thanks!

@github-actions

This comment was marked as resolved.

Comment thread superset/semantic_layers/registry.py Resolved
Comment thread superset/semantic_layers/registry.py Resolved
Comment thread superset/extensions/storage/secrets.py Resolved
Comment thread superset/semantic_layers/registry.py Resolved
@github-actions github-actions Bot removed the requires:rebase Requires rebasing on top of current master label Nov 4, 2026
@betodealmeida

Copy link
Copy Markdown
Member Author

cc: @mikebridge

@github-actions github-actions Bot added the requires:rebase Requires rebasing on top of current master label Nov 18, 2026

@mikebridge mikebridge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 old rData. On re-entry fetchViews reads the current viewsGenRef, and fetchGenRef isn't advanced by runtime-form edits, so the stale response passes isCurrent(). This is the sibling of the runtime-schema thread you already fixed with runtimeDataRef.

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_storage table, and the previous release's read and list routes have no store predicate. 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.get reads through db.session while set uses 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.set takes DistributedLock before writing; with the default key-value lock that goes through db.session, so the "independent of the caller's transaction" guarantee doesn't seem to hold, and test_secret_changes_survive_outer_rollback patches 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 declaring GLOBAL containment scope, or caching catalogs on cache_token alone, 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.

@michael-s-molina

Copy link
Copy Markdown
Member

Thanks for this, Beto. The OAuth2 flow is a good use of the extension storage work, and reusing the existing encryption and the SecretsMigrator makes sense. I focused on the storage and migration side. I didn't review the frontend or the tests, so I have nothing to say about them.

Main question: discriminator column vs. separate table

Right now the only thing keeping browser-readable code away from secrets is that every query against extension_storage filters on store. ExtensionStorageDAO defaults store to PERSISTENT, so a forgotten argument fails safe for secrets, but nothing guarantees every path filters. For OAuth tokens I'd want that guarantee to be structural.

A separate table (roughly extension_id, user_fk, key, value, plus audit) would give us that:

  • The REST API and SDK can't reach it.
  • Quota is independent.
  • The migration is additive. The downgrade wouldn't need to delete rows to keep secrets from becoming browser-readable, so the UPDATING.md warning goes away.
  • We don't rebuild the unique constraint and index on extension_storage, which may be large.

Duplication should be small. Secrets don't need .shared, resource linking, list, codec choice or the encrypt flag. derive_user_key, _encrypt_value, _decrypt_value and the lock are already shared helpers.

If you'd rather keep one table, can you:

  1. Make store a required argument on every DAO method, with no default?
  2. Route all queries through a single scoped-query helper, and add a test or lint rule against direct db.session.query(ExtensionStorage) elsewhere?
  3. Add parametrized tests that seed a secret with the same key and call every REST route, expecting a miss?
  4. Confirm that user deletion, extension uninstall and any admin tooling handle secrets rows deliberately?

@mikebridge

Copy link
Copy Markdown
Member

A separate table (roughly extension_id, user_fk, key, value, plus audit) would give us that:

@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.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Possible DB migration conflict

@betodealmeida the base branch master picked up a new database migration under superset/migrations/ after this PR was opened, and this PR adds or modifies migration files too. Alembic migrations form a single linear chain, so if both branch off the same parent revision, merging this PR as-is could leave the migration history with two heads instead of one, which makes superset db upgrade fail with Multiple head revisions are present.

To check and, if needed, fix this:

  1. Rebase this branch onto the latest master.
  2. Run superset db heads. If it lists more than one revision, you have a conflict.
  3. In your new migration's script, update down_revision (and the Revises comment) to point at the other head instead, so the chain has a single head again.

See Merging DB Migrations in the contributing guide for a worked example.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Possible DB migration conflict

@betodealmeida the base branch master picked up a new database migration under superset/migrations/ after this PR was opened, and this PR adds or modifies migration files too. Alembic migrations form a single linear chain, so if both branch off the same parent revision, merging this PR as-is could leave the migration history with two heads instead of one, which makes superset db upgrade fail with Multiple head revisions are present.

To check and, if needed, fix this:

  1. Rebase this branch onto the latest master.
  2. Run superset db heads. If it lists more than one revision, you have a conflict.
  3. In your new migration's script, update down_revision (and the Revises comment) to point at the other head instead, so the chain has a single head again.

See Merging DB Migrations in the contributing guide for a worked example.

@villebro

Copy link
Copy Markdown
Member

On @michael-s-molina concern about shared vs dedicated table, my thoughts:

  • Having a shared table has the benefit of less maintenance burden, and there's less risk of divergent indexes etc.
  • We already store encrypted data in the persistent table ({ encrypt: true }), so there's precedent of sensitive data in it. Having a separate table for backend secrets may cause confusion on where sensitive data is stored.

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.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Possible DB migration conflict

@betodealmeida the base branch master picked up a new database migration under superset/migrations/ after this PR was opened, and this PR adds or modifies migration files too. Alembic migrations form a single linear chain, so if both branch off the same parent revision, merging this PR as-is could leave the migration history with two heads instead of one, which makes superset db upgrade fail with Multiple head revisions are present.

To check and, if needed, fix this:

  1. Rebase this branch onto the latest master.
  2. Run superset db heads. If it lists more than one revision, you have a conflict.
  3. In your new migration's script, update down_revision (and the Revises comment) to point at the other head instead, so the chain has a single head again.

See Merging DB Migrations in the contributing guide for a worked example.

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@michael-s-molina

Copy link
Copy Markdown
Member

My comment about column vs. separate table is not a blocker from me. @villebro suggestion below might address my concerns:

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.

I'll leave the call to @betodealmeida and I'm fine with a column or separate table.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Possible DB migration conflict

@betodealmeida the base branch master picked up a new database migration under superset/migrations/ after this PR was opened, and this PR adds or modifies migration files too. Alembic migrations form a single linear chain, so if both branch off the same parent revision, merging this PR as-is could leave the migration history with two heads instead of one, which makes superset db upgrade fail with Multiple head revisions are present.

To check and, if needed, fix this:

  1. Rebase this branch onto the latest master.
  2. Run superset db heads. If it lists more than one revision, you have a conflict.
  3. In your new migration's script, update down_revision (and the Revises comment) to point at the other head instead, so the chain has a single head again.

See Merging DB Migrations in the contributing guide for a worked example.

geido added a commit that referenced this pull request Nov 24, 2026
…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>
geido added a commit that referenced this pull request Nov 24, 2026
…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>
geido added a commit that referenced this pull request Nov 24, 2026
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>
geido added a commit that referenced this pull request Nov 24, 2026
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>
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Possible DB migration conflict

@betodealmeida the base branch master picked up a new database migration under superset/migrations/ after this PR was opened, and this PR adds or modifies migration files too. Alembic migrations form a single linear chain, so if both branch off the same parent revision, merging this PR as-is could leave the migration history with two heads instead of one, which makes superset db upgrade fail with Multiple head revisions are present.

To check and, if needed, fix this:

  1. Rebase this branch onto the latest master.
  2. Run superset db heads. If it lists more than one revision, you have a conflict.
  3. In your new migration's script, update down_revision (and the Revises comment) to point at the other head instead, so the chain has a single head again.

See Merging DB Migrations in the contributing guide for a worked example.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Related to the REST API doc Namespace | Anything related to documentation i18n Namespace | Anything related to localization packages requires:rebase Requires rebasing on top of current master risk:db-migration PRs that require a DB migration size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants