Skip to content

Commit 785af81

Browse files
moiseenkovAnton Nitochkin
authored andcommitted
Add config option [secrets]backends_order
1 parent 940c7bb commit 785af81

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:
@@ -1290,6 +1336,41 @@ components:
12901336
- count
12911337
title: CalendarTimeRangeResponse
12921338
description: Represents a summary of DAG runs for a specific calendar time range.
1339+
Config:
1340+
properties:
1341+
sections:
1342+
items:
1343+
$ref: '#/components/schemas/ConfigSection'
1344+
type: array
1345+
title: Sections
1346+
additionalProperties: false
1347+
type: object
1348+
required:
1349+
- sections
1350+
title: Config
1351+
description: List of config sections with their options.
1352+
ConfigOption:
1353+
properties:
1354+
key:
1355+
type: string
1356+
title: Key
1357+
value:
1358+
anyOf:
1359+
- type: string
1360+
- prefixItems:
1361+
- type: string
1362+
- type: string
1363+
type: array
1364+
maxItems: 2
1365+
minItems: 2
1366+
title: Value
1367+
additionalProperties: false
1368+
type: object
1369+
required:
1370+
- key
1371+
- value
1372+
title: ConfigOption
1373+
description: Config option.
12931374
ConfigResponse:
12941375
properties:
12951376
page_size:
@@ -1346,6 +1427,23 @@ components:
13461427
- theme
13471428
title: ConfigResponse
13481429
description: configuration serializer.
1430+
ConfigSection:
1431+
properties:
1432+
name:
1433+
type: string
1434+
title: Name
1435+
options:
1436+
items:
1437+
$ref: '#/components/schemas/ConfigOption'
1438+
type: array
1439+
title: Options
1440+
additionalProperties: false
1441+
type: object
1442+
required:
1443+
- name
1444+
- options
1445+
title: ConfigSection
1446+
description: Config Section Schema.
13491447
ConnectionHookFieldBehavior:
13501448
properties:
13511449
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
@@ -87,3 +94,32 @@ def get_configs() -> ConfigResponse:
8794
config.update({key: value for key, value in additional_config.items()})
8895

8996
return ConfigResponse.model_validate(config)
97+
98+
99+
@config_router.get(
100+
"/backends_order",
101+
responses={
102+
**create_openapi_http_exception_doc(
103+
[
104+
status.HTTP_404_NOT_FOUND,
105+
status.HTTP_406_NOT_ACCEPTABLE,
106+
]
107+
),
108+
},
109+
response_model=Config,
110+
dependencies=[Depends(requires_authenticated())],
111+
)
112+
def get_backends_order_value(
113+
accept: HeaderAcceptJsonOrText,
114+
):
115+
section, option = "secrets", "backends_order"
116+
if not conf.has_option(section, option):
117+
raise HTTPException(
118+
status_code=status.HTTP_404_NOT_FOUND,
119+
detail=f"Option [{section}/{option}] not found.",
120+
)
121+
122+
value = conf.get(section, option)
123+
124+
config = Config(sections=[ConfigSection(name=section, options=[ConfigOption(key=option, value=value)])])
125+
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
@@ -1331,6 +1331,20 @@ secrets:
13311331
sensitive: true
13321332
example: ~
13331333
default: ""
1334+
backends_order:
1335+
description: |
1336+
Comma-separated list of secret backends. These backends will be used in the order they are specified.
1337+
Please note that the `environment_variable` and `metastore` are required values and cannot be removed
1338+
from the list. Supported values are:
1339+
1340+
* ``custom``: Custom secret backend specified in the ``secrets[backend]`` configuration option.
1341+
* ``environment_variable``: Standard environment variable backend
1342+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1343+
* ``metastore``: Standard metastore backend ``airflow.secrets.metastore.MetastoreBackend``.
1344+
version_added: 3.2.0
1345+
type: string
1346+
example: ~
1347+
default: "custom,environment_variable,metastore"
13341348
use_cache:
13351349
description: |
13361350
.. note:: |experimental|
@@ -1650,6 +1664,22 @@ workers:
16501664
sensitive: true
16511665
example: ~
16521666
default: ""
1667+
backends_order:
1668+
description: |
1669+
Comma-separated list of secret backends for workers. These backends will be used in the order they are
1670+
specified. Please note that the ``environment_variable`` and ``execution_api`` are required values and
1671+
cannot be removed from the list. Supported values are:
1672+
1673+
* ``custom``: Custom secret backend specified in the ``workers[secrets_backend]`` configuration
1674+
option.
1675+
* ``environment_variable``: Standard environment variable backend
1676+
``airflow.secrets.environment_variables.EnvironmentVariablesBackend``.
1677+
* ``execution_api``: Standard execution_api backend
1678+
``airflow.sdk.execution_time.secrets.execution_api.ExecutionAPISecretsBackend``.
1679+
version_added: 3.2.0
1680+
type: string
1681+
example: ~
1682+
default: "custom,environment_variable,execution_api"
16531683
min_heartbeat_interval:
16541684
description: |
16551685
The minimum interval (in seconds) at which the worker checks the task instance's

0 commit comments

Comments
 (0)