-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulepipe.py
372 lines (313 loc) · 13.2 KB
/
rulepipe.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
import os
import time
import json
import logging
import hashlib
from redis import Redis
from db_mongo import Mongo
from db_local import LocalDB
from dotenv import load_dotenv
class Data(dict):
def __getitem__(self, name):
print("__getitem__ called for {}".format(name))
fields = name.split('.')
c = self
for field in fields:
print(c, field)
c = dict.__getitem__(c, field)
return c
class RuleOperations(object):
operations = {
"gt": lambda a,b: a > b,
"gte": lambda a,b: a >= b,
"lt": lambda a,b: a < b,
"lte": lambda a,b: a <= b,
"eq": lambda a,b: a == b,
"ne": lambda a,b: a != b,
"mod": lambda a,b: a % b,
"sum": lambda args: sum(args),
"any": lambda args: any(args),
"all": lambda args: all(args),
"fromfile": lambda d,fname: RuleOperations.fromfile(d, fname)
}
files = {}
@staticmethod
def eval(rule, data):
logging.debug("Evauluating: " + str(data))
return RuleOperations.get_operation(
rule["condition"],
data[rule["field"]],
rule["value"]
)
@staticmethod
def get_operation(condition, data, value):
logging.debug("Condition: {}, Data: {}, Value: {}".format(
condition, data, value))
logging.debug(RuleOperations.operations[condition](data, value))
return RuleOperations.operations[condition](data, value)
@staticmethod
def fromfile(d, fname):
print(d, fname)
if not fname in RuleOperations.files.keys():
hashes = []
for line in open(fname, 'r').readlines():
hashes.append((line.strip().split(':')[0]))
RuleOperations.files[fname] = hashes
print(list(RuleOperations.files[fname]))
print(RuleOperations.files[fname])
return d in RuleOperations.files[fname]
class RuleManager(object):
def __init__(self):
logging.info('Rule manager initializing')
logging.debug('Database client initializing')
self.load_environment_variables()
self.init_database_client()
if(self.ENV["USE_CACHE"]):
logging.info("Caching active")
self.init_redis_client(self.ENV["REDIS_IP"], self.ENV["REDIS_PORT"])
def load_environment_variables(self):
self.load_dotenv_if_defined()
self.ENV = {
"DB_TYPE" : str(os.getenv("RULEPIPE_DB_TYPE", "local")).lower(),
"DB_IP" : os.getenv("RULEPIPE_DB_IP", "127.0.0.1"),
"DB_PORT" : int(os.getenv("RULEPIPE_DB_PORT", 27017)),
"DB_USER" : os.getenv("RULEPIPE_DB_USER", "rulepipe"),
"DB_PASSWORD" : os.getenv("RULEPIPE_DB_PASSWORD", ""),
"DB_NAME" : os.getenv("RULEPIPE_DB_NAME", "rulepipe"),
"DB_AUTHENTICATE" : json.loads(os.getenv("RULEPIPE_DB_AUTHENTICATE", "False").lower()),
"USE_CACHE" : json.loads(os.getenv("RULEPIPE_USE_CACHE", "False").lower()),
"REDIS_IP" : os.getenv("RULEPIPE_REDIS_IP", "127.0.0.1"),
"REDIS_PORT" : int(os.getenv("RULEPIPE_REDIS_PORT", 6379))
}
def load_dotenv_if_defined(self):
env_file = os.getenv("RULEPIPE_ENVFILE")
if(env_file):
logging.info("Using Environment File : " + str(env_file))
load_dotenv(env_file)
else:
logging.info("Environment file not specified. Looking for in-folder .env file")
load_dotenv()
def init_database_client(self):
"""
Initializes a Rule Management with specified database.
By default, local (and in-memory) dictionary object is used as a DB.
In "local" db, it will not be persistent.
"""
if self.ENV["DB_TYPE"] == "local":
logging.info("Database type: In-memory database")
self.db = LocalDB()
elif self.ENV["DB_TYPE"] == "mongo" or self.ENV["DB_TYPE"] == "mongodb":
logging.info("Database type: MongoDB.")
if self.ENV["DB_AUTHENTICATE"]:
self.db = Mongo(
ip =self.ENV["DB_IP"],
port =self.ENV["DB_PORT"],
username=self.ENV["DB_USER"],
password=self.ENV["DB_PASSWORD"],
auth =True,
db_name =self.ENV["DB_NAME"]
)
else:
self.db = Mongo(
ip =self.ENV["DB_IP"],
port =self.ENV["DB_PORT"],
db_name=self.ENV["DB_NAME"]
)
else:
logging.error("This database type is not supporting.")
def init_redis_client(self, host, port):
self.redis = Redis(host, port)
def add_rule_json_as_string(self, name, rule_string):
"""
Adds a JSON formatted string rule into Rule Database as JSON
"""
return self.add_rule_json(name, json.loads(rule_string))
def add_rule_json(self, name, rule):
"""
Adds a rule into Rule Database as JSON
"""
if 'type' not in rule:
error_message = "At least one 'type' value required to add new rule."
logging.error(error_message)
raise KeyError(error_message)
is_added_to_database = self.db.add_rule(name, rule)
if is_added_to_database and self.ENV["USE_CACHE"]:
logging.debug("New rule caching...")
rule_name_hash = self.md5(name)
rule_time_hash = self.md5(name + "_cache_time")
self.redis.set(rule_name_hash, str(rule))
self.redis.set(rule_time_hash, str(time.time()))
return True
def delete_rule(self, name):
if self.ENV["USE_CACHE"]:
rule_name_hash = self.md5(name)
rule_time_hash = self.md5(name + "_cache_time")
self.redis.delete(rule_name_hash, rule_time_hash)
return self.db.delete_rule(name)
def execute_rule_json_as_string(self, name, data_string):
"""
Runs a JSON formatted rule string and returns the result
"""
return self.execute_rule_json(name, Data(json.loads(data_string)))
def execute_rule_json(self, name, data):
"""
Runs rule using given data and returns the result
"""
if self.ENV["USE_CACHE"] and not self.redis == None:
logging.debug("Checking is Redis connected : " + str(self.redis.ping()))
return self.execute_rule_json_with_caching(name, data)
else:
return self.execute_rule_json_without_caching(name, data)
def md5(self, text):
return hashlib.md5(str(text).encode()).hexdigest()
def is_cached_statement_updated(self, rule_time_hash, statement_time_hash):
rule_time = self.redis.get(rule_time_hash)
statement_time = self.redis.get(statement_time_hash)
if(
rule_time == None or rule_time == b'None' or
statement_time == None or statement_time == b'None'
):
return True
return float(rule_time) > float(statement_time)
def execute_rule_json_with_caching(self, name, data):
rule_name_hash = self.md5(name)
rule_time_hash = self.md5(name + "_cache_time")
statement_name_hash = self.md5(name + "_" + str(data))
statement_time_hash = self.md5(statement_name_hash + "_cache_time")
logging.debug("Checking Rule flow existance in cache with { rule_name_hash: " + rule_name_hash + " , rule_time_hash: " + rule_time_hash + " }")
if (
self.redis.get(rule_name_hash) == b'None' or
self.redis.get(rule_name_hash) == None or
self.redis.get(rule_time_hash) == b'None' or
self.redis.get(rule_time_hash) == None
):
logging.debug("Rule flow not found in cache, fetching from db...")
flow_db_record = self.db.get_flow(name)
if flow_db_record:
logging.debug("Rule fetched from db, caching...")
self.redis.set(rule_name_hash, str(flow_db_record))
self.redis.set(rule_time_hash, str(time.time()))
else:
logging.error("Rule couldnt fetch from db.")
return
logging.debug("Rule flow found in cache, fetching...")
flow = [json.loads(str(self.redis.get(rule_name_hash), 'utf-8').replace("\'", "\""))]
logging.debug("Rule Flow: " + str(flow.__class__) + " : " + str(flow))
logging.debug("Checking Statement result existance in cache with { statement_name_hash: " + statement_name_hash + " , statement_time_hash: " + statement_time_hash + " }")
if(
self.redis.get(statement_name_hash) == b'None' or self.redis.get(statement_name_hash) == None or
self.is_cached_statement_updated(rule_time_hash, statement_time_hash)):
logging.debug("rule_time : " + str(self.redis.get(rule_time_hash)) + ", statement_time : " + str(self.redis.get(statement_time_hash)) + "\n")
logging.debug("Statement not found or become old in cache , executing...")
response = self.process_steps(flow, data)
logging.debug("Statement execute completed. Caching...")
self.redis.set(statement_name_hash, str(response))
self.redis.set(statement_time_hash, str(time.time()))
else:
logging.debug("Statement found in cache, fetching...")
response = str(self.redis.get(statement_name_hash), 'utf-8')
logging.debug("Statement: " + str(response))
return response
def execute_rule_json_without_caching(self, name, data):
flow = self.db.get_flow(name)
response = self.process_steps(flow, data)
return response
def add_rule_code(self, name, rule):
"""
Adds a rule into Rule Database as code.
DANGER: Be really careful if you are planning to use this.
May be INSECURE.
"""
logging.critical("This function is not activated yet: add_rule_code")
self.add_rule_json(name, rule)
def get_rule_list(self):
"""
Returns saved rules list.
"""
return list(self.db.get_rules())
def execute_rule_code(self, name, data):
"""
Runs rule using given data and returns the result
DANGER: Be really careful if you are planning to use this.
May be INSECURE.
"""
logging.critical("This function is not activated yet: execute_rule_code")
pass
def processRule(self, step, data):
results = []
for rule in step["rules"]:
results.append(RuleOperations.eval(rule, data))
return RuleOperations.operations[step["match"]](results)
def process_steps(self, flow, data):
if(flow):
for step in flow:
logging.debug("Processing Step: " + str(step))
if step["type"] == "rule":
result = self.processRule(step, data)
logging.debug("Result: " + str(result))
return result
elif step["type"] == "ruleset":
logging.debug("RuleSet found, continuing recursively.")
rulesetResults = []
for rule in step["rules"]:
logging.debug("Rule sent #149:" + str(rule) + str(type(rule)))
rulesetResults.append(self.process_steps([rule], data))
return RuleOperations.operations[step["match"]](rulesetResults)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
rules = RuleManager()
print(rules.get_rule_list())
rules.add_rule_json_as_string("emre6", """
{
"type": "ruleset",
"match": "all",
"rules": [
{
"type": "rule",
"match": "all",
"rules": [
{
"field": "responseTimeInSeconds",
"condition": "lte",
"value": 0.45
},
{
"field": "statusCode",
"condition": "gte",
"value": 200
}
]
},
{
"type": "rule",
"match": "any",
"rules": [
{
"field": "responseTimeInSeconds",
"condition": "lte",
"value": 3.45
},
{
"field": "details.importance",
"condition": "gte",
"value": 5
}
]
}
]
}
""")
print(rules.execute_rule_json_as_string("emre6", """
{
"responseTimeInSeconds": 0.1,
"statusCode": 200,
"details": {
"name": "mymetric",
"importance": 7
}
}
"""))
print(rules.get_rule_list())
print(rules.delete_rule("emre6"))
print(rules.delete_rule("emre6"))
print(rules.get_rule_list())