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
Changes from 3 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
19 changes: 13 additions & 6 deletions stix_shifter_utils/stix_transmission/utils/RestApiClientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@

self.server_cert_file_content = None
self.ssl_context = False

print('cert_verify: ' + str(cert_verify))
Fixed Show fixed Hide fixed
if isinstance(cert_verify, bool):
# verify certificate non self signed case
if cert_verify:
self.ssl_context = True
# self signed cert provided
elif isinstance(cert_verify, str):
self.server_cert_file_content = cert_verify
self.ssl_context = True
print('self.ssl_context : ' + str(self.ssl_context))
if self.ssl_context:
self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
self.ssl_context.verify_mode = ssl.CERT_REQUIRED
self.ssl_context.check_hostname = True

self.headers = headers
self.url_modifier_function = url_modifier_function
Expand All @@ -86,11 +92,12 @@
f.write(self.server_cert_file_content)
except IOError:
self.logger.error('Failed to setup certificate')

self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
self.ssl_context.load_verify_locations(self.server_cert_name)
self.ssl_context.verify_mode = ssl.CERT_REQUIRED
self.ssl_context.check_hostname = True

if self.ssl_context and self.server_cert_file_content:
try:
self.ssl_context.load_verify_locations(self.server_cert_name)
except Exception as ex:
self.logger.debug('Unable to load the certificate for ssl context. Reasons: Connection does not require certificate or unexpected exception while loading the certificate: ' + str(ex))

url = None
actual_headers = self.headers.copy()
Expand Down
Loading