# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsimportwarningsfromabcimportABCfromtypingimportTYPE_CHECKINGfromairflow.exceptionsimportRemovedInAirflow3WarningifTYPE_CHECKING:fromairflow.models.connectionimportConnection
[docs]classBaseSecretsBackend(ABC):"""Abstract base class to retrieve Connection object given a conn_id or Variable given a key."""@staticmethod
[docs]defbuild_path(path_prefix:str,secret_id:str,sep:str="/")->str:""" Given conn_id, build path for Secrets Backend. :param path_prefix: Prefix of the path to get secret :param secret_id: Secret id :param sep: separator used to concatenate connections_prefix and conn_id. Default: "/" """returnf"{path_prefix}{sep}{secret_id}"
[docs]defget_conn_value(self,conn_id:str)->str|None:""" Retrieve from Secrets Backend a string value representing the Connection object. If the client your secrets backend uses already returns a python dict, you should override ``get_connection`` instead. :param conn_id: connection id """raiseNotImplementedError
[docs]defdeserialize_connection(self,conn_id:str,value:str)->Connection:""" Given a serialized representation of the airflow Connection, return an instance. Looks at first character to determine how to deserialize. :param conn_id: connection id :param value: the serialized representation of the Connection object :return: the deserialized Connection """fromairflow.models.connectionimportConnectionvalue=value.strip()ifvalue[0]=="{":returnConnection.from_json(conn_id=conn_id,value=value)else:returnConnection(conn_id=conn_id,uri=value)
[docs]defget_conn_uri(self,conn_id:str)->str|None:""" Get conn_uri from Secrets Backend. This method is deprecated and will be removed in a future release; implement ``get_conn_value`` instead. :param conn_id: connection id """raiseNotImplementedError()
[docs]defget_connection(self,conn_id:str)->Connection|None:""" Return connection object with a given ``conn_id``. Tries ``get_conn_value`` first and if not implemented, tries ``get_conn_uri`` :param conn_id: connection id """value=Nonenot_implemented_get_conn_value=False# TODO: after removal of ``get_conn_uri`` we should not catch NotImplementedError heretry:value=self.get_conn_value(conn_id=conn_id)exceptNotImplementedError:not_implemented_get_conn_value=Truewarnings.warn("Method `get_conn_uri` is deprecated. Please use `get_conn_value`.",RemovedInAirflow3Warning,stacklevel=2,)ifnot_implemented_get_conn_value:try:value=self.get_conn_uri(conn_id=conn_id)exceptNotImplementedError:raiseNotImplementedError(f"Secrets backend {self.__class__.__name__} neither implements ""`get_conn_value` nor `get_conn_uri`. Method `get_conn_uri` is ""deprecated and will be removed in a future release. Please implement `get_conn_value`.")ifvalue:returnself.deserialize_connection(conn_id=conn_id,value=value)else:returnNone
[docs]defget_connections(self,conn_id:str)->list[Connection]:""" Return connection object with a given ``conn_id``. :param conn_id: connection id """warnings.warn("This method is deprecated. Please use ""`airflow.secrets.base_secrets.BaseSecretsBackend.get_connection`.",RemovedInAirflow3Warning,stacklevel=2,)conn=self.get_connection(conn_id=conn_id)ifconn:return[conn]return[]
[docs]defget_variable(self,key:str)->str|None:""" Return value for Airflow Variable. :param key: Variable Key :return: Variable Value """raiseNotImplementedError()
[docs]defget_config(self,key:str)->str|None:""" Return value for Airflow Config Key. :param key: Config Key :return: Config Value """returnNone