Skip to content

Commit dc852b4

Browse files
authored
feat(gcp-compute): add automatic restart check for VM instances (#9271)
1 parent 1250f58 commit dc852b4

File tree

9 files changed

+423
-0
lines changed

9 files changed

+423
-0
lines changed

prowler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to the **Prowler SDK** are documented in this file.
77
### Added
88
- `cloudstorage_uses_vpc_service_controls` check for GCP provider [(#9256)](https://github.com/prowler-cloud/prowler/pull/9256)
99
- `repository_immutable_releases_enabled` check for GitHub provider [(#9162)](https://github.com/prowler-cloud/prowler/pull/9162)
10+
- `compute_instance_automatic_restart_enabled` check for GCP provider [(#9271)](https://github.com/prowler-cloud/prowler/pull/9271)
1011

1112
---
1213

prowler/providers/gcp/services/compute/compute_instance_automatic_restart_enabled/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Provider": "gcp",
3+
"CheckID": "compute_instance_automatic_restart_enabled",
4+
"CheckTitle": "Compute Engine VM instances have Automatic Restart enabled",
5+
"CheckType": [],
6+
"ServiceName": "compute",
7+
"SubServiceName": "",
8+
"ResourceIdTemplate": "",
9+
"Severity": "medium",
10+
"ResourceType": "compute.googleapis.com/Instance",
11+
"Description": "**Google Compute Engine virtual machine instances** are evaluated to ensure that **Automatic Restart** is enabled. This feature allows the Google Cloud Compute Engine service to automatically restart VM instances when they are terminated due to non-user-initiated reasons such as maintenance events, hardware failures, or software failures.",
12+
"Risk": "VM instances without Automatic Restart enabled will not recover automatically from host maintenance events or unexpected failures, potentially leading to prolonged service downtime and requiring manual intervention to restore services.",
13+
"RelatedUrl": "",
14+
"AdditionalURLs": [
15+
"https://www.trendmicro.com/cloudoneconformity/knowledge-base/gcp/ComputeEngine/enable-automatic-restart.html",
16+
"https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options"
17+
],
18+
"Remediation": {
19+
"Code": {
20+
"CLI": "gcloud compute instances update <INSTANCE_NAME> --restart-on-failure --zone=<ZONE>",
21+
"NativeIaC": "",
22+
"Other": "1) Open Google Cloud Console → Compute Engine → VM instances\n2) Click on the instance name to view details\n3) Click 'Edit' at the top of the page\n4) Under 'Availability policies', set 'Automatic restart' to 'On (recommended)'\n5) Click 'Save' at the bottom of the page",
23+
"Terraform": "```hcl\n# Example: enable Automatic Restart for a Compute Engine VM instance\nresource \"google_compute_instance\" \"example\" {\n name = var.instance_name\n machine_type = var.machine_type\n zone = var.zone\n\n scheduling {\n automatic_restart = true\n on_host_maintenance = \"MIGRATE\"\n }\n}\n```"
24+
},
25+
"Recommendation": {
26+
"Text": "Enable the Automatic Restart feature for Compute Engine VM instances to enhance system reliability by automatically recovering from crashes or system-initiated terminations. This setting does not interfere with user-initiated shutdowns or stops.",
27+
"Url": "https://hub.prowler.com/check/compute_instance_automatic_restart_enabled"
28+
}
29+
},
30+
"Categories": [
31+
"resilience"
32+
],
33+
"DependsOn": [],
34+
"RelatedTo": [],
35+
"Notes": "VM instances missing the 'scheduling.automaticRestart' field are treated as having Automatic Restart enabled (defaults to true). Preemptible instances and instances with provisioning model set to SPOT are automatically marked as PASS, as they cannot have Automatic Restart enabled by design."
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from prowler.lib.check.models import Check, Check_Report_GCP
2+
from prowler.providers.gcp.services.compute.compute_client import compute_client
3+
4+
5+
class compute_instance_automatic_restart_enabled(Check):
6+
"""
7+
Ensure Compute Engine VM instances have Automatic Restart enabled.
8+
9+
Reports PASS if a VM instance has automatic restart enabled, otherwise FAIL.
10+
"""
11+
12+
def execute(self) -> list[Check_Report_GCP]:
13+
findings = []
14+
for instance in compute_client.instances:
15+
report = Check_Report_GCP(metadata=self.metadata(), resource=instance)
16+
17+
# Preemptible and Spot VMs cannot have automatic restart enabled
18+
if instance.preemptible or instance.provisioning_model == "SPOT":
19+
report.status = "FAIL"
20+
report.status_extended = (
21+
f"VM Instance {instance.name} is a Preemptible or Spot instance, "
22+
"which cannot have Automatic Restart enabled by design."
23+
)
24+
elif instance.automatic_restart:
25+
report.status = "PASS"
26+
report.status_extended = (
27+
f"VM Instance {instance.name} has Automatic Restart enabled."
28+
)
29+
else:
30+
report.status = "FAIL"
31+
report.status_extended = f"VM Instance {instance.name} does not have Automatic Restart enabled."
32+
33+
findings.append(report)
34+
35+
return findings

prowler/providers/gcp/services/compute/compute_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def _get_instances(self, zone):
133133
)
134134
for disk in instance.get("disks", [])
135135
],
136+
automatic_restart=instance.get("scheduling", {}).get(
137+
"automaticRestart", False
138+
),
139+
preemptible=instance.get("scheduling", {}).get(
140+
"preemptible", False
141+
),
142+
provisioning_model=instance.get("scheduling", {}).get(
143+
"provisioningModel", "STANDARD"
144+
),
136145
project_id=project_id,
137146
)
138147
)
@@ -365,6 +374,9 @@ class Instance(BaseModel):
365374
service_accounts: list
366375
ip_forward: bool
367376
disks_encryption: list
377+
automatic_restart: bool = False
378+
preemptible: bool = False
379+
provisioning_model: str = "STANDARD"
368380

369381

370382
class Network(BaseModel):

tests/providers/gcp/gcp_fixtures.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ def mock_api_instances_calls(client: MagicMock, service: str):
759759
"diskType": "disk_type",
760760
}
761761
],
762+
"scheduling": {
763+
"automaticRestart": False,
764+
"preemptible": False,
765+
"provisioningModel": "STANDARD",
766+
},
762767
},
763768
{
764769
"name": "instance2",
@@ -785,6 +790,11 @@ def mock_api_instances_calls(client: MagicMock, service: str):
785790
"diskType": "disk_type",
786791
}
787792
],
793+
"scheduling": {
794+
"automaticRestart": False,
795+
"preemptible": False,
796+
"provisioningModel": "STANDARD",
797+
},
788798
},
789799
]
790800
}

tests/providers/gcp/services/compute/compute_automatic_restart_enabled/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)