Skip to content

Test Case for check inspector_ec2_scan_enabled#262

Open
Gunjan-Katre-Comprinno wants to merge 2 commits intocomprinnotech:testcases_devfrom
Gunjan-Katre-Comprinno:test_inspector_ec2_scan_enabled
Open

Test Case for check inspector_ec2_scan_enabled#262
Gunjan-Katre-Comprinno wants to merge 2 commits intocomprinnotech:testcases_devfrom
Gunjan-Katre-Comprinno:test_inspector_ec2_scan_enabled

Conversation

@Gunjan-Katre-Comprinno
Copy link

Certainly! Here's the test description for TestInspectorEC2ScanEnabled in the format you requested:


Context

This change adds a unit test for the inspector_ec2_scan_enabled check, which validates whether Amazon Inspector2 EC2 standard scanning is enabled for the account. This aligns with AWS security best practices and supports automated detection of vulnerable EC2 instances.
Fixes potential oversight in test coverage for Inspector2 EC2 resource scanning status.


Description

The test suite TestInspectorEC2ScanEnabled includes the following scenarios:

  • EC2 scan status is ENABLED → check passes.
  • EC2 scan status is DISABLED → check fails.
  • EC2 scan status is SUSPENDED → check fails.
  • EC2 scan status is TRANSITIONING → check returns unknown.
  • API failure while fetching Inspector account status → check returns unknown with appropriate error message.

Dependencies: Mocked AWS clients (inspector2, sts) using unittest.mock. No external AWS access is required.


Checklist


License

I confirm that my contribution is made under the terms of the Apache 2.0 license.



report = self.check.execute(self.mock_session)

assert report.status == CheckStatus.UNKNOWN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠 Areas for Improvement

⚠️ 1. Missing a Case Where No ec2 Key Exists in resourceState

In real-world scenarios, it’s possible that the ec2 key is not present at all (e.g., misconfigured Inspector2 response or service not onboarded yet).

Recommendation:
Add a test like this:

def test_ec2_scan_status_missing(self):
    self.mock_inspector_client.batch_get_account_status.return_value = {
        "accounts": [{}]
    }

    report = self.check.execute(self.mock_session)
    assert report.status == CheckStatus.UNKNOWN
    assert any("transitional" in r.summary.lower() or "unknown" in r.status.name.lower()
               for r in report.resource_ids_status)

This would ensure the check handles partial or malformed responses gracefully.


⚠️ 2. ClientError Not Explicitly Tested

While you do simulate a generic Exception, testing for AWS-specific ClientError would align with how other AWS SDKs (like IAM or S3) often fail.

Why it matters: It helps ensure your except Exception block is robust enough to catch and format structured errors cleanly.

Suggestion:

from botocore.exceptions import ClientError

def test_client_error_handling(self):
    self.mock_inspector_client.batch_get_account_status.side_effect = ClientError(
        error_response={"Error": {"Code": "AccessDenied", "Message": "Access denied"}},
        operation_name="BatchGetAccountStatus"
    )
    report = self.check.execute(self.mock_session)
    assert report.status == CheckStatus.UNKNOWN
    assert any("access denied" in r.summary.lower() for r in report.resource_ids_status)

⚠️ 3. Account ID Not Used in Summary

The check fetches account_id from STS, but that value is never surfaced in the summary. Including it might help users correlate findings, especially in org-level scanning setups.

Not mandatory, but worth considering for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants