This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
firewall_injector.py
executable file
·161 lines (137 loc) · 5.72 KB
/
firewall_injector.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
#!/usr/bin/python3
# coding: utf-8
import argparse
import socket
import requests
import json
import os.path
from os import path
POLYCUBED_ADDR = 'localhost'
POLYCUBED_PORT = 9000
REQUESTS_TIMEOUT = 20 # seconds
MODE = 'TC'
polycubed_endpoint = 'http:https://{}:{}/polycube/v1'
def main():
global polycubed_endpoint
args = parseArguments()
addr = args['address']
port = args['port']
cube_name = args['cube_name']
interface_name = args['peer_interface']
debug = args['debug']
mode = args['mode']
polycubed_endpoint = polycubed_endpoint.format(addr, port)
already_exists, cube = checkIfServiceExists(cube_name)
if already_exists:
print(f'Firewall {cube_name} already exist')
attached_interface = cube['parent']
if attached_interface:
if attached_interface != interface_name:
detach_from_interface(cube_name, attached_interface)
attach_to_interface(cube_name, interface_name)
else:
attach_to_interface(cube_name, interface_name)
else:
createInstance(cube_name, debug, mode)
attach_to_interface(cube_name, interface_name)
def checkIfServiceExists(cube_name):
try:
response = requests.get(
f'{polycubed_endpoint}/firewall/{cube_name}', timeout=REQUESTS_TIMEOUT)
response.raise_for_status()
return True, json.loads(response.content)
except requests.exceptions.HTTPError:
return False, None
except requests.exceptions.ConnectionError:
print('Connection error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.Timeout:
print('Timeout error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.RequestException:
print('Error: unable to connect to polycube daemon.')
exit(1)
def createInstance(cube_name, debug, mode):
try:
print(f'Creating new Firewall instance named {cube_name}')
response = requests.post(f'{polycubed_endpoint}/firewall/{cube_name}',
json.dumps({
'type': mode,
'loglevel': 'debug' if debug is True else 'OFF'}),
timeout=REQUESTS_TIMEOUT)
response.raise_for_status()
except requests.exceptions.HTTPError:
print(f'Error: {response.content.decode("UTF-8")}')
exit(1)
except requests.exceptions.ConnectionError:
print('Connection error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.Timeout:
print('Timeout error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.RequestException:
print('Error: unable to connect to polycube daemon.')
exit(1)
def detach_from_interface(cube_name, interface):
try:
print(f'Detaching {cube_name} from {interface}')
response = requests.post(f'{polycubed_endpoint}/detach',
json.dumps(
{'cube': cube_name, 'port': interface}),
timeout=REQUESTS_TIMEOUT)
response.raise_for_status()
except requests.exceptions.HTTPError:
print(f'Error: {response.content.decode("UTF-8")}')
exit(1)
except requests.exceptions.ConnectionError:
print('Connection error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.Timeout:
print('Timeout error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.RequestException:
print('Error: unable to connect to polycube daemon.')
exit(1)
def attach_to_interface(cube_name, interface):
try:
print(f'Attaching {cube_name} to {interface}')
response = requests.post(f'{polycubed_endpoint}/attach',
json.dumps(
{'cube': cube_name, 'port': interface, 'position': 'first'}),
timeout=REQUESTS_TIMEOUT)
response.raise_for_status()
except requests.exceptions.HTTPError:
print(f'Error: {response.content.decode("UTF-8")}')
exit(1)
except requests.exceptions.ConnectionError:
print('Connection error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.Timeout:
print('Timeout error: unable to connect to polycube daemon.')
exit(1)
except requests.exceptions.RequestException:
print('Error: unable to connect to polycube daemon.')
exit(1)
def showVersion():
with open(f'{path.dirname(__file__)}/../VERSION', 'r') as fp:
return '%(prog)s - Version ' + fp.readline()
def parseArguments():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'cube_name', help='indicates the name of the cube', type=str)
parser.add_argument(
'peer_interface', help='indicates the network interface to connect the cube to', type=str)
parser.add_argument(
'-a', '--address', help='set the polycube daemon ip address', type=str, default=POLYCUBED_ADDR)
parser.add_argument(
'-p', '--port', help='set the polycube daemon port', type=int, default=POLYCUBED_PORT)
parser.add_argument(
'-m', '--mode', help='set the probe mode (TC / XDP_SKB / XDP_DRV)', type=str, default=MODE)
parser.add_argument(
'-d', '--debug', help='set the probe log level debug', action='store_true')
parser.add_argument('-v', '--version', action='version',
version=showVersion())
return parser.parse_args().__dict__
if __name__ == '__main__':
main()