-
Notifications
You must be signed in to change notification settings - Fork 0
/
eh_analysis_priorities.py
226 lines (193 loc) · 7.97 KB
/
eh_analysis_priorities.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
#!/usr/bin/python
# from __builtin__ import None
# from __builtin__ import None
DOCUMENTATION = '''
---
module: eh_analysis_priorities
version_added:
short_description: Manage extrahop analysis priorities groups
description:
- Allow adding and removing device groups to/from the extrahop analysis priorities list
options:
eda:
descripton: the hostname of the EDA targetted
required: True
apiKey:
description: the API key to interact with the rest API
required: True
state:
description:
- Action of whitelist module
- assigned/unassigned
required: True
isMemberOf:
description:
- This can either be a list of specific group names, or "any" to include devices of any defined group. Group names can be partial for multiple matches
i.e. 'Citrix" will include 'Citrix Servers', 'Citrix Brokers', and 'Citrix StoreFront'
required: True
Example:
- name: Whitelist Devices
eh_whitelist:
eda: "{{ inventory_hostname }}"
apiKey: "{{ api.key }}"
state: "assigned"
isMemberOf:
- any
'''
import re
import json
import_fail = False
try:
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3.exceptions import SNIMissingWarning
from requests.packages.urllib3.exceptions import InsecurePlatformWarning
import requests.packages
except:
import_fail = True
from ansible.module_utils.basic import AnsibleModule
ANALYSIS_PATH='/api/v1/analysispriority/config/0'
def getDeviceGroupIdsFromGroupName(eda,apikey,httpsession,groupName, type):
# Check if this is a group ID or a group Name
httpsession.headers.update({'Authorization': 'ExtraHop apikey='+apikey})
groupIds = []
if type == "device":
uri = 'https://'+eda+'/api/v1/devicegroups?all=false&name='+groupName
rsp = httpsession.get(uri, verify=False)
if rsp.status_code !=200:
return False, "URI: " + uri + " returned Status Code: " + str(rsp.status_code) + " " + rsp.text
groups = json.loads(rsp.text)
for group in groups:
groupIds.append(group["id"])
elif type == "activity":
uri = 'https://'+eda+'/api/v1/activitygroups'
rsp = httpsession.get(uri, verify=False)
if rsp.status_code !=200:
return False, "URI: " + uri + " returned Status Code: " + str(rsp.status_code) + " " + rsp.text
groups = json.loads(rsp.text)
for group in groups:
if groupName in group["display"]:
groupIds.append(group["oid"])
else:
return False, "No matching group type"
return True, groupIds
def getAnalysisPrioritiesList(eda,httpsession,apiKey):
httpsession.headers.update({'Authorization': 'ExtraHop apikey='+apiKey})
rsp = httpsession.get('https://'+eda+ANALYSIS_PATH, verify=False)
if rsp.status_code != 200:
return False, "status code: "+str(rsp.status_code) + " " + rsp.text
analysisList = json.loads(rsp.text)
if len(analysisList) == 0:
module.exit_json(changed=False)
else:
return True, analysisList
def modifyAnalysisPrioritiesList(eda,httpsession,apiKey,priorityList,deviceGroupIDs,action,level,state,types):
httpsession.headers.update({'Authorization': 'ExtraHop apikey='+apiKey})
autofill_advanced = priorityList["autofill_advanced"]
autofill_standard = priorityList["autofill_standard"]
advanced_rules = priorityList["advanced_rules"] or []
standard_rules = priorityList["standard_rules"] or []
if types == "device":
types = "device_group"
elif types == "activity":
types = "activity_group"
if state == "assigned":
for deviceGroupID in deviceGroupIDs:
if level == "advanced":
if len(advanced_rules) == 0:
advanced_rules.append({
"type": types,
"object_id": deviceGroupID,
"description": "added via API"
})
else:
for entry in advanced_rules:
if deviceGroupID == entry["object_id"]:
return False, "DeviceGroup already exists in the list"
advanced_rules.append({
"type": types,
"object_id": deviceGroupID,
"description": "added via API"
})
if level == "standard":
if len(standard_rules) == 0:
standard_rules.append({
"type": types,
"object_id": deviceGroupID,
"description": "added via API"
})
else:
for entry in standard_rules:
if deviceGroupID == entry["object_id"]:
return False, "DeviceGroup already exists in the list"
standard_rules.append({
"type": types,
"object_id": deviceGroupID,
"description": "added via API"
})
elif state == "unassigned":
status = False
for deviceGroupID in deviceGroupIDs:
if level == "advanced":
for entry in advanced_rules:
if deviceGroupID == entry["object_id"]:
advanced_rules.remove(entry)
status = True
if level == "standard":
for entry in standard_rules:
if deviceGroupID == entry["object_id"]:
standard_rules.remove(entry)
status = True
if status != True:
return False, "Unable to remove entry from Analysis Priorities List"
postBody = {
"autofill_advanced":autofill_advanced,
"autofill_standard":autofill_standard,
"advanced_rules":advanced_rules,
"standard_rules":standard_rules
}
rsp = httpsession.put('https://'+eda+ANALYSIS_PATH, json=postBody, verify=False)
if rsp.status_code != 204:
return False, "status code: "+str(rsp.status_code)+" "+rsp.text
else:
return True, None
def main():
module = AnsibleModule(
argument_spec = dict(
eda = dict(required=True, type='str'),
apiKey = dict(required=True, type='str', no_log=True),
level = dict(required=True, type='str', choices=['advanced','standard']),
state = dict(required=True, type='str', choices=['assigned','unassigned']),
types = dict(required=True, type='str', choices=['device','activity']),
isMemberOf = dict(required=True, type='list')
),
supports_check_mode = False)
apikey = module.params['apiKey']
eda = module.params['eda']
state = module.params['state']
level = module.params['level']
types = module.params['types']
isMemberOf = module.params['isMemberOf']
# Supress InsecureRequestWarning errors
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(SNIMissingWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
s = requests.Session()
for groupName in isMemberOf:
success,data = getDeviceGroupIdsFromGroupName(eda, apikey, s, groupName, types)
if (success):
deviceGroupIDs = data
else:
module.fail_json(msg=data)
success, message = getAnalysisPrioritiesList(eda,s,apikey)
if not success:
module.fail_json(msg="Unable to get Analysis Priorities List. " + message)
else:
priorityList = message
action = state[0:len(state)-2]
success, message = modifyAnalysisPrioritiesList(eda,s,apikey,priorityList,deviceGroupIDs,action,level,state,types)
if not success:
module.fail_json(msg="Unable to modify Analysis Priorities List. " + message)
module.exit_json(changed=True)
if __name__ == '__main__':
main()