cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e2a8302bbca9804507e64d77feeecb04298260b5

Branches

Tags

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

Clone

HTTPS

Download ZIP

component-tests/config.py

123lines · modecode

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