-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·622 lines (559 loc) · 20.4 KB
/
app.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
from paramiko import SSHClient
from typing import Dict
from flask import Flask, jsonify, request
from marshmallow import fields, Schema, ValidationError, post_load, validates, validate
from sqlalchemy.orm.exc import NoResultFound
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flask_sqlalchemy import SQLAlchemy
import json
from ipaddress import IPv4Address, AddressValueError
from create_db import Interface, Address
from connection import Connection, Iproute2Error
from config import database_path
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = database_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
spec = APISpec(
title="Network Interfaces Management Service",
version="1.0",
openapi_version="3.0.3",
info=dict(description="This API allows to create, change and delete network interfaces on internal VM via ssh."),
plugins=[FlaskPlugin(), MarshmallowPlugin()],
)
def startup(conn: Connection) -> None:
for interface in db.session.query(Interface).all():
try:
conn.ip_link_add(interface)
except Iproute2Error as e:
if e.args[0]['message'] == ['RTNETLINK answers: File exists\n']:
connection.ip_link_set(interface, mtu=interface.mtu)
else:
raise e
conn.set_addresses(interface)
class ConciseAddressSchema(Schema):
id = fields.Integer(dump_only=True)
address = fields.Str(required=True)
@validates('address')
def validate_ip(self, address: str):
try:
IPv4Address(address)
except AddressValueError:
raise ValidationError(f'{address} is not a valid IP address.')
@post_load
def init_address(self, data: Dict, **kwargs):
return Address(**data)
class AddressSchema(ConciseAddressSchema):
interface_id = fields.Integer(required=True)
class InterfaceSchema(Schema):
id = fields.Integer(dump_only=True)
name = fields.Str(required=True, validate=validate.Regexp('^[0-9A-Za-z_]+$'))
mtu = fields.Integer(missing=1500, validate=validate.Range(0, 999999999))
addresses = fields.List(fields.Nested(ConciseAddressSchema))
@post_load
def init_interface(self, data: Dict, **kwargs):
return Interface(**{k: v for (k, v) in data.items() if k != 'addresses'})
@app.route('/interfaces', methods=['GET'])
def get_interfaces():
"""Return all interfaces.
---
get:
summary: Return all interfaces
description: Return all interfaces created through API.
operationId: get_interfaces
responses:
200:
description: list of all interfaces created through API
content:
application/json:
schema:
type: array
items: Interface
"""
return interface_schema.dumps(db.session.query(Interface).all(), many=True, indent=2)
@app.route('/interfaces', methods=['POST'])
def post_interface():
"""Add interface.
---
post:
summary: Add new interface
description: Create interface with given name and, possibly, set its mtu. Also can bind one or many \
specified IP addresses to newly created interface. Name MUST NOT match name of any existing interface, including \
system interfaces.
operationId: post_interface
requestBody:
content:
application/json:
schema: Interface
responses:
201:
description: interface was successfully created
content:
application/json:
schema: Interface
400:
description: bad request
content:
application/json:
schema: Error
409:
description: interface cannot be created, because name is already in use
content:
application/json:
schema: Error
"""
# initialize and create interface
interface = interface_schema.load(request.json)
connection.ip_link_add(interface)
db.session.add(interface)
db.session.flush()
try:
# initialize and create addresses
addresses = [Address(**data, interface_id=interface.id) for data in request.json.get('addresses', [])]
for address in addresses:
connection.ip_address_add(address, interface)
db.session.add_all(addresses)
except Exception as e:
connection.ip_link_delete(interface)
raise e
db.session.commit()
return interface_schema.dumps(interface, indent=2), 201
@app.route('/interfaces/<int:int_id>', methods=['GET'])
def get_interface(int_id: int):
"""Return selected interface.
---
get:
summary: Return selected interface
description: Return interface with given id.
operationId: get_interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
responses:
200:
description: interface with given id
content:
application/json:
schema: Interface
404:
description: interface not found
content:
application/json:
schema: Error
"""
return interface_schema.dumps(db.session.query(Interface).filter(Interface.id == int_id).one(), indent=2)
@app.route('/interfaces/<int:int_id>', methods=['PUT'])
def put_interface(int_id: int): # TODO: requestBody schema (partial=True)
"""Change interface.
---
put:
summary: Change interface
description: Change name and/or mtu of interface previously created through API. If a list of addresses \
is passed, deletes addresses outside of this list and creates missing addresses. Name MUST NOT match name of any \
existing interface, including system interfaces.
operationId: put_interface
requestBody:
content:
application/json:
schema: Interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
responses:
200:
description: interface was successfully changed
content:
application/json:
schema: Interface
400:
description: bad request
content:
application/json:
schema: Error
404:
description: interface not found
content:
application/json:
schema: Error
409:
description: interface cannot be changed, because name is already in use
content:
application/json:
schema: Error
"""
interface = db.session.query(Interface).filter_by(id=int_id).one()
# change interface
interface_schema.load(request.json, partial=True)
connection.ip_link_set(interface, name=request.json.get('name'), mtu=request.json.get('mtu'))
interface.name = request.json.get('name', interface.name)
interface.mtu = request.json.get('mtu', interface.mtu)
# delete/create addresses
if 'addresses' in request.json:
interface.addresses = [Address(**data) for data in request.json['addresses']]
db.session.add_all(interface.addresses)
connection.set_addresses(interface)
db.session.commit()
return interface_schema.dumps(interface, indent=2)
@app.route('/interfaces/<int:int_id>', methods=['DELETE'])
def delete_interface(int_id: int):
"""Delete interface.
---
delete:
summary: Delete interface
description: Delete interface previously created through API.
operationId: delete_interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
responses:
204:
description: interface deleted
404:
description: interface not found
content:
application/json:
schema: Error
"""
interface = db.session.query(Interface).filter_by(id=int_id).one()
connection.ip_link_delete(interface)
db.session.delete(interface)
db.session.commit()
return '', 204
@app.route('/addresses', methods=['GET'])
def get_addresses():
"""Return all IP addresses.
---
get:
summary: Return all IP addresses
description: Return all IP addresses assigned to some interfaces through API.
operationId: get_addresses
responses:
200:
description: list of all addresses assigned through API
content:
application/json:
schema:
type: array
items: Address
"""
return address_schema.dumps(db.session.query(Address).all(), many=True, indent=2)
@app.route('/addresses', methods=['POST'])
def post_addresses():
"""Assign address.
---
post:
summary: Assign address
description: Assign address to a previously created interface.
operationId: post_addresses
requestBody:
content:
application/json:
schema: Address
responses:
201:
description: address successfully assigned
content:
application/json:
schema: Address
400:
description: bad request
content:
application/json:
schema: Error
404:
description: interface not found
content:
application/json:
schema: Error
409:
description: this address is already assigned to this interface
content:
application/json:
schema: Error
"""
address = address_schema.load(request.json)
connection.ip_address_add(address, db.session.query(Interface).filter_by(id=address.interface_id).one())
db.session.add(address)
db.session.commit()
return address_schema.dumps(address, indent=2), 201
@app.route('/addresses/<int:addr_id>', methods=['GET'])
def get_address(addr_id: int):
"""Return selected address.
---
get:
summary: Return selected address
description: Return address with given id.
operationId: get_address
parameters:
- in: path
name: addr_id
description: address id
schema:
type: integer
responses:
200:
description: address with given id
content:
application/json:
schema: Address
404:
description: address not found
content:
application/json:
schema: Error
"""
return address_schema.dumps(db.session.query(Address).filter_by(id=addr_id).one(), indent=2)
@app.route('/addresses/<int:addr_id>', methods=['DELETE'])
def delete_address(addr_id: int):
"""Delete address.
---
delete:
summary: Delete address
description: Delete address previously assigned through API.
operationId: delete_address
parameters:
- in: path
name: addr_id
description: address id
schema:
type: integer
responses:
204:
description: address deleted
404:
description: address not found
content:
application/json:
schema: Error
"""
address = db.session.query(Address).filter_by(id=addr_id).one()
connection.ip_address_delete(address, db.session.query(Interface).filter_by(id=address.interface_id).one())
db.session.delete(address)
db.session.commit()
return '', 204
@app.route('/interfaces/<int:int_id>/addresses', methods=['GET'])
def get_addresses_by_interface(int_id: int):
"""Return addresses by interface.
---
get:
summary: Return addresses by interface
description: Return all IP addresses assigned to given interface through API.
operationId: get_addresses_by_interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
responses:
200:
description: all IP addresses assigned to given interface through API
content:
application/json:
schema:
type: array
items: ConciseAddress
404:
description: interface not found
content:
application/json:
schema: Error
"""
db.session.query(Interface).filter_by(id=int_id).one()
return concise_address_schema.dumps(db.session.query(Address).filter_by(interface_id=int_id).all(),
many=True, indent=2)
@app.route('/interfaces/<int:int_id>/addresses', methods=['POST'])
def post_addresses_by_interface(int_id: int):
"""Assign address.
---
post:
summary: Assign address
description: Assign address to given interface.
operationId: post_addresses_by_interface
requestBody:
content:
application/json:
schema: Address
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
responses:
201:
description: address successfully assigned
content:
application/json:
schema: ConciseAddress
400:
description: bad request
content:
application/json:
schema: Error
404:
description: interface not found
content:
application/json:
schema: Error
409:
description: this address is already assigned to this interface
content:
application/json:
schema: Error
"""
address = concise_address_schema.load(request.json)
address.interface_id = int_id
connection.ip_address_add(address, db.session.query(Interface).filter_by(id=int_id).one())
db.session.add(address)
db.session.commit()
return concise_address_schema.dumps(address, indent=2), 201
@app.route('/interfaces/<int:int_id>/addresses/<int:addr_id>', methods=['GET'])
def get_address_by_interface(int_id: int, addr_id: int):
"""Return selected address.
---
get:
summary: Return selected address
description: Return address with given id assigned to interface with given id.
operationId: get_address_by_interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
- in: path
name: addr_id
description: address id
schema:
type: integer
responses:
200:
description: address with given id
content:
application/json:
schema: ConciseAddress
404:
description: address not found or interface_id of address does not match given int_id
content:
application/json:
schema: Error
"""
return concise_address_schema.dumps(db.session.query(Address).
filter_by(interface_id=int_id).
filter_by(id=addr_id).one(), indent=2)
@app.route('/interfaces/<int:int_id>/addresses/<int:addr_id>', methods=['DELETE'])
def delete_address_by_interface(int_id: int, addr_id: int):
"""Delete address.
---
delete:
summary: Delete address
description: Delete address previously assigned through API to given interface.
operationId: delete_address_by_interface
parameters:
- in: path
name: int_id
description: interface id
schema:
type: integer
- in: path
name: addr_id
description: address id
schema:
type: integer
responses:
204:
description: address deleted
404:
description: address not found or interface_id of address does not match given int_id
content:
application/json:
schema: Error
"""
address = db.session.query(Address).filter_by(interface_id=int_id).filter_by(id=addr_id).one()
connection.ip_address_delete(address, db.session.query(Interface).filter_by(id=int_id).one())
db.session.delete(address)
db.session.commit()
return '', 204
@app.errorhandler(ValidationError)
def validation_error(error):
return jsonify({'error': error.messages}), 400
@app.errorhandler(NoResultFound)
def not_found(_):
return jsonify({'error': 'No result found.'}), 404
@app.errorhandler(Iproute2Error)
def iproute2_error(error):
args_ = error.args[0]
if args_['message'] == ['RTNETLINK answers: File exists\n']:
if args_['command'].startswith('sudo ip link add'):
return jsonify({'error': f'Interface name {args_["interface"].name} is already in use.'}), 409
elif args_['command'].startswith('sudo ip link set'):
return jsonify({'error': f'Interface name {args_["name"]} is already in use.'}), 409
elif args_['command'].startswith('sudo ip address add'):
return jsonify({'error': f'Address {args_["address"].address} is already assigned to interface \
{args_["interface"].name}. No need to assign again.'}), 409
return jsonify({'error': str(args_)}), 500
@app.errorhandler(400)
def bad_request(_):
return jsonify({'error': 'Bad request.'}), 400
@app.errorhandler(404)
def not_found(_):
return jsonify({'error': 'Not found.'}), 404
@app.errorhandler(405)
def method_not_allowed(_):
return jsonify({'error': 'Method not allowed.'}), 405
def generate_spec():
spec.components.schema("Interface", schema=InterfaceSchema)
spec.components.schema("Address", schema=AddressSchema)
spec.components.schema("Error", {"properties": {"error": {"type": "string"}}}) # TODO ValidationError?
with app.test_request_context():
spec.path(view=get_interfaces)
spec.path(view=get_interface)
spec.path(view=post_interface)
spec.path(view=put_interface)
spec.path(view=delete_interface)
spec.path(view=get_address)
spec.path(view=get_addresses)
spec.path(view=post_addresses)
spec.path(view=delete_address)
spec.path(view=get_address_by_interface)
spec.path(view=get_addresses_by_interface)
spec.path(view=post_addresses_by_interface)
spec.path(view=delete_address_by_interface)
with open('openapi.json', 'w') as f:
f.write(json.dumps(spec.to_dict(), indent=2))
with SSHClient() as ssh:
interface_schema = InterfaceSchema()
address_schema = AddressSchema()
concise_address_schema = ConciseAddressSchema()
connection = Connection(ssh)
startup(connection)
generate_spec()
if __name__ == '__main__':
app.run(debug=True)
# TODO: IPv6 addresses
# methods:
# GET /interfaces
# POST /interfaces
# GET /interfaces/1
# PUT /interfaces/1
# DELETE /interfaces/1
# GET /addresses
# POST /addresses
# GET /addresses/1
# DELETE /addresses/1
# GET /interfaces/1/addresses
# POST /interfaces/1/addresses
# GET /interfaces/1/addresses/1
# DELETE /interfaces/1/addresses/1