Skip to content

Commit a5092ed

Browse files
moiseenkovAnton Nitochkin
authored andcommitted
Add config option [secrets]backends_order
1 parent 516cef1 commit a5092ed

File tree

21 files changed

+782
-55
lines changed

21 files changed

+782
-55
lines changed

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:
@@ -1312,6 +1358,41 @@ components:
13121358
- count
13131359
title: CalendarTimeRangeResponse
13141360
description: Represents a summary of DAG runs for a specific calendar time range.
1361+
Config:
1362+
properties:
1363+
sections:
1364+
items:
1365+
$ref: '#/components/schemas/ConfigSection'
1366+
type: array
1367+
title: Sections
1368+
additionalProperties: false
1369+
type: object
1370+
required:
1371+
- sections
1372+
title: Config
1373+
description: List of config sections with their options.
1374+
ConfigOption:
1375+
properties:
1376+
key:
1377+
type: string
1378+
title: Key
1379+
value:
1380+
anyOf:
1381+
- type: string
1382+
- prefixItems:
1383+
- type: string
1384+
- type: string
1385+
type: array
1386+
maxItems: 2
1387+
minItems: 2
1388+
title: Value
1389+
additionalProperties: false
1390+
type: object
1391+
required:
1392+
- key
1393+
- value
1394+
title: ConfigOption
1395+
description: Config option.
13151396
ConfigResponse:
13161397
properties:
13171398
page_size:
@@ -1370,6 +1451,23 @@ components:
13701451
- theme
13711452
title: ConfigResponse
13721453
description: configuration serializer.
1454+
ConfigSection:
1455+
properties:
1456+
name:
1457+
type: string
1458+
title: Name
1459+
options:
1460+
items:
1461+
$ref: '#/components/schemas/ConfigOption'
1462+
type: array
1463+
title: Options
1464+
additionalProperties: false
1465+
type: object
1466+
required:
1467+
- name
1468+
- options
1469+
title: ConfigSection
1470+
description: Config Section Schema.
13731471
ConnectionHookFieldBehavior:
13741472
properties:
13751473
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
@@ -66,3 +73,32 @@ def get_configs() -> ConfigResponse:
6673
config.update({key: value for key, value in additional_config.items()})
6774

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,20 @@ secrets:
13231323
sensitive: true
13241324
example: ~
13251325
default: ""
1326+
backends_order:
1327+
description: |
1328+
Comma-separated list of secret backends. These backends will be used in the order they are specified.
1329+
Please note that the `environment_variable` and `metastore` are required values and cannot be removed
1330+
from the list. Supported values are:
1331+
1332+
* ``custom``: Custom secret backend specified in the ``secrets[backend]`` configuration option.
1333+
* ``environment_variable``: Standard environment variable backend
1334+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1335+
* ``metastore``: Standard metastore backend ``airflow.secrets.metastore.MetastoreBackend``.
1336+
version_added: 3.2.0
1337+
type: string
1338+
example: ~
1339+
default: "custom,environment_variable,metastore"
13261340
use_cache:
13271341
description: |
13281342
.. note:: |experimental|
@@ -1623,6 +1637,22 @@ workers:
16231637
sensitive: true
16241638
example: ~
16251639
default: ""
1640+
backends_order:
1641+
description: |
1642+
Comma-separated list of secret backends for workers. These backends will be used in the order they are
1643+
specified. Please note that the ``environment_variable`` and ``execution_api`` are required values and
1644+
cannot be removed from the list. Supported values are:
1645+
1646+
* ``custom``: Custom secret backend specified in the ``workers[secrets_backend]`` configuration
1647+
option.
1648+
* ``environment_variable``: Standard environment variable backend
1649+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1650+
* ``execution_api``: Standard execution_api backend
1651+
``airflow.sdk.execution_time.secrets.execution_api.ExecutionAPISecretsBackend``.
1652+
version_added: 3.2.0
1653+
type: string
1654+
example: ~
1655+
default: "custom,environment_variable,execution_api"
16261656
min_heartbeat_interval:
16271657
description: |
16281658
The minimum interval (in seconds) at which the worker checks the task instance's

0 commit comments

Comments
 (0)