cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.11.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

component-tests/config.py

104lines · modecode

1#!/usr/bin/env python
2import copy
3
4from dataclasses import dataclass, InitVar
5
6from constants import METRICS_PORT, PROXY_DNS_PORT
7
8# frozen=True raises exception when assigning to fields. This emulates immutability
9
10
11@dataclass(frozen=True)
12class BaseConfig:
13 cloudflared_binary: str
14 no_autoupdate: bool = True
15 metrics: str = f'localhost:{METRICS_PORT}'
16
17 def merge_config(self, additional):
18 config = copy.copy(additional)
19 config['no-autoupdate'] = self.no_autoupdate
20 config['metrics'] = self.metrics
21 return config
22
23
24@dataclass(frozen=True)
25class NamedTunnelBaseConfig(BaseConfig):
26 # The attributes of the parent class are ordered before attributes in this class,
27 # so we have to use default values here and check if they are set in __post_init__
28 tunnel: str = None
29 credentials_file: str = None
30 ingress: list = None
31
32 def __post_init__(self):
33 if self.tunnel is None:
34 raise TypeError("Field tunnel is not set")
35 if self.credentials_file is None:
36 raise TypeError("Field credentials_file is not set")
37 if self.ingress is None:
38 raise TypeError("Field ingress is not set")
39
40 def merge_config(self, additional):
41 config = super(NamedTunnelBaseConfig, self).merge_config(additional)
42 config['tunnel'] = self.tunnel
43 config['credentials-file'] = self.credentials_file
44 # In some cases we want to override default ingress, such as in config tests
45 if 'ingress' not in config:
46 config['ingress'] = self.ingress
47 return config
48
49
50@dataclass(frozen=True)
51class NamedTunnelConfig(NamedTunnelBaseConfig):
52 full_config: dict = None
53 additional_config: InitVar[dict] = {}
54
55 def __post_init__(self, additional_config):
56 # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
57 # https://docs.python.org/3/library/dataclasses.html#frozen-instances
58 object.__setattr__(self, 'full_config',
59 self.merge_config(additional_config))
60
61 def get_url(self):
62 return "https://" + self.ingress[0]['hostname']
63
64
65@dataclass(frozen=True)
66class ClassicTunnelBaseConfig(BaseConfig):
67 hostname: str = None
68 origincert: str = None
69
70 def __post_init__(self):
71 if self.hostname is None:
72 raise TypeError("Field tunnel is not set")
73 if self.origincert is None:
74 raise TypeError("Field credentials_file is not set")
75
76 def merge_config(self, additional):
77 config = super(ClassicTunnelBaseConfig, self).merge_config(additional)
78 config['hostname'] = self.hostname
79 config['origincert'] = self.origincert
80 return config
81
82
83@dataclass(frozen=True)
84class ClassicTunnelConfig(ClassicTunnelBaseConfig):
85 full_config: dict = None
86 additional_config: InitVar[dict] = {}
87
88 def __post_init__(self, additional_config):
89 # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
90 # https://docs.python.org/3/library/dataclasses.html#frozen-instances
91 object.__setattr__(self, 'full_config',
92 self.merge_config(additional_config))
93
94 def get_url(self):
95 return "https://" + self.hostname
96
97
98@dataclass(frozen=True)
99class ProxyDnsConfig(BaseConfig):
100 full_config = {
101 "port": PROXY_DNS_PORT,
102 "no-autoupdate": True,
103 }
104
105