From 6e26d73cf92bce285acf9de07330c676c28feed4 Mon Sep 17 00:00:00 2001 From: SeaBlooms Date: Mon, 21 Apr 2025 20:36:17 -0600 Subject: [PATCH 1/4] Create J1QLdeferredResponse.py --- examples/J1QLdeferredResponse.py | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/J1QLdeferredResponse.py diff --git a/examples/J1QLdeferredResponse.py b/examples/J1QLdeferredResponse.py new file mode 100644 index 0000000..821e62e --- /dev/null +++ b/examples/J1QLdeferredResponse.py @@ -0,0 +1,90 @@ +import os +import time +import json +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.dev.jupiterone.io" + +# JupiterOne GraphQL API headers +j1_graphql_headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + token, + 'Jupiterone-Account': acct +} + +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 (snyk_finding | snyk_finding_coordinate | insightvm_finding | github_finding | semgrep_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=10, backoff_factor=2, 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(1) # Sleep 1 second 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)) From 5f42b849e3e6cfed11e0c50bcde47968ff31830b Mon Sep 17 00:00:00 2001 From: Colin Blumer Date: Mon, 21 Apr 2025 20:46:11 -0600 Subject: [PATCH 2/4] Potential fix for code scanning alert no. 42: Unused import Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- examples/J1QLdeferredResponse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/J1QLdeferredResponse.py b/examples/J1QLdeferredResponse.py index 821e62e..887bdaf 100644 --- a/examples/J1QLdeferredResponse.py +++ b/examples/J1QLdeferredResponse.py @@ -1,6 +1,6 @@ import os import time -import json + import requests from requests.adapters import HTTPAdapter, Retry From 05d2d90e685a9f28374dde536dc7649b5f52ae9a Mon Sep 17 00:00:00 2001 From: Colin Blumer Date: Mon, 21 Apr 2025 20:55:11 -0600 Subject: [PATCH 3/4] Update J1QLdeferredResponse.py --- examples/J1QLdeferredResponse.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/J1QLdeferredResponse.py b/examples/J1QLdeferredResponse.py index 887bdaf..107653e 100644 --- a/examples/J1QLdeferredResponse.py +++ b/examples/J1QLdeferredResponse.py @@ -1,6 +1,5 @@ import os import time - import requests from requests.adapters import HTTPAdapter, Retry @@ -9,7 +8,7 @@ token = os.environ.get("JUPITERONE_TOKEN") # JupiterOne GraphQL API: -j1_graphql_url = "https://graphql.dev.jupiterone.io" +j1_graphql_url = "https://graphql.us.jupiterone.io" # JupiterOne GraphQL API headers j1_graphql_headers = { @@ -38,7 +37,7 @@ """ gql_variables = { - "query": "find (snyk_finding | snyk_finding_coordinate | insightvm_finding | github_finding | semgrep_finding)", + "query": "FIND Finding", "deferredResponse": "FORCE", "cursor": "", "flags": { @@ -50,6 +49,7 @@ "query": gql_query, "variables": gql_variables } + all_query_results = [] cursor = None @@ -62,7 +62,7 @@ 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) + # print(download_url) download_response = s.get(download_url).json() @@ -76,15 +76,15 @@ 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'])) + # print(len(download_response['data'])) # Update cursor from response if 'cursor' in download_response: cursor = download_response['cursor'] - print(cursor) + # print(cursor) else: break -# print(all_query_results) +print(all_query_results) print(len(all_query_results)) From 3c7b91410eb9dd7d7abdd33e27057ccb21b8aae3 Mon Sep 17 00:00:00 2001 From: SeaBlooms Date: Tue, 22 Apr 2025 14:04:33 -0600 Subject: [PATCH 4/4] Update J1QLdeferredResponse.py --- examples/J1QLdeferredResponse.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/J1QLdeferredResponse.py b/examples/J1QLdeferredResponse.py index 821e62e..bc0158b 100644 --- a/examples/J1QLdeferredResponse.py +++ b/examples/J1QLdeferredResponse.py @@ -38,7 +38,7 @@ """ gql_variables = { - "query": "find (snyk_finding | snyk_finding_coordinate | insightvm_finding | github_finding | semgrep_finding)", + "query": "FIND Finding", "deferredResponse": "FORCE", "cursor": "", "flags": { @@ -50,6 +50,7 @@ "query": gql_query, "variables": gql_variables } + all_query_results = [] cursor = None @@ -58,7 +59,7 @@ payload['variables']['cursor'] = cursor s = requests.Session() - retries = Retry(total=10, backoff_factor=2, status_forcelist=[502, 503, 504, 429]) + 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'] @@ -69,7 +70,7 @@ status = download_response['status'] while status == 'IN_PROGRESS': - time.sleep(1) # Sleep 1 second between checking status + time.sleep(0.2) # Sleep 200 milliseconds between checking status download_response = s.get(download_url).json() # fetch results data from download URL