forked from auth0/jupiterone-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJ1QLdeferredResponse.py
More file actions
90 lines (71 loc) · 2.1 KB
/
J1QLdeferredResponse.py
File metadata and controls
90 lines (71 loc) · 2.1 KB
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
import os
import time
import requests
from requests.adapters import HTTPAdapter, Retry
# JupiterOne API creds
acct = os.environ.get("JUPITERONE_ACCOUNT")
token = os.environ.get("JUPITERONE_TOKEN")
# JupiterOne GraphQL API:
j1_graphql_url = "https://graphql.us.jupiterone.io"
# JupiterOne GraphQL API headers
j1_graphql_headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + (token or ''),
'Jupiterone-Account': acct or ''
}
gql_query = """
query J1QL(
$query: String!
$variables: JSON
$cursor: String
$deferredResponse: DeferredResponseOption
) {
queryV1(
query: $query
variables: $variables
deferredResponse: $deferredResponse
cursor: $cursor
) {
type
url
}
}
"""
gql_variables = {
"query": "FIND Finding",
"deferredResponse": "FORCE",
"cursor": "",
"flags": {
"variableResultSize": True
},
}
payload = {
"query": gql_query,
"variables": gql_variables
}
all_query_results = []
cursor = None
while True:
payload['variables']['cursor'] = cursor
s = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504, 429])
s.mount('https://', HTTPAdapter(max_retries=retries))
url_response = s.post(j1_graphql_url, headers=j1_graphql_headers, json=payload)
download_url = url_response.json()['data']['queryV1']['url']
# print(download_url)
download_response = s.get(download_url).json()
status = download_response['status']
while status == 'IN_PROGRESS':
time.sleep(0.2) # Sleep 200 milliseconds between checking status
download_response = s.get(download_url).json() # fetch results data from download URL
status = download_response['status'] # update 'status' for next iteration
all_query_results.extend(download_response['data']) # add results to all results list
# print(len(download_response['data']))
# Update cursor from response
if 'cursor' in download_response:
cursor = download_response['cursor']
# print(cursor)
else:
break
print(all_query_results)
print(len(all_query_results))