Add warning message for encoded password in gbs config message. 29/301129/4
authorwanchao-xu <wanchao.xu@samsung.com>
Thu, 9 Nov 2023 05:38:03 +0000 (13:38 +0800)
committerwanchao-xu <wanchao.xu@samsung.com>
Fri, 10 Nov 2023 07:28:38 +0000 (15:28 +0800)
Change-Id: I075aa73d788932086a540ac6351956f23240f070
Signed-off-by: wanchao-xu <wanchao.xu@samsung.com>
gitbuildsys/conf.py

index 50bc0e03a4949e33f02d83709a9f488a15ea74bf..da1742e56ef763226e89932e1d5dbdb5f1fbd3a0 100644 (file)
@@ -69,7 +69,7 @@ class BrainConfigParser(ConfigParser):
 
         return ConfigParser._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
@@ -78,9 +78,15 @@ class BrainConfigParser(ConfigParser):
         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]
@@ -120,6 +126,7 @@ class BrainConfigParser(ConfigParser):
                            (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
@@ -134,14 +141,26 @@ class BrainConfigParser(ConfigParser):
         # 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.
         """
@@ -153,7 +172,7 @@ class BrainConfigParser(ConfigParser):
 
         # 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.
@@ -348,6 +367,7 @@ url = http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Unified/lat
         #For python3.x set() to call add(), it will show error: TypeError: unhashable type: 'BrainConfigParser'
         #This is because for set() in python3.x it can only add which can be hashed, such as int, float, tuple, str
         dirty = []
+        comment = "# WARNING: passwordx can be decoded, don't expose gbs.conf to the outside"
 
         all_sections = set()
         for layer in self._cfgparsers:
@@ -368,7 +388,8 @@ url = http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Unified/lat
                             cfgparser.set_into_file(sec,
                                                     key + 'x',
                                                     encode_passwd(plainpass),
-                                                    key)
+                                                    key,
+                                                    comment)
                             if (cfgparser not in dirty):
                                 dirty.append(cfgparser)