Skip to content

Support Modal OAuth client credentials on the modal connection - #77829

Merged
kaxil merged 2 commits into
apache:mainfrom
astronomer:modal-oauth-credentials
Dec 1, 2026
Merged

kaxil merged 2 commits into
apache:mainfrom
astronomer:modal-oauth-credentials

Conversation

@kaxil

@kaxil kaxil commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

The modal connection type models one of Modal's two ways to authenticate a client: an API token id and secret. Modal's SDK also accepts OAuth client credentials (modal.Client.from_oauth_credentials), which Modal describes as the method for managing Modal on behalf of third-party users. A deployment that is handed OAuth credentials instead of an API token cannot put them on the connection today, and has to set MODAL_OAUTH_* environment variables on every worker.

This adds three optional extra fields to the connection: oauth_refresh_token, oauth_client_id and oauth_client_secret. When all three are set and login/password are empty, ModalHook builds its client with from_oauth_credentials, on both the sync path and the async path the trigger uses.

export AIRFLOW_CONN_MODAL_DEFAULT='{"conn_type": "modal", "extra": {"oauth_refresh_token": "...", "oauth_client_id": "oc-...", "oauth_client_secret": "ov-..."}}'

I ran it against modal 1.6.3 and Modal's live API with placeholder credentials, as a Dag task using a connection saved through the UI: the hook resolves ModalOAuthCredentials(client_id='oc-not-real'), and both test_connection and lookup_app come back with Modal's AuthError: OAuth refresh token is malformed, so the three values reach Modal as OAuth credentials. A successful OAuth login was not exercised: that needs a Modal-issued OAuth client.

Design rationale

Half-filled and mixed connections raise instead of guessing. The hook already raises ModalConnectionError when one of the two token fields is set, because falling back to ambient credentials would hide the misconfiguration. The same rule now covers one or two of the three OAuth fields, and a token field set together with an OAuth field. The Modal SDK rejects the token-plus-OAuth combination on its own ambient path too.

The credentials live in the extra, not in new columns or a second connection type. The extra is encrypted at rest, and Airflow's secrets masker redacts oauth_refresh_token and oauth_client_secret by name, so they come back as *** from the REST API. A separate connection type would split one hook across two forms for the same service.

ModalHook.credentials now returns a token pair, a ModalOAuthCredentials, or None. The Common AI Modal sandbox backend reads credentials is not None as "the credentials came from the connection", and that stays true for OAuth connections. Its rejected-credentials message said "Update its login and password", which is reworded here because it would be wrong for an OAuth connection.

The async path reads the extra with get_async_extra_dejson. Until now get_async_client only read login and password. Connection.extra_dejson masks secrets through a synchronous supervisor call, which must not run on the triggerer's event loop, so the async path awaits the common.compat helper and passes the dict to the shared validator.

Gotchas

  • The modal floor moves from 1.5.0 to 1.5.5: Client.from_oauth_credentials is absent in 1.5.3 and present in 1.5.5.
  • Testing an OAuth connection leaves one open client behind per test in the process that ran it. Client.verify opens and closes a throwaway client but takes only a token pair, and modal has no public Client.close() before 1.6.4. The docs say so. Testing an API token connection is unchanged.
  • from_oauth_credentials gained an oauth_jwt_key alternative to the client secret in modal 1.6.3 (it needs the modal[oauth] extra). This PR covers the client-secret form only.

Modal connection form with the OAuth fields

The modal connection type only modelled an API token, so a deployment that
was given Modal OAuth client credentials had to fall back to MODAL_OAUTH_*
environment variables on every worker.

The connection now accepts oauth_refresh_token, oauth_client_id and
oauth_client_secret in its extra. With all three set and login/password
empty, ModalHook builds the client with Client.from_oauth_credentials, on
both the sync and the async (trigger) path. A token field together with an
OAuth field, or only some of the OAuth fields, raises ModalConnectionError
instead of falling back to ambient credentials.

Client.from_oauth_credentials first shipped in modal 1.5.5, so the floor
moves from 1.5.0 to 1.5.5.

The async path now reads the connection extra with get_async_extra_dejson,
since Connection.extra_dejson masks secrets through a synchronous
supervisor call that must not run on the triggerer's event loop.

The Common AI Modal sandbox backend's rejected-credentials message no
longer says "login and password", which would be wrong for an OAuth
connection.
Comment thread providers/modal/docs/connections.rst Resolved
The "What this provider does not do yet" list still said the connection
supports token authentication only, which contradicted the new OAuth
section. It now names what is still unsupported, OAuth clients that use a
JWT key, and points at the connection docs.
@kaxil
kaxil enabled auto-merge (squash) December 1, 2026 17:55
@kaxil
kaxil merged commit d79abe6 into apache:main Dec 1, 2026
93 checks passed
@kaxil
kaxil deleted the modal-oauth-credentials branch December 1, 2026 22:16
@shahar1 shahar1 mentioned this pull request Dec 2, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants