Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
HORSE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Iulisloi Zacarias
HORSE
Commits
b2d97e8f
Commit
b2d97e8f
authored
1 year ago
by
Chukwuemeka Michael Muonagor
Browse files
Options
Downloads
Patches
Plain Diff
Update whatif_loop.py
parent
dc72f2ec
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/whatif_loop.py
+22
-21
22 additions, 21 deletions
app/whatif_loop.py
with
22 additions
and
21 deletions
app/whatif_loop.py
+
22
−
21
View file @
b2d97e8f
...
...
@@ -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 proceed
s
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
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment