98 lines
4.4 KiB
Python
98 lines
4.4 KiB
Python
"""Credential containment is restricted to disposable laboratory identities."""
|
|
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import unittest
|
|
from unittest.mock import Mock, patch
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
|
class RevokeCredentialsScopeTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.clients = {name: Mock() for name in ("s3", "sns", "iam", "ses")}
|
|
env = {
|
|
"EVIDENCE_BUCKET": "test-evidence",
|
|
"SNS_ALERTS_ARN": "test-alerts",
|
|
"ALERT_EMAIL": "",
|
|
"PROTECTED_IAM_USERS": "admin-tesis,tesis-ir-temp-recovery",
|
|
}
|
|
path = Path(__file__).resolve().parents[1] / (
|
|
"terraform-tesis-ir-secure-portal/playbooks/revoke_credentials/handler.py"
|
|
)
|
|
with patch.dict(os.environ, env), patch("boto3.client", side_effect=self.clients.get):
|
|
spec = importlib.util.spec_from_file_location("revoke_scope", path)
|
|
self.handler = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(self.handler)
|
|
self.clients["s3"].get_object.return_value = {"Body": io.BytesIO(b"[]")}
|
|
self.clients["s3"].exceptions.NoSuchKey = type("NoSuchKey", (ClientError,), {})
|
|
self.clients["iam"].list_access_keys.return_value = {
|
|
"AccessKeyMetadata": [{"AccessKeyId": "demo-key-id", "Status": "Active"}]
|
|
}
|
|
|
|
def invoke(self, username, incident_type="CREDENTIAL_LEAK"):
|
|
return self.handler.lambda_handler({"Records": [{"body": json.dumps({
|
|
"incident_type": incident_type,
|
|
"affectedResource": "iam:user/" + username,
|
|
"sourceIPAddress": "198.51.100.20",
|
|
"incident_id": "scope-regression",
|
|
})}]}, None)
|
|
|
|
def assert_no_side_effects(self):
|
|
for client in self.clients.values():
|
|
self.assertEqual(client.mock_calls, [])
|
|
|
|
def test_rejects_non_lab_users_without_side_effects(self):
|
|
for incident in ("CREDENTIAL_LEAK", "PRIVILEGE_ESCALATION"):
|
|
for username in ("production-user", "tesis-ir-temp-", "tesis-ir-temp-user/path"):
|
|
with self.subTest(incident=incident, username=username):
|
|
result = self.invoke(username, incident)
|
|
self.assertEqual(result["results"], [{
|
|
"status": "skipped", "reason": "outside_lab_scope",
|
|
}])
|
|
self.assert_no_side_effects()
|
|
|
|
def test_rejects_protected_users_without_side_effects(self):
|
|
for username in ("admin-tesis", "tesis-ir-temp-recovery"):
|
|
with self.subTest(username=username):
|
|
result = self.invoke(username)
|
|
self.assertEqual(result["results"], [{
|
|
"status": "skipped", "reason": "protected_user",
|
|
}])
|
|
self.assert_no_side_effects()
|
|
|
|
def test_missing_audit_object_starts_new_history(self):
|
|
self.clients["s3"].get_object.side_effect = self.clients["s3"].exceptions.NoSuchKey(
|
|
{"Error": {"Code": "NoSuchKey"}}, "GetObject"
|
|
)
|
|
result = self.invoke("tesis-ir-temp-regression")
|
|
self.assertEqual(result["results"][0]["action"], "CONTAINED")
|
|
self.assertEqual(self.clients["s3"].put_object.call_count, 2)
|
|
|
|
def test_audit_read_failures_do_not_overwrite_history(self):
|
|
failures = (
|
|
ClientError({"Error": {"Code": "AccessDenied"}}, "GetObject"),
|
|
json.JSONDecodeError("corrupt history", "{", 0),
|
|
)
|
|
for failure in failures:
|
|
with self.subTest(failure=type(failure).__name__):
|
|
self.clients["s3"].get_object.side_effect = failure
|
|
with self.assertRaises(type(failure)):
|
|
self.invoke("tesis-ir-temp-regression")
|
|
self.clients["s3"].put_object.assert_not_called()
|
|
self.clients["sns"].publish.assert_not_called()
|
|
|
|
def test_disposable_user_preserves_containment_and_audit(self):
|
|
result = self.invoke("tesis-ir-temp-actor")
|
|
self.assertEqual(result["results"][0]["action"], "CONTAINED")
|
|
iam = self.clients["iam"]
|
|
iam.update_access_key.assert_called_once_with(
|
|
UserName="tesis-ir-temp-actor", AccessKeyId="demo-key-id", Status="Inactive",
|
|
)
|
|
self.assertEqual(iam.put_user_policy.call_args.kwargs["PolicyName"], "EmergencyDenyAll")
|
|
self.assertEqual(self.clients["s3"].put_object.call_count, 2)
|
|
self.clients["sns"].publish.assert_called_once()
|