-
Notifications
You must be signed in to change notification settings - Fork 390
/
haproxy_validator.py
209 lines (193 loc) · 6.94 KB
/
haproxy_validator.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
import logging
import inspect
import ConfigParser
import keystone_auth
import os
import sys
import haproxy_cert
class CustomAttr(object):
"""This type handles non-flat data-types like
int, str, bool.
"""
def __init__(self, key, value):
self._value = value
self._key = key
def validate(self, conf_list):
return
def post_validation(self, conf_list):
return
class CustomAttrTlsContainer(CustomAttr):
def __init__(self, key, value, custom_attr_conf_file=None):
super(CustomAttrTlsContainer, self).__init__(key, value)
if not custom_attr_conf_file:
logging.error("Missing custom attr conf file name in vrouter-agent.conf")
raise Exception()
if not self._read_config(custom_attr_conf_file):
logging.error("Error reading %s" % custom_attr_conf_file)
raise Exception()
cert_dict = self._parse_args(self._config, 'CERT')
if not cert_dict or not 'cert_manager' in cert_dict:
logging.error("Missing CERT section")
raise Exception()
if cert_dict['cert_manager'] == 'Barbican_Cert_Manager':
# Make sure keystone credentials are present
auth_dict = self._parse_args(self._config, 'KEYSTONE')
if not auth_dict:
logging.error("Missing KEYSTONE section")
raise Exception()
identity = keystone_auth.Identity(auth_dict)
else:
identity = None
self.cert_manager = getattr(haproxy_cert,
cert_dict['cert_manager'])(identity=identity)
def _read_config(self, conf_file):
config = ConfigParser.ConfigParser()
if not len(config.read(conf_file)):
return False
else:
self._config = config
return True
def _parse_args(self, config, section):
try:
return dict(config.items(section))
except Exception as e:
logging.error(str(e))
return None
def validate(self):
if self._key != 'tls_container':
return False
if (self.cert_manager and \
self.cert_manager._validate_tls_secret(self._value)):
tls_pem_string = self.cert_manager._populate_tls_pem(self._value)
self._value = tls_pem_string
return True
else:
logging.error("TLS container invalid")
return False
def post_validation(self):
return self._value
custom_attributes_dict = {
'global': {
'max_conn': {
'type': int,
'limits': [1, 65535],
'cmd': 'maxconn %d'
},
'max_conn_rate': {
'type': int,
'limits': [1, 65535],
'cmd': 'maxconnrate %d'
},
'max_sess_rate': {
'type': int,
'limits': [1, 65535],
'cmd': 'maxsessrate %d'
},
'max_ssl_conn': {
'type': int,
'limits': [1, 65535],
'cmd': 'maxsslconn %d'
},
'max_ssl_rate': {
'type': int,
'limits': [1, 65535],
'cmd': 'maxsslrate %d'
},
'ssl_ciphers': {
'type': str,
'limits': [1, 100],
'cmd': 'ssl-default-bind-ciphers %s'
},
'tune_http_max_header': {
'type': int,
'limits': [1, 128],
'cmd': 'tune.http.maxhdr %d'
},
'tune_ssl_max_record': {
'type': int,
'limits': [1, 16384],
'cmd': 'tune.ssl.maxrecord %d'
}
},
'default': {
'server_timeout': {
'type': int,
'limits': [1, 5000000],
'cmd': 'timeout server %d'
},
'client_timeout': {
'type': int,
'limits': [1, 5000000],
'cmd': 'timeout client %d'
},
'connect_timeout': {
'type': int,
'limits': [1, 5000000],
'cmd': 'timeout connect %d'
}
},
'vip': {
'http_server_close': {
'type': bool,
'limits': ['True', 'False'],
'cmd': '%soption http-server-close'
},
'rate_limit_sessions': {
'type': int,
'limits': [1, 65535],
'cmd': 'rate-limit sessions %d'
},
'tls_container': {
'type': CustomAttrTlsContainer,
'limits': None,
'cmd': None
}
},
'pool': {},
}
def validate_custom_attributes(config, section, custom_attr_conf_file=None):
section_dict = {}
if 'custom-attributes' in config and section in custom_attributes_dict:
custom_attributes = config['custom-attributes']
for key, value in custom_attributes.iteritems():
if key in custom_attributes_dict[section]:
#Sanitize the value
try:
type_attr = custom_attributes_dict[section][key]['type']
limits = custom_attributes_dict[section][key]['limits']
if type_attr == int:
value = type_attr(value)
if value in range(limits[0], limits[1]):
section_dict.update({key:value})
else:
logging.info("Skipping key: %s, value: %s due to" \
"validation failure" % (key, value))
elif type_attr == str:
if len(value) in range(limits[0], limits[1]):
section_dict.update({key:value})
else:
logging.info("Skipping key: %s, value: %s due to" \
"validation failure" % (key, value))
elif type_attr == bool:
if value in limits:
if value == 'True':
value = ''
elif value == 'False':
value = 'no '
section_dict.update({key:value})
else:
logging.info("Skipping key: %s, value: %s due to" \
"validation failure" % (key, value))
elif inspect.isclass(type_attr):
new_custom_attr = type_attr(key, value,
custom_attr_conf_file)
if new_custom_attr.validate():
value = new_custom_attr.post_validation()
section_dict.update({key:value})
else:
logging.info("Skipping key: %s, value: %s due to" \
"validation failure" % (key, value))
except Exception as e:
logging.error(str(e))
continue
return section_dict