kaxil commented on a change in pull request #8436:
URL: https://github.com/apache/airflow/pull/8436#discussion_r414129996



##########
File path: airflow/secrets/local_filesystem.py
##########
@@ -0,0 +1,266 @@
+#
+# 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 retreiving connections and variables from local file
+"""
+import json
+import logging
+import os
+from collections import defaultdict
+from inspect import signature
+from json import JSONDecodeError
+from typing import Any, Dict, List, Optional, Set, Tuple
+
+from airflow.exceptions import AirflowException, AirflowFileParseException, 
FileSyntaxError
+from airflow.secrets.base_secrets import BaseSecretsBackend
+from airflow.utils.file import COMMENT_PATTERN
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+log = logging.getLogger(__name__)
+
+
+def get_connection_parameter_names() -> Set[str]:
+    """Returns :class:`airflow.models.connection.Connection` constructor 
parameters."""
+    from airflow.models.connection import Connection
+
+    return {k for k in signature(Connection.__init__).parameters.keys() if k 
!= "self"}
+
+
+def _parser_env_file(file_path: str) -> Tuple[Dict[str, List[str]], 
List[FileSyntaxError]]:
+    """
+    Parse a file in the ``.env '' format.
+
+   .. code-block:: text
+
+        
MY_CONN_ID=my-conn-type://my-login:my-pa%2Fssword@my-host:5432/my-schema?param1=val1&param2=val2
+
+    :param file_path: The location of the file that will be processed.
+    :type file_path: str
+    :return: Tuple with mapping of key and list of values and list of syntax 
errors
+    """
+    with open(file_path) as f:
+        content = f.read()
+
+    secrets: Dict[str, List[str]] = defaultdict(list)
+    errors: List[FileSyntaxError] = []
+    for line_no, line in enumerate(content.splitlines(), 1):
+        if not line:
+            # Ignore empty line
+            continue
+
+        if COMMENT_PATTERN.match(line):
+            # Ignore comments:
+            continue
+
+        var_parts: List[str] = line.split("=", 2)
+        if len(var_parts) != 2:
+            errors.append(
+                FileSyntaxError(
+                    line_no=line_no,
+                    message='Invalid line format. The line should contain at 
least one equal sign ("=").',
+                )
+            )
+            continue
+
+        key, value = var_parts
+        if not key:
+            errors.append(FileSyntaxError(line_no=line_no, message="Invalid 
line format. Key is empty.",))
+        secrets[key].append(value)
+    return secrets, errors
+
+
+def _parse_json_file(file_path: str) -> Tuple[Dict[str, Any], 
List[FileSyntaxError]]:
+    """
+    Parse a file in the JSON format.
+
+    :param file_path: The location of the file that will be processed.
+    :type file_path: str
+    :return: Tuple with mapping of key and list of values and list of syntax 
errors
+    """
+    with open(file_path) as f:
+        content = f.read()
+
+    if not content:
+        return {}, [FileSyntaxError(line_no=1, message="The file is empty.")]
+    try:
+        secrets = json.loads(content)
+    except JSONDecodeError as e:
+        return {}, [FileSyntaxError(line_no=int(e.lineno), message=e.msg)]
+    if not isinstance(secrets, dict):
+        return {}, [FileSyntaxError(line_no=1, message="The file should 
contain the object.")]
+
+    return secrets, []
+
+
+FILE_PARSERS = {
+    "env": _parser_env_file,
+    "json": _parse_json_file,
+}
+
+
+def _parse_secret_file(file_path: str) -> Dict[str, Any]:
+    """
+    Based on the file extension format, selects a parser and parse the file.
+
+    :param file_path: The location of the file that will be processed.
+    :type file_path: str
+    :return: Map of secret key (e.g. connection ID) and value.
+    """
+    if not os.path.exists(file_path):
+        raise AirflowException(
+            f"File {file_path} was not found. Check the configuration of your 
secret backend."
+        )
+
+    log.debug("Parsing file: %s", file_path)
+
+    ext = file_path.rsplit(".", 2)[-1].lower()
+
+    if ext not in FILE_PARSERS:
+        raise AirflowException("Unsupported file format. The file must have 
the extension .env or .json")
+
+    secrets, parse_errors = FILE_PARSERS[ext](file_path)
+
+    log.debug("Parsed file: len(parse_errors)=%d, len(secrets)=%d", 
len(parse_errors), len(secrets))
+
+    if parse_errors:
+        raise AirflowFileParseException(
+            f"Failed to load the secret file.", file_path=file_path, 
parse_errors=parse_errors
+        )
+
+    return secrets
+
+
+def _create_connection(conn_id: str, value: Any):
+    """
+    Creates a connection based on a URL or JSON object.
+    """
+    from airflow.models import Connection

Review comment:
       ```suggestion
       from airflow.models.connection import Connection
   ```




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


Reply via email to