-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.py
More file actions
486 lines (394 loc) · 13.6 KB
/
Copy pathproxy.py
File metadata and controls
486 lines (394 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import time
import logging
import shutil
import subprocess
from typing import List, Union
from pathlib import Path
import toml
from manager.routers.notification_api import (
_manager_notification,
register_notification,
)
from manager.constants import (
DAEPLOY_PROXY_DASHBOARD_IDENTIFIER,
DAEPLOY_MANAGER_PROXY_CONFIG_FILE,
DAEPLOY_AUTH_MIDDLEWARE,
DAEPLOY_AUTH_MIDDLEWARE_IDENTIFIER,
DAEPLOY_DATA_DIR,
get_proxy_config_path,
get_proxy_domain_name,
get_proxy_http_port,
get_proxy_https_port,
get_internal_manager_url,
auth_enabled,
https_proxy,
configuration_email,
letsencrypt_staging_server,
)
from manager.exceptions import TraefikError
LOGGER = logging.getLogger(__name__)
def get_static_config_path():
return Path(get_proxy_config_path())
def get_dynamic_config_path():
return get_static_config_path() / Path("dynamic")
def run_proxy():
"""Starts a traefik instance and register an atexit handler for shutdown
Returns:
object: Function handle for stopping proxy
Raises:
TraefikError: If Traefik could not start up properly.
"""
LOGGER.info("Starting traefik proxy!")
config_file_path = (get_static_config_path() / Path("traefik.toml")).resolve()
process = subprocess.Popen(
["traefik", "--configfile", str(config_file_path), "--accesslog=true"],
stdout=None,
stderr=None,
)
# Grace period
time.sleep(1)
# Check that traefik started without any trouble
if process.poll():
_, stderr = process.communicate()
LOGGER.error("Traefik could not start!")
LOGGER.error(f"Traefik return code: {process.returncode}")
LOGGER.error(stderr)
raise TraefikError("Traefik could not start! Check the logs!")
def cleanup():
process.kill()
shutil.rmtree(get_static_config_path())
return cleanup
def write_toml_file(file_path: Path, content: dict):
"""Writes a configuration file with 'content' to
'file_path'
Args:
file_path (Path): Path to file that will be written
content (dict): Content to write
"""
LOGGER.info(f"Writing configuration file: {file_path}")
LOGGER.debug(f"with content: {content}")
file_path.parent.mkdir(parents=True, exist_ok=True)
with file_path.open(mode="w") as file_handle:
toml.dump(
content,
file_handle,
)
def delete_toml_file(file_path: Path):
"""Remove a configuration file at 'file_path'
Args:
file_path (Path): Path to file that will be removed
"""
LOGGER.info(f"Deleting configuration file: {file_path}")
try:
file_path.unlink()
except FileNotFoundError:
# Path.unlink(missing_ok=True) gives same behavior but was
# not introduced until python 3.8
pass
def write_static_configuration():
"""Writes a predefined static configuration file for the proxy"""
static_config = {
"entryPoints": {
"web": {
"address": f":{get_proxy_http_port()}",
},
"websecure": {
"address": f":{get_proxy_https_port()}",
},
},
"providers": {
"providersThrottleDuration": "0.5s",
"file": {
"directory": str(get_dynamic_config_path().resolve()),
"watch": True,
},
},
"api": {
"dashboard": True,
},
}
email = configuration_email()
if https_proxy():
acme_path = DAEPLOY_DATA_DIR / "acme.json"
if not email:
msg = (
"For HTTPS we recommend you set an email address with"
" the DAEPLOY_CONFIG_EMAIL environment variable, to get notified about"
" your certificates."
)
LOGGER.warning(msg)
register_notification(_manager_notification(msg))
static_config["entryPoints"]["web"]["http"] = {
"redirections": {
"entryPoint": {
"to": "websecure",
"scheme": "https",
}
}
}
static_config["certificatesResolvers"] = {
"cert-resolver": {
"acme": {
"email": email or "dummy@email.com",
"storage": str(acme_path),
"httpChallenge": {"entryPoint": "web"},
}
}
}
if letsencrypt_staging_server():
static_config["certificatesResolvers"]["cert-resolver"]["acme"][
"caServer"
] = "https://acme-staging-v02.api.letsencrypt.org/directory"
# Make sure the dynamic path exists
get_dynamic_config_path().mkdir(parents=True, exist_ok=True)
# Write static configuration
LOGGER.info("Writing static configuration file")
write_toml_file(get_static_config_path() / Path("traefik.toml"), static_config)
def add_dynamic_configuration(file_name: str, content: dict):
"""Add a single file dynamic configuration
Args:
file_name (str): Name of file to be added
content (dict): Content of file
"""
file_path = get_dynamic_config_path() / Path(file_name).with_suffix(".toml")
write_toml_file(file_path, content)
def remove_dynamic_configuration(file_name: str):
"""Remove a single file dynamic configuration
Args:
file_name (str): Name of file to be removed
"""
file_path = get_dynamic_config_path() / Path(file_name).with_suffix(".toml")
delete_toml_file(file_path)
def get_router_configuration(
rule: str,
service: str,
middlewares: Union[List[str], None],
tls: bool,
) -> dict:
"""Get the router configuration for one rule.
Args:
rule (str): Router rule
service (str): Service name to run on the router
middlewares (Union[List[str], None]): Any middlewared associated with the router
tls (bool): Should the router have TLS
Returns:
dict: Router configuration
"""
config = {
"rule": rule,
"service": service,
}
if tls:
config["tls"] = {"certresolver": "cert-resolver"}
if middlewares:
config["middlewares"] = middlewares
return config
def get_proxy_dashboard_configuration() -> dict:
"""Returns configuration for the traefik internal dashboard
Returns:
dict: configuration
"""
rule = f"""Host(`{get_proxy_domain_name()}`) && \
(PathPrefix(`/proxy/dashboard`) || PathPrefix(`/api`))"""
config = {
"http": {
"routers": {
"traefik_dashboard": get_router_configuration(
rule=rule,
service="api@internal",
middlewares=["traefik-dashboard-prefix-stripper"],
tls=https_proxy(),
)
},
"middlewares": {
"traefik-dashboard-prefix-stripper": {
"stripPrefix": {"prefixes": ["/proxy"], "forceSlash": False}
}
},
}
}
if auth_enabled():
config["http"]["routers"]["traefik_dashboard"]["middlewares"].append(
DAEPLOY_AUTH_MIDDLEWARE
)
return config
def get_manager_configuration() -> dict:
"""Returns configuration for the manager dashboard
Returns:
dict: configuration
"""
rule = f"""Host(`{get_proxy_domain_name()}`)"""
rule_login = f"""Host(`{get_proxy_domain_name()}`) && PathPrefix(`/auth/login`)"""
# Static UI assets (CSS, fonts, logo) must load on the login page *before*
# the user authenticates, so they are served by a router without the auth
# middleware. These are non-sensitive presentation files only.
rule_assets = f"""Host(`{get_proxy_domain_name()}`) && PathPrefix(`/assets`)"""
config = {
"http": {
"routers": {
"manager": get_router_configuration(
rule=rule,
service="manager_service",
middlewares=None,
tls=https_proxy(),
),
"login_page": get_router_configuration(
rule=rule_login,
service="manager_service",
middlewares=None,
tls=https_proxy(),
),
"static_assets": get_router_configuration(
rule=rule_assets,
service="manager_service",
middlewares=None,
tls=https_proxy(),
),
},
"services": {
"manager_service": {
"loadBalancer": {
"servers": [
{
"url": get_internal_manager_url(),
}
]
}
}
},
}
}
if auth_enabled():
config["http"]["routers"]["manager"]["middlewares"] = [DAEPLOY_AUTH_MIDDLEWARE]
return config
def get_auth_middleware_configuration() -> dict:
"""Returns configuration for the auth middleware
Returns:
dict: configuration
"""
return {
"http": {
"middlewares": {
DAEPLOY_AUTH_MIDDLEWARE: {
"forwardAuth": {
"address": f"{get_internal_manager_url()}/auth/verify",
}
}
}
}
}
def initial_setup():
"""Perform initial setup of proxy
Returns:
object: Function handle to kill proxy and clean up configuration files
"""
# Create and write static configuration
write_static_configuration()
# Add core dynamic configurations
add_dynamic_configuration(
DAEPLOY_PROXY_DASHBOARD_IDENTIFIER, get_proxy_dashboard_configuration()
)
# Add manager to root
add_dynamic_configuration(
DAEPLOY_MANAGER_PROXY_CONFIG_FILE, get_manager_configuration()
)
# Add auth middleware
add_dynamic_configuration(
DAEPLOY_AUTH_MIDDLEWARE_IDENTIFIER, get_auth_middleware_configuration()
)
# Start traefik and return function handle for cleaning up
return run_proxy()
def get_base_service_config(service_name: str) -> dict:
"""Returns a standard service configuration with routing and prefix stripper.
Only the actual service is missing.
Args:
service_name (str): Name of the service to create the base config for
Returns:
dict: Routing and prefix configuration
"""
path_prefix = f"/services/{service_name}"
prefix_strip_middleware = f"{service_name}_prefix_stripper"
rule = f"Host(`{get_proxy_domain_name()}`) && PathPrefix(`{path_prefix}`)"
config = {
"http": {
"routers": {
service_name: get_router_configuration(
rule=rule,
service=service_name,
middlewares=[prefix_strip_middleware],
tls=https_proxy(),
),
},
"middlewares": {
prefix_strip_middleware: {
"stripPrefix": {"prefixes": [path_prefix], "forceSlash": False}
}
},
"services": {},
}
}
if auth_enabled():
config["http"]["routers"][service_name]["middlewares"].append(
DAEPLOY_AUTH_MIDDLEWARE
)
return config
def create_new_service_configuration(name: str, version: str, address: str):
"""Creates a dymanic configuration file for a new service.
Args:
name (str): Name of the service
version (str): version of the service
address (str): URL to the service
"""
LOGGER.info(f"Creating configuration for service: {name}")
service_name = f"{name}_{version}"
file_name = f"service_{service_name}_configuration.toml"
config = get_base_service_config(service_name)
# Create service configurations
services = {}
services[service_name] = {
"loadBalancer": {
"servers": [
{
"url": address,
}
]
}
}
config["http"]["services"] = services
add_dynamic_configuration(file_name, config)
def create_mirror_configuration(
name: str, main_version: str, shadow_versions: List[str] = None
):
"""Sets up a configuration file for shadow deployment. Where any calls sent
to /services/service_version/ is mirrored to all services with that name.
Args:
name (str): Name of the service
main_version (str): Version of the main service
shadow_versions (List[str], optional): List of versions of the shadow
services. Defaults to None.
"""
file_name = f"service_{name}_configuration.toml"
config = get_base_service_config(name)
shadow_versions = shadow_versions or []
mirrors = [{"name": f"{name}_{ver}", "percent": 100} for ver in shadow_versions]
main_service_name = f"{name}_{main_version}"
services = {}
services[name] = {
"mirroring": {
"service": main_service_name,
"mirrors": mirrors,
}
}
config["http"]["services"] = services
add_dynamic_configuration(file_name, config)
def remove_service_configuration(name: str, version: str):
"""Remove a service configuration.
Args:
name (str): Service name
version (str): Service version
"""
service_name = f"{name}_{version}"
file_name = f"service_{service_name}_configuration.toml"
mirror_file_name = f"service_{name}_configuration.toml"
remove_dynamic_configuration(file_name)
remove_dynamic_configuration(mirror_file_name)