From eb1965a434360b3198768302f4196488d7c2511f Mon Sep 17 00:00:00 2001
From: Bryan Fraschetti <bryan.fraschetti@canonical.com>
Date: Mon, 3 Feb 2025 16:13:19 -0500
Subject: [PATCH] Fix: GCE _get_data crashes if DHCP lease fails (#5998)
 
This commit addresses issue #5997 which reported crashes in init-local
when cloud-init was examining GCELocal as a potential datasource. When
all NICs failed at DHCP discovery cloud-init attempts to log the events
by dereferencing a value that was never assigned.
 
This commit modifies the _get_data function of DataSourceGCE.py by
adding an empty dictionary definition for the ret variable at the
top level of the function and some debugging logs when a candidate NIC
fails to obtain a DHCP lease. At the same time, the commit replaces the
direct key access operator on ret with the safe lookup method get(). This
commit also adds a unit test that mocks the observed situation.
 
Reference:https://github.com/canonical/cloud-init/commit/eb1965a434360b3198768302f4196488d7c2511f
Conflict:not change test_gce.py (M_PATH and net.find_candidate_nics doesn't exist)
 
Fixes GH-5997
---
 cloudinit/sources/DataSourceGCE.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
 
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
index 9f838bd..53b686f 100644
--- a/cloudinit/sources/DataSourceGCE.py
+++ b/cloudinit/sources/DataSourceGCE.py
@@ -73,19 +73,20 @@ class DataSourceGCE(sources.DataSource):
 
     def _get_data(self):
         url_params = self.get_url_params()
+        ret = {}
         ret = util.log_time(
             LOG.debug, 'Crawl of GCE metadata service',
             read_md, kwargs={'address': self.metadata_address,
                              'url_params': url_params})
 
-        if not ret['success']:
-            if ret['platform_reports_gce']:
-                LOG.warning(ret['reason'])
+        if not ret.get("success"):
+            if ret.get("platform_reports_gce"):
+                LOG.warning(ret.get("reason"))
             else:
-                LOG.debug(ret['reason'])
+                LOG.debug(ret.get("reason"))
             return False
-        self.metadata = ret['meta-data']
-        self.userdata_raw = ret['user-data']
+        self.metadata = ret.get("meta-data")
+        self.userdata_raw = ret.get("user-data")
         return True
 
     @property
-- 
2.33.0

