Skip to content
Snippets Groups Projects
Commit b2d97e8f authored by Chukwuemeka Michael Muonagor's avatar Chukwuemeka Michael Muonagor
Browse files

Update whatif_loop.py

parent dc72f2ec
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,16 @@ import random
import string
import pandas as pd
import delete_intents_elasticsearch
import yaml
import config
port = config.port
elastic_host = config.elastic_host
elastic_port = config.elastic_port
parameters = config.parameters
workflow_url = config.workflow_url
elasticsearch_url = config.elasticsearch_url
es = Elasticsearch(elasticsearch_url)
def whatif_send_fun(policy_dict, whatif_send_url):
sent_whatif = []
......@@ -22,7 +31,6 @@ def whatif_send_fun(policy_dict, whatif_send_url):
whatif_question['action'] = policy_dict['action']
whatif_question['duration'] = str(policy_dict['duration'])
whatif_question['id'] = whatif_id
#print('what if question: ', whatif_question)
l = len(sent_whatif)
if l != 0 and whatif_question != sent_whatif[l-1]:
sent_whatif.append(whatif_question)
......@@ -37,7 +45,7 @@ def whatif_send_fun(policy_dict, whatif_send_url):
#the function runs through the awaiting intents elasticsearch index every 60 secs
#and sends what-if questions to the SAN for the hosts in the awaiting intents index
def whatif_loop_fun(es, whatif_send_url):
print('entered whatif loop')
#print('entered whatif loop')
while True:
intent_index = es.exists(index="awaiting_intents", id=1)
if intent_index == True:
......@@ -54,21 +62,10 @@ def whatif_loop_fun(es, whatif_send_url):
time.sleep(60)
#when a what-if answer is received from the SAN
#the IBI proceed with the intent if the response from the SAN is not acceptable
def whatif_receive_fun(whatif_receive, ip):
#the IBI proceeds with the intent if the response from the SAN is acceptable
def whatif_receive_fun(whatif_receive):
import policy_configurator
with open('/code/app/config.yml') as f:
parameters = yaml.safe_load(f)
port = parameters['port']
elastic_host = parameters['elasticsearch_ip']
elastic_port = parameters['elasticsearch_port']
#workflow_url = "http://" + ip + ":" + port + parameters['to_send_workflow']
workflow_url = parameters['rtr_api_url']
stored_intents_url = "http://" + ip + ":" + port + parameters['to_view_or_delete_intents']
elasticsearch_url = "http://" + elastic_host + ":" + elastic_port
es = Elasticsearch(elasticsearch_url)
stored_intents_url = config.stored_intents_url
whatif_answer = {}
if whatif_receive.what_if_response == 'ok':
print('proceeding with intent')
......@@ -80,7 +77,7 @@ def whatif_receive_fun(whatif_receive, ip):
whatif_answer['duration'] = whatif_receive.duration
whatif_answer['id'] = whatif_receive.id
whatif_answer['what_if_response'] = whatif_receive.what_if_response
df_policy = pd.read_csv('/code/app/policy_store.csv')
df_policy = pd.read_csv(config.policy_store_directory)
for ind in df_policy.index:
if df_policy['action'][ind] == whatif_answer['action']:
whatif_answer['priority'] = df_policy['priority'][ind]
......@@ -91,13 +88,17 @@ def whatif_receive_fun(whatif_receive, ip):
resp = es.search(index="awaiting_intents", size=100, query={"match_all": {}})
for ind in range(len(resp['hits']['hits'])):
hit1 = resp['hits']['hits'][ind]['_source']
#print('hit1 for awaiting intents: ', hit1)
if hit1['intent_type'] == whatif_answer['intent_type'] and hit1['threat'] == whatif_answer['threat'] and \
str(hit1['host']) == str(whatif_answer['host']) and hit1['action'] == whatif_answer['action'] and \
str(hit1['duration']) == str(whatif_answer['duration']) and hit1['id'] == whatif_answer['id']:
delete_intents_elasticsearch.delete_intents_elasticsearch_fun(elasticsearch_url, resp['hits']['hits'][ind]['_id'],
"awaiting_intents")
#es.delete(index="awaiting_intents", id=hit1['id'])
def del_whatif_fun(policy_dict):
resp = es.search(index="awaiting_intents", size=100, query={"match_all": {}})
for ind in range(len(resp['hits']['hits'])):
hit1 = resp['hits']['hits'][ind]['_source']
if hit1['threat'] == policy_dict['threat'] and hit1['host'] == policy_dict['host']:
delete_intents_elasticsearch.delete_intents_elasticsearch_fun(elasticsearch_url, resp['hits']['hits'][ind]['_id'],
"awaiting_intents")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment