Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/airflow/_shared/secrets_backend/base.py: 34%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

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. 

17from __future__ import annotations 

18 

19from abc import ABC 

20 

21 

22class BaseSecretsBackend(ABC): 

23 """Abstract base class to retrieve Connection object given a conn_id or Variable given a key.""" 

24 

25 @staticmethod 

26 def build_path(path_prefix: str, secret_id: str, sep: str = "/") -> str: 

27 """ 

28 Given conn_id, build path for Secrets Backend. 

29 

30 :param path_prefix: Prefix of the path to get secret 

31 :param secret_id: Secret id 

32 :param sep: separator used to concatenate connections_prefix and conn_id. Default: "/" 

33 """ 

34 return f"{path_prefix}{sep}{secret_id}" 

35 

36 def get_conn_value(self, conn_id: str) -> str | None: 

37 """ 

38 Retrieve from Secrets Backend a string value representing the Connection object. 

39 

40 If the client your secrets backend uses already returns a python dict, you should override 

41 ``get_connection`` instead. 

42 

43 :param conn_id: connection id 

44 """ 

45 raise NotImplementedError 

46 

47 def get_variable(self, key: str, team_name: str | None = None) -> str | None: 

48 """ 

49 Return value for Airflow Variable. 

50 

51 :param key: Variable Key 

52 :param team_name: Team name associated to the task trying to access the variable (if any) 

53 :return: Variable Value 

54 """ 

55 raise NotImplementedError() 

56 

57 def get_config(self, key: str) -> str | None: 

58 """ 

59 Return value for Airflow Config Key. 

60 

61 :param key: Config Key 

62 :return: Config Value 

63 """ 

64 return None 

65 

66 @staticmethod 

67 def _get_connection_class(): 

68 """ 

69 Detect which Connection class to use based on execution context. 

70 

71 Returns SDK Connection in worker context, core Connection in server context. 

72 """ 

73 import os 

74 

75 process_context = os.environ.get("_AIRFLOW_PROCESS_CONTEXT", "").lower() 

76 if process_context == "client": 

77 # Client context (worker, dag processor, triggerer) 

78 from airflow.sdk.definitions.connection import Connection 

79 

80 return Connection 

81 

82 # Server context (scheduler, API server, etc.) 

83 from airflow.models.connection import Connection 

84 

85 return Connection 

86 

87 def deserialize_connection(self, conn_id: str, value: str): 

88 """ 

89 Given a serialized representation of the airflow Connection, return an instance. 

90 

91 Auto-detects which Connection class to use based on execution context. 

92 Uses Connection.from_json() for JSON format, Connection(uri=...) for URI format. 

93 

94 :param conn_id: connection id 

95 :param value: the serialized representation of the Connection object 

96 :return: the deserialized Connection 

97 """ 

98 conn_class = self._get_connection_class() 

99 

100 value = value.strip() 

101 if value[0] == "{": 

102 return conn_class.from_json(value=value, conn_id=conn_id) 

103 

104 # TODO: Only sdk has from_uri defined on it. Is it worthwhile developing the core path or not? 

105 if hasattr(conn_class, "from_uri"): 

106 return conn_class.from_uri(conn_id=conn_id, uri=value) 

107 return conn_class(conn_id=conn_id, uri=value) 

108 

109 def get_connection(self, conn_id: str): 

110 """ 

111 Return connection object with a given ``conn_id``. 

112 

113 :param conn_id: connection id 

114 :return: Connection object or None 

115 """ 

116 value = self.get_conn_value(conn_id=conn_id) 

117 if value: 

118 return self.deserialize_connection(conn_id=conn_id, value=value) 

119 return None