Skip to content

Commit 7bed338

Browse files
committed
add compliance and alert rule evaluation methods
1 parent 735b4bb commit 7bed338

File tree

3 files changed

+454
-85
lines changed

3 files changed

+454
-85
lines changed

examples/examples.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,24 @@
426426
print("evaluate_alert_rule()")
427427
print(json.dumps(evaluate_alert_rule_r, indent=1))
428428

429-
429+
# get_compliance_framework_item_details
430+
r = j1.get_compliance_framework_item_details(item_id="<GUID>")
431+
print("get_compliance_framework_item_details()")
432+
print(json.dumps(r, indent=1))
433+
434+
# list alert rule evaluation results
435+
r = j1.list_alert_rule_evaluation_results(rule_id="<GUID>")
436+
print("list_alert_rule_evaluation_results()")
437+
print(json.dumps(r, indent=1))
438+
439+
# fetch_evaluation_result_download_url
440+
r = j1.fetch_evaluation_result_download_url(raw_data_key="RULE_EVALUATION/<GUID>/query0.json")
441+
print("fetch_evaluation_result_download_url()")
442+
print(json.dumps(r, indent=1))
443+
444+
# fetch_downloaded_evaluation_results
445+
r = j1.fetch_downloaded_evaluation_results(download_url="https://download.us.jupiterone.io/<GUID>%2FRULE_EVALUATION%2F<GUID>%2F<epoch>%2Fquery0.json?token=<TOKEN>&Expires=<epoch>")
446+
print("fetch_downloaded_evaluation_results()")
447+
print(json.dumps(r, indent=1))
430448

431449

jupiterone/client.py

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from warnings import warn
77
from typing import Dict, List
88
from datetime import datetime
9+
import time
910

1011
import re
1112
import requests
@@ -45,6 +46,9 @@
4546
UPDATE_RULE_INSTANCE,
4647
EVALUATE_RULE_INSTANCE,
4748
QUESTIONS,
49+
COMPLIANCE_FRAMEWORK_ITEM,
50+
LIST_COLLECTION_RESULTS,
51+
GET_RAW_DATA_DOWNLOAD_URL,
4852
)
4953

5054

@@ -1010,42 +1014,52 @@ def evaluate_alert_rule(self, rule_id: str = None):
10101014
response = self._execute_query(EVALUATE_RULE_INSTANCE, variables=variables)
10111015
return response
10121016

1013-
def fetch_latest_evaluation_results(self):
1014-
"""Fetch latest Alert Rules configured in J1 account
1017+
def list_alert_rule_evaluation_results(self, rule_id: str = None):
1018+
"""Fetch a list of Evaluation Results for an Alert Rule configured in J1 account
10151019
10161020
"""
1017-
results = []
1018-
1019-
data = {
1020-
"query": LIST_RULE_INSTANCES,
1021-
"flags": {
1022-
"variableResultSize": True
1023-
}
1021+
variables = {
1022+
"collectionType": "RULE_EVALUATION",
1023+
"collectionOwnerId": rule_id,
1024+
"beginTimestamp": 0,
1025+
"endTimestamp": round(time.time() * 1000),
1026+
"limit": 40
10241027
}
10251028

1026-
r = requests.post(url=self.graphql_url, headers=self.headers, json=data, verify=True).json()
1027-
results.extend(r['data']['listRuleInstances']['questionInstances'])
1029+
response = self._execute_query(LIST_COLLECTION_RESULTS, variables=variables)
1030+
return response
10281031

1029-
while r['data']['listRuleInstances']['pageInfo']['hasNextPage'] == True:
1032+
def fetch_evaluation_result_download_url(self, raw_data_key: str = None):
1033+
"""Fetch evaluation result Download URL for Alert Rule configured in J1 account
10301034
1031-
cursor = r['data']['listRuleInstances']['pageInfo']['endCursor']
1035+
"""
1036+
variables = {
1037+
"rawDataKey": raw_data_key
1038+
}
10321039

1033-
# cursor query until last page fetched
1034-
data = {
1035-
"query": LIST_RULE_INSTANCES,
1036-
"variables": {
1037-
"cursor": cursor
1038-
},
1039-
"flags":{
1040-
"variableResultSize": True
1041-
}
1042-
}
1040+
response = self._execute_query(GET_RAW_DATA_DOWNLOAD_URL, variables=variables)
1041+
return response
10431042

1044-
r = requests.post(url=self.graphql_url, headers=self.headers, json=data, verify=True).json()
1045-
results.extend(r['data']['listRuleInstances']['questionInstances'])
1043+
def fetch_downloaded_evaluation_results(self, download_url: str = None):
1044+
"""Return full Alert Rule J1QL results from Download URL
1045+
1046+
"""
1047+
# initiate requests session and implement retry logic of 5 request retries with 1 second between
1048+
s = requests.Session()
1049+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
1050+
s.mount('https://', HTTPAdapter(max_retries=retries))
1051+
1052+
try:
1053+
response = s.get(
1054+
download_url, timeout=60
1055+
)
1056+
1057+
return response.json()
1058+
1059+
except Exception as e:
1060+
1061+
return e
10461062

1047-
return results
1048-
10491063
def list_questions(self):
10501064
"""List all defined Questions configured in J1 account Questions Library
10511065
@@ -1080,4 +1094,17 @@ def list_questions(self):
10801094
r = requests.post(url=self.graphql_url, headers=self.headers, json=data, verify=True).json()
10811095
results.extend(r['data']['questions']['questions'])
10821096

1083-
return results
1097+
return results
1098+
1099+
def get_compliance_framework_item_details(self, item_id: str = None):
1100+
"""Fetch Details of a Compliance Framework Requirement configured in J1 account
1101+
1102+
"""
1103+
variables = {
1104+
"input": {
1105+
"id": item_id
1106+
}
1107+
}
1108+
1109+
response = self._execute_query(COMPLIANCE_FRAMEWORK_ITEM, variables=variables)
1110+
return response

0 commit comments

Comments
 (0)