xinbinhuang commented on a change in pull request #7795: [AIRFLOW-7104] Add 
Secret backend for GCP Secrets Manager
URL: https://github.com/apache/airflow/pull/7795#discussion_r396049768
 
 

 ##########
 File path: airflow/providers/google/secrets/secrets_manager.py
 ##########
 @@ -0,0 +1,125 @@
+# 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.
+
+"""
+Objects relating to sourcing connections from GCP Secrets Manager
+"""
+from typing import List, Optional
+
+from cached_property import cached_property
+from google.api_core.exceptions import NotFound
+from google.api_core.gapic_v1.client_info import ClientInfo
+from google.cloud.secretmanager_v1 import SecretManagerServiceClient
+
+from airflow import version
+from airflow.models import Connection
+from airflow.providers.google.cloud.utils.credentials_provider import 
get_credentials_and_project_id
+from airflow.secrets import BaseSecretsBackend
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+
+class GcpSecretsManagerSecretsBackend(BaseSecretsBackend, LoggingMixin):
+    """
+    Retrieves Connection object from GCP Secrets Manager
+
+    Configurable via ``airflow.cfg`` as follows:
+
+    .. code-block:: ini
+
+        [secrets]
+        backend = 
airflow.providers.google.secrets.secrets_manager.GcpSecretsManagerSecretsBackend
+        backend_kwargs = {"connections_prefix": "airflow/connections"}
+
+    For example, if secret id is ``airflow/connections/smtp_default``, this 
would be accessible
+    if you provide ``{"connections_prefix": "airflow/connections"}`` and 
request conn_id ``smtp_default``.
+
+    :param connections_prefix: Specifies the prefix of the secret to read to 
get Connections.
+    :type connections_prefix: str
+    :param gcp_key_path: Path to GCP Credential JSON file;
+        use default credentials in the current environment if not provided.
+    :type gcp_key_path: str
+    :param gcp_scopes: Comma-separated string containing GCP scopes
+    :type gcp_scopes: str
+    """
+    def __init__(
+        self,
+        connections_prefix: str = "airflow/connections",
+        gcp_key_path: Optional[str] = None,
+        gcp_scopes: Optional[str] = None,
+        **kwargs
+    ):
+        self.connections_prefix = connections_prefix.rstrip("/")
+        self.credentials, self.project_id = get_credentials_and_project_id(
 
 Review comment:
   A quick question: I understand that `get_credentials_and_project_id` can be 
expensive. But I don't understand the impact of putting it in the constructor 
vs. in other methods. 
   
   Doesn't we always call the client method after constructing an instance? 
Otherwise, what would be the reason for constructing the instance without 
calling the method?
   
   Another confusion is that if we put it in the constructor, we only need to 
call the method once during instantiation, and then we can keep calling other 
methods without invoking the HTTP request. Wouldn't it be better than making 
this HTTP request every time when we call the method that contains it?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to