Example #1
0
def _parse_secret_file(file_path: str) -> Dict[str, Any]:
    """
    Based on the file extension format, selects a parser, and parses 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 Secrets 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 or .yaml"
        )

    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("Failed to load the secret file.",
                                        file_path=file_path,
                                        parse_errors=parse_errors)

    return secrets
Example #2
0
def _parse_secret_file(file: _io.TextIOWrapper) -> Dict[str, Any]:
    """
    Based on the file extension format, selects a parser, and parses the file.

    :param file: TextIOWrapper object of the file that will be processed.
    :type file: _io.TextIOWrapper
    :return: Map of secret key (e.g. connection ID) and value.
    """

    log.debug("Parsing file: %s", file.name)

    ext = file.name.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)

    log.debug("Parsed file: len(parse_errors)=%d, len(secrets)=%d", len(parse_errors), len(secrets))

    if parse_errors:
        raise AirflowFileParseException(
            "Failed to load the secret file.", file_path=file.name, parse_errors=parse_errors
        )

    return secrets