Exemple #1
0
    def authenticate(self):
        """
        Launch the authentication process: for 'auto' provider, it means first to find the relevant
        provider, then call its authenticate() method. Almost every subsequent operation will then
        be delegated to that provider.
        """
        mapping_override = self.config.resolve("lexicon:auto:mapping_override")
        mapping_override_processed = {}
        if mapping_override:
            for one_mapping in mapping_override.split(","):
                one_mapping_processed = one_mapping.split(":")
                mapping_override_processed[
                    one_mapping_processed[0]] = one_mapping_processed[1]

        override_provider = mapping_override_processed.get(self.domain)
        if override_provider:
            provider = [
                element for element in AVAILABLE_PROVIDERS.items()
                if element[0] == override_provider
            ][0]
            LOGGER.info(
                "Provider authoritatively mapped for domain %s: %s.",
                self.domain,
                provider.__name__,
            )
            (provider_name, provider_module) = provider
        else:
            (provider_name,
             provider_module) = _relevant_provider_for_domain(self.domain)
            LOGGER.info("Provider discovered for domain %s: %s.", self.domain,
                        provider_name)

        new_config = helper_config.ConfigResolver()
        new_config.with_dict({"provider_name": provider_name})

        target_prefix = "auto_{0}_".format(provider_name)
        for config_source in self.config._config_sources:
            if not isinstance(config_source, helper_config.ArgsConfigSource):
                new_config.with_config_source(config_source)
            else:
                # ArgsConfigSource needs to be reprocessed to rescope the provided
                # args to the delegate provider
                new_dict = {}
                for (
                        key,
                        value,
                ) in config_source._parameters.items():
                    if key.startswith(target_prefix):
                        new_param_name = re.sub("^{0}".format(target_prefix),
                                                "", key)
                        if provider_name not in new_dict:
                            new_dict[provider_name] = {}
                        new_dict[provider_name][new_param_name] = value
                    elif not key.startswith("auto_"):
                        new_dict[key] = value
                new_config.with_dict(new_dict)

        self.proxy_provider = provider_module.Provider(new_config)
        self.proxy_provider.authenticate()
Exemple #2
0
def _gather_parameters(provider):
    env_variables_of_interest = {
        name: value
        for name, value in os.environ.items()
        if name.startswith("LETSENCRYPT_") or name.startswith("PFX_")
        or name.startswith("CERTS_") or name in
        ["CRON_TIME_STRING", "DOCKER_CLUSTER_PROVIDER", "DEPLOY_HOOK"]
    }

    command_line_params = {}
    command = [
        *shlex.split(os.environ.get("LEXICON_OPTIONS", "")),
        provider,
        "create",
        "example.net",
        "TXT",
        *shlex.split(os.environ.get("LEXICON_PROVIDER_OPTIONS", "")),
    ]
    try:
        args, _ = LEXICON_ARGPARSER.parse_known_args(command)
    except SystemExit:  # pragma: nocover
        args = None

    resolver = config.ConfigResolver()
    if args:
        resolver.with_args(args)
    resolver.with_env()
    resolver.with_config_dir(os.path.dirname(LEGACY_CONFIGURATION_PATH))

    lexicon_files_config: Dict[str, Any] = {}
    for source in resolver._config_sources:
        if isinstance(source, config.FileConfigSource):
            _deep_merge(lexicon_files_config, source._parameters)
        elif isinstance(source, config.EnvironmentConfigSource):
            env_variables_of_interest.update(source._parameters)
        elif isinstance(source, config.ArgsConfigSource):
            command_line_params = {
                provider: {
                    key: value
                    for key, value in source._parameters.items()
                    if key not in (
                        "delegated",
                        "config_dir",
                        "provider_name",
                        "action",
                        "domain",
                        "type",
                        "name",
                        "content",
                        "ttl",
                        "priority",
                        "identifier",
                        "log_level",
                        "output",
                    ) and value is not None
                }
            }
            if source._parameters["delegated"]:
                command_line_params["delegated"] = source._parameters[
                    "delegated"]

    return env_variables_of_interest, lexicon_files_config, command_line_params