-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleAsCode.py
278 lines (213 loc) · 10.1 KB
/
ruleAsCode.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
import typer
from pathlib import Path
from os import remove
from wgeasywall.utils.general.filedir import create_temporary_copy
from wgeasywall.utils.mongo.gridfsmongo import upload, findAbstract,delete
from coolname import generate_slug
from wgeasywall.utils.general.configParser import get_configuration
from typing import Optional
from wgeasywall.utils.ruleAsCode.generate import createRules, migrateToNFT
app = typer.Typer()
@app.command()
def import_function(
funcFile: Path = typer.Option(...,"--function-file",help="The function definition file"),
force : Optional[bool] = typer.Option(False,"--force",help="Force adding and overwriting function even if the function exists"),
):
'''
Get a function manifest file and import it into the database
------------
Example:
wgeasywall RaaC import-function --function-file RaaCManifest/Conntrack.yaml
'''
if not funcFile.is_file():
typer.echo("ERROR: Function Definition file can't be found!",err=True)
raise typer.Exit(code=1)
funcDefiDict = get_configuration(funcFile)
if (type(dict) and 'ErrorCode' in funcDefiDict):
typer.echo("ERORR: Can't read Function Definition file. {0}".format(funcDefiDict['ErrorMsg']),err=True)
raise typer.Exit(code=1)
functionName = funcDefiDict['Func']['Name']
version = funcDefiDict['Func']['Version']
query = {'filename':'{0}.yaml'.format(functionName)}
files = findAbstract('RaaC','function',query=query)
ifRuleExist = False
fileID2Delete = None
for file in files:
if(file.uniqueName == version):
ifRuleExist = True
fileID2Delete = file._id
if (ifRuleExist):
doOverwrite = False
confirmOverwrite = False
if(force):
doOverwrite = True
else:
confirmOverwrite = typer.confirm("The function '{0}' with the version '{1}' exists. do you want to overwrite it?".format(functionName,version))
if (confirmOverwrite):
doOverwrite = True
if (not doOverwrite):
typer.echo("Overwriting abort.",err=True)
raise typer.Exit(code=0)
deleteResult = delete(db='RaaC',fs='function',fileID=fileID2Delete)
if (deleteResult != True):
typer.echo('ERROR: Unable to overwrite.',err=True)
raise typer.Exit(code=1)
funcTempPath = create_temporary_copy(path=funcFile,networkName="{0}.yaml".format(functionName))
upload(db='RaaC',fs='function',filePath=funcTempPath,uniqueName=version)
remove(funcTempPath)
typer.echo("The provided Function definition '{0}' with the version '{1}' is added to the database.".format(functionName,version))
@app.command()
def import_action(
actionFile: Path = typer.Option(...,"--action-file",help="The action definition file"),
force : Optional[bool] = typer.Option(False,"--force",help="Force adding and overwriting action even if the action exists"),
):
'''
Get an action manifest file and import it into the database
------------
Example:
wgeasywall RaaC import-action --action-file RaaCManifest/DROP.yaml
'''
if not actionFile.is_file():
typer.echo("ERROR: Action Definition file can't be found!",err=True)
raise typer.Exit(code=1)
funcDefiDict = get_configuration(actionFile)
if (type(dict) and 'ErrorCode' in funcDefiDict):
typer.echo("ERORR: Can't read Action Definition file. {0}".format(funcDefiDict['ErrorMsg']),err=True)
raise typer.Exit(code=1)
actionName = funcDefiDict['Action']['Name']
version = funcDefiDict['Action']['Version']
query = {'filename':'{0}.yaml'.format(actionName)}
files = findAbstract('RaaC','action',query=query)
ifRuleExist = False
fileID2Delete = None
for file in files:
if(file.uniqueName == version):
ifRuleExist = True
fileID2Delete = file._id
if (ifRuleExist):
doOverwrite = False
confirmOverwrite = False
if(force):
doOverwrite = True
else:
confirmOverwrite = typer.confirm("The function '{0}' with the version '{1}' exists. do you want to overwrite it?".format(actionName,version))
if (confirmOverwrite):
doOverwrite = True
if (not doOverwrite):
typer.echo("Overwriting abort.",err=True)
raise typer.Exit(code=0)
deleteResult = delete(db='RaaC',fs='action',fileID=fileID2Delete)
if (deleteResult != True):
typer.echo('ERROR: Unable to overwrite.',err=True)
raise typer.Exit(code=1)
funcTempPath = create_temporary_copy(path=actionFile,networkName="{0}.yaml".format(actionName))
upload(db='RaaC',fs='action',filePath=funcTempPath,uniqueName=version)
remove(funcTempPath)
typer.echo("The provided action definition '{0}' with the version '{1}' is added to the database.".format(actionName,version))
@app.command()
def generate_rule(
rule : str = typer.Option(...,"--rule",help="The rule should be parsed"),
actionVersion: str = typer.Option("@latest","--action-version",help="The version of Action"),
functionVersion: str = typer.Option("@latest","--function-version",help="The version of Function"),
nft: Optional[bool] = typer.Option(False,"--nft",help="Generate NFT syntax")
):
'''
Get a RaaC definition and generate corresponding IPTables rules.
------------
Example:
wgeasywall RaaC generate-rule --rule 'General(protocol=tcp:srcIP=192.168.0.1:dstSet=Berlin:dstPorts=443,80)::General(protocol=tcp)->ACCEPT()'
'''
ruleEnd = createRules(
function=rule,
actionVersion=actionVersion,
functionVersion=functionVersion
)
if (type(ruleEnd) == dict):
typer.echo("ERROR: {0}".format(ruleEnd['ErrorMsg']),err=True)
raise typer.Exit(code=1)
# ruleEnd is List of List
for rule in ruleEnd:
rule2show = ' '.join(rule)
if (not nft):
typer.echo(rule2show)
else:
nftRule = migrateToNFT(rule2show)
nftRuleComponents = nftRule.split(" ")
desiredIndex = nftRuleComponents.index("FORWARD")
typer.echo(' '.join(nftRuleComponents[desiredIndex+1:]))
@app.command()
def delete_function(
function: str = typer.Option(...,"--function",help="The function name"),
version: str = typer.Option(None,"--version",help="The version of function. Use @latest to get the latest")
):
'''
Get a function name and version to delete it from the database
------------
Example:
# delete all versions of 'conntrack' function from the database
wgeasywall RaaC delete-function --function conntrack
---
# delete only 'beta' version of 'conntrack' function from the database
wgeasywall RaaC delete-function --function conntrack --version beta
'''
query = {'filename':'{0}.yaml'.format(function)}
files = findAbstract('RaaC','function',query=query)
if(len(files) == 0):
typer.echo("ERROR: The function '{0}' doesn't exist on the database.".format(function),err=True)
raise typer.Exit(code=1)
if (version == '@latest'):
deleteResult = delete('RaaC','function',files[-1]._id)
if(type(deleteResult) == dict and 'ErrorCode' in deleteResult):
typer.echo("ERROR: Function can't be deleted : {0}".format(deleteResult['ErrorMsg']),err=True)
raise typer.Exit(code=1)
typer.echo("The latest version of function '{0}' is deleted".format(function))
raise typer.Exit(code=0)
for file in files:
if(version != None and file.uniqueName != version):
continue
deleteResult = delete('RaaC','function',file._id)
if(type(deleteResult) == dict and 'ErrorCode' in deleteResult):
typer.echo("ERROR: Function can't be deleted : {0}".format(deleteResult['ErrorMsg']),err=True)
raise typer.Exit(code=1)
typer.echo("The version '{1}' of function '{0}' is deleted.".format(function,file.uniqueName))
raise typer.Exit(code=0)
typer.echo("ERROR: The '{0}' version of function '{1}' doesn't exist to be deleted".format(version,function),err=True)
raise typer.Exit(code=1)
@app.command()
def delete_action(
action: str = typer.Option(...,"--action",help="The action name"),
version: str = typer.Option(None,"--version",help="The version of action. Use @latest to get the latest")
):
'''
Get a action name and version to delete it from the database
------------
Example:
# delete all versions of 'reject' action from the database
wgeasywall RaaC delete-action --action reject
---
# delete only 'beta' version of 'reject' action from the database
wgeasywall RaaC delete-action --action reject --version beta
'''
query = {'filename':'{0}.yaml'.format(action)}
files = findAbstract('RaaC','action',query=query)
if(len(files) == 0):
typer.echo("ERROR: The action '{0}' doesn't exist on the database.".format(action),err=True)
raise typer.Exit(code=1)
if (version == '@latest'):
deleteResult = delete('RaaC','action',files[-1]._id)
if(type(deleteResult) == dict and 'ErrorCode' in deleteResult):
typer.echo("ERROR: Action can't be deleted : {0}".format(deleteResult['ErrorMsg']),err=True)
raise typer.Exit(code=1)
typer.echo("The latest version of action '{0}' is deleted".format(action))
raise typer.Exit(code=0)
for file in files:
if(version != None and file.uniqueName != version):
continue
deleteResult = delete('RaaC','action',file._id)
if(type(deleteResult) == dict and 'ErrorCode' in deleteResult):
typer.echo("ERROR: Action can't be deleted : {0}".format(deleteResult['ErrorMsg']),err=True)
raise typer.Exit(code=1)
typer.echo("The version '{1}' of action '{0}' is deleted.".format(action,file.uniqueName))
raise typer.Exit(code=0)
typer.echo("ERROR: The '{0}' version of action '{1}' doesn't exist to be deleted".format(version,action),err=True)
raise typer.Exit(code=1)