Sandro Bonazzola has uploaded a new change for review.

Change subject: db: drop the obfuscation of database password
......................................................................

db: drop the obfuscation of database password

Added support for the new configuration structure
for database related parameters.
If the new configuration structure can't be found
fallback to legacy pg_pass file.
Don't ask the DB password if already read from
configuration files.

Change-Id: I763f5a9d1d3307ee384281b13db5c6c9436060df
Bug-Url: https://bugzilla.redhat.com/958532
Signed-off-by: Sandro Bonazzola <sbona...@redhat.com>
---
M src/__main__.py
M src/config.py.in.in
2 files changed, 99 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-log-collector 
refs/changes/41/14741/1

diff --git a/src/__main__.py b/src/__main__.py
index 74e9126..98d8225 100755
--- a/src/__main__.py
+++ b/src/__main__.py
@@ -34,8 +34,10 @@
 import tempfile
 import atexit
 import time
-from helper import hypervisors
+import re
+import glob
 
+from helper import hypervisors
 from ovirt_log_collector import config
 
 STREAM_LOG_FORMAT = '%(levelname)s: %(message)s'
@@ -50,11 +52,10 @@
 
 # Default DB connection params
 pg_user = 'postgres'
-pg_pass = '12345'
+pg_pass = None
 pg_dbname = 'engine'
 pg_dbhost = 'localhost'
 pg_dbport = '5432'
-
 
 t = gettext.translation('logcollector', fallback=True)
 _ = t.ugettext
@@ -177,6 +178,89 @@
             return stdout
         else:
             raise Exception(stderr)
+
+
+class EngineConfigFile(object):
+    _COMMENT_EXPR = re.compile(r'\s*#.*$')
+    _BLANK_EXPR = re.compile(r'^\s*$')
+    _VALUE_EXPR = re.compile(r'^\s*(?P<key>\w+)\s*=\s*(?P<value>.*?)\s*$')
+    _REF_EXPR = re.compile(r'\$\{(?P<ref>\w+)\}')
+
+    def _loadLine(self, line):
+        # Remove comments:
+        commentMatch = self._COMMENT_EXPR.search(line)
+        if commentMatch is not None:
+            line = line[:commentMatch.start()] + line[commentMatch.end():]
+
+        # Skip empty lines:
+        emptyMatch = self._BLANK_EXPR.search(line)
+        if emptyMatch is None:
+            # Separate name from value:
+            keyValueMatch = self._VALUE_EXPR.search(line)
+            if keyValueMatch is not None:
+                key = keyValueMatch.group('key')
+                value = keyValueMatch.group('value')
+
+                # Strip quotes from value:
+                if len(value) >= 2 and value[0] == '"' and value[-1] == '"':
+                    value = value[1:-1]
+
+                # Expand references to other parameters:
+                while True:
+                    refMatch = self._REF_EXPR.search(value)
+                    if refMatch is None:
+                        break
+                    refKey = refMatch.group('ref')
+                    refValue = self._values.get(refKey)
+                    if refValue is None:
+                        break
+                    value = '%s%s%s' % (
+                        value[:refMatch.start()],
+                        refValue,
+                        value[refMatch.end():],
+                    )
+
+                self._values[key] = value
+
+    def __init__(self, files=[]):
+        super(EngineConfigFile, self).__init__()
+
+        self._values = {}
+
+        for filename in files:
+            self.loadFile(filename)
+            for filed in sorted(
+                glob.glob(
+                    os.path.join(
+                        '%s.d' % filename,
+                        '*.conf',
+                    )
+                )
+            ):
+                self.loadFile(filed)
+
+    def loadFile(self, filename):
+        if os.path.exists(filename):
+            with open(filename, 'r') as f:
+                for line in f:
+                    self._loadLine(line)
+
+    def get(self, name, default=None):
+        return self._values.get(name, default)
+
+    def getboolean(self, name, default=None):
+        text = self.get(name)
+        if text is None:
+            return default
+        else:
+            return text.lower() in ('t', 'true', 'y', 'yes', '1')
+
+    def getinteger(self, name, default=None):
+        value = self.get(name)
+        if value is None:
+            return default
+        else:
+            return int(value)
 
 
 class Configuration(dict):
@@ -1114,7 +1198,15 @@
         print "This tool requires root permissions to run."
         sys.exit(ExitCodes.CRITICAL)
 
-    setup_pg_defaults()
+    engine_config = EngineConfigFile([config.ENGINE_CONF])
+    if not engine_config.get('ENGINE_DB_PASSWORD'):
+        setup_pg_defaults()
+    else:
+        pg_pass = engine_config.get('ENGINE_DB_PASSWORD')
+        pg_user = engine_config.get('ENGINE_DB_USER')
+        pg_dbname = engine_config.get('ENGINE_DB_DATABASE')
+        pg_dbhost = engine_config.get('ENGINE_DB_HOST')
+        pg_dbport = engine_config.get('ENGINE_DB_PORT')
 
     DEFAULT_SCRATCH_DIR = tempfile.mkdtemp(prefix='logcollector-')
 
@@ -1349,7 +1441,8 @@
         "",
         "--pg-pass",
         dest="pg_pass",
-        help=SUPPRESS_HELP
+        help=SUPPRESS_HELP,
+        default=pg_pass
     )
 
     db_group.add_option(
diff --git a/src/config.py.in.in b/src/config.py.in.in
index 39a1704..052e16f 100644
--- a/src/config.py.in.in
+++ b/src/config.py.in.in
@@ -6,6 +6,7 @@
 PACKAGE_VERSION = "@PACKAGE_VERSION@"
 
 FILE_PG_PASS = "@engineconfigdir_POST@/.pgpass"
+ENGINE_CONF = "@engineconfigdir_POST@/engine.conf"
 
 DEFAULT_CA_PEM = "@sysconfdir_POST@/pki/ovirt-engine/ca.pem"
 DEFAULT_SSH_KEY = "@sysconfdir_POST@/pki/ovirt-engine/keys/engine_id_rsa"


--
To view, visit http://gerrit.ovirt.org/14741
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I763f5a9d1d3307ee384281b13db5c6c9436060df
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-log-collector
Gerrit-Branch: master
Gerrit-Owner: Sandro Bonazzola <sbona...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to