From 3634678465e7b8f8608bcb9a1f5773ae7837cbe9 Mon Sep 17 00:00:00 2001
From: Shreenidhi Shedi <53473811+sshedi@users.noreply.github.com>
Date: Fri, 17 Mar 2023 03:01:22 +0530
Subject: [PATCH] Handle non existent ca-cert-config situation (#2073)

Reference:https://github.com/canonical/cloud-init/commit/3634678465e7b8f8608bcb9a1f5773ae7837cbe9
Conflict:only check cert file

Currently if a cert file doesn't exist, cc_ca_certs module crashes
This fix makes it possible to handle it gracefully.

Also, out_lines variable may not be available if os.stat returns 0.
This issue is also taken care of.

Added tests for the same.
---
 cloudinit/config/cc_ca_certs.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/cloudinit/config/cc_ca_certs.py b/cloudinit/config/cc_ca_certs.py
index bd7bead..2b3210b 100644
--- a/cloudinit/config/cc_ca_certs.py
+++ b/cloudinit/config/cc_ca_certs.py
@@ -110,7 +110,8 @@ def update_cert_config(distro_cfg):
 
     @param distro_cfg: A hash providing _distro_ca_certs_configs function.
     """
-    if distro_cfg['ca_cert_config'] is None:
+    ca_cert_cfg_fn = distro_cfg["ca_cert_config"]
+    if not ca_cert_cfg_fn or not os.path.exists(ca_cert_cfg_fn):
         return
     if os.stat(distro_cfg['ca_cert_config']).st_size == 0:
         # If the CA_CERT_CONFIG file is empty (i.e. all existing
-- 
2.33.0


