Beispiel #1
0
    def test_disable_verify_ssl(self):
        configuration = Configuration()
        self.assertTrue(configuration.verify_ssl)

        _disable_verify_ssl()

        configuration = Configuration()
        self.assertFalse(configuration.verify_ssl)
Beispiel #2
0
    def get_conn(self) -> client.ApiClient:
        """Returns kubernetes api session for use with requests"""
        in_cluster = self._coalesce_param(
            self.in_cluster,
            self.conn_extras.get("extra__kubernetes__in_cluster") or None)
        cluster_context = self._coalesce_param(
            self.cluster_context,
            self.conn_extras.get("extra__kubernetes__cluster_context") or None)
        kubeconfig_path = self._coalesce_param(
            self.config_file,
            self.conn_extras.get("extra__kubernetes__kube_config_path")
            or None)

        kubeconfig = self.conn_extras.get(
            "extra__kubernetes__kube_config") or None
        num_selected_configuration = len(
            [o for o in [in_cluster, kubeconfig, kubeconfig_path] if o])

        if num_selected_configuration > 1:
            raise AirflowException(
                "Invalid connection configuration. Options kube_config_path, "
                "kube_config, in_cluster are mutually exclusive. "
                "You can only use one option at a time.")

        disable_verify_ssl = self._coalesce_param(
            self.disable_verify_ssl,
            _get_bool(self._get_field("disable_verify_ssl")))
        disable_tcp_keepalive = self._coalesce_param(
            self.disable_tcp_keepalive,
            _get_bool(self._get_field("disable_tcp_keepalive")))

        # BEGIN apply settings from core kubernetes configuration
        # this section should be removed in next major release
        deprecation_warnings: List[Tuple[str, Any]] = []
        if disable_verify_ssl is None and self._deprecated_core_disable_verify_ssl is True:
            deprecation_warnings.append(('verify_ssl', False))
            disable_verify_ssl = self._deprecated_core_disable_verify_ssl
        # by default, hook will try in_cluster first. so we only need to
        # apply core airflow config and alert when False and in_cluster not otherwise set.
        if in_cluster is None and self._deprecated_core_in_cluster is False:
            deprecation_warnings.append(
                ('in_cluster', self._deprecated_core_in_cluster))
            in_cluster = self._deprecated_core_in_cluster
        if not cluster_context and self._deprecated_core_cluster_context:
            deprecation_warnings.append(
                ('cluster_context', self._deprecated_core_cluster_context))
            cluster_context = self._deprecated_core_cluster_context
        if not kubeconfig_path and self._deprecated_core_config_file:
            deprecation_warnings.append(
                ('config_file', self._deprecated_core_config_file))
            kubeconfig_path = self._deprecated_core_config_file
        if disable_tcp_keepalive is None and self._deprecated_core_disable_tcp_keepalive is True:
            deprecation_warnings.append(('enable_tcp_keepalive', False))
            disable_tcp_keepalive = True
        if deprecation_warnings:
            self._deprecation_warning_core_param(deprecation_warnings)
        # END apply settings from core kubernetes configuration

        if disable_verify_ssl is True:
            _disable_verify_ssl()
        if disable_tcp_keepalive is not True:
            _enable_tcp_keepalive()

        if in_cluster:
            self.log.debug(
                "loading kube_config from: in_cluster configuration")
            self._is_in_cluster = True
            config.load_incluster_config()
            return client.ApiClient()

        if kubeconfig_path is not None:
            self.log.debug("loading kube_config from: %s", kubeconfig_path)
            self._is_in_cluster = False
            config.load_kube_config(
                config_file=kubeconfig_path,
                client_configuration=self.client_configuration,
                context=cluster_context,
            )
            return client.ApiClient()

        if kubeconfig is not None:
            with tempfile.NamedTemporaryFile() as temp_config:
                self.log.debug(
                    "loading kube_config from: connection kube_config")
                temp_config.write(kubeconfig.encode())
                temp_config.flush()
                self._is_in_cluster = False
                config.load_kube_config(
                    config_file=temp_config.name,
                    client_configuration=self.client_configuration,
                    context=cluster_context,
                )
            return client.ApiClient()

        return self._get_default_client(cluster_context=cluster_context)