cloudflare/cloudflared

Public

mirrored fromhttps://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4d95ab73f584a5a6439803648f013031ca1dd4ac

Branches

Tags

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

Clone

HTTPS

Download ZIP

component-tests/config.py

101lines · modecode

1#!/usr/bin/env python
2import copy
3import json
4import base64
5
6from dataclasses import dataclass, InitVar
7
8from constants import METRICS_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 hostname: str = None
34
35 def __post_init__(self):
36 if self.tunnel is None:
37 raise TypeError("Field tunnel is not set")
38 if self.credentials_file is None:
39 raise TypeError("Field credentials_file is not set")
40 if self.ingress is None:
41 raise TypeError("Field ingress is not set")
42
43 def merge_config(self, additional):
44 config = super(NamedTunnelBaseConfig, self).merge_config(additional)
45 if 'tunnel' not in config:
46 config['tunnel'] = self.tunnel
47 if 'credentials-file' not in config:
48 config['credentials-file'] = self.credentials_file
49 # In some cases we want to override default ingress, such as in config tests
50 if 'ingress' not in config:
51 config['ingress'] = self.ingress
52 return config
53
54
55@dataclass(frozen=True)
56class NamedTunnelConfig(NamedTunnelBaseConfig):
57 full_config: dict = None
58 additional_config: InitVar[dict] = {}
59
60 def __post_init__(self, additional_config):
61 # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
62 # https://docs.python.org/3/library/dataclasses.html#frozen-instances
63 object.__setattr__(self, 'full_config',
64 self.merge_config(additional_config))
65
66 def get_url(self):
67 return "https://" + self.hostname
68
69 def base_config(self):
70 config = self.full_config.copy()
71
72 # removes the tunnel reference
73 del(config["tunnel"])
74 del(config["credentials-file"])
75
76 return config
77
78 def get_tunnel_id(self):
79 return self.full_config["tunnel"]
80
81 def get_token(self):
82 creds = self.get_credentials_json()
83 token_dict = {"a": creds["AccountTag"], "t": creds["TunnelID"], "s": creds["TunnelSecret"]}
84 token_json_str = json.dumps(token_dict)
85 return base64.b64encode(token_json_str.encode('utf-8'))
86
87 def get_credentials_json(self):
88 with open(self.credentials_file) as json_file:
89 return json.load(json_file)
90
91@dataclass(frozen=True)
92class QuickTunnelConfig(BaseConfig):
93 full_config: dict = None
94 additional_config: InitVar[dict] = {}
95
96 def __post_init__(self, additional_config):
97 # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
98 # https://docs.python.org/3/library/dataclasses.html#frozen-instances
99 object.__setattr__(self, 'full_config',
100 self.merge_config(additional_config))
101
102