Example #1
0
    def test_get_credentials_file(self, tmp_path, component_tests_config):
        config = component_tests_config()
        tunnel_id = config.get_tunnel_id()

        cred_file = tmp_path / "test_get_credentials_file.json"
        token_args = ["--origincert", cert_path(), "token", "--cred-file", cred_file, tunnel_id]
        start_cloudflared(tmp_path, config, token_args)

        with open(cred_file) as json_file:
            assert config.get_credentials_json() == json.load(json_file)
Example #2
0
    def run_service(self, tmp_path, config, root=False, use_token=False):
        args = ["service", "install"]

        if use_token:
            args.append(config.get_token())

        try:
            service = start_cloudflared(
                tmp_path, config, cfd_args=args, cfd_pre_args=[], capture_output=False, root=root, skip_config_flag=use_token)
            yield service
        finally:
            start_cloudflared(
                tmp_path, config, cfd_args=["service", "uninstall"], cfd_pre_args=[], capture_output=False, root=root, skip_config_flag=use_token)
Example #3
0
 def test_no_connection_shutdown(self, tmp_path, component_tests_config, signal, protocol):
     config = component_tests_config(self._extra_config(protocol))
     with start_cloudflared(
             tmp_path, config, new_process=True, capture_output=False) as cloudflared:
         wait_tunnel_ready(tunnel_url=config.get_url())
         with self.within_grace_period():
             self.terminate_by_signal(cloudflared, signal)
Example #4
0
    def test_validate_ingress_rules(self, tmp_path, component_tests_config):
        extra_config = {
            'ingress': [{
                "hostname": "example.com",
                "service": "https://localhost:8000",
                "originRequest": {
                    "originServerName": "test.example.com",
                    "caPool": "/etc/certs/ca.pem"
                },
            }, {
                "hostname": "api.example.com",
                "path": "login",
                "service": "https://localhost:9000",
            }, {
                "hostname": "wss.example.com",
                "service": "wss://localhost:8000",
            }, {
                "hostname": "ssh.example.com",
                "service": "ssh://localhost:8000",
            }, {
                "service": "http_status:404"
            }],
        }
        config = component_tests_config(extra_config)
        validate_args = ["ingress", "validate"]
        _ = start_cloudflared(tmp_path, config, validate_args)

        self.match_rule(tmp_path, config, "http://example.com/index.html", 1)
        self.match_rule(tmp_path, config, "https://example.com/index.html", 1)
        self.match_rule(tmp_path, config, "https://api.example.com/login", 2)
        self.match_rule(tmp_path, config, "https://wss.example.com", 3)
        self.match_rule(tmp_path, config, "https://ssh.example.com", 4)
        self.match_rule(tmp_path, config, "https://api.example.com", 5)
Example #5
0
def run_test_scenario(tmp_path, component_tests_config, cfd_mode, run_proxy_dns):
    expect_proxy_dns = run_proxy_dns
    expect_tunnel = False

    if cfd_mode == CfdModes.NAMED:
        expect_tunnel = True
        pre_args = ["tunnel"]
        args = ["run"]
    elif cfd_mode == CfdModes.CLASSIC:
        expect_tunnel = True
        pre_args = []
        args = []
    elif cfd_mode == CfdModes.PROXY_DNS:
        expect_proxy_dns = True
        pre_args = []
        args = ["proxy-dns", "--port", str(constants.PROXY_DNS_PORT)]
    else:
        assert False, f"Unknown cfd_mode {cfd_mode}"

    config = component_tests_config(cfd_mode=cfd_mode, run_proxy_dns=run_proxy_dns)
    with start_cloudflared(tmp_path, config, cfd_pre_args=pre_args, cfd_args=args, new_process=True, capture_output=False):
        if expect_tunnel:
            wait_tunnel_ready()
        else:
            check_tunnel_not_connected()
        verify_proxy_dns(expect_proxy_dns)
Example #6
0
 def test_named_reconnect(self, tmp_path, component_tests_config):
     config = component_tests_config(self.extra_config)
     with start_cloudflared(tmp_path,
                            config,
                            new_process=True,
                            allow_input=True) as cloudflared:
         # Repeat the test multiple times because some issues only occur after multiple reconnects
         self.assert_reconnect(config, cloudflared, 5)
Example #7
0
    def test_get_token(self, tmp_path, component_tests_config):
        config = component_tests_config()
        tunnel_id = config.get_tunnel_id()

        token_args = ["--origincert", cert_path(), "token", tunnel_id]
        output = start_cloudflared(tmp_path, config, token_args)

        assert parse_token(config.get_token()) == parse_token(output.stdout)
Example #8
0
 def test_classic_reconnect(self, tmp_path, component_tests_config):
     extra_config = copy.copy(self.extra_config)
     extra_config["hello-world"] = True
     config = component_tests_config(additional_config=extra_config,
                                     named_tunnel=False)
     with start_cloudflared(tmp_path,
                            config,
                            cfd_args=[],
                            new_process=True,
                            allow_input=True) as cloudflared:
         self.assert_reconnect(config, cloudflared, 1)
Example #9
0
 def test_logging_to_dir(self, tmp_path, component_tests_config):
     log_dir = tmp_path / "logs"
     extra_config = {
         "loglevel": "debug",
         # Convert from pathlib.Path to str
         "log-directory": str(log_dir),
     }
     config = component_tests_config(extra_config)
     with start_cloudflared(tmp_path,
                            config,
                            new_process=True,
                            capture_output=False):
         wait_tunnel_ready(tunnel_url=config.get_url())
         self.assert_log_to_dir(config, log_dir)
Example #10
0
 def test_logging_to_file(self, tmp_path, component_tests_config):
     log_file = tmp_path / self.default_log_file
     extra_config = {
         # Convert from pathlib.Path to str
         "logfile": str(log_file),
     }
     config = component_tests_config(extra_config)
     with start_cloudflared(tmp_path,
                            config,
                            new_process=True,
                            capture_output=False):
         wait_tunnel_ready(tunnel_url=config.get_url())
         self.assert_log_in_file(log_file)
         self.assert_json_log(log_file)
Example #11
0
    def test_shutdown_once_no_connection(self, tmp_path, component_tests_config, signal, protocol):
        config = component_tests_config(self._extra_config(protocol))
        with start_cloudflared(
                tmp_path, config, new_process=True, capture_output=False) as cloudflared:
            wait_tunnel_ready(tunnel_url=config.get_url())

            connected = threading.Condition()
            in_flight_req = threading.Thread(
                target=self.stream_request, args=(config, connected, True, ))
            in_flight_req.start()

            with connected:
                connected.wait(self.timeout)
            with self.within_grace_period():
                # Send signal after the SSE connection is established
                self.terminate_by_signal(cloudflared, signal)
                self.wait_eyeball_thread(in_flight_req, self.grace_period)
Example #12
0
 def test_logging_to_terminal(self, tmp_path, component_tests_config):
     config = component_tests_config()
     with start_cloudflared(tmp_path, config,
                            new_process=True) as cloudflared:
         wait_tunnel_ready(tunnel_url=config.get_url())
         self.assert_log_to_terminal(cloudflared)
Example #13
0
    def match_rule(self, tmp_path, config, url, rule_num):
        args = ["ingress", "rule", url]
        match_rule = start_cloudflared(tmp_path, config, args)

        assert f"Matched rule #{rule_num}".encode() in match_rule.stdout