Add warning message for encoded password in gbs config message.
authorwanchao-xu <wanchao.xu@samsung.com>
Thu, 9 Nov 2023 05:38:03 +0000 (13:38 +0800)
committerwanchao-xu <wanchao.xu@samsung.com>
Tue, 2 Apr 2024 06:49:36 +0000 (14:49 +0800)
Change-Id: I075aa73d788932086a540ac6351956f23240f070
Signed-off-by: wanchao-xu <wanchao.xu@samsung.com>
gitbuildsys/conf.py

index 177ba42475cab48231b294bf544293fefec7e07e..6059853d4b3afaa04d973a2781e98b8980be42fb 100644 (file)
@@ -67,7 +67,7 @@ class BrainConfigParser(SafeConfigParser):
 
         return SafeConfigParser._read(self, fptr, fname)
 
-    def _set_into_file(self, section, option, value, replace_opt=None):
+    def _set_into_file(self, section, option, value, replace_opt=None, comment=None):
         """Set the value in the file contents
 
         Parsing logic and lot of the code was copied directly from the
@@ -76,9 +76,15 @@ class BrainConfigParser(SafeConfigParser):
         cursect = None                        # None, or a str
         optname = None
         new_line = '%s = %s\n' % (option, value)
+        new_lineno = None
         new_line_written = False
         last_section_line = None
 
+        # add line break for comment
+        if comment is not None:
+            if not comment.endswith('\n'):
+                comment += '\n'
+
         lineno = -1
         for lineno in range(len(self._flines)):
             line = self._flines[lineno]
@@ -118,6 +124,7 @@ class BrainConfigParser(SafeConfigParser):
                            (optname == option or optname == replace_opt):
                             if not new_line_written:
                                 self._flines[lineno] = new_line
+                                new_lineno = lineno
                                 new_line_written = True
                             else:
                                 # Just remove all matching lines, if we've
@@ -132,14 +139,26 @@ class BrainConfigParser(SafeConfigParser):
         # Insert new key
         if not new_line_written:
             if last_section_line is not None:
-                self._flines.insert(last_section_line + 1, new_line)
+                if comment is not None:
+                    self._flines.insert(last_section_line + 1, comment)
+                    self._flines.insert(last_section_line + 2, new_line)
+                else:
+                    self._flines.insert(last_section_line + 1, new_line)
             else:
                 if lineno >= 0:
                     self._flines.insert(lineno + 1, '\n')
                 self._flines.insert(lineno + 2, '[%s]\n' % section)
-                self._flines.insert(lineno + 3, new_line)
+                if comment is not None:
+                    self._flines.insert(lineno + 3, comment)
+                    self._flines.insert(lineno + 4, new_line)
+                else:
+                    self._flines.insert(lineno + 3, new_line)
+        else:
+            if comment is not None and new_lineno is not None:
+                self._flines.insert(new_lineno, comment)
+
 
-    def set_into_file(self, section, option, value, replace_opt=None):
+    def set_into_file(self, section, option, value, replace_opt=None, comment=None):
         """When set new value, need to update the readin file lines,
         which can be saved back to file later.
         """
@@ -151,7 +170,7 @@ class BrainConfigParser(SafeConfigParser):
 
         # If the code reach here, it means the section and key are ok
         try:
-            self._set_into_file(section, option, value, replace_opt)
+            self._set_into_file(section, option, value, replace_opt, comment)
         except Exception as err:
             # This really shouldn't happen, we've already once parsed the file
             # contents successfully.
@@ -343,7 +362,8 @@ url = http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Unified/lat
 
     def _check_passwd(self):
         'convert passwd item to passwdx and then update origin conf files'
-        dirty = set()
+        dirty = []
+        comment = "# WARNING: passwordx can be decoded, don't expose gbs.conf to the outside"
 
         all_sections = set()
         for layer in self._cfgparsers:
@@ -364,8 +384,10 @@ url = http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Unified/lat
                             cfgparser.set_into_file(sec,
                                                     key + 'x',
                                                     encode_passwd(plainpass),
-                                                    key)
-                            dirty.add(cfgparser)
+                                                    key,
+                                                    comment)
+                            if (cfgparser not in dirty):
+                                dirty.append(cfgparser)
 
         if dirty:
             log.warning('plaintext password in config files will '