-
Notifications
You must be signed in to change notification settings - Fork 78
/
nginx.py
executable file
·623 lines (496 loc) · 17.9 KB
/
nginx.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
"""
Python library for editing NGINX serverblocks.
python-nginx
(c) 2016 Jacob Cook
Licensed under GPLv3, see LICENSE.md
"""
import re
import logging
INDENT = ' '
DEBUG=False
logging.basicConfig(level=logging.DEBUG if DEBUG else logging.INFO)
class Error(Exception):
pass
class ParseError(Error):
pass
def bump_child_depth(obj, depth):
children = getattr(obj, 'children', [])
for child in children:
child._depth = depth + 1
bump_child_depth(child, child._depth)
class Conf(object):
"""
Represents an nginx configuration.
A `Conf` can consist of any number of server blocks, as well as Upstream
and other types of containers. It can also include top-level comments.
"""
def __init__(self, *args):
"""
Initialize object.
:param *args: Any objects to include in this Conf.
"""
self.children = list(args)
def add(self, *args):
"""
Add object(s) to the Conf.
:param *args: Any objects to add to the Conf.
:returns: full list of Conf's child objects
"""
self.children.extend(args)
return self.children
def remove(self, *args):
"""
Remove object(s) from the Conf.
:param *args: Any objects to remove from the Conf.
:returns: full list of Conf's child objects
"""
for x in args:
self.children.remove(x)
return self.children
def filter(self, btype='', name=''):
"""
Return child object(s) of this Conf that satisfy certain criteria.
:param str btype: Type of object to filter by (e.g. 'Key')
:param str name: Name of key OR container value to filter by
:returns: full list of matching child objects
"""
filtered = []
for x in self.children:
if name and isinstance(x, Key) and x.name == name:
filtered.append(x)
elif isinstance(x, Container) and x.__class__.__name__ == btype \
and x.value == name:
filtered.append(x)
elif not name and btype and x.__class__.__name__ == btype:
filtered.append(x)
return filtered
@property
def servers(self):
"""Return a list of child Server objects."""
return [x for x in self.children if isinstance(x, Server)]
@property
def server(self):
"""Convenience property to fetch the first available server only."""
return self.servers[0]
@property
def as_list(self):
"""Return all child objects in nested lists of strings."""
return [x.as_list for x in self.children]
@property
def as_dict(self):
"""Return all child objects in nested dict."""
return {'conf': [x.as_dict for x in self.children]}
@property
def as_strings(self):
"""Return the entire Conf as nginx config strings."""
ret = []
for x in self.children:
if isinstance(x, (Key, Comment)):
ret.append(x.as_strings)
else:
for y in x.as_strings:
ret.append(y)
if ret:
ret[-1] = re.sub('}\n+$', '}\n', ret[-1])
return ret
class Container(object):
"""
Represents a type of child block found in an nginx config.
Intended to be subclassed by various types of child blocks, like
Locations or Geo blocks.
"""
def __init__(self, value, *args):
"""
Initialize object.
:param str value: Value to be used in name (e.g. regex for Location)
:param *args: Any objects to include in this Conf.
"""
self.name = ''
self.value = value
self._depth = 0
self.children = list(args)
bump_child_depth(self, self._depth)
def add(self, *args):
"""
Add object(s) to the Container.
:param *args: Any objects to add to the Container.
:returns: full list of Container's child objects
"""
self.children.extend(args)
bump_child_depth(self, self._depth)
return self.children
def remove(self, *args):
"""
Remove object(s) from the Container.
:param *args: Any objects to remove from the Container.
:returns: full list of Container's child objects
"""
for x in args:
self.children.remove(x)
return self.children
def filter(self, btype='', name=''):
"""
Return child object(s) of this Server block that meet certain criteria.
:param str btype: Type of object to filter by (e.g. 'Key')
:param str name: Name of key OR container value to filter by
:returns: full list of matching child objects
"""
filtered = []
for x in self.children:
if name and isinstance(x, Key) and x.name == name:
filtered.append(x)
elif isinstance(x, Container) and x.__class__.__name__ == btype \
and x.value == name:
filtered.append(x)
elif not name and btype and x.__class__.__name__ == btype:
filtered.append(x)
return filtered
@property
def locations(self):
"""Return a list of child Location objects."""
return [x for x in self.children if isinstance(x, Location)]
@property
def comments(self):
"""Return a list of child Comment objects."""
return [x for x in self.children if isinstance(x, Comment)]
@property
def keys(self):
"""Return a list of child Key objects."""
return [x for x in self.children if isinstance(x, Key)]
@property
def as_list(self):
"""Return all child objects in nested lists of strings."""
return [self.name, self.value, [x.as_list for x in self.children]]
@property
def as_dict(self):
"""Return all child objects in nested dict."""
dicts = [x.as_dict for x in self.children]
return {'{0} {1}'.format(self.name, self.value): dicts}
@property
def as_strings(self):
"""Return the entire Container as nginx config strings."""
ret = []
container_title = (INDENT * self._depth)
container_title += '{0}{1} {{\n'.format(
self.name, (' {0}'.format(self.value) if self.value else '')
)
ret.append(container_title)
for x in self.children:
if isinstance(x, Key):
ret.append(INDENT + x.as_strings)
elif isinstance(x, Comment):
if x.inline and len(ret) >= 1:
ret[-1] = ret[-1].rstrip('\n') + ' ' + x.as_strings
else:
ret.append(INDENT + x.as_strings)
elif isinstance(x, Container):
y = x.as_strings
ret.append('\n' + y[0])
for z in y[1:]:
ret.append(INDENT + z)
else:
y = x.as_strings
ret.append(INDENT + y)
ret[-1] = re.sub('}\n+$', '}\n', ret[-1])
ret.append('}\n\n')
return ret
class Comment(object):
"""Represents a comment in an nginx config."""
def __init__(self, comment, inline=False):
"""
Initialize object.
:param str comment: Value of the comment
:param bool inline: This comment is on the same line as preceding item
"""
self.comment = comment
self.inline = inline
@property
def as_list(self):
"""Return comment as nested list of strings."""
return [self.comment]
@property
def as_dict(self):
"""Return comment as dict."""
return {'#': self.comment}
@property
def as_strings(self):
"""Return comment as nginx config string."""
return '# {0}\n'.format(self.comment)
class Http(Container):
"""Container for HTTP sections in the main NGINX conf file."""
def __init__(self, *args):
"""Initialize."""
super(Http, self).__init__('', *args)
self.name = 'http'
class Server(Container):
"""Container for server block configurations."""
def __init__(self, *args):
"""Initialize."""
super(Server, self).__init__('', *args)
self.name = 'server'
@property
def as_dict(self):
"""Return all child objects in nested dict."""
return {'server': [x.as_dict for x in self.children]}
class Location(Container):
"""Container for Location-based options."""
def __init__(self, value, *args):
"""Initialize."""
super(Location, self).__init__(value, *args)
self.name = 'location'
class Events(Container):
"""Container for Event-based options."""
def __init__(self, *args):
"""Initialize."""
super(Events, self).__init__('', *args)
self.name = 'events'
class LimitExcept(Container):
"""Container for specifying HTTP method restrictions."""
def __init__(self, value, *args):
"""Initialize."""
super(LimitExcept, self).__init__(value, *args)
self.name = 'limit_except'
class Types(Container):
"""Container for MIME type mapping."""
def __init__(self, *args):
"""Initialize."""
super(Types, self).__init__('', *args)
self.name = 'types'
class If(Container):
"""Container for If conditionals."""
def __init__(self, value, *args):
"""Initialize."""
super(If, self).__init__(value, *args)
self.name = 'if'
class Upstream(Container):
"""Container for upstream configuration (reverse proxy)."""
def __init__(self, value, *args):
"""Initialize."""
super(Upstream, self).__init__(value, *args)
self.name = 'upstream'
class Geo(Container):
"""
Container for geo module configuration.
See docs here: http://nginx.org/en/docs/http/ngx_http_geo_module.html
"""
def __init__(self, value, *args):
"""Initialize."""
super(Geo, self).__init__(value, *args)
self.name = 'geo'
class Map(Container):
"""Container for map configuration."""
def __init__(self, value, *args):
"""Initialize."""
super(Map, self).__init__(value, *args)
self.name = 'map'
class Stream(Container):
"""Container for stream sections in the main NGINX conf file."""
def __init__(self, *args):
"""Initialize."""
super(Stream, self).__init__('', *args)
self.name = 'stream'
class Key(object):
"""Represents a simple key/value object found in an nginx config."""
def __init__(self, name, value):
"""
Initialize object.
:param *args: Any objects to include in this Server block.
"""
self.name = name
self.value = value
@property
def as_list(self):
"""Return key as nested list of strings."""
return [self.name, self.value]
@property
def as_dict(self):
"""Return key as dict key/value."""
return {self.name: self.value}
@property
def as_strings(self):
"""Return key as nginx config string."""
if self.value == '' or self.value is None:
return '{0};\n'.format(self.name)
if type(self.value) == str and '"' not in self.value and (';' in self.value or '#' in self.value):
return '{0} "{1}";\n'.format(self.name, self.value)
return '{0} {1};\n'.format(self.name, self.value)
def loads(data, conf=True):
"""
Load an nginx configuration from a provided string.
:param str data: nginx configuration
:param bool conf: Load object(s) into a Conf object?
"""
f = Conf() if conf else []
lopen = []
index = 0
while True:
m = re.compile(r'^\s*events\s*{').search(data[index:])
if m:
logging.debug("Open (Events)")
e = Events()
lopen.insert(0, e)
index += m.end()
continue
m = re.compile(r'^\s*http\s*{').search(data[index:])
if m:
logging.debug("Open (Http)")
h = Http()
lopen.insert(0, h)
index += m.end()
continue
m = re.compile(r'^\s*stream\s*{').search(data[index:])
if m:
logging.debug("Open (Stream)")
s = Stream()
lopen.insert(0, s)
index += m.end()
continue
m = re.compile(r'^\s*server\s*{').search(data[index:])
if m:
logging.debug("Open (Server)")
s = Server()
lopen.insert(0, s)
index += m.end()
continue
n = re.compile(r'(?!\B"[^"]*);(?![^"]*"\B)')
m = re.compile(r'^\s*location\s+(.*?".*?".*?|.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (Location) {0}".format(m.group(1)))
l = Location(m.group(1))
lopen.insert(0, l)
index += m.end()
continue
m = re.compile(r'^\s*if\s+(.*?".*?".*?|.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (If) {0}".format(m.group(1)))
ifs = If(m.group(1))
lopen.insert(0, ifs)
index += m.end()
continue
m = re.compile(r'^\s*upstream\s+(.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (Upstream) {0}".format(m.group(1)))
u = Upstream(m.group(1))
lopen.insert(0, u)
index += m.end()
continue
m = re.compile(r'^\s*geo\s+(.*?".*?".*?|.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (Geo) {0}".format(m.group(1)))
g = Geo(m.group(1))
lopen.insert(0, g)
index += m.end()
continue
m = re.compile(r'^\s*map\s+(.*?".*?".*?|.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (Map) {0}".format(m.group(1)))
g = Map(m.group(1))
lopen.insert(0, g)
index += m.end()
continue
m = re.compile(r'^\s*limit_except\s+(.*?".*?".*?|.*?)\s*{').search(data[index:])
if m and not n.search(m.group()):
logging.debug("Open (LimitExcept) {0}".format(m.group(1)))
l = LimitExcept(m.group(1))
lopen.insert(0, l)
index += m.end()
continue
m = re.compile(r'^\s*types\s*{').search(data[index:])
if m:
logging.debug("Open (Types)")
l = Types()
lopen.insert(0, l)
index += m.end()
continue
m = re.compile(r'^(\s*)#[ \r\t\f]*(.*?)\n').search(data[index:])
if m:
logging.debug("Comment ({0})".format(m.group(2)))
c = Comment(m.group(2), inline='\n' not in m.group(1))
if lopen and isinstance(lopen[0], Container):
lopen[0].add(c)
else:
f.add(c) if conf else f.append(c)
index += m.end() - 1
continue
m = re.compile(r'^\s*}').search(data[index:])
if m:
if isinstance(lopen[0], Container):
logging.debug("Close ({0})".format(lopen[0].__class__.__name__))
c = lopen[0]
lopen.pop(0)
if lopen and isinstance(lopen[0], Container):
lopen[0].add(c)
else:
f.add(c) if conf else f.append(c)
index += m.end()
continue
if ";" not in data[index:] and "}" in data[index:]:
# If there is still something to parse, expect ';' otherwise
# the Key regexp can get stuck due to regexp catastrophic backtracking
raise ParseError("Config syntax, missing ';' at index: {}".format(index))
double = r'\s*"[^"]*"'
single = r'\s*\'[^\']*\''
normal = r'\s*[^;\s]*'
s1 = r'{}|{}|{}'.format(double, single, normal)
s = r'^\s*({})\s*((?:{})+);'.format(s1, s1)
m = re.compile(s).search(data[index:])
if m:
logging.debug("Key {0} {1}".format(m.group(1), m.group(2)))
k = Key(m.group(1), m.group(2))
if lopen and isinstance(lopen[0], (Container, Server)):
lopen[0].add(k)
else:
f.add(k) if conf else f.append(k)
index += m.end()
continue
m = re.compile(r'^\s*(\S+);').search(data[index:])
if m:
logging.debug("Key {0}".format(m.group(1)))
k = Key(m.group(1), '')
if lopen and isinstance(lopen[0], (Container, Server)):
lopen[0].add(k)
else:
f.add(k) if conf else f.append(k)
index += m.end()
continue
break
return f
def load(fobj):
"""
Load an nginx configuration from a provided file-like object.
:param obj fobj: nginx configuration
"""
return loads(fobj.read())
def loadf(path):
"""
Load an nginx configuration from a provided file path.
:param file path: path to nginx configuration on disk
"""
with open(path, 'r') as f:
return load(f)
def dumps(obj):
"""
Dump an nginx configuration to a string.
:param obj obj: nginx object (Conf, Server, Container)
:returns: nginx configuration as string
"""
return ''.join(obj.as_strings)
def dump(obj, fobj):
"""
Write an nginx configuration to a file-like object.
:param obj obj: nginx object (Conf, Server, Container)
:param obj fobj: file-like object to write to
:returns: file-like object that was written to
"""
fobj.write(dumps(obj))
return fobj
def dumpf(obj, path):
"""
Write an nginx configuration to file.
:param obj obj: nginx object (Conf, Server, Container)
:param str path: path to nginx configuration on disk
:returns: path the configuration was written to
"""
with open(path, 'w') as f:
dump(obj, f)
return path