Skip to content

Commit

Permalink
Fix tests, warnings and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nabla-c0d3 committed Jan 3, 2025
1 parent 7112e3d commit 2cda80c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
6 changes: 3 additions & 3 deletions api_sample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import List

Expand All @@ -25,7 +25,7 @@ def _print_failed_scan_command_attempt(scan_command_attempt: ScanCommandAttempt)

def main() -> None:
print("=> Starting the scans")
date_scans_started = datetime.utcnow()
date_scans_started = datetime.now(timezone.utc)

# First create the scan requests for each server that we want to scan
try:
Expand Down Expand Up @@ -104,7 +104,7 @@ def main() -> None:
# Lastly, save the all the results to a JSON file
json_file_out = Path("api_sample_results.json")
print(f"\n\n=> Saving scan results to {json_file_out}")
example_json_result_output(json_file_out, all_server_scan_results, date_scans_started, datetime.utcnow())
example_json_result_output(json_file_out, all_server_scan_results, date_scans_started, datetime.now(timezone.utc))

# And ensure we are able to parse them
print(f"\n\n=> Parsing scan results from {json_file_out}")
Expand Down
6 changes: 3 additions & 3 deletions sslyze/cli/console_output.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import fields
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import TextIO, Optional

Expand All @@ -23,7 +23,7 @@
class ObserverToGenerateConsoleOutput(ScannerObserver):
def __init__(self, file_to: TextIO, json_path_out: Optional[Path] = None) -> None:
self._file_to = file_to
self._date_scans_started = datetime.utcnow()
self._date_scans_started = datetime.now(timezone.utc)

# Used to print the path where the JSON output was written
self._json_path_out = json_path_out
Expand Down Expand Up @@ -101,7 +101,7 @@ def server_scan_completed(self, server_scan_result: ServerScanResult) -> None:
self._file_to.write("\n\n" + self._format_title(scan_txt) + scan_command_results_str)

def all_server_scans_completed(self) -> None:
scans_duration = datetime.utcnow() - self._date_scans_started
scans_duration = datetime.now(timezone.utc) - self._date_scans_started
self._file_to.write("\n")
self._file_to.write(
self._format_title(f"Scans Completed in {scans_duration.seconds}.{scans_duration.microseconds} s")
Expand Down
2 changes: 1 addition & 1 deletion sslyze/connection_helpers/tls_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _open_socket(server_location: ServerNetworkLocation, network_timeout: int) -
# enabled in the client; for example client only supports EC cipher suites but server returned an RSA certificate
"wrong certificate type": "Server returned wrong certificate type",
"invalid encoding": "TLS error: Invalid encoding",
"certificate unknown": "TLS alert: certificate unknown"
"certificate unknown": "TLS alert: certificate unknown",
}


Expand Down
18 changes: 9 additions & 9 deletions tests/json_tests/test_json_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path

from sslyze.json.json_output import SslyzeOutputAsJson, ServerScanResultAsJson
Expand All @@ -21,8 +21,8 @@ def test(self):
json_output = SslyzeOutputAsJson(
server_scan_results=[ServerScanResultAsJson.model_validate(result) for result in all_server_scan_results],
invalid_server_strings=[],
date_scans_started=datetime.utcnow(),
date_scans_completed=datetime.utcnow(),
date_scans_started=datetime.now(timezone.utc),
date_scans_completed=datetime.now(timezone.utc),
)
json_output_as_str = json_output.model_dump_json()
assert json_output_as_str
Expand All @@ -38,8 +38,8 @@ def test_connectivity_test_failed(self):
json_output = SslyzeOutputAsJson(
server_scan_results=[ServerScanResultAsJson.model_validate(server_scan_result)],
invalid_server_strings=[],
date_scans_started=datetime.utcnow(),
date_scans_completed=datetime.utcnow(),
date_scans_started=datetime.now(timezone.utc),
date_scans_completed=datetime.now(timezone.utc),
)
json_output_as_str = json_output.model_dump_json()
assert json_output_as_str
Expand All @@ -63,8 +63,8 @@ def test_server_scan_completed_scan_command(self):
json_output = SslyzeOutputAsJson(
server_scan_results=[ServerScanResultAsJson.model_validate(server_scan_result)],
invalid_server_strings=[],
date_scans_started=datetime.utcnow(),
date_scans_completed=datetime.utcnow(),
date_scans_started=datetime.now(timezone.utc),
date_scans_completed=datetime.now(timezone.utc),
)
json_output_as_str = json_output.model_dump_json()
assert json_output_as_str
Expand All @@ -91,8 +91,8 @@ def test_server_scan_completed_but_scan_command_returned_error(self):
json_output = SslyzeOutputAsJson(
server_scan_results=[ServerScanResultAsJson.model_validate(server_scan_result)],
invalid_server_strings=[],
date_scans_started=datetime.utcnow(),
date_scans_completed=datetime.utcnow(),
date_scans_started=datetime.now(timezone.utc),
date_scans_completed=datetime.now(timezone.utc),
)
json_output_as_str = json_output.model_dump_json()
assert json_output_as_str
Expand Down
1 change: 1 addition & 0 deletions tests/plugins_tests/test_http_headers_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_works_when_client_auth_succeeded(self) -> None:
class _MockHttpResponse(HTTPResponse):
status: int
_headers: Dict[str, str]
fp = None

def getheader(self, name: str, default=None):
"""Replicate HTTPResponse's API."""
Expand Down
17 changes: 12 additions & 5 deletions tests/test_mozilla_tls_profile/test_mozilla_config_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,24 @@ def test_badssl_compliant_with_modern(self):
with pytest.raises(ServerNotCompliantWithMozillaTlsConfiguration):
checker.check_server(against_config=mozilla_config, server_scan_result=server_scan_result)

def test_multi_certs_deployment_compliant_with_old(self, server_scan_result_for_google):
# Give the scan results for google.com which has multiple leaf certificates
# When checking if the server is compliant with the Mozilla "old" TLS config
checker = MozillaTlsConfigurationChecker.get_default()
def test_solo_cert_deployment_compliant_with_old(self):
# Given the scan results for a server that is compliant with the "old" Mozilla config
scanner = Scanner()
scanner.queue_scans([ServerScanRequest(server_location=ServerNetworkLocation(hostname="www.mozilla.com"))])
server_scan_result = next(scanner.get_results())

# When checking if the server is compliant with the Mozilla "old" TLS config
# It succeeds and the server is returned as compliant
checker = MozillaTlsConfigurationChecker.get_default()
checker.check_server(
against_config=MozillaTlsConfigurationEnum.OLD,
server_scan_result=server_scan_result_for_google,
server_scan_result=server_scan_result,
)

def test_multi_certs_deployment_compliant_with_old(self):
# TODO(AD): Implement this test
pass

def test_multi_certs_deployment_not_compliant_with_intermediate(self, server_scan_result_for_google):
# Give the scan results for google.com which has multiple leaf certificates
# When checking if the server is compliant with the Mozilla "intermediate" TLS config
Expand Down
6 changes: 3 additions & 3 deletions tests/web_servers/scan_localhost.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

import sys
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum

from sslyze import (
Expand All @@ -36,7 +36,7 @@ class WebServerSoftwareEnum(str, Enum):
def main(server_software_running_on_localhost: WebServerSoftwareEnum) -> None:
# Queue all scan commands against a server running on localhost
print("Starting scan.")
date_scans_started = datetime.utcnow()
date_scans_started = datetime.now(timezone.utc)
scanner = Scanner()
scanner.queue_scans([ServerScanRequest(server_location=ServerNetworkLocation("localhost", 443))])

Expand Down Expand Up @@ -177,7 +177,7 @@ def main(server_software_running_on_localhost: WebServerSoftwareEnum) -> None:
final_json_output = SslyzeOutputAsJson(
server_scan_results=[ServerScanResultAsJson.model_validate(server_scan_result)],
date_scans_started=date_scans_started,
date_scans_completed=datetime.utcnow(),
date_scans_completed=datetime.now(timezone.utc),
invalid_server_strings=[],
)
final_json_output.model_dump_json()
Expand Down

0 comments on commit 2cda80c

Please sign in to comment.