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
Expand Up @@ -111,7 +111,7 @@
EgressEnforcement = Literal["strict", "sni"]

# What reading credentials from the connection can raise before Modal is reached at all: a
# named connection that does not exist, or one that sets only half of the token.
# named connection that does not exist, or one whose credential fields are half filled.
_CREDENTIAL_ERRORS = (AirflowNotFoundException, ModalConnectionError)
# Serializes building a backend's Modal client. ModalHook resolves its connection and builds
# its client without a lock, so concurrent first calls on a backend shared by several agent
Expand Down Expand Up @@ -1093,10 +1093,10 @@ def _as_sandbox_error(self, error: modal.exception.Error, *, sandbox: str | None
return SandboxError(f"The command hit its deadline: {error}")
if isinstance(error, modal.exception.AuthError):
if self._hook.credentials is not None:
# The token came from the connection, so worker settings would change nothing.
# The credentials came from the connection, so worker settings would change nothing.
return SandboxTerminalError(
f"Modal rejected the token on connection {self._hook.modal_conn_id!r}. Update its "
f"login and password: {error}"
f"Modal rejected the credentials on connection {self._hook.modal_conn_id!r}. "
f"Update the connection: {error}"
)
return SandboxTerminalError(
"Modal rejected the credentials. Create a 'modal' connection, run 'modal token "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def test_a_rejected_connection_token_points_at_the_connection(
modal_connection()
fake.Sandbox.create_error = fake.exception.AuthError("token revoked")

with pytest.raises(SandboxTerminalError, match="token on connection 'my_modal'") as raised:
with pytest.raises(SandboxTerminalError, match="credentials on connection 'my_modal'") as raised:
backend_class(modal_conn_id="my_modal").create(spec=SandboxSpec())
assert "MODAL_TOKEN_ID" not in str(raised.value)

Expand Down
2 changes: 1 addition & 1 deletion providers/modal/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ PIP package Version required
========================================== ==================
``apache-airflow`` ``>=3.0.0``
``apache-airflow-providers-common-compat`` ``>=1.12.0``
``modal`` ``>=1.5.0``
``modal`` ``>=1.5.5``
========================================== ==================

The changelog for the provider package can be found in the
Expand Down
68 changes: 56 additions & 12 deletions providers/modal/docs/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
Modal Connection
================

The `Modal <https://modal.com/>`__ connection type holds a Modal API token and, optionally,
the Modal environment to work in. :class:`~airflow.providers.modal.hooks.modal.ModalHook`
turns it into a ``modal.Client`` that other Modal SDK calls accept through their ``client=``
argument.
The `Modal <https://modal.com/>`__ connection type holds a Modal API token or Modal OAuth
client credentials and, optionally, the Modal environment to work in.
:class:`~airflow.providers.modal.hooks.modal.ModalHook` turns it into a ``modal.Client`` that
other Modal SDK calls accept through their ``client=`` argument.

Default Connection IDs
----------------------
Expand All @@ -41,14 +41,47 @@ Token Secret (password)
The Modal token secret (``as-...``) that pairs with the token id.

Extra (optional)
A JSON dictionary. One key is recognized:
A JSON dictionary. These keys are recognized:

* ``environment``: the Modal `environment <https://modal.com/docs/guide/environments>`__
(for example ``main`` or ``dev``) that ``ModalHook.lookup_app`` scopes app lookups to.
Sandboxes inherit the environment from their app. When unset, the Modal SDK resolves it
from ``MODAL_ENVIRONMENT`` or the active profile in ``~/.modal.toml``.
* ``oauth_refresh_token``, ``oauth_client_id``, ``oauth_client_secret``: Modal OAuth client
credentials, see :ref:`howto/connection:modal:oauth`.

The Modal *workspace* is not a connection field: Modal derives it from the token.
The Modal *workspace* is not a connection field: Modal derives it from the credentials.

.. _howto/connection:modal:oauth:

OAuth client credentials
Comment thread
kaxil marked this conversation as resolved.
Resolved
------------------------

Modal has a second way to authenticate a client, meant for managing Modal on behalf of
another user: an OAuth refresh token together with the id and secret of a Modal-issued OAuth
client. Use it when you were given those three values instead of an API token. Leave Token ID
and Token Secret empty and set all three in the extra:

``oauth_refresh_token``
The refresh token that Modal's token endpoint returned.

``oauth_client_id``
The Modal-issued OAuth client id (``oc-...``).

``oauth_client_secret``
The Modal-issued OAuth client secret (``ov-...``).

The hook passes them to ``modal.Client.from_oauth_credentials``. Airflow encrypts the whole
extra at rest, and masks the refresh token and the client secret in logs and when the
connection is read back through the REST API.

The hook reads the refresh token from the connection and never writes one back. If Modal
issues you a new refresh token, update the connection.

Testing an OAuth connection from the UI or with ``airflow connections test`` leaves one open
client behind per test in the process that ran it, until that process exits. The Modal SDK
can open and close a throwaway client only for an API token, and before 1.6.4 has no public
way to close any other client. Testing an API token connection does not have this cost.

Credential precedence
---------------------
Expand All @@ -57,12 +90,17 @@ The hook resolves credentials in this order:

1. Token id and secret on the connection. The client is built from exactly those, so a
worker that also has ``MODAL_TOKEN_ID`` in its environment still uses the connection.
2. A connection with neither token field set, or no ``modal_default`` connection at all.
The Modal SDK's own resolution applies: ``MODAL_TOKEN_ID`` / ``MODAL_TOKEN_SECRET`` from
the environment, then the active profile in ``~/.modal.toml``. This keeps a worker that
already ran ``modal token new`` working without any Airflow configuration.
3. Exactly one of the two token fields set. The hook raises: a half-filled connection is a
misconfiguration, and falling back to ambient credentials would hide it.
2. All three OAuth fields in the extra, with Token ID and Token Secret empty. The client is
built from exactly those.
3. A connection with no credential field set, or no ``modal_default`` connection at all.
The Modal SDK's own resolution applies: ``MODAL_TOKEN_ID`` / ``MODAL_TOKEN_SECRET``, or
``MODAL_OAUTH_REFRESH_TOKEN`` / ``MODAL_OAUTH_CLIENT_ID`` / ``MODAL_OAUTH_CLIENT_SECRET``,
from the environment, then the active profile in ``~/.modal.toml``. This keeps a worker
that already ran ``modal token new`` working without any Airflow configuration.
4. Anything in between. The hook raises when exactly one of the two token fields is set,
when one or two of the three OAuth fields are set, or when a token field and an OAuth
field are both set: a half-filled connection is a misconfiguration, and falling back to
ambient credentials would hide it.

A connection id other than ``modal_default`` that does not exist is always an error, since
the author named it on purpose.
Expand All @@ -82,4 +120,10 @@ URI form:

export AIRFLOW_CONN_MODAL_DEFAULT='modal://ak-...:as-...@?environment=main'

OAuth client credentials, JSON form:

.. code-block:: bash

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

For invoking deployed functions and running sandboxes through the hook, see :doc:`usage`.
2 changes: 1 addition & 1 deletion providers/modal/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ PIP package Version required
========================================== ==================
``apache-airflow`` ``>=3.0.0``
``apache-airflow-providers-common-compat`` ``>=1.12.0``
``modal`` ``>=1.5.0``
``modal`` ``>=1.5.5``
========================================== ==================

Downloading official packages
Expand Down
9 changes: 5 additions & 4 deletions providers/modal/docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ What this provider does not do yet
* No operators or sensors for deployed functions; blocking calls in a ``@task`` are the
pattern. ``@task.modal`` (:ref:`howto/decorator:modal`) is the one deferrable path.
* No executor. Tasks run on Airflow workers and call out to Modal; they do not run inside Modal.
* Token authentication only. Modal's OAuth client credentials are not modeled on the
connection.
* *Test connection* on the connection form checks that the token authenticates against the
Modal API. It does not check that the ``environment`` exists or that any app is deployed.
* No OAuth clients that authenticate with a JWT key (``oauth_jwt_key``). The connection takes
an API token or OAuth client credentials with a client secret, see
:ref:`howto/connection:modal:oauth`.
* *Test connection* on the connection form checks that the credentials authenticate against
the Modal API. It does not check that the ``environment`` exists or that any app is deployed.
25 changes: 25 additions & 0 deletions providers/modal/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ connection-types:
Modal environment to scope lookups and sandboxes to (for example main or dev).
Leave empty to let the Modal SDK resolve it from MODAL_ENVIRONMENT or the active
profile in ~/.modal.toml.
oauth_refresh_token:
label: OAuth Refresh Token
schema:
type:
- string
- 'null'
format: password
description: >-
OAuth refresh token issued by Modal's token endpoint. Set together with the OAuth
client id and secret, and leave Token ID and Token Secret empty.
oauth_client_id:
label: OAuth Client ID
schema:
type:
- string
- 'null'
description: Modal-issued OAuth client id (oc-...).
oauth_client_secret:
label: OAuth Client Secret
schema:
type:
- string
- 'null'
format: password
description: Modal-issued OAuth client secret (ov-...).
ui-field-behaviour:
hidden-fields:
- host
Expand Down
2 changes: 1 addition & 1 deletion providers/modal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ requires-python = ">=3.11"
dependencies = [
"apache-airflow>=3.0.0",
"apache-airflow-providers-common-compat>=1.12.0", # use next version
"modal>=1.5.0",
"modal>=1.5.5",
]

[dependency-groups]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,22 @@ def get_provider_info():
"label": "Environment",
"schema": {"type": ["string", "null"]},
"description": "Modal environment to scope lookups and sandboxes to (for example main or dev). Leave empty to let the Modal SDK resolve it from MODAL_ENVIRONMENT or the active profile in ~/.modal.toml.",
}
},
"oauth_refresh_token": {
"label": "OAuth Refresh Token",
"schema": {"type": ["string", "null"], "format": "password"},
"description": "OAuth refresh token issued by Modal's token endpoint. Set together with the OAuth client id and secret, and leave Token ID and Token Secret empty.",
},
"oauth_client_id": {
"label": "OAuth Client ID",
"schema": {"type": ["string", "null"]},
"description": "Modal-issued OAuth client id (oc-...).",
},
"oauth_client_secret": {
"label": "OAuth Client Secret",
"schema": {"type": ["string", "null"], "format": "password"},
"description": "Modal-issued OAuth client secret (ov-...).",
},
},
"ui-field-behaviour": {
"hidden-fields": ["host", "schema", "port"],
Expand Down
Loading