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
65 changes: 52 additions & 13 deletions astronomer_starship/_af2/starship_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from functools import partial
from functools import partial, wraps
from http import HTTPStatus
from typing import TYPE_CHECKING

Expand All @@ -20,8 +20,36 @@
from typing import Callable


# restrict access to users with access to the Airflow configuration, which is more or less equivalent to admin access.
requires_config_read = auth.has_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG)])
# Baseline for every route: users with access to the Airflow configuration.
# Routes that read or write Airflow resources add the matching resource permission through `requires_access`.
CONFIG_READ = (permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG)
requires_config_read = auth.has_access([CONFIG_READ])

READ = permissions.ACTION_CAN_READ
CREATE = permissions.ACTION_CAN_CREATE
EDIT = permissions.ACTION_CAN_EDIT
DELETE = permissions.ACTION_CAN_DELETE


def requires_access(**permissions_by_method):
"""`requires_config_read`, plus the FAB permissions listed for the request method, for example
`@requires_access(GET=[(READ, permissions.RESOURCE_POOL)], POST=[(CREATE, permissions.RESOURCE_POOL)])`
"""

def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
return auth.has_access([CONFIG_READ, *permissions_by_method[request.method]])(f)(*args, **kwargs)

return wrapper

return decorator


def _crud_access(resource, *methods):
"""`requires_access` with the FAB action matching each request method"""
actions = {"GET": READ, "POST": CREATE, "PATCH": EDIT, "DELETE": DELETE}
return requires_access(**{method: [(actions[method], resource)] for method in methods})


def starship_route( # noqa: C901
Expand Down Expand Up @@ -141,14 +169,15 @@ def info(self) -> str:
starship_compat = StarshipCompatabilityLayer()
return starship_route(get=starship_compat.get_info)

@requires_config_read
# Environment variables can hold connections (`AIRFLOW_CONN_*`) and variables (`AIRFLOW_VAR_*`).
@requires_access(GET=[(READ, permissions.RESOURCE_CONNECTION), (READ, permissions.RESOURCE_VARIABLE)])
@expose("/env_vars", methods=["GET"])
@csrf.exempt
def env_vars(self):
starship_compat = StarshipCompatabilityLayer()
return starship_route(get=starship_compat.get_env_vars)

@requires_config_read
@_crud_access(permissions.RESOURCE_POOL, "GET", "POST", "DELETE")
@expose("/pools", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def pools(self):
Expand All @@ -160,7 +189,7 @@ def pools(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.pool_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_VARIABLE, "GET", "POST", "DELETE")
@expose("/variables", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def variables(self):
Expand All @@ -172,7 +201,7 @@ def variables(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.variable_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_CONNECTION, "GET", "POST", "DELETE")
@expose("/connections", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def connections(self):
Expand All @@ -184,7 +213,7 @@ def connections(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.connection_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_DAG, "GET", "PATCH")
@expose("/dags", methods=["GET", "PATCH"])
@csrf.exempt
def dags(self):
Expand All @@ -195,7 +224,7 @@ def dags(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.dag_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_DAG_RUN, "GET", "POST", "DELETE")
@expose("/dag_runs", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def dag_runs(self):
Expand All @@ -207,7 +236,7 @@ def dag_runs(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.dag_runs_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_TASK_INSTANCE, "GET", "POST")
@expose("/task_instances", methods=["GET", "POST"])
@csrf.exempt
def task_instances(self):
Expand All @@ -218,7 +247,7 @@ def task_instances(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_instances_attrs()),
)

@requires_config_read
@_crud_access(permissions.RESOURCE_TASK_INSTANCE, "GET", "POST")
@expose("/task_instance_history", methods=["GET", "POST"])
@csrf.exempt
def task_instance_history(self):
Expand All @@ -229,7 +258,13 @@ def task_instance_history(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_instance_history_attrs()),
)

@requires_config_read
# FAB has no create or delete permission for task logs, and no create permission for XComs.
# These routes use the task instance permissions instead.
@requires_access(
GET=[(READ, permissions.RESOURCE_TASK_LOG)],
POST=[(CREATE, permissions.RESOURCE_TASK_INSTANCE)],
DELETE=[(DELETE, permissions.RESOURCE_TASK_INSTANCE)],
)
@expose("/task_log", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def task_logs(self):
Expand All @@ -241,7 +276,11 @@ def task_logs(self):
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_log_attrs()),
)

@requires_config_read
@requires_access(
GET=[(READ, permissions.RESOURCE_XCOM)],
POST=[(CREATE, permissions.RESOURCE_TASK_INSTANCE)],
DELETE=[(DELETE, permissions.RESOURCE_XCOM)],
)
@expose("/xcom", methods=["GET", "POST", "DELETE"])
@csrf.exempt
def xcom(self):
Expand Down
116 changes: 103 additions & 13 deletions astronomer_starship/_af3/starship_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
from functools import partial
from typing import TYPE_CHECKING, Annotated

from airflow.api_fastapi.app import get_auth_manager
from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity, DagDetails
from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.core_api.security import requires_access_configuration
from airflow.api_fastapi.core_api.security import (
GetUserDep,
requires_access_configuration,
requires_access_connection,
requires_access_pool,
requires_access_variable,
)
from airflow.plugins_manager import AirflowPlugin
from fastapi import Depends, FastAPI, Request
from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from starlette.concurrency import run_in_threadpool

from astronomer_starship._af3.starship_compatability import StarshipAirflow, StarshipCompatabilityLayer
from astronomer_starship.common import HttpError, get_kwargs_fn, telescope
Expand Down Expand Up @@ -128,9 +137,67 @@ async def starship_compat() -> StarshipAirflow:
return StarshipCompatabilityLayer()


async def _dag_ids_in_request(request: Request) -> "set[str]":
"""Every `dag_id` the request names, in the query string, the JSON body, or the records of a bulk JSON body.

Starship handlers read `dag_id` from the body before the query string (see `get_from_request`),
and bulk bodies (e.g. `{"dag_runs": [...]}`) name a DAG per record, so all of them must be authorized.
"""
dag_ids = [request.query_params.get("dag_id")]
if await request.body():
try:
body = await request.json()
except ValueError:
raise HTTPException(400, "Request body is not valid JSON") from None
if isinstance(body, dict):
dag_ids.append(body.get("dag_id"))
for value in body.values():
if isinstance(value, list):
dag_ids.extend(item.get("dag_id") for item in value if isinstance(item, dict))
if any(dag_id is not None and not isinstance(dag_id, str) for dag_id in dag_ids):
raise HTTPException(400, "'dag_id' must be a string")
return {dag_id for dag_id in dag_ids if dag_id and dag_id != "~"}


def _is_authorized_dags(method, access_entity, dag_ids, user) -> bool:
from airflow.configuration import conf
from airflow.models import DagModel

# Airflow 3.1+ can scope DAGs to teams
multi_team = conf.getboolean("core", "multi_team", fallback=False) and hasattr(DagModel, "get_team_name")

def details(dag_id):
if dag_id and multi_team:
return DagDetails(id=dag_id, team_name=DagModel.get_team_name(dag_id))
return DagDetails(id=dag_id)

return get_auth_manager().batch_is_authorized_dag(
requests=[
{"method": method, "access_entity": access_entity, "details": details(dag_id)}
for dag_id in sorted(dag_ids) or [None]
],
user=user,
)


def requires_access_dags(method, access_entity=None):
"""Like Airflow's `requires_access_dag`, but for every DAG the request names, see `_dag_ids_in_request`.
Without a `dag_id`, the user needs access to all DAGs.
"""

async def inner(request: Request, user: GetUserDep) -> None:
dag_ids = await _dag_ids_in_request(request)
# The auth manager may query the database, keep it off the event loop
if not await run_in_threadpool(_is_authorized_dags, method, access_entity, dag_ids, user):
raise HTTPException(403, "Forbidden")

return inner


router = AirflowRouter(
tags=["Starship"],
# restrict access to users with access to the Airflow configuration, which is more or less equivalent to admin access.
# Baseline for every route: users with access to the Airflow configuration.
# Routes that read or write Airflow resources add the matching resource check.
dependencies=[Depends(requires_access_configuration("GET"))],
)

Expand Down Expand Up @@ -171,15 +238,21 @@ def info(
):
return starship_route(get=starship_compat.get_info)

@router.get("/env_vars")
@router.get(
"/env_vars",
# Environment variables can hold connections (`AIRFLOW_CONN_*`) and variables (`AIRFLOW_VAR_*`).
dependencies=[Depends(requires_access_connection("GET")), Depends(requires_access_variable("GET"))],
)
@staticmethod
def env_vars(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
starship_compat: Annotated[StarshipAirflow, Depends(starship_compat)],
):
return starship_route(get=starship_compat.get_env_vars)

@router.api_route("/pools", methods=["GET", "POST", "DELETE"])
@router.get("/pools", dependencies=[Depends(requires_access_pool("GET"))])
@router.post("/pools", dependencies=[Depends(requires_access_pool("POST"))])
@router.delete("/pools", dependencies=[Depends(requires_access_pool("DELETE"))])
@staticmethod
def pools(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -192,7 +265,9 @@ def pools(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.pool_attrs()),
)

@router.api_route("/variables", methods=["GET", "POST", "DELETE"])
@router.get("/variables", dependencies=[Depends(requires_access_variable("GET"))])
@router.post("/variables", dependencies=[Depends(requires_access_variable("POST"))])
@router.delete("/variables", dependencies=[Depends(requires_access_variable("DELETE"))])
@staticmethod
def variables(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -205,7 +280,9 @@ def variables(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.variable_attrs()),
)

@router.api_route("/connections", methods=["GET", "POST", "DELETE"])
@router.get("/connections", dependencies=[Depends(requires_access_connection("GET"))])
@router.post("/connections", dependencies=[Depends(requires_access_connection("POST"))])
@router.delete("/connections", dependencies=[Depends(requires_access_connection("DELETE"))])
@staticmethod
def connections(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -218,7 +295,8 @@ def connections(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.connection_attrs()),
)

@router.api_route("/dags", methods=["GET", "PATCH"])
@router.get("/dags", dependencies=[Depends(requires_access_dags("GET"))])
@router.patch("/dags", dependencies=[Depends(requires_access_dags("PUT"))])
@staticmethod
def dags(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -230,7 +308,9 @@ def dags(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.dag_attrs()),
)

@router.api_route("/dag_runs", methods=["GET", "POST", "DELETE"])
@router.get("/dag_runs", dependencies=[Depends(requires_access_dags("GET", DagAccessEntity.RUN))])
@router.post("/dag_runs", dependencies=[Depends(requires_access_dags("POST", DagAccessEntity.RUN))])
@router.delete("/dag_runs", dependencies=[Depends(requires_access_dags("DELETE", DagAccessEntity.RUN))])
@staticmethod
def dag_runs(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -243,7 +323,8 @@ def dag_runs(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.dag_runs_attrs()),
)

@router.api_route("/task_instances", methods=["GET", "POST"])
@router.get("/task_instances", dependencies=[Depends(requires_access_dags("GET", DagAccessEntity.TASK_INSTANCE))])
@router.post("/task_instances", dependencies=[Depends(requires_access_dags("POST", DagAccessEntity.TASK_INSTANCE))])
@staticmethod
def task_instances(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -255,7 +336,12 @@ def task_instances(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_instances_attrs()),
)

@router.api_route("/task_instance_history", methods=["GET", "POST"])
@router.get(
"/task_instance_history", dependencies=[Depends(requires_access_dags("GET", DagAccessEntity.TASK_INSTANCE))]
)
@router.post(
"/task_instance_history", dependencies=[Depends(requires_access_dags("POST", DagAccessEntity.TASK_INSTANCE))]
)
@staticmethod
def task_instance_history(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -267,7 +353,9 @@ def task_instance_history(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_instance_history_attrs()),
)

@router.api_route("/task_log", methods=["GET", "POST", "DELETE"])
@router.get("/task_log", dependencies=[Depends(requires_access_dags("GET", DagAccessEntity.TASK_LOGS))])
@router.post("/task_log", dependencies=[Depends(requires_access_dags("POST", DagAccessEntity.TASK_INSTANCE))])
@router.delete("/task_log", dependencies=[Depends(requires_access_dags("DELETE", DagAccessEntity.TASK_INSTANCE))])
@staticmethod
def task_logs(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand All @@ -280,7 +368,9 @@ def task_logs(
kwargs_fn=partial(get_kwargs_fn, attrs=starship_compat.task_log_attrs()),
)

@router.api_route("/xcom", methods=["GET", "POST", "DELETE"])
@router.get("/xcom", dependencies=[Depends(requires_access_dags("GET", DagAccessEntity.XCOM))])
@router.post("/xcom", dependencies=[Depends(requires_access_dags("POST", DagAccessEntity.XCOM))])
@router.delete("/xcom", dependencies=[Depends(requires_access_dags("DELETE", DagAccessEntity.XCOM))])
@staticmethod
def xcom(
starship_route: Annotated[StarshipRoute, Depends(starship_route)],
Expand Down
8 changes: 8 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ gcloud config set project <PROJECT_NAME>
gcloud composer environments run <ENVIRONMENT_NAME> --location <LOCATION> users add-role -- -e <USER_EMAIL> -r Admin
```

## Permissions

**Which permissions does a user need to use Starship?**

_The `Admin` role has everything Starship needs. Custom roles need read access to the Airflow configuration for every Starship request. On top of that, each page needs the permission matching what it reads or writes: for example, migrating connections needs read, create and delete on Connections, and the Env Vars page needs read on both Connections and Variables._

_DAG history migrations need write access to DAG runs, task instances and XComs. On Airflow 3, this includes create on XComs, matching Airflow's own XCom API. On Airflow 2, FAB has no XCom create permission, so Starship checks create on Task Instances instead._

## Airflow 3 support

**Is Airflow 3 supported?**
Expand Down
Loading
Loading