Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure certificate is verified when required by RestApiClientAsync #1620

Merged
merged 16 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, connection, configuration):
connection.get('port'),
headers,
url_modifier_function,
cert_verify=connection.get('selfSignedCert', True))
cert_verify=connection.get('selfSignedCert'))

async def ping_data_source(self):
endpoint = self.endpoint_start + '/status'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, connection, configuration):
self.client = RestApiClientAsync(connection.get('host'),
connection.get('port'),
headers,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)

async def ping_data_source(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@

class TestArcsightConnection(TestCase):
@staticmethod
def test_is_async():
@patch('ssl.SSLContext.load_verify_locations')
def test_is_async(self):
"""to check connector is async"""
entry_point = EntryPoint(CONNECTION, CONFIG)
check_async = entry_point.is_async()
assert check_async

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.ping_data_source', autospec=True)
def test_ping(mock_ping):
@patch('ssl.SSLContext.load_verify_locations')
def test_ping(self, mock_ping):
"""to check the ping status of connector"""
mock_ping.return_value = get_mock_response(200, '{"sessionId":"2"}', 'byte')
transmission = stix_transmission.StixTransmission('arcsight', CONNECTION, CONFIG)
Expand All @@ -43,7 +45,8 @@ def test_ping(mock_ping):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.ping_data_source', autospec=True)
def test_ping_exception(mock_ping):
@patch('ssl.SSLContext.load_verify_locations')
def test_ping_exception(self, mock_ping):
"""to check the ping exception of the connector"""
mock_ping.return_value = get_mock_response(400, '{"errors": [{"code": 1009, "message": "Server session not '
'found"}]}', 'byte')
Expand All @@ -58,7 +61,8 @@ def test_ping_exception(mock_ping):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.ping_data_source', autospec=True)
def test_auth_exception(mock_ping):
@patch('ssl.SSLContext.load_verify_locations')
def test_auth_exception(self, mock_ping):
"""to check auth token generation exception"""
mock_ping.return_value = get_mock_response(503, '{"error": "Unauthorized"}', 'byte')
transmission = stix_transmission.StixTransmission('arcsight', CONNECTION, CONFIG)
Expand All @@ -73,7 +77,8 @@ def test_auth_exception(mock_ping):
@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api', autospec=True)
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_user_session_id')
def test_create_query_connection(mock_session_id, mock_query_res):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_query_connection(self, mock_session_id, mock_query_res):
"""to create the query search and get search id"""
mock_session_id.return_value = 'Dhoup23b3wL7tBlWWIeFPg8JHEf29qD1tNRJba4Jsyg.'
mock_query_res.return_value = get_mock_response(200, '{"sessionId":"2"}', 'byte')
Expand All @@ -90,7 +95,8 @@ def test_create_query_connection(mock_session_id, mock_query_res):
@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api', autospec=True)
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_user_session_id')
def test_create_query_error(mock_session_id, mock_query_res):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_query_error(self, mock_session_id, mock_query_res):
"""query search error check"""
mock_session_id.return_value = 'Dhoup23b3wL7tBlWWIeFPg8JHEf29qD1tNRJba4Jsyg.'
mock_query_res.return_value = get_mock_response(400, '{"errors": [{"code": 1111, '
Expand All @@ -109,7 +115,8 @@ def test_create_query_error(mock_session_id, mock_query_res):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.create_search')
def test_create_query_exception(mock_query_exception):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_query_exception(self, mock_query_exception):
"""query search exception handling"""
mock_query_exception.side_effect = ConnectionError(
"('Connection aborted.', ConnectionResetError(10054, 'An existing "
Expand All @@ -128,7 +135,8 @@ def test_create_query_exception(mock_query_exception):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_results(mock_create_results):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_results(self, mock_create_results):
"""to get the search results"""
response = {
"fields": [{
Expand Down Expand Up @@ -194,7 +202,8 @@ def test_create_results(mock_create_results):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_results_registry(mock_create_results):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_results_registry(self, mock_create_results):
"""to get search result with registry - connector specific"""
response = {
"fields": [{
Expand Down Expand Up @@ -251,7 +260,8 @@ def test_create_results_registry(mock_create_results):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_results_empty(mock_create_results):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_results_empty(self, mock_create_results):
"""to get query results with empty response"""
response = {}
mock_create_results.return_value = get_mock_response(200, json.dumps(response), 'byte')
Expand All @@ -268,7 +278,8 @@ def test_create_results_empty(mock_create_results):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_search_results')
def test_create_results_exception(mock_create_results):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_results_exception(self, mock_create_results):
"""to get http exception in result search"""
mock_create_results.side_effect = ConnectionError(
"('Connection aborted.', ConnectionResetError(10054, 'An existing "
Expand All @@ -287,7 +298,8 @@ def test_create_results_exception(mock_create_results):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_result_error(mock_delete_error):
@patch('ssl.SSLContext.load_verify_locations')
def test_result_error(self, mock_delete_error):
"""to get error in result search"""
error = {"errors": [{'code': 1009, 'message': 'Server session not found'}]}
mock_delete_error.return_value = get_mock_response(400, json.dumps(error), 'byte')
Expand All @@ -302,7 +314,8 @@ def test_result_error(mock_delete_error):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_delete_query_connection(mock_delete_query):
@patch('ssl.SSLContext.load_verify_locations')
def test_delete_query_connection(self, mock_delete_query):
"""to delete the query search using search id"""
mock_delete_query.return_value = get_mock_response(200, "", 'byte')
transmission = stix_transmission.StixTransmission('arcsight', CONNECTION, CONFIG)
Expand All @@ -314,7 +327,8 @@ def test_delete_query_connection(mock_delete_query):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_delete_query_error(mock_delete_error):
@patch('ssl.SSLContext.load_verify_locations')
def test_delete_query_error(self, mock_delete_error):
"""to delete the query with invalid session id - error"""
error = {"errors": [{"code": 1002, "message": "User session BCP7NIkbiLBkXx2FwdkU7ma9O7bJAWng1k. is not valid"}]}
mock_delete_error.return_value = get_mock_response(400, json.dumps(error), 'byte')
Expand All @@ -330,7 +344,8 @@ def test_delete_query_error(mock_delete_error):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.delete_search')
def test_delete_query_exception(mock_delete_exception):
@patch('ssl.SSLContext.load_verify_locations')
def test_delete_query_exception(self, mock_delete_exception):
"""to get http exception when delete the query search"""
mock_delete_exception.side_effect = ConnectionError(
"('Connection aborted.', ConnectionResetError(10054, 'An existing "
Expand All @@ -347,7 +362,8 @@ def test_delete_query_exception(mock_delete_exception):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_status(mock_create_status):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_status(self, mock_create_status):
"""to get search status of the query - COMPLETED"""
response = {'status': 'complete', 'result_type': 'histogram', 'hit': 1004,
'scanned': 1561219, 'elapsed': '00:00:00.530', 'message': []}
Expand All @@ -363,7 +379,8 @@ def test_create_status(mock_create_status):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_status_running(mock_create_status):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_status_running(self, mock_create_status):
"""to get search status of the query - RUNNING"""
response = {'status': 'running', 'result_type': 'histogram', 'hit': 2000,
'scanned': 1561210, 'elapsed': '00:00:00.530', 'message': []}
Expand All @@ -380,7 +397,8 @@ def test_create_status_running(mock_create_status):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_create_status_complete(mock_create_status):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_status_complete(self, mock_create_status):
"""to get search status of the query - COMPLETED"""
response = {'status': 'running', 'result_type': 'histogram', 'hit': 5000,
'scanned': 1561210, 'elapsed': '00:00:00.530', 'message': []}
Expand All @@ -397,7 +415,8 @@ def test_create_status_complete(mock_create_status):

@staticmethod
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_search_status')
def test_create_status_exception(mock_status_exception):
@patch('ssl.SSLContext.load_verify_locations')
def test_create_status_exception(self, mock_status_exception):
"""to get http exception when status check"""
mock_status_exception.side_effect = ConnectionError(
"('Connection aborted.', ConnectionResetError(10054, 'An existing "
Expand All @@ -412,7 +431,8 @@ def test_create_status_exception(mock_status_exception):

@staticmethod
@patch('stix_shifter_utils.stix_transmission.utils.RestApiClientAsync.RestApiClientAsync.call_api')
def test_status_error(mock_delete_error):
@patch('ssl.SSLContext.load_verify_locations')
def test_status_error(self, mock_delete_error):
"""to get error when check with invalid user session id"""
error = {"errors": [{"code": 1002, "message": "User session BCP7NIkbiLBkXx2FwdkU7ma9O7bJAWng1k. is not valid"}]}
mock_delete_error.return_value = get_mock_response(400, json.dumps(error), 'byte')
Expand All @@ -429,7 +449,8 @@ def test_status_error(mock_delete_error):
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.delete_search')
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_search_status')
@patch('stix_shifter_modules.arcsight.stix_transmission.api_client.APIClient.get_search_results')
def test_arcsight_logger_down(mock_results, mock_status, mock_delete):
@patch('ssl.SSLContext.load_verify_locations')
def test_arcsight_logger_down(self, mock_results, mock_status, mock_delete):
"""arcsight logger down error"""
error = 'The application is currently unavailable. Please retry shortly.'
mock_results.return_value = get_mock_response(503, error, 'byte')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def init_async_client(self):
self.client = RestApiClientAsync(self.host,
self.connection.get('port', None),
headers,
cert_verify=self.connection.get('selfSignedCert', True)
cert_verify=self.connection.get('selfSignedCert')
)

async def ping_box(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, connection, configuration):
self.client = RestApiClientAsync(connection.get('host'),
connection.get('port'),
headers,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)
self.timeout = connection['options'].get('timeout')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, connection, configuration):
connection.get('host'),
connection.get('port'),
headers,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)
self.timeout = connection['options'].get('timeout')
self.result_limit = connection['options'].get('result_limit')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, connection, configuration):
self.auth = configuration.get('auth')
self.headers = {'Content-Type': 'application/json'}
self.client = RestApiClientAsync(connection.get('host'), connection.get('port'), headers=self.headers,
cert_verify=connection.get('selfSignedCert', True))
cert_verify=connection.get('selfSignedCert'))
self.result_limit = connection['options'].get('result_limit')
self.timeout = connection['options'].get('timeout')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def __init__(self, connection, configuration):
self.client = RestApiClientAsync(connection.get('host'),
connection.get('port', None),
headers,
url_modifier_function=url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
url_modifier_function=url_modifier_function
)
self.timeout = connection['options'].get('timeout')
self._client_id = auth['client_id']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, connection, configuration):
connection.get('port'),
headers,
url_modifier_function=url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)

self.timeout = connection['options'].get('timeout')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_results_response_exception(self, mock_results_response):
transmission = stix_transmission.StixTransmission('elastic_ecs', connection, config)
results_response = transmission.results(search_id, offset, length)

assert results_response['code'] == 'unknown'
assert results_response['code'] == 'certificate_fail'
assert results_response['success'] is False


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, connection, configuration):
None,
headers,
url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)

async def ping_data_source(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, connection, configuration):
headers['Authorization'] = "Basic %s" % token.decode('ascii')
self.client = RestApiClientAsync(connection.get('host'),
port=None,
headers=headers, url_modifier_function=None, cert_verify=True, auth=None
headers=headers, url_modifier_function=None, auth=None
)

async def ping_data_source(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, connection, configuration):
None,
headers,
url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)

def add_endpoint_to_url_header(self, url, endpoint, headers):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, connection, configuration):
connection.get('port'),
self.headers,
url_modifier_function=url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)
self.timeout = connection['options'].get('timeout')
self.app_id = auth['app_id']
Expand Down
2 changes: 1 addition & 1 deletion stix_shifter_modules/rhacs/stix_transmission/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, connection, configuration):
connection.get('port', None),
headers,
url_modifier_function=url_modifier_function,
cert_verify=connection.get('selfSignedCert', True)
cert_verify=connection.get('selfSignedCert')
)
self.timeout = connection['options'].get('timeout')

Expand Down
Loading
Loading