From 5f7e1e0c312dd626cc0d4a0f78c86d440948f229 Mon Sep 17 00:00:00 2001
From: Nikos Skalkotos <skalkoto@grnet.gr>
Date: Wed, 8 Aug 2012 15:40:27 +0300
Subject: [PATCH] Do not allow empty USERS metadata fields

If snf-image-creator does not find any passworded user, then the
USERS metadata field is not created at all.
---
 image_creator/os_type/unix.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/image_creator/os_type/unix.py b/image_creator/os_type/unix.py
index 842d27f..fa70805 100644
--- a/image_creator/os_type/unix.py
+++ b/image_creator/os_type/unix.py
@@ -51,6 +51,11 @@ class Unix(OSBase):
         super(Unix, self).__init__(rootdev, ghandler, output)
 
         self.meta["USERS"] = " ".join(self._get_passworded_users())
+        # Delete the USERS metadata if empty
+        if not len(self.meta['USERS']):
+            self.out.warn("No passworded users found!")
+            del self.meta['USERS']
+
 
     def _get_passworded_users(self):
         users = []
@@ -77,6 +82,9 @@ class Unix(OSBase):
             self.out.output("Removing all user accounts with id greater than "
                             "1000")
 
+        if 'USERS' not in self.meta:
+            return
+
         # Remove users from /etc/passwd
         passwd = []
         removed_users = {}
@@ -92,6 +100,11 @@ class Unix(OSBase):
                 passwd.append(':'.join(fields))
 
         self.meta['USERS'] = " ".join(metadata_users)
+
+        # Delete the USERS metadata if empty
+        if not len(self.meta['USERS']):
+            del self.meta['USERS']
+
         self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
 
         # Remove the corresponding /etc/shadow entries
-- 
GitLab