dstandish commented on a change in pull request #6376: [WIP] [AIRFLOW-5705] Add 
creds backend classes including AWS SSM
URL: https://github.com/apache/airflow/pull/6376#discussion_r359163658
 
 

 ##########
 File path: airflow/creds/aws_ssm.py
 ##########
 @@ -0,0 +1,127 @@
+# -*- coding: utf-8 -*-
+#
+# 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
+
+import boto3
+
+from airflow import conf
+from airflow.creds import CONN_ENV_PREFIX, BaseCredsBackend
+from airflow.models import Connection
+
+
+class AwsSsmCredsBackend(BaseCredsBackend):
+    """
+    Retrieves Connection object from AWS SSM Parameter Store
+
+    Configurable via airflow config like so:
+
+    .. code-block:: ini
+
+        [aws_ssm_creds]
+        ssm_prefix = /airflow
+        profile_name = default
+
+    For example, if ssm path is ``/airflow/AIRFLOW_CONN_SMTP_DEFAULT``, this 
would be accessible if you
+    provide ``ssm_prefix = /airflow`` and conn_id ``smtp_default``.
+
+    """
+
+    conf_section = "aws_ssm_creds"
+    conf_key_ssm_prefix = "ssm_prefix"
+    default_prefix = "/airflow"
+    conf_key_profile_name = "profile_name"
+    default_profile = "default"
+
+    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,
+        )
+        if ssm_prefix[-1] == "/":
+            ssm_prefix = ssm_prefix[:-1]
+        return ssm_prefix
+
+    @property
+    def aws_profile_name(self) -> str:
+        """
+        Gets AWS profile to use from conf.
+
+        :return:
+        """
+        profile_name = conf.get(
+            section=self.conf_section,
+            key=self.conf_key_profile_name,
+            fallback=self.default_profile,
+        )
+        return profile_name
+
+    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)
 
 Review comment:
   this is a good point.  
   
   if there is no `.aws/config`, then this fails.
   
   i have made 2 updates to address this.
   
   1. if conf key is missing, use `profile_name=None`.  
   
   2. rather than supplying `profile = default` in `default_config.cfg`, i have 
used `profile =` i.e. no value.  
   
   When `Session(profile=None)` is the call, this accomplishes following 
objectives:
   * aws id / key / session env variables work even if no `.aws` files
   * `default` profile in `~/.aws/credentials` will be used if no aws id / key 
/ session env variables available
   

----------------------------------------------------------------
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