|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from azure.core.exceptions import ResourceNotFoundError |
| 20 | +from azure.identity import DefaultAzureCredential |
| 21 | +from azure.keyvault.secrets import SecretClient |
| 22 | +from cached_property import cached_property |
| 23 | + |
| 24 | +from airflow.secrets import BaseSecretsBackend |
| 25 | +from airflow.utils.log.logging_mixin import LoggingMixin |
| 26 | + |
| 27 | + |
| 28 | +class AzureKeyVaultBackend(BaseSecretsBackend, LoggingMixin): |
| 29 | + """ |
| 30 | + Retrieves Airflow Connections or Variables from Azure Key Vault secrets. |
| 31 | +
|
| 32 | + The Azure Key Vault can be configured as a secrets backend in the ``airflow.cfg``: |
| 33 | +
|
| 34 | + .. code-block:: ini |
| 35 | +
|
| 36 | + [secrets] |
| 37 | + backend = airflow.providers.microsoft.azure.secrets.azure_key_vault.AzureKeyVaultBackend |
| 38 | + backend_kwargs = {"connections_prefix": "airflow-connections", "vault_url": "<azure_key_vault_uri>"} |
| 39 | +
|
| 40 | + For example, if the secrets prefix is ``airflow-connections-smtp-default``, this would be accessible |
| 41 | + if you provide ``{"connections_prefix": "airflow-connections"}`` and request conn_id ``smtp-default``. |
| 42 | + And if variables prefix is ``airflow-variables-hello``, this would be accessible |
| 43 | + if you provide ``{"variables_prefix": "airflow-variables"}`` and request variable key ``hello``. |
| 44 | +
|
| 45 | + :param connections_prefix: Specifies the prefix of the secret to read to get Connections |
| 46 | + :type connections_prefix: str |
| 47 | + :param variables_prefix: Specifies the prefix of the secret to read to get Variables |
| 48 | + :type variables_prefix: str |
| 49 | + :param config_prefix: Specifies the prefix of the secret to read to get Variables. |
| 50 | + :type config_prefix: str |
| 51 | + :param vault_url: The URL of an Azure Key Vault to use |
| 52 | + :type vault_url: str |
| 53 | + :param sep: separator used to concatenate secret_prefix and secret_id. Default: "-" |
| 54 | + :type sep: str |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + connections_prefix: str = 'airflow-connections', |
| 60 | + variables_prefix: str = 'airflow-variables', |
| 61 | + config_prefix: str = 'airflow-config', |
| 62 | + vault_url: str = '', |
| 63 | + sep: str = '-', |
| 64 | + **kwargs, |
| 65 | + ): |
| 66 | + super().__init__() |
| 67 | + self.vault_url = vault_url |
| 68 | + self.connections_prefix = connections_prefix.rstrip(sep) |
| 69 | + self.variables_prefix = variables_prefix.rstrip(sep) |
| 70 | + self.config_prefix = config_prefix.rstrip(sep) |
| 71 | + self.sep = sep |
| 72 | + self.kwargs = kwargs |
| 73 | + |
| 74 | + @cached_property |
| 75 | + def client(self): |
| 76 | + """ |
| 77 | + Create a Azure Key Vault client. |
| 78 | + """ |
| 79 | + credential = DefaultAzureCredential() |
| 80 | + client = SecretClient(vault_url=self.vault_url, credential=credential, **self.kwargs) |
| 81 | + return client |
| 82 | + |
| 83 | + def get_conn_uri(self, conn_id: str) -> Optional[str]: |
| 84 | + """ |
| 85 | + Get an Airflow Connection URI from an Azure Key Vault secret |
| 86 | +
|
| 87 | + :param conn_id: The Airflow connection id to retrieve |
| 88 | + :type conn_id: str |
| 89 | + """ |
| 90 | + return self._get_secret(self.connections_prefix, conn_id) |
| 91 | + |
| 92 | + def get_variable(self, key: str) -> Optional[str]: |
| 93 | + """ |
| 94 | + Get an Airflow Variable from an Azure Key Vault secret. |
| 95 | +
|
| 96 | + :param key: Variable Key |
| 97 | + :type key: str |
| 98 | + :return: Variable Value |
| 99 | + """ |
| 100 | + return self._get_secret(self.variables_prefix, key) |
| 101 | + |
| 102 | + def get_config(self, key: str) -> Optional[str]: |
| 103 | + """ |
| 104 | + Get Airflow Configuration |
| 105 | +
|
| 106 | + :param key: Configuration Option Key |
| 107 | + :return: Configuration Option Value |
| 108 | + """ |
| 109 | + return self._get_secret(self.config_prefix, key) |
| 110 | + |
| 111 | + @staticmethod |
| 112 | + def build_path(path_prefix: str, secret_id: str, sep: str = '-') -> str: |
| 113 | + """ |
| 114 | + Given a path_prefix and secret_id, build a valid secret name for the Azure Key Vault Backend. |
| 115 | + Also replaces underscore in the path with dashes to support easy switching between |
| 116 | + environment variables, so ``connection_default`` becomes ``connection-default``. |
| 117 | +
|
| 118 | + :param path_prefix: The path prefix of the secret to retrieve |
| 119 | + :type path_prefix: str |
| 120 | + :param secret_id: Name of the secret |
| 121 | + :type secret_id: str |
| 122 | + :param sep: Separator used to concatenate path_prefix and secret_id |
| 123 | + :type sep: str |
| 124 | + """ |
| 125 | + path = f'{path_prefix}{sep}{secret_id}' |
| 126 | + return path.replace('_', sep) |
| 127 | + |
| 128 | + def _get_secret(self, path_prefix: str, secret_id: str) -> Optional[str]: |
| 129 | + """ |
| 130 | + Get an Azure Key Vault secret value |
| 131 | +
|
| 132 | + :param path_prefix: Prefix for the Path to get Secret |
| 133 | + :type path_prefix: str |
| 134 | + :param secret_id: Secret Key |
| 135 | + :type secret_id: str |
| 136 | + """ |
| 137 | + name = self.build_path(path_prefix, secret_id, self.sep) |
| 138 | + try: |
| 139 | + secret = self.client.get_secret(name=name) |
| 140 | + return secret.value |
| 141 | + except ResourceNotFoundError as ex: |
| 142 | + self.log.debug('Secret %s not found: %s', name, ex) |
| 143 | + return None |
0 commit comments