You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/03/10 13:27:03 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #6376: [AIRFLOW-5705] Add secrets backend and support for AWS SSM

kaxil commented on a change in pull request #6376: [AIRFLOW-5705] Add secrets backend and support for AWS SSM
URL: https://github.com/apache/airflow/pull/6376#discussion_r390310469
 
 

 ##########
 File path: airflow/providers/amazon/aws/secrets/ssm.py
 ##########
 @@ -0,0 +1,120 @@
+#
+# 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 AWS SSM Parameter Store
+"""
+
+from typing import List, Optional
+
+import boto3
+
+from airflow.configuration import conf
+from airflow.models import Connection
+from airflow.secrets import CONN_ENV_PREFIX, BaseSecretsBackend
+
+
+class AwsSsmSecretsBackend(BaseSecretsBackend):
+    """
+    Retrieves Connection object from AWS SSM Parameter Store
+
+    Configurable via airflow config like so:
+
+    .. code-block:: ini
+
+        [secrets_backend]
+        class_list = airflow.providers.amazon.aws.secrets.ssm.AwsSsmSecretsBackend
+        aws_ssm_prefix = /airflow
+        aws_ssm_profile_name = default
+
+    For example, if ssm path is ``/airflow/AIRFLOW_CONN_SMTP_DEFAULT``, this would be accessible if you
+    provide ``aws_ssm_prefix = /airflow`` and conn_id ``smtp_default``.
+
+    """
+
+    CONF_SECTION = "secrets_backend"
+    CONF_KEY_SSM_PREFIX = "aws_ssm_prefix"
+    DEFAULT_PREFIX = "/airflow"
+    CONF_KEY_PROFILE_NAME = "aws_ssm_profile_name"
+
+    def __init__(self, *args, **kwargs):
+        pass
+
+    @property
+    def ssm_prefix(self) -> str:
+        """
+        Gets ssm prefix from conf.
+
+        Ensures that there is no trailing slash.
+
+        :return:
+        """
+        ssm_prefix = conf.get(
+            section=self.CONF_SECTION, key=self.CONF_KEY_SSM_PREFIX, fallback=self.DEFAULT_PREFIX
+        )
+        return ssm_prefix.rstrip("/")
+
+    @property
+    def aws_profile_name(self) -> Optional[str]:
+        """
+        Gets AWS profile to use from conf.
+
+        :return:
+        """
+        profile_name = conf.get(
+            section=self.CONF_SECTION, key=self.CONF_KEY_PROFILE_NAME, fallback=None
+        )
+        return profile_name or None
+
+    def build_ssm_path(self, conn_id: str):
+        """
+        Given conn_id, build SSM path.
+
+        Assumes connection params use same naming convention as env vars, but may have arbitrary prefix.
+
+        :param conn_id:
+        :return:
+        """
+        param_name = (CONN_ENV_PREFIX + conn_id).upper()
+        param_path = self.ssm_prefix + "/" + param_name
+        return param_path
+
+    def get_conn_uri(self, conn_id):
+        """
+        Get param value
+
+        :param conn_id:
+        :return:
+        """
+        session = boto3.Session(profile_name=self.aws_profile_name)
+        client = session.client("ssm")
+        response = client.get_parameter(
+            Name=self.build_ssm_path(conn_id=conn_id), WithDecryption=True
+        )
+        value = response["Parameter"]["Value"]
+        return value
+
+    def get_connections(self, conn_id) -> List[Connection]:
 
 Review comment:
   Yes, please, if we don't need instances, just make it class method

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services