cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.12.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

component-tests/config.py

104lines · modeblame

a7344435cthuang5 years ago1#!/usr/bin/env python
2import copy
3
f23e33c0cthuang5 years ago4from dataclasses import dataclass, InitVar
a7344435cthuang5 years ago5
1e8dea91Nuno Diegues4 years ago6from constants import METRICS_PORT, PROXY_DNS_PORT
a7344435cthuang5 years ago7
8# frozen=True raises exception when assigning to fields. This emulates immutability
f23e33c0cthuang5 years ago9
10
a7344435cthuang5 years ago11@dataclass(frozen=True)
25cfbec0cthuang5 years ago12class BaseConfig:
13cloudflared_binary: str
a7344435cthuang5 years ago14no_autoupdate: bool = True
15metrics: str = f'localhost:{METRICS_PORT}'
16
17def merge_config(self, additional):
18config = copy.copy(additional)
19config['no-autoupdate'] = self.no_autoupdate
20config['metrics'] = self.metrics
21return config
22
23
24@dataclass(frozen=True)
25cfbec0cthuang5 years ago25class NamedTunnelBaseConfig(BaseConfig):
a7344435cthuang5 years ago26# 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__
28tunnel: str = None
29credentials_file: str = None
f23e33c0cthuang5 years ago30ingress: list = None
a7344435cthuang5 years ago31
32def __post_init__(self):
33if self.tunnel is None:
34raise TypeError("Field tunnel is not set")
35if self.credentials_file is None:
36raise TypeError("Field credentials_file is not set")
f23e33c0cthuang5 years ago37if self.ingress is None:
38raise TypeError("Field ingress is not set")
a7344435cthuang5 years ago39
40def merge_config(self, additional):
41config = super(NamedTunnelBaseConfig, self).merge_config(additional)
42config['tunnel'] = self.tunnel
43config['credentials-file'] = self.credentials_file
f23e33c0cthuang5 years ago44# In some cases we want to override default ingress, such as in config tests
45if 'ingress' not in config:
46config['ingress'] = self.ingress
a7344435cthuang5 years ago47return config
48
49
f23e33c0cthuang5 years ago50@dataclass(frozen=True)
51class NamedTunnelConfig(NamedTunnelBaseConfig):
52full_config: dict = None
53additional_config: InitVar[dict] = {}
54
55def __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
58object.__setattr__(self, 'full_config',
59self.merge_config(additional_config))
60
61def get_url(self):
62return "https://" + self.ingress[0]['hostname']
63
64
a7344435cthuang5 years ago65@dataclass(frozen=True)
25cfbec0cthuang5 years ago66class ClassicTunnelBaseConfig(BaseConfig):
a7344435cthuang5 years ago67hostname: str = None
68origincert: str = None
69
70def __post_init__(self):
71if self.hostname is None:
72raise TypeError("Field tunnel is not set")
73if self.origincert is None:
74raise TypeError("Field credentials_file is not set")
75
76def merge_config(self, additional):
77config = super(ClassicTunnelBaseConfig, self).merge_config(additional)
f23e33c0cthuang5 years ago78config['hostname'] = self.hostname
a7344435cthuang5 years ago79config['origincert'] = self.origincert
80return config
81
82
f23e33c0cthuang5 years ago83@dataclass(frozen=True)
84class ClassicTunnelConfig(ClassicTunnelBaseConfig):
85full_config: dict = None
86additional_config: InitVar[dict] = {}
87
88def __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
91object.__setattr__(self, 'full_config',
92self.merge_config(additional_config))
93
94def get_url(self):
95return "https://" + self.hostname
1e8dea91Nuno Diegues4 years ago96
97
98@dataclass(frozen=True)
99class ProxyDnsConfig(BaseConfig):
100full_config = {
101"port": PROXY_DNS_PORT,
102"no-autoupdate": True,
103}
104