|
| 1 | +import os |
| 2 | +import time |
| 3 | +import requests |
| 4 | +from requests.adapters import HTTPAdapter, Retry |
| 5 | + |
| 6 | +# JupiterOne API creds |
| 7 | +acct = os.environ.get("JUPITERONE_ACCOUNT") |
| 8 | +token = os.environ.get("JUPITERONE_TOKEN") |
| 9 | + |
| 10 | +# JupiterOne GraphQL API: |
| 11 | +j1_graphql_url = "https://graphql.us.jupiterone.io" |
| 12 | + |
| 13 | +# JupiterOne GraphQL API headers |
| 14 | +j1_graphql_headers = { |
| 15 | + 'Content-Type': 'application/json', |
| 16 | + 'Authorization': 'Bearer ' + token, |
| 17 | + 'Jupiterone-Account': acct |
| 18 | +} |
| 19 | + |
| 20 | +gql_query = """ |
| 21 | +query J1QL( |
| 22 | + $query: String! |
| 23 | + $variables: JSON |
| 24 | + $cursor: String |
| 25 | + $deferredResponse: DeferredResponseOption |
| 26 | +) { |
| 27 | + queryV1( |
| 28 | + query: $query |
| 29 | + variables: $variables |
| 30 | + deferredResponse: $deferredResponse |
| 31 | + cursor: $cursor |
| 32 | + ) { |
| 33 | + type |
| 34 | + url |
| 35 | + } |
| 36 | +} |
| 37 | +""" |
| 38 | + |
| 39 | +gql_variables = { |
| 40 | + "query": "FIND Finding", |
| 41 | + "deferredResponse": "FORCE", |
| 42 | + "cursor": "", |
| 43 | + "flags": { |
| 44 | + "variableResultSize": True |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +payload = { |
| 49 | + "query": gql_query, |
| 50 | + "variables": gql_variables |
| 51 | +} |
| 52 | + |
| 53 | +all_query_results = [] |
| 54 | +cursor = None |
| 55 | + |
| 56 | +while True: |
| 57 | + |
| 58 | + payload['variables']['cursor'] = cursor |
| 59 | + |
| 60 | + s = requests.Session() |
| 61 | + retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504, 429]) |
| 62 | + s.mount('https://', HTTPAdapter(max_retries=retries)) |
| 63 | + url_response = s.post(j1_graphql_url, headers=j1_graphql_headers, json=payload) |
| 64 | + download_url = url_response.json()['data']['queryV1']['url'] |
| 65 | + # print(download_url) |
| 66 | + |
| 67 | + download_response = s.get(download_url).json() |
| 68 | + |
| 69 | + status = download_response['status'] |
| 70 | + |
| 71 | + while status == 'IN_PROGRESS': |
| 72 | + time.sleep(0.2) # Sleep 200 milliseconds between checking status |
| 73 | + |
| 74 | + download_response = s.get(download_url).json() # fetch results data from download URL |
| 75 | + |
| 76 | + status = download_response['status'] # update 'status' for next iteration |
| 77 | + |
| 78 | + all_query_results.extend(download_response['data']) # add results to all results list |
| 79 | + # print(len(download_response['data'])) |
| 80 | + |
| 81 | + # Update cursor from response |
| 82 | + if 'cursor' in download_response: |
| 83 | + cursor = download_response['cursor'] |
| 84 | + # print(cursor) |
| 85 | + |
| 86 | + else: |
| 87 | + break |
| 88 | + |
| 89 | +print(all_query_results) |
| 90 | +print(len(all_query_results)) |
0 commit comments