Skip to content

Commit eff0f03

Browse files
authored
Update guide for Google Cloud Secret Manager Backend (#10172)
1 parent 0c77ea8 commit eff0f03

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed

airflow/providers/google/cloud/secrets/secret_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class CloudSecretManagerBackend(BaseSecretsBackend, LoggingMixin):
6464
:type gcp_keyfile_dict: dict
6565
:param gcp_scopes: Comma-separated string containing GCP scopes
6666
:type gcp_scopes: str
67-
:param project_id: Project id (if you want to override the project_id from credentials)
67+
:param project_id: Project ID to read the secrets from. If not passed, the project ID from credentials
68+
will be used.
6869
:type project_id: str
69-
:param sep: separator used to concatenate connections_prefix and conn_id. Default: "-"
70+
:param sep: Separator used to concatenate connections_prefix and conn_id. Default: "-"
7071
:type sep: str
7172
"""
7273
def __init__(

docs/howto/use-alternative-secrets-backend.rst

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -383,48 +383,76 @@ Note that the secret ``Key`` is ``value``, and secret ``Value`` is ``world`` and
383383

384384
.. _secret_manager_backend:
385385

386-
GCP Secret Manager Backend
387-
^^^^^^^^^^^^^^^^^^^^^^^^^^
386+
Google Cloud Secret Manager Backend
387+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388388

389-
To enable GCP Secrets Manager to retrieve connection/variables, specify :py:class:`~airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend`
390-
as the ``backend`` in ``[secrets]`` section of ``airflow.cfg``.
389+
This topic describes how to configure Airflow to use `Secret Manager <https://cloud.google.com/secret-manager/docs>`__ as
390+
a secret backend and how to manage secrets.
391391

392-
Available parameters to ``backend_kwargs``:
392+
Before you begin
393+
""""""""""""""""
393394

394-
* ``connections_prefix``: Specifies the prefix of the secret to read to get Connections.
395-
* ``variables_prefix``: Specifies the prefix of the secret to read to get Variables.
396-
* ``gcp_key_path``: Path to GCP Credential JSON file
397-
* ``gcp_scopes``: Comma-separated string containing GCP scopes
398-
* ``sep``: separator used to concatenate connections_prefix and conn_id. Default: "-"
395+
`Configure Secret Manager and your local environment <https://cloud.google.com/secret-manager/docs/configuring-secret-manager>`__, once per project.
399396

400-
Note: The full GCP Secrets Manager secret id should follow the pattern "[a-zA-Z0-9-_]".
397+
Enabling the secret backend
398+
"""""""""""""""""""""""""""
401399

402-
Here is a sample configuration if you want to just retrieve connections:
400+
To enable the secret backend for Google Cloud Secrets Manager to retrieve connection/variables,
401+
specify :py:class:`~airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend`
402+
as the ``backend`` in ``[secrets]`` section of ``airflow.cfg``.
403+
404+
Here is a sample configuration if you want to use it:
403405

404406
.. code-block:: ini
405407
406408
[secrets]
407409
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
408-
backend_kwargs = {"connections_prefix": "airflow-connections", "sep": "-"}
409410
410-
Here is a sample configuration if you want to just retrieve variables:
411+
You can also set this with environment variables.
411412

412-
.. code-block:: ini
413+
.. code-block:: bash
413414
414-
[secrets]
415-
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
416-
backend_kwargs = {"variables_prefix": "airflow-variables", "sep": "-"}
415+
export AIRFLOW__SECRETS__BACKEND=airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
417416
418-
and if you want to retrieve both Variables and connections use the following sample config:
417+
You can verify the correct setting of the configuration options with the ``airflw config get-value`` command.
418+
419+
.. code-block:: bash
420+
421+
$ airflow config get-value secrets backend
422+
airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
423+
424+
Backend parameters
425+
""""""""""""""""""
426+
427+
The next step is to configure backend parameters using the ``backend_kwargs`` options. You can pass
428+
the following parameters:
429+
430+
* ``connections_prefix``: Specifies the prefix of the secret to read to get Connections. Default: ``"airflow-connections"``
431+
* ``variables_prefix``: Specifies the prefix of the secret to read to get Variables. Default: ``"airflow-variables"``
432+
* ``gcp_key_path``: Path to GCP Credential JSON file.
433+
* ``gcp_keyfile_dict``: Dictionary of keyfile parameters.
434+
* ``gcp_scopes``: Comma-separated string containing GCP scopes.
435+
* ``sep``: Separator used to concatenate connections_prefix and conn_id. Default: "-"
436+
* ``project_id``: Project ID to read the secrets from. If not passed, the project ID from credentials will be used.
437+
438+
All options should be passed as a JSON dictionary.
439+
440+
For example, if you want to set parameter ``connections_prefix`` to ``"airflow-tenant-primary"`` and parameter ``variables_prefix`` to ``"variables_prefix"``, your configuration file should look like this:
419441

420442
.. code-block:: ini
421443
422444
[secrets]
423445
backend = airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend
424-
backend_kwargs = {"connections_prefix": "airflow-connections", "variables_prefix": "airflow-variables", "sep": "-"}
446+
backend_kwargs = {"connections_prefix": "airflow-tenant-primary", "variables_prefix": "airflow-tenant-primary"}
447+
448+
Set-up credentials
449+
""""""""""""""""""
425450

451+
You can configure the credentials in three ways:
426452

427-
When ``gcp_key_path`` is not provided, it will use the Application Default Credentials (ADC) to obtain credentials.
453+
* By default, Application Default Credentials (ADC) is used obtain credentials.
454+
* ``gcp_key_path`` option in ``backend_kwargs`` option - allows you to configure authorizations with a service account stored in local file.
455+
* ``gcp_keyfile_dict`` option in ``backend_kwargs`` option - allows you to configure authorizations with a service account stored in Airflow configuration.
428456

429457
.. note::
430458

@@ -433,8 +461,43 @@ When ``gcp_key_path`` is not provided, it will use the Application Default Crede
433461
* `google.auth.default <https://google-auth.readthedocs.io/en/latest/reference/google.auth.html#google.auth.default>`__
434462
* `Setting Up Authentication for Server to Server Production Applications <https://cloud.google.com/docs/authentication/production>`__
435463

436-
The value of the Secrets Manager secret id must be the :ref:`connection URI representation <generating_connection_uri>`
437-
of the connection object.
464+
Managing secrets
465+
""""""""""""""""
466+
467+
If you want to configure a connection, you need to save it as a :ref:`connection URI representation <generating_connection_uri>`.
468+
Variables should be saved as plain text.
469+
470+
In order to manage secrets, you can use the ``gcloud`` tool or other supported tools. For more information, take a look at:
471+
`Managing secrets <https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets>`__ in Google Cloud Documentation.
472+
473+
The name of the secret must fit the following formats:
474+
475+
* for variable: ``[connections_prefix][sep][variable_name]``
476+
* for connection: ``[variable_prefix][sep][connection_name]``
477+
478+
where:
479+
480+
* ``connections_prefix`` - fixed value defined in the ``connections_prefix`` parameter in backend configuration. Default: ``airflow-connections``.
481+
* ``variable_prefix`` - fixed value defined in the ``variable_prefix`` parameter in backend configuration. Default: ``airflow-variables``.
482+
* ``sep`` - fixed value defined in the ``sep`` parameter in backend configuration. Default: ``-``.
483+
484+
The Cloud Secrets Manager secret name should follow the pattern ``[a-zA-Z0-9-_]``.
485+
486+
If you have the default backend configuration and you want to create a connection with ``conn_id``
487+
equals ``first-connection``, you should create secret named ``airflow-connections-first-connection``.
488+
You can do it with the gcloud tools as in the example below.
489+
490+
.. code-block:: bash
491+
492+
echo "mysql://example.org" | gcloud beta secrets create airflow-connections-first-connection --data-file=-
493+
494+
If you have the default backend configuration and you want to create a variable named ``first-variable``,
495+
you should create a secret named ``airflow-variables-first-variable``. You can do it with the gcloud
496+
command as in the example below.
497+
498+
.. code-block:: bash
499+
500+
echo "content" | gcloud beta secrets create airflow-variables-first-variable --data-file=-
438501
439502
.. _roll_your_own_secrets_backend:
440503

0 commit comments

Comments
 (0)