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

Safe Connections Report unit tests #2515

Merged
merged 8 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -40,7 +40,6 @@ def generate_data(self, input_ooi: str, valid_time: datetime) -> Dict[str, Any]:
sc_ips = {}
number_of_ips = len(ips)
number_of_available = number_of_ips
finding_types = []

for ip in ips:
finding_types = self.octopoes_api_connector.query(
Expand All @@ -51,8 +50,6 @@ def generate_data(self, input_ooi: str, valid_time: datetime) -> Dict[str, Any]:
)

cipher_findings = list(filter(lambda finding: finding.id in CIPHER_FINDINGS, finding_types))
finding_types.extend(cipher_findings)

sc_ips[ip.reference] = cipher_findings
number_of_available -= 1 if cipher_findings else 0

Expand All @@ -61,5 +58,4 @@ def generate_data(self, input_ooi: str, valid_time: datetime) -> Dict[str, Any]:
"sc_ips": sc_ips,
"number_of_available": number_of_available,
"number_of_ips": number_of_ips,
"finding_types": finding_types,
}
31 changes: 31 additions & 0 deletions rocky/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,37 @@ def finding_types():
]


@pytest.fixture
def cipher_finding_types():
return [
KATFindingType(
id="KAT-RECOMMENDATION-BAD-CIPHER",
description="Fake description...",
recommendation="Fake recommendation...",
risk_score=3.0,
risk_severity=RiskLevelSeverity.RECOMMENDATION,
),
KATFindingType(
madelondohmen marked this conversation as resolved.
Show resolved Hide resolved
id="KAT-CRITICAL-BAD-CIPHER",
description="Fake description...",
recommendation="Fake recommendation...",
risk_score=10.0,
risk_severity=RiskLevelSeverity.CRITICAL,
),
]


@pytest.fixture
def cipher_finding_type():
return KATFindingType(
id="KAT-MEDIUM-BAD-CIPHER",
description="Fake description...",
recommendation="Fake recommendation...",
risk_score=6.0,
risk_severity=RiskLevelSeverity.MEDIUM,
)


@pytest.fixture
def plugin_details():
return parse_plugin(
Expand Down
86 changes: 86 additions & 0 deletions rocky/tests/reports/test_safe_connections_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from reports.report_types.safe_connections_report.report import SafeConnectionsReport


def test_safe_connections_report_no_finding_types(mock_octopoes_api_connector, valid_time, hostname):
mock_octopoes_api_connector.oois = {
hostname.reference: hostname,
}
mock_octopoes_api_connector.queries = {
"Hostname.<hostname[is ResolvedHostname].address": {
hostname.reference: [],
},
}

report = SafeConnectionsReport(mock_octopoes_api_connector)

data = report.generate_data(str(hostname.reference), valid_time)

assert data["sc_ips"] == {}
assert data["number_of_available"] == 0
assert data["number_of_ips"] == 0


def test_safe_connections_report_single_cipher_finding_type(
mock_octopoes_api_connector, valid_time, ipaddressv4, cipher_finding_type, finding_types
):
mock_octopoes_api_connector.oois = {
ipaddressv4.reference: ipaddressv4,
}
mock_octopoes_api_connector.queries = {
"IPAddress.<address[is IPPort].<ip_port [is IPService]"
".<ip_service [is TLSCipher].<ooi[is Finding].finding_type": {
ipaddressv4.reference: [cipher_finding_type, finding_types[0]],
},
}

report = SafeConnectionsReport(mock_octopoes_api_connector)

data = report.generate_data(str(ipaddressv4.reference), valid_time)

assert data["sc_ips"][ipaddressv4.reference] == [cipher_finding_type]
assert data["number_of_available"] == 0
assert data["number_of_ips"] == 1


def test_safe_connections_report_multiple_cipher_finding_types(
mock_octopoes_api_connector, valid_time, ipaddressv4, cipher_finding_types, finding_types
):
mock_octopoes_api_connector.oois = {
ipaddressv4.reference: ipaddressv4,
}
mock_octopoes_api_connector.queries = {
"IPAddress.<address[is IPPort].<ip_port [is IPService]"
".<ip_service [is TLSCipher].<ooi[is Finding].finding_type": {
ipaddressv4.reference: cipher_finding_types + finding_types,
},
}

report = SafeConnectionsReport(mock_octopoes_api_connector)

data = report.generate_data(str(ipaddressv4.reference), valid_time)

assert data["sc_ips"][ipaddressv4.reference] == cipher_finding_types
assert data["number_of_available"] == 0
assert data["number_of_ips"] == 1


def test_safe_connections_report_no_cipher_finding_types(
mock_octopoes_api_connector, valid_time, ipaddressv4, finding_types
):
mock_octopoes_api_connector.oois = {
ipaddressv4.reference: ipaddressv4,
}
mock_octopoes_api_connector.queries = {
"IPAddress.<address[is IPPort].<ip_port [is IPService]"
".<ip_service [is TLSCipher].<ooi[is Finding].finding_type": {
ipaddressv4.reference: finding_types,
},
}

report = SafeConnectionsReport(mock_octopoes_api_connector)

data = report.generate_data(str(ipaddressv4.reference), valid_time)

assert data["sc_ips"][ipaddressv4.reference] == []
assert data["number_of_available"] == 1
assert data["number_of_ips"] == 1