Skip to content

Commit 4ff5632

Browse files
moiseenkovAnton Nitochkin
authored andcommitted
Add config option [secrets]backends_order
1 parent d4c87b2 commit 4ff5632

21 files changed

Lines changed: 787 additions & 50 deletions

File tree

airflow-core/docs/security/secrets/secrets-backend/index.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ When looking up a connection/variable, by default Airflow will search environmen
3939
database second.
4040

4141
If you enable an alternative secrets backend, it will be searched first, followed by environment variables,
42-
then metastore. This search ordering is not configurable. Though, in some alternative secrets backend you might have
42+
then metastore. Though, in some alternative secrets backend you might have
4343
the option to filter which connection/variable/config is searched in the secret backend. Please look at the
4444
documentation of the secret backend you are using to see if such option is available.
4545

4646
On the other hand, if a workers secrets backend is defined, the order of lookup has higher priority for the workers secrets
4747
backend and then the secrets backend.
4848

49+
The secrets backends search ordering is also configurable via the configuration option ``[secrets]backends_order``.
50+
4951
.. warning::
5052

5153
When using environment variables or an alternative secrets backend to store secrets or variables, it is possible to create key collisions.
@@ -64,12 +66,21 @@ The ``[secrets]`` section has the following options:
6466
[secrets]
6567
backend =
6668
backend_kwargs =
69+
backends_order =
6770
6871
Set ``backend`` to the fully qualified class name of the backend you want to enable.
6972

7073
You can provide ``backend_kwargs`` with json and it will be passed as kwargs to the ``__init__`` method of
7174
your secrets backend.
7275

76+
``backends_order`` is a comma-separated list of secret backends. These backends will be used in the order they are specified.
77+
Please note that the ``environment_variable`` and ``metastore`` are required values and cannot be removed
78+
from the list. Supported values are:
79+
80+
* ``custom``: Custom secret backend specified in the ``secrets[backend]`` configuration option.
81+
* ``environment_variable``: Standard environment variable backend ``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
82+
* ``metastore``: Standard metastore backend ``airflow.secrets.metastore.MetastoreBackend``.
83+
7384
If you want to check which secret backend is currently set, you can use ``airflow config get-value secrets backend`` command as in
7485
the example below.
7586

@@ -89,13 +100,21 @@ configure separate secrets backend for workers, you can do that using:
89100
[workers]
90101
secrets_backend =
91102
secrets_backend_kwargs =
92-
103+
backends_order =
93104
94105
Set ``secrets_backend`` to the fully qualified class name of the backend you want to enable.
95106

96107
You can provide ``secrets_backend_kwargs`` with json and it will be passed as kwargs to the ``__init__`` method of
97108
your secrets backend for the workers.
98109

110+
``backends_order`` is a comma-separated list of secret backends for workers. These backends will be used in the order they are specified.
111+
Please note that the ``environment_variable`` and ``execution_api`` are required values and cannot be removed
112+
from the list. Supported values are:
113+
114+
* ``custom``: Custom secret backend specified in the ``workers[secrets_backend]`` configuration option.
115+
* ``environment_variable``: Standard environment variable backend ``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
116+
* ``execution_api``: Standard execution_api backend ``airflow.sdk.execution_time.secrets.execution_api.ExecutionAPISecretsBackend``.
117+
99118
If you want to check which secret backend is currently set, you can use ``airflow config get-value workers secrets_backend`` command as in
100119
the example below.
101120

airflow-core/src/airflow/api_fastapi/core_api/openapi/_private_ui.yaml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,52 @@ paths:
9494
security:
9595
- OAuth2PasswordBearer: []
9696
- HTTPBearer: []
97+
/ui/backends_order:
98+
get:
99+
tags:
100+
- Config
101+
summary: Get Backends Order Value
102+
operationId: get_backends_order_value
103+
security:
104+
- OAuth2PasswordBearer: []
105+
- HTTPBearer: []
106+
parameters:
107+
- name: accept
108+
in: header
109+
required: false
110+
schema:
111+
type: string
112+
enum:
113+
- application/json
114+
- text/plain
115+
- '*/*'
116+
default: '*/*'
117+
title: Accept
118+
responses:
119+
'200':
120+
description: Successful Response
121+
content:
122+
application/json:
123+
schema:
124+
$ref: '#/components/schemas/Config'
125+
'404':
126+
content:
127+
application/json:
128+
schema:
129+
$ref: '#/components/schemas/HTTPExceptionResponse'
130+
description: Not Found
131+
'406':
132+
content:
133+
application/json:
134+
schema:
135+
$ref: '#/components/schemas/HTTPExceptionResponse'
136+
description: Not Acceptable
137+
'422':
138+
description: Validation Error
139+
content:
140+
application/json:
141+
schema:
142+
$ref: '#/components/schemas/HTTPValidationError'
97143
/ui/connections/hook_meta:
98144
get:
99145
tags:
@@ -1336,6 +1382,41 @@ components:
13361382
- count
13371383
title: CalendarTimeRangeResponse
13381384
description: Represents a summary of DAG runs for a specific calendar time range.
1385+
Config:
1386+
properties:
1387+
sections:
1388+
items:
1389+
$ref: '#/components/schemas/ConfigSection'
1390+
type: array
1391+
title: Sections
1392+
additionalProperties: false
1393+
type: object
1394+
required:
1395+
- sections
1396+
title: Config
1397+
description: List of config sections with their options.
1398+
ConfigOption:
1399+
properties:
1400+
key:
1401+
type: string
1402+
title: Key
1403+
value:
1404+
anyOf:
1405+
- type: string
1406+
- prefixItems:
1407+
- type: string
1408+
- type: string
1409+
type: array
1410+
maxItems: 2
1411+
minItems: 2
1412+
title: Value
1413+
additionalProperties: false
1414+
type: object
1415+
required:
1416+
- key
1417+
- value
1418+
title: ConfigOption
1419+
description: Config option.
13391420
ConfigResponse:
13401421
properties:
13411422
fallback_page_limit:
@@ -1398,6 +1479,23 @@ components:
13981479
- multi_team
13991480
title: ConfigResponse
14001481
description: configuration serializer.
1482+
ConfigSection:
1483+
properties:
1484+
name:
1485+
type: string
1486+
title: Name
1487+
options:
1488+
items:
1489+
$ref: '#/components/schemas/ConfigOption'
1490+
type: array
1491+
title: Options
1492+
additionalProperties: false
1493+
type: object
1494+
required:
1495+
- name
1496+
- options
1497+
title: ConfigSection
1498+
description: Config Section Schema.
14011499
ConnectionHookFieldBehavior:
14021500
properties:
14031501
hidden:

airflow-core/src/airflow/api_fastapi/core_api/routes/ui/config.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919
from json import loads
2020
from typing import Any
2121

22-
from fastapi import Depends, status
22+
from fastapi import Depends, HTTPException, status
2323

24+
from airflow.api_fastapi.common.headers import HeaderAcceptJsonOrText
2425
from airflow.api_fastapi.common.router import AirflowRouter
2526
from airflow.api_fastapi.common.types import UIAlert
27+
from airflow.api_fastapi.core_api.datamodels.config import (
28+
Config,
29+
ConfigOption,
30+
ConfigSection,
31+
)
2632
from airflow.api_fastapi.core_api.datamodels.ui.config import ConfigResponse
2733
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
2834
from airflow.api_fastapi.core_api.security import requires_authenticated
35+
from airflow.api_fastapi.core_api.services.public.config import _response_based_on_accept
2936
from airflow.configuration import conf
3037
from airflow.settings import DASHBOARD_UIALERTS
3138
from airflow.utils.log.log_reader import TaskLogReader
@@ -67,3 +74,32 @@ def get_configs() -> ConfigResponse:
6774
config.update({key: value for key, value in additional_config.items()})
6875

6976
return ConfigResponse.model_validate(config)
77+
78+
79+
@config_router.get(
80+
"/backends_order",
81+
responses={
82+
**create_openapi_http_exception_doc(
83+
[
84+
status.HTTP_404_NOT_FOUND,
85+
status.HTTP_406_NOT_ACCEPTABLE,
86+
]
87+
),
88+
},
89+
response_model=Config,
90+
dependencies=[Depends(requires_authenticated())],
91+
)
92+
def get_backends_order_value(
93+
accept: HeaderAcceptJsonOrText,
94+
):
95+
section, option = "secrets", "backends_order"
96+
if not conf.has_option(section, option):
97+
raise HTTPException(
98+
status_code=status.HTTP_404_NOT_FOUND,
99+
detail=f"Option [{section}/{option}] not found.",
100+
)
101+
102+
value = conf.get(section, option)
103+
104+
config = Config(sections=[ConfigSection(name=section, options=[ConfigOption(key=option, value=value)])])
105+
return _response_based_on_accept(accept, config)

airflow-core/src/airflow/config_templates/config.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,22 @@ secrets:
13391339
sensitive: true
13401340
example: ~
13411341
default: ""
1342+
backends_order:
1343+
description: |
1344+
.. note:: |experimental|
1345+
1346+
Comma-separated list of secret backends. These backends will be used in the order they are specified.
1347+
Please note that the `environment_variable` and `metastore` are required values and cannot be removed
1348+
from the list. Supported values are:
1349+
1350+
* ``custom``: Custom secret backend specified in the ``secrets[backend]`` configuration option.
1351+
* ``environment_variable``: Standard environment variable backend
1352+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1353+
* ``metastore``: Standard metastore backend ``airflow.secrets.metastore.MetastoreBackend``.
1354+
version_added: 3.2.0
1355+
type: string
1356+
example: ~
1357+
default: "custom,environment_variable,metastore"
13421358
use_cache:
13431359
description: |
13441360
.. note:: |experimental|
@@ -1632,6 +1648,24 @@ workers:
16321648
sensitive: true
16331649
example: ~
16341650
default: ""
1651+
backends_order:
1652+
description: |
1653+
.. note:: |experimental|
1654+
1655+
Comma-separated list of secret backends for workers. These backends will be used in the order they are
1656+
specified. Please note that the ``environment_variable`` and ``execution_api`` are required values and
1657+
cannot be removed from the list. Supported values are:
1658+
1659+
* ``custom``: Custom secret backend specified in the ``workers[secrets_backend]`` configuration
1660+
option.
1661+
* ``environment_variable``: Standard environment variable backend
1662+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1663+
* ``execution_api``: Standard execution_api backend
1664+
``airflow.sdk.execution_time.secrets.execution_api.ExecutionAPISecretsBackend``.
1665+
version_added: 3.2.0
1666+
type: string
1667+
example: ~
1668+
default: "custom,environment_variable,execution_api"
16351669
min_heartbeat_interval:
16361670
description: |
16371671
The minimum interval (in seconds) at which the worker checks the task instance's

0 commit comments

Comments
 (0)