|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from tests.providers.gcp.gcp_fixtures import ( |
| 4 | + GCP_PROJECT_ID, |
| 5 | + GCP_US_CENTER1_LOCATION, |
| 6 | + set_mocked_gcp_provider, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class TestComputeInstanceDeletionProtectionEnabled: |
| 11 | + def test_compute_no_instances(self): |
| 12 | + compute_client = mock.MagicMock() |
| 13 | + compute_client.instances = [] |
| 14 | + |
| 15 | + with ( |
| 16 | + mock.patch( |
| 17 | + "prowler.providers.common.provider.Provider.get_global_provider", |
| 18 | + return_value=set_mocked_gcp_provider(), |
| 19 | + ), |
| 20 | + mock.patch( |
| 21 | + "prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled.compute_client", |
| 22 | + new=compute_client, |
| 23 | + ), |
| 24 | + ): |
| 25 | + from prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled import ( |
| 26 | + compute_instance_deletion_protection_enabled, |
| 27 | + ) |
| 28 | + |
| 29 | + check = compute_instance_deletion_protection_enabled() |
| 30 | + result = check.execute() |
| 31 | + assert len(result) == 0 |
| 32 | + |
| 33 | + def test_instance_deletion_protection_enabled(self): |
| 34 | + compute_client = mock.MagicMock() |
| 35 | + |
| 36 | + with ( |
| 37 | + mock.patch( |
| 38 | + "prowler.providers.common.provider.Provider.get_global_provider", |
| 39 | + return_value=set_mocked_gcp_provider(), |
| 40 | + ), |
| 41 | + mock.patch( |
| 42 | + "prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled.compute_client", |
| 43 | + new=compute_client, |
| 44 | + ), |
| 45 | + ): |
| 46 | + from prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled import ( |
| 47 | + compute_instance_deletion_protection_enabled, |
| 48 | + ) |
| 49 | + from prowler.providers.gcp.services.compute.compute_service import Instance |
| 50 | + |
| 51 | + compute_client.project_ids = [GCP_PROJECT_ID] |
| 52 | + compute_client.region = GCP_US_CENTER1_LOCATION |
| 53 | + |
| 54 | + compute_client.instances = [ |
| 55 | + Instance( |
| 56 | + name="test-instance", |
| 57 | + id="1234567890", |
| 58 | + zone=f"{GCP_US_CENTER1_LOCATION}-a", |
| 59 | + region=GCP_US_CENTER1_LOCATION, |
| 60 | + public_ip=False, |
| 61 | + metadata={}, |
| 62 | + shielded_enabled_vtpm=True, |
| 63 | + shielded_enabled_integrity_monitoring=True, |
| 64 | + confidential_computing=False, |
| 65 | + service_accounts=[ |
| 66 | + {"email": "123-compute@developer.gserviceaccount.com"} |
| 67 | + ], |
| 68 | + ip_forward=False, |
| 69 | + disks_encryption=[], |
| 70 | + project_id=GCP_PROJECT_ID, |
| 71 | + deletion_protection=True, |
| 72 | + ) |
| 73 | + ] |
| 74 | + |
| 75 | + check = compute_instance_deletion_protection_enabled() |
| 76 | + result = check.execute() |
| 77 | + |
| 78 | + assert len(result) == 1 |
| 79 | + assert result[0].status == "PASS" |
| 80 | + assert ( |
| 81 | + result[0].status_extended |
| 82 | + == f"VM Instance {compute_client.instances[0].name} has deletion protection enabled." |
| 83 | + ) |
| 84 | + assert result[0].resource_id == "1234567890" |
| 85 | + assert result[0].resource_name == "test-instance" |
| 86 | + assert result[0].location == GCP_US_CENTER1_LOCATION |
| 87 | + assert result[0].project_id == GCP_PROJECT_ID |
| 88 | + |
| 89 | + def test_instance_deletion_protection_disabled(self): |
| 90 | + compute_client = mock.MagicMock() |
| 91 | + |
| 92 | + with ( |
| 93 | + mock.patch( |
| 94 | + "prowler.providers.common.provider.Provider.get_global_provider", |
| 95 | + return_value=set_mocked_gcp_provider(), |
| 96 | + ), |
| 97 | + mock.patch( |
| 98 | + "prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled.compute_client", |
| 99 | + new=compute_client, |
| 100 | + ), |
| 101 | + ): |
| 102 | + from prowler.providers.gcp.services.compute.compute_instance_deletion_protection_enabled.compute_instance_deletion_protection_enabled import ( |
| 103 | + compute_instance_deletion_protection_enabled, |
| 104 | + ) |
| 105 | + from prowler.providers.gcp.services.compute.compute_service import Instance |
| 106 | + |
| 107 | + compute_client.project_ids = [GCP_PROJECT_ID] |
| 108 | + compute_client.region = GCP_US_CENTER1_LOCATION |
| 109 | + |
| 110 | + compute_client.instances = [ |
| 111 | + Instance( |
| 112 | + name="test-instance", |
| 113 | + id="1234567890", |
| 114 | + zone=f"{GCP_US_CENTER1_LOCATION}-a", |
| 115 | + region=GCP_US_CENTER1_LOCATION, |
| 116 | + public_ip=False, |
| 117 | + metadata={}, |
| 118 | + shielded_enabled_vtpm=True, |
| 119 | + shielded_enabled_integrity_monitoring=True, |
| 120 | + confidential_computing=False, |
| 121 | + service_accounts=[ |
| 122 | + { |
| 123 | + "email": f"{GCP_PROJECT_ID}-compute@developer.gserviceaccount.com" |
| 124 | + } |
| 125 | + ], |
| 126 | + ip_forward=False, |
| 127 | + disks_encryption=[], |
| 128 | + project_id=GCP_PROJECT_ID, |
| 129 | + deletion_protection=False, |
| 130 | + ) |
| 131 | + ] |
| 132 | + |
| 133 | + check = compute_instance_deletion_protection_enabled() |
| 134 | + result = check.execute() |
| 135 | + |
| 136 | + assert len(result) == 1 |
| 137 | + assert result[0].status == "FAIL" |
| 138 | + assert ( |
| 139 | + result[0].status_extended |
| 140 | + == f"VM Instance {compute_client.instances[0].name} does not have deletion protection enabled." |
| 141 | + ) |
| 142 | + assert result[0].resource_id == "1234567890" |
| 143 | + assert result[0].resource_name == "test-instance" |
| 144 | + assert result[0].location == GCP_US_CENTER1_LOCATION |
| 145 | + assert result[0].project_id == GCP_PROJECT_ID |
0 commit comments