This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
config.py
1699 lines (1476 loc) · 61 KB
/
config.py
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import logging
import os
logger = logging.getLogger('marathon_lb')
class ConfigTemplate:
def __init__(self, name, value, overridable, description):
self.name = name
self.full_name = 'HAPROXY_' + name
self.value = value
self.default_value = value
self.overridable = overridable
self.description = description
class ConfigTemplater(object):
def add_template(self, template):
self.t[template.name] = template
def global_default_options(self):
default = 'redispatch,http-server-close,dontlognull'
options = os.getenv('HAPROXY_GLOBAL_DEFAULT_OPTIONS', default)
template = ' option {opt}\n'
lines = sorted(set(template.format(opt=opt.strip())
for opt in options.split(',')))
return ''.join(lines)
def load(self):
self.add_template(
ConfigTemplate(name='HEAD',
value='''\
global
log /dev/log local0
log /dev/log local1 notice
spread-checks 5
max-spread-checks 15000
maxconn 50000
tune.ssl.default-dh-param 2048
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:\
ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:\
ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:\
DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:\
ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES128-SHA256:\
DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:\
AES256-SHA256:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3 no-tlsv10 no-tls-tickets
ssl-default-server-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:\
ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:\
ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:\
DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:\
ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES128-SHA256:\
DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:\
AES256-SHA256:!aNULL:!MD5:!DSS
ssl-default-server-options no-sslv3 no-tlsv10 no-tls-tickets
stats socket /var/run/haproxy/socket expose-fd listeners
server-state-file global
server-state-base /var/state/haproxy/
lua-load /marathon-lb/getpids.lua
lua-load /marathon-lb/getconfig.lua
lua-load /marathon-lb/getmaps.lua
lua-load /marathon-lb/signalmlb.lua
defaults
load-server-state-from-file global
log global
retries 3
backlog 10000
maxconn 10000
timeout connect 3s
timeout client 30s
timeout server 30s
timeout tunnel 3600s
timeout http-keep-alive 1s
timeout http-request 15s
timeout queue 30s
timeout tarpit 60s
''' + self.global_default_options() + '''\
listen stats
bind 0.0.0.0:9090
balance
mode http
stats enable
http-request use-service prometheus-exporter if { path /metrics }
monitor-uri /_haproxy_health_check
acl getpid path /_haproxy_getpids
http-request use-service lua.getpids if getpid
acl getvhostmap path /_haproxy_getvhostmap
http-request use-service lua.getvhostmap if getvhostmap
acl getappmap path /_haproxy_getappmap
http-request use-service lua.getappmap if getappmap
acl getconfig path /_haproxy_getconfig
http-request use-service lua.getconfig if getconfig
acl signalmlbhup path /_mlb_signal/hup
http-request use-service lua.signalmlbhup if signalmlbhup
acl signalmlbusr1 path /_mlb_signal/usr1
http-request use-service lua.signalmlbusr1 if signalmlbusr1
''',
overridable=False,
description='''\
The head of the HAProxy config. This contains global settings
and defaults.
'''))
self.add_template(
ConfigTemplate(name='USERLIST_HEAD',
value='''
userlist user_{backend}
user {user} password {passwd}
''',
overridable=True,
description='''\
The userlist for basic HTTP auth.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_HEAD',
value='''
frontend marathon_http_in
bind *:80
mode http
''',
overridable=False,
description='''\
An HTTP frontend that binds to port *:80 by default and gathers
all virtual hosts as defined by the `HAPROXY_{n}_VHOST` label.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_APPID_HEAD',
value='''
frontend marathon_http_appid_in
bind *:9091
mode http
''',
overridable=False,
description='''\
An HTTP frontend that binds to port *:9091 by default and gathers
all apps in HTTP mode.
To use this frontend to forward to your app, configure the app with
`HAPROXY_0_MODE=http` then you can access it via a call to the :9091
with the header "X-Marathon-App-Id" set to the Marathon AppId.
Note multiple HTTP ports being exposed by the same marathon app are not
supported. Only the first HTTP port is available via this frontend.
'''))
# TODO(lloesche): make certificate path dynamic and allow multi-certs
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_HEAD',
value='''
frontend marathon_https_in
bind *:443 ssl {sslCerts}
mode http
''',
overridable=False,
description='''\
An HTTPS frontend for encrypted connections that binds to port *:443 by
default and gathers all virtual hosts as defined by the
`HAPROXY_{n}_VHOST` label. You must modify this file to
include your certificate.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_GROUPED_FRONTEND_HEAD',
value='''
frontend marathon_https_in
bind *:443
mode tcp
tcp-request inspect-delay 5s
tcp-request content accept if { req_ssl_hello_type 1 }
''',
overridable=False,
description='''\
An HTTPS frontend for encrypted connections that binds to port *:443 by
default and gathers all virtual hosts as defined by the
`HAPROXY_{n}_VHOST` label. Useful for adding client certificated per domain.
Works only with an enabled group-https-by-vhost flag.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_GROUPED_VHOST_FRONTEND_HEAD',
value='''
frontend {name}
mode http
bind abns@{name} accept-proxy ssl {sslCerts}{bindOpts}
''',
overridable=False,
description='''\
An HTTPS frontend for vhost.
Works only with an enabled group-https-by-vhost flag.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_GROUPED_VHOST_BACKEND_HEAD',
value='''
backend {name}
server loopback-for-tls abns@{name} send-proxy-v2
''',
overridable=False,
description='''\
An HTTPS backend for vhost.
Works only with an enabled group-https-by-vhost flag.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_GROUPED_VHOST_FRONTEND_ACL',
value='''\
use_backend {backend} if {{ req_ssl_sni -i {host} }}
''',
overridable=False,
description='''\
A route rule https entrypoint.
Works only with an enabled group-https-by-vhost flag.
'''))
self.add_template(
ConfigTemplate(name='FRONTEND_HEAD',
value='''
frontend {backend}
bind {bindAddr}:{servicePort}{sslCert}{bindOptions}
mode {mode}
''',
overridable=True,
description='''\
Defines the address and port to bind to for this frontend.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_HEAD',
value='''
backend {backend}
balance {balance}
mode {mode}
''',
overridable=True,
description='''\
Defines the type of load balancing, roundrobin by default,
and connection mode, TCP or HTTP.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_REDIRECT_HTTP_TO_HTTPS',
value='''\
redirect scheme https code 301 if !{{ ssl_fc }} host_{cleanedUpHostname}
''',
overridable=True,
description='''\
This template is used with backends where the
`HAPROXY_{n}_REDIRECT_TO_HTTPS` label is set to true
'''))
self.add_template(
ConfigTemplate(name='BACKEND_REDIRECT_HTTP_TO_HTTPS_WITH_PATH',
value='''\
redirect scheme https code 301 if !{{ ssl_fc }} host_{cleanedUpHostname}\
path_{backend}
''',
overridable=True,
description='''\
Same as `HAPROXY_BACKEND_REDIRECT_HTTP_TO_HTTPS`,
but includes a path.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_HSTS_OPTIONS',
value='''\
rspadd ''' + r'''Strict-Transport-Security:\ max-age=15768000
''',
overridable=True,
description='''\
This template is used for the backend where the
`HAPROXY_{n}_USE_HSTS` label is set to true.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL',
value='''\
acl host_{cleanedUpHostname} hdr(host) -i {hostname}
use_backend {backend} if host_{cleanedUpHostname}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host
of the `HAPROXY_HTTP_FRONTEND_HEAD`
'''))
self.add_template(
ConfigTemplate(name='MAP_HTTP_FRONTEND_ACL',
value='''\
use_backend %[req.hdr(host),lower,regsub(:.*$,,),\
map({haproxy_dir}/domain2backend.map)]
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host
of the `HAPROXY_HTTP_FRONTEND_HEAD` using haproxy maps.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_WITH_AUTH',
value='''\
acl host_{cleanedUpHostname} hdr(host) -i {hostname}
acl auth_{cleanedUpHostname} http_auth(user_{backend})
http-request auth realm "{realm}" if host_{cleanedUpHostname}\
!auth_{cleanedUpHostname}
use_backend {backend} if host_{cleanedUpHostname}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host
of the `HAPROXY_HTTP_FRONTEND_HEAD` thru HTTP basic auth.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_ONLY',
value='''\
acl host_{cleanedUpHostname} hdr(host) -i {hostname}
''',
overridable=True,
description='''\
Define the ACL matching a particular hostname, but unlike
`HAPROXY_HTTP_FRONTEND_ACL`, only do the ACL portion. Does not glue
the ACL to the backend. This is useful only in the case of multiple
vhosts routing to the same backend.
'''))
self.add_template(
ConfigTemplate(name='MAP_HTTP_FRONTEND_ACL_ONLY',
value='''\
use_backend %[req.hdr(host),lower,regsub(:.*$,,),\
map({haproxy_dir}/domain2backend.map)]
''',
overridable=True,
description='''\
Define the ACL matching a particular hostname, This is useful only in the case
of multiple vhosts routing to the same backend in haproxy map.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ROUTING_ONLY',
value='''\
use_backend {backend} if host_{cleanedUpHostname}
''',
overridable=True,
description='''\
This is the counterpart to `HAPROXY_HTTP_FRONTEND_ACL_ONLY` which
glues the acl name to the appropriate backend.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ROUTING_ONLY_WITH_AUTH',
value='''\
acl auth_{cleanedUpHostname} http_auth(user_{backend})
http-request auth realm "{realm}" if host_{cleanedUpHostname} \
!auth_{cleanedUpHostname}
use_backend {backend} if host_{cleanedUpHostname}
''',
overridable=True,
description='''\
This is the counterpart to `HAPROXY_HTTP_FRONTEND_ACL_ONLY` which
glues the acl name to the appropriate backend, and add http basic auth.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_WITH_PATH',
value='''\
acl host_{cleanedUpHostname} hdr(host) -i {hostname}
acl path_{backend} path_beg {path}
use_backend {backend} if host_{cleanedUpHostname} path_{backend}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host with path
of the `HAPROXY_HTTP_FRONTEND_HEAD`.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_WITH_AUTH_AND_PATH',
value='''\
acl host_{cleanedUpHostname} hdr(host) -i {hostname}
acl auth_{cleanedUpHostname} http_auth(user_{backend})
acl path_{backend} path_beg {path}
http-request auth realm "{realm}" if host_{cleanedUpHostname} \
path_{backend} !auth_{cleanedUpHostname}
use_backend {backend} if host_{cleanedUpHostname} path_{backend}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host with path
of the `HAPROXY_HTTP_FRONTEND_HEAD` thru HTTP basic auth.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_ONLY_WITH_PATH',
value='''\
acl path_{backend} path_beg {path}
''',
overridable=True,
description='''\
Define the ACL matching a particular hostname with path, but unlike
`HAPROXY_HTTP_FRONTEND_ACL_WITH_PATH`, only do the ACL portion. Does not glue
the ACL to the backend. This is useful only in the case of multiple
vhosts routing to the same backend
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ACL_ONLY_WITH_PATH_AND_AUTH',
value='''\
acl path_{backend} path_beg {path}
acl auth_{cleanedUpHostname} http_auth(user_{backend})
''',
overridable=True,
description='''\
Define the ACL matching a particular hostname with path and auth, but unlike
`HAPROXY_HTTP_FRONTEND_ACL_WITH_PATH`, only do the ACL portion. Does not glue
the ACL to the backend. This is useful only in the case of multiple
vhosts routing to the same backend
'''))
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_ACL_ONLY_WITH_PATH',
value='''\
acl path_{backend} path_beg {path}
''',
overridable=True,
description='''\
Same as HTTP_FRONTEND_ACL_ONLY_WITH_PATH, but for HTTPS.
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_ROUTING_ONLY_WITH_PATH',
value='''\
use_backend {backend} if host_{cleanedUpHostname} path_{backend}
''',
overridable=True,
description='''\
This is the counterpart to `HAPROXY_HTTP_FRONTEND_ACL_ONLY_WITH_PATH` which
glues the acl names to the appropriate backend
'''))
self.add_template(
ConfigTemplate(name='\
HTTP_FRONTEND_ROUTING_ONLY_WITH_PATH_AND_AUTH',
value='''\
http-request auth realm "{realm}" if host_{cleanedUpHostname} \
path_{backend} !auth_{cleanedUpHostname}
use_backend {backend} if host_{cleanedUpHostname} path_{backend}
''',
overridable=True,
description='''\
This is the counterpart to `HAPROXY_HTTP_FRONTEND_ACL_ONLY_WITH_PATH` which
glues the acl names to the appropriate backend
'''))
# XXX Missing function set label in app, as well as not appending
# a label. Since neither exist, not adding this for now.
self.add_template(
ConfigTemplate(name='\
HTTPS_FRONTEND_ROUTING_ONLY_WITH_PATH_AND_AUTH',
value='''\
http-request auth realm "{realm}" if host_{cleanedUpHostname} \
path_{backend} !auth_{cleanedUpHostname}
use_backend {backend} if host_{cleanedUpHostname} path_{backend}
''',
overridable=True,
description='''\
This is the counterpart to `HAPROXY_HTTP_FRONTEND_ACL_ONLY_WITH_PATH` which
glues the acl names to the appropriate backend
'''))
self.add_template(
ConfigTemplate(name='HTTP_FRONTEND_APPID_ACL',
value='''\
acl app_{cleanedUpAppId} hdr(x-marathon-app-id) -i {appId}
use_backend {backend} if app_{cleanedUpAppId}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding app
of the `HAPROXY_HTTP_FRONTEND_APPID_HEAD`.
'''))
self.add_template(
ConfigTemplate(name='MAP_HTTP_FRONTEND_APPID_ACL',
value='''\
use_backend %[req.hdr(x-marathon-app-id),lower,\
map({haproxy_dir}/app2backend.map)]
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding app
of the `HAPROXY_HTTP_FRONTEND_APPID_HEAD` using haproxy maps.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_ACL',
value='''\
use_backend {backend} if {{ ssl_fc_sni {hostname} }}
''',
overridable=True,
description='''\
The ACL that performs the SNI based hostname matching
for the `HAPROXY_HTTPS_FRONTEND_HEAD` template.
'''))
self.add_template(
ConfigTemplate(name='MAP_HTTPS_FRONTEND_ACL',
value='''\
use_backend %[ssl_fc_sni,lower,map({haproxy_dir}/domain2backend.map)]
''',
overridable=True,
description='''\
The ACL that performs the SNI based hostname matching
for the `HAPROXY_HTTPS_FRONTEND_HEAD` template using haproxy maps
'''))
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_ACL_WITH_AUTH',
value='''\
acl auth_{cleanedUpHostname} http_auth(user_{backend})
http-request auth realm "{realm}" if {{ ssl_fc_sni {hostname} }} \
!auth_{cleanedUpHostname}
use_backend {backend} if {{ ssl_fc_sni {hostname} }}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host
of the `HAPROXY_HTTPS_FRONTEND_HEAD` thru HTTP basic auth.
'''))
# XXX Missing function set label in app, as well as not appending
# a label. Since neither exist, not adding this for now.
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_AUTH_ACL_ONLY',
value='''\
acl auth_{cleanedUpHostname} http_auth(user_{backend})
''',
overridable=True,
description='''\
The http auth ACL to the corresponding virtual host.
'''))
# XXX Missing function set label in app, as well as not appending
# a label. Since neither exist, not adding this for now.
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_AUTH_REQUEST_ONLY',
value='''\
http-request auth realm "{realm}" if {{ ssl_fc_sni {hostname} }} \
!auth_{cleanedUpHostname}
''',
overridable=True,
description='''\
The http auth request to the corresponding virtual host.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_ACL_WITH_PATH',
value='''\
use_backend {backend} if {{ ssl_fc_sni {hostname} }} path_{backend}
''',
overridable=True,
description='''\
The ACL that performs the SNI based hostname matching with path
for the `HAPROXY_HTTPS_FRONTEND_HEAD` template.
'''))
self.add_template(
ConfigTemplate(name='HTTPS_FRONTEND_ACL_WITH_AUTH_AND_PATH',
value='''\
acl auth_{cleanedUpHostname} http_auth(user_{backend})
http-request auth realm "{realm}" if {{ ssl_fc_sni {hostname} }} \
path_{backend} !auth_{cleanedUpHostname}
use_backend {backend} if {{ ssl_fc_sni {hostname} }} path_{backend}
''',
overridable=True,
description='''\
The ACL that glues a backend to the corresponding virtual host with path
of the `HAPROXY_HTTPS_FRONTEND_HEAD` thru HTTP basic auth.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_HTTP_OPTIONS',
value='''\
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
''',
overridable=True,
description='''\
Sets HTTP headers, for example X-Forwarded-For and X-Forwarded-Proto.
'''))
self.add_template(
ConfigTemplate(name='HTTP_BACKEND_PROXYPASS_GLUE',
value=('''\
http-request set-header Host {hostname}''' + r'''
reqirep "^([^ :]*)\ {proxypath}/?(.*)" "\1\ /\2"
'''),
overridable=True,
description='''\
Backend glue for `HAPROXY_{n}_HTTP_BACKEND_PROXYPASS_PATH`.
'''))
self.add_template(
ConfigTemplate(name='HTTP_BACKEND_REVPROXY_GLUE',
value='''\
acl hdr_location res.hdr(Location) -m found
rspirep "^Location: (https?://{hostname}(:[0-9]+)?)?(/.*)" "Location: \
{rootpath}" if hdr_location
''',
overridable=True,
description='''\
Backend glue for `HAPROXY_{n}_HTTP_BACKEND_REVPROXY_PATH`.
'''))
self.add_template(
ConfigTemplate(name='HTTP_BACKEND_REDIR',
value='''\
acl is_root path -i /
acl is_domain hdr(host) -i {hostname}
redirect code 301 location {redirpath} if is_domain is_root
''',
overridable=True,
description='''\
Set the path to redirect the root of the domain to
Ex: HAPROXY_0_HTTP_BACKEND_REDIR = '/my/content'
'''))
self.add_template(
ConfigTemplate(name='BACKEND_HTTP_HEALTHCHECK_OPTIONS',
value='''\
option httpchk GET {healthCheckPath}
timeout check {healthCheckTimeoutSeconds}s
''',
overridable=True,
description='''\
Sets HTTP health check options, for example timeout check and httpchk GET.
Parameters of the first health check for this service are exposed as:
* healthCheckPortIndex
* healthCheckPort
* healthCheckProtocol
* healthCheckPath
* healthCheckTimeoutSeconds
* healthCheckIntervalSeconds
* healthCheckGracePeriodSeconds
* healthCheckMaxConsecutiveFailures
* healthCheckFalls is set to healthCheckMaxConsecutiveFailures + 1
* healthCheckPortOptions is set to ` port {healthCheckPort}`
Defaults to empty string.
Example:
```
option httpchk GET {healthCheckPath}
timeout check {healthCheckTimeoutSeconds}s
```
'''))
self.add_template(
ConfigTemplate(name='BACKEND_TCP_HEALTHCHECK_OPTIONS',
value='',
overridable=True,
description='''\
Sets TCP health check options, for example timeout check.
Parameters of the first health check for this service are exposed as:
* healthCheckPortIndex
* healthCheckPort
* healthCheckProtocol
* healthCheckTimeoutSeconds
* healthCheckIntervalSeconds
* healthCheckGracePeriodSeconds
* healthCheckMaxConsecutiveFailures
* healthCheckFalls is set to healthCheckMaxConsecutiveFailures + 1
* healthCheckPortOptions is set to ` port {healthCheckPort}`
Defaults to empty string.
Example:
```
timeout check {healthCheckTimeoutSeconds}s
```
'''))
self.add_template(
ConfigTemplate(name='BACKEND_STICKY_OPTIONS',
value='''\
cookie mesosphere_server_id insert indirect nocache
''',
overridable=True,
description='''\
Sets a cookie for services where `HAPROXY_{n}_STICKY` is true.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_SERVER_OPTIONS',
value='''\
server {serverName} {host_ipv4}:{port} id {serverId}{cookieOptions}\
{healthCheckOptions}{otherOptions}
''',
overridable=True,
description='''\
The options for each server added to a backend.
'''))
self.add_template(
ConfigTemplate(name='BACKEND_SERVER_HTTP_HEALTHCHECK_OPTIONS',
value='''\
check inter {healthCheckIntervalSeconds}s fall {healthCheckFalls}\
{healthCheckPortOptions}
''',
overridable=True,
description='''\
Sets HTTP health check options for a single server, e.g. check inter.
Parameters of the first health check for this service are exposed as:
* healthCheckPortIndex
* healthCheckPort
* healthCheckProtocol
* healthCheckPath
* healthCheckTimeoutSeconds
* healthCheckIntervalSeconds
* healthCheckGracePeriodSeconds
* healthCheckMaxConsecutiveFailures
* healthCheckFalls is set to healthCheckMaxConsecutiveFailures + 1
* healthCheckPortOptions is set to ` port {healthCheckPort}`
Defaults to empty string.
Example:
```
check inter {healthCheckIntervalSeconds}s fall {healthCheckFalls}
```
'''))
self.add_template(
ConfigTemplate(name='BACKEND_SERVER_TCP_HEALTHCHECK_OPTIONS',
value='''\
check inter {healthCheckIntervalSeconds}s fall {healthCheckFalls}\
{healthCheckPortOptions}
''',
overridable=True,
description='''\
Sets TCP health check options for a single server, e.g. check inter.
Parameters of the first health check for this service are exposed as:
* healthCheckPortIndex
* healthCheckPort
* healthCheckProtocol
* healthCheckTimeoutSeconds
* healthCheckIntervalSeconds
* healthCheckGracePeriodSeconds
* healthCheckMaxConsecutiveFailures
* healthCheckFalls is set to healthCheckMaxConsecutiveFailures + 1
* healthCheckPortOptions is set to ` port {healthCheckPort}`
Defaults to empty string.
Example:
```
check inter {healthCheckIntervalSeconds}s fall {healthCheckFalls}
```
'''))
self.add_template(
ConfigTemplate(name='FRONTEND_BACKEND_GLUE',
value='''\
use_backend {backend}
''',
overridable=True,
description='''\
This option glues the backend to the frontend.
'''))
self.add_template(
ConfigTemplate(name='HTTP_BACKEND_NETWORK_ALLOWED_ACL',
value='''\
acl network_allowed src {network_allowed}
''',
overridable=True,
description='''\
This option set the IPs (or IP ranges) having access to the HTTP backend.
'''))
self.add_template(
ConfigTemplate(name='HTTP_BACKEND_ACL_ALLOW_DENY',
value='''\
http-request allow if network_allowed
http-request deny
''',
overridable=False,
description='''\
This option denies all IPs (or IP ranges) not explicitly allowed to access\
the HTTP backend.
Use with `HAPROXY_HTTP_BACKEND_NETWORK_ALLOWED_ACL`.
'''))
self.add_template(
ConfigTemplate(name='TCP_BACKEND_NETWORK_ALLOWED_ACL',
value='''\
acl network_allowed src {network_allowed}
''',
overridable=True,
description='''\
This option set the IPs (or IP ranges) having access to the TCP backend.
'''))
self.add_template(
ConfigTemplate(name='TCP_BACKEND_ACL_ALLOW_DENY',
value='''\
tcp-request content accept if network_allowed
tcp-request content reject
''',
overridable=False,
description='''\
This option denies all IPs (or IP ranges) not explicitly allowed to access\
the TCP backend.
Use with HAPROXY_TCP_BACKEND_ACL_ALLOW_DENY.
'''))
def __init__(self, directory='templates'):
self.__template_directory = directory
self.t = dict()
self.load()
self.__load_templates()
def __load_templates(self):
'''Look in environment variables for templates. If not set in env,
load template files if they exist. Othwerwise it sets defaults'''
for template in self.t:
name = self.t[template].full_name
if os.environ.get(name):
logger.info('overriding %s from environment variable', name)
env_template_val = os.environ.get(name)
# Handle escaped endlines
self.t[template].value = env_template_val.replace("\\n", "\n")
else:
try:
filename = os.path.join(self.__template_directory, name)
with open(filename) as f:
logger.info('overriding %s from %s', name, filename)
self.t[template].value = f.read()
except IOError:
logger.debug("setting default value for %s", name)
def get_descriptions(self):
descriptions = '''\
## Templates
The following is a list of the available HAProxy templates.
Some templates are global-only (such as `HAPROXY_HEAD`), but most may
be overridden on a per service port basis using the
`HAPROXY_{n}_...` syntax.
'''
desc_template = '''\
## `{full_name}`
*{overridable}*
Specified as {specifiedAs}.
{description}
**Default template for `{full_name}`:**
```
{default}```
'''
override_example = '''\
**Example Marathon label to override `{full_name}` for port 0 of an app:**
```
"HAPROXY_0_{name}": "{flat}"
```
'''
for tname in sorted(self.t.keys()):
t = self.t[tname]
spec = "`HAPROXY_" + t.name + "` template"
if t.overridable:
spec += " or with label `HAPROXY_{n}_" + t.name + "`"
descriptions += desc_template.format(
full_name=t.full_name,
specifiedAs=spec,
overridable="Overridable per app" if t.overridable
else "Global",
description=t.description,
default=t.default_value
)
if t.overridable:
descriptions += override_example.format(
full_name=t.full_name,
name=t.name,
# Escape out backslashes, quotes, and endlines
flat=(
t.default_value
.replace('\\', '\\\\')
.replace('"', ' \\\"')
.replace('\n', '\\n')
)
)
descriptions += '''\
## Other Labels
These labels may be used to configure other app settings.
'''
desc_template = '''\
## `{full_name}`
*{perServicePort}*
Specified as {specifiedAs}.
{description}
'''
for label in labels:
if label.name not in self.t:
if label.name == 'GROUP':
# this one is a special snowflake
spec = "`HAPROXY_{n}_" + label.name + "`" + " or " + \
"`HAPROXY_" + label.name + "`"
elif label.perServicePort:
spec = "`HAPROXY_{n}_" + label.name + "`"
else:
spec = "`HAPROXY_" + label.name + "`"
descriptions += desc_template.format(
full_name=label.full_name.replace('_{0}_', '_{n}_'),
specifiedAs=spec,
perServicePort="per service port" if label.perServicePort
else "per app",
description=label.description
)
return descriptions
@property
def haproxy_head(self):
return self.t['HEAD'].value
@property
def haproxy_http_frontend_head(self):
return self.t['HTTP_FRONTEND_HEAD'].value
@property
def haproxy_http_frontend_appid_head(self):
return self.t['HTTP_FRONTEND_APPID_HEAD'].value
@property
def haproxy_http_backend_acl_allow_deny(self):
return self.t['HTTP_BACKEND_ACL_ALLOW_DENY'].value
@property
def haproxy_tcp_backend_acl_allow_deny(self):
return self.t['TCP_BACKEND_ACL_ALLOW_DENY'].value
@property
def haproxy_https_frontend_head(self):
return self.t['HTTPS_FRONTEND_HEAD'].value
@property
def haproxy_https_grouped_frontend_head(self):
return self.t['HTTPS_GROUPED_FRONTEND_HEAD'].value
@property
def haproxy_https_grouped_vhost_frontend_head(self):
return self.t['HTTPS_GROUPED_VHOST_FRONTEND_HEAD'].value
@property
def haproxy_https_grouped_vhost_backend_head(self):
return self.t['HTTPS_GROUPED_VHOST_BACKEND_HEAD'].value
@property
def haproxy_https_grouped_vhost_frontend_acl(self):
return self.t['HTTPS_GROUPED_VHOST_FRONTEND_ACL'].value
def haproxy_userlist_head(self, app):
if 'HAPROXY_{0}_USERLIST_HEAD' in app.labels:
return app.labels['HAPROXY_{0}_USERLIST_HEAD']
return self.t['USERLIST_HEAD'].value
def haproxy_http_frontend_acl_with_auth(self, app):
if 'HAPROXY_{0}_HTTP_FRONTEND_ACL_WITH_AUTH' in app.labels:
return app.labels['HAPROXY_{0}_HTTP_FRONTEND_ACL_WITH_AUTH']
return self.t['HTTP_FRONTEND_ACL_WITH_AUTH'].value
def haproxy_https_frontend_acl_with_auth(self, app):
if 'HAPROXY_{0}_HTTPS_FRONTEND_ACL_WITH_AUTH' in app.labels:
return app.labels['HAPROXY_{0}_HTTPS_FRONTEND_ACL_WITH_AUTH']
return self.t['HTTPS_FRONTEND_ACL_WITH_AUTH'].value
def haproxy_http_frontend_acl_with_auth_and_path(self, app):
if 'HAPROXY_{0}_HTTP_FRONTEND_ACL_WITH_AUTH_AND_PATH' in app.labels:
return \
app.labels['HAPROXY_{0}_HTTP_FRONTEND_ACL_WITH_AUTH_AND_PATH']
return self.t['HTTP_FRONTEND_ACL_WITH_AUTH_AND_PATH'].value
def haproxy_https_frontend_acl_with_auth_and_path(self, app):
if 'HAPROXY_{0}_HTTPS_FRONTEND_ACL_WITH_AUTH_AND_PATH' in app.labels:
return \
app.labels['HAPROXY_{0}_HTTPS_FRONTEND_ACL_WITH_AUTH_AND_PATH']
return self.t['HTTPS_FRONTEND_ACL_WITH_AUTH_AND_PATH'].value