Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions jupiterone/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# see https://github.com/PyCQA/pylint/issues/409

import json
from warnings import warn
from typing import Dict, List

import requests
Expand Down Expand Up @@ -50,11 +51,11 @@ def __init__(self, account: str = None, token: str = None, url: str = DEFAULT_UR
self.account = account
self.token = token
self.url = url
self.query_endpoint = self.url + "/graphql"
self.query_endpoint = self.url
self.rules_endpoint = self.url + "/rules/graphql"
self.headers = {
"Authorization": "Bearer {}".format(self.token),
"Jupiterone-Account": self.account,
"JupiterOne-Account": self.account,
}

@property
Expand Down Expand Up @@ -124,7 +125,7 @@ def _execute_query(self, query: str, variables: Dict = None) -> Dict:
raise JupiterOneApiRetryError("JupiterOne API rate limit exceeded.")

elif response.status_code in [504]:
raise JupiterOneApiRetryError("Bad Gateway error. Check network route and try again.")
raise JupiterOneApiRetryError("Gateway Timeout.")

elif response.status_code in [500]:
raise JupiterOneApiError("JupiterOne API internal server error.")
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

install_reqs = [
'requests',
'retrying',
'warnings'
'retrying'
]

setup(name='jupiterone',
Expand Down
2 changes: 2 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = ..
2 changes: 1 addition & 1 deletion tests/test_create_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io',
callback=request_callback,
content_type='application/json',
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_create_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io',
callback=request_callback,
content_type='application/json',
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_delete_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io',
callback=request_callback,
content_type='application/json',
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_delete_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io',
callback=request_callback,
content_type='application/json',
)
Expand Down
102 changes: 54 additions & 48 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

from jupiterone.client import JupiterOneClient
from jupiterone.constants import QUERY_V1
from jupiterone.errors import JupiterOneApiError
from jupiterone.errors import (
JupiterOneApiRetryError,
JupiterOneApiError
)

def build_results(response_code: int = 200, cursor: str = None, max_pages: int = 1):
pages = Counter(requests=0)
Expand Down Expand Up @@ -74,9 +77,8 @@ def request_callback(request):

@responses.activate
def test_execute_query():

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand All @@ -103,7 +105,7 @@ def test_execute_query():
def test_limit_skip_query_v1():

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand All @@ -125,13 +127,13 @@ def test_limit_skip_query_v1():
def test_cursor_query_v1():

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(cursor='cursor_value'),
content_type='application/json',
)

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand Down Expand Up @@ -177,7 +179,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=request_callback,
content_type='application/json',
)
Expand Down Expand Up @@ -226,7 +228,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=request_callback,
content_type='application/json',
)
Expand All @@ -245,19 +247,19 @@ def request_callback(request):
@responses.activate
def test_retry_on_limit_skip_query():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=429),
content_type='application/json',
)

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=503),
content_type='application/json',
)

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand All @@ -278,19 +280,19 @@ def test_retry_on_limit_skip_query():
@responses.activate
def test_retry_on_cursor_query():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=429),
content_type='application/json',
)

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=503),
content_type='application/json',
)

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand All @@ -309,7 +311,7 @@ def test_retry_on_cursor_query():
@responses.activate
def test_avoid_retry_on_limit_skip_query():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=404),
content_type='application/json',
)
Expand All @@ -326,7 +328,7 @@ def test_avoid_retry_on_limit_skip_query():
@responses.activate
def test_avoid_retry_on_cursor_query():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(response_code=404),
content_type='application/json',
)
Expand All @@ -341,7 +343,7 @@ def test_avoid_retry_on_cursor_query():
@responses.activate
def test_warn_limit_and_skip_deprecated():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_results(),
content_type='application/json',
)
Expand All @@ -360,7 +362,7 @@ def test_warn_limit_and_skip_deprecated():
@responses.activate
def test_unauthorized_query_v1():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_error_results(401, b'Unauthorized', 'text/plain'),
content_type='application/json',
)
Expand All @@ -371,14 +373,13 @@ def test_unauthorized_query_v1():
with pytest.raises(JupiterOneApiError) as exc_info:
j1.query_v1(query)

assert exc_info.value.args[0] == 'JupiterOne API query is unauthorized, check credentials.'

assert "401: Unauthorized" in str(exc_info.value.args[0])

@responses.activate
def test_unexpected_string_error_query_v1():
def test_five_hundred_error_query_v1():
responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
callback=build_error_results(500, 'String exception on server', 'text/plain'),
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_error_results(500, 'Internal Server Error', 'text/plain'),
content_type='application/json',
)

Expand All @@ -388,17 +389,17 @@ def test_unexpected_string_error_query_v1():
with pytest.raises(JupiterOneApiError) as exc_info:
j1.query_v1(query)

assert exc_info.value.args[0] == '500:String exception on server'
assert exc_info.value.args[0] == 'JupiterOne API internal server error.'


@responses.activate
def test_unexpected_json_error_query_v1():
def test_bad_gateway_error_query_v1():
error_json = {
'error': 'Bad Gateway'
}

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io/',
callback=build_error_results(502, json.dumps(error_json), ),
content_type='application/json',
)
Expand All @@ -409,25 +410,30 @@ def test_unexpected_json_error_query_v1():
with pytest.raises(JupiterOneApiError) as exc_info:
j1.query_v1(query)

assert exc_info.value.args[0] == '502:Bad Gateway'


@responses.activate
def test_unexpected_json_errors_query_v1():
errors_json = {
'errors': ['First error', 'Second error', 'Third error']
}

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
callback=build_error_results(500, json.dumps(errors_json), ),
content_type='application/json',
)

j1 = JupiterOneClient(account='testAccount', token='bogusToken')
query = "find Host with _id='1' return tree"

with pytest.raises(JupiterOneApiError) as exc_info:
j1.query_v1(query)

assert exc_info.value.args[0] == "500:['First error', 'Second error', 'Third error']"
# @responses.activate
# def test_rate_limit_error_query_v1():
# responses.add_callback(
# responses.POST, 'https://graphql.us.jupiterone.io/',
# callback=build_error_results(429, "Too Many Requests", ),
# content_type='application/json',
# )

# j1 = JupiterOneClient(account='testAccount', token='bogusToken')
# query = "find Host with _id='1' return tree"

# with pytest.raises(JupiterOneApiError) as exc_info:
# j1.query_v1(query)

# @responses.activate
# def test_service_unavailable_error_query_v1():
# responses.add_callback(
# responses.POST, 'https://graphql.us.jupiterone.io/',
# callback=build_error_results(503, "Service Unavailable", ),
# content_type='application/json',
# )

# j1 = JupiterOneClient(account='testAccount', token='bogusToken')
# query = "find Host with _id='1' return tree"

# with pytest.raises(JupiterOneApiRetryError) as exc_info:
# j1.query_v1(query)
2 changes: 1 addition & 1 deletion tests/test_update_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def request_callback(request):
return (200, headers, json.dumps(response))

responses.add_callback(
responses.POST, 'https://api.us.jupiterone.io/graphql',
responses.POST, 'https://graphql.us.jupiterone.io',
callback=request_callback,
content_type='application/json',
)
Expand Down