Blame SOURCES/0024-Convert-configuration-option-strings-into-native-dat.patch

45cc94
From 883ef5b965bb7b02a701037748436f8daa9d0643 Mon Sep 17 00:00:00 2001
45cc94
From: Rob Crittenden <rcritten@redhat.com>
45cc94
Date: Fri Apr 29 11:42:53 2022 -0400
45cc94
Subject: Convert configuration option strings into native data
45cc94
 types
45cc94
45cc94
When loading options from the configuration file they will all
45cc94
be strings. This breaks existing boolean checks (if <something>)
45cc94
and some assumptions about integer types (e.g. timeout, indent).
45cc94
45cc94
So try to detect the data type, defaulting to remain as a string.
45cc94
45cc94
Also hardcode some type validation for known keys to prevent
45cc94
things like debug=foo (which would evaluate to True).
45cc94
45cc94
https://bugzilla.redhat.com/show_bug.cgi?id=2079861
45cc94
45cc94
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
45cc94
---
45cc94
 src/ipahealthcheck/core/config.py | 47 ++++++++++++++++++++++++++++++-
45cc94
 src/ipahealthcheck/core/output.py |  2 +-
45cc94
 tests/test_config.py              | 16 ++++++++++-
45cc94
 tests/test_options.py             |  2 +-
45cc94
 4 files changed, 63 insertions(+), 4 deletions(-)
45cc94
45cc94
diff --git a/src/ipahealthcheck/core/config.py b/src/ipahealthcheck/core/config.py
45cc94
index 14ae27f..3cd4e8e 100644
45cc94
--- a/src/ipahealthcheck/core/config.py
45cc94
+++ b/src/ipahealthcheck/core/config.py
45cc94
@@ -71,6 +71,27 @@ class Config:
45cc94
             self.__d[key] = d[key]
45cc94
 
45cc94
 
45cc94
+def convert_string(value):
45cc94
+    """
45cc94
+    Reading options from the configuration file will leave them as
45cc94
+    strings. This breaks boolean values so attempt to convert them.
45cc94
+    """
45cc94
+    if not isinstance(value, str):
45cc94
+        return value
45cc94
+
45cc94
+    if value.lower() in (
45cc94
+        "true",
45cc94
+        "false",
45cc94
+    ):
45cc94
+        return value.lower() == 'true'
45cc94
+    else:
45cc94
+        try:
45cc94
+            value = int(value)
45cc94
+        except ValueError:
45cc94
+            pass
45cc94
+    return value
45cc94
+
45cc94
+
45cc94
 def read_config(config_file):
45cc94
     """
45cc94
     Simple configuration file reader
45cc94
@@ -100,6 +121,30 @@ def read_config(config_file):
45cc94
     items = parser.items(CONFIG_SECTION)
45cc94
 
45cc94
     for (key, value) in items:
45cc94
-        config[key] = value
45cc94
+        if len(value) == 0 or value is None:
45cc94
+            logging.error(
45cc94
+                "Empty value for %s in %s [%s]",
45cc94
+                key, config_file, CONFIG_SECTION
45cc94
+            )
45cc94
+            return None
45cc94
+        else:
45cc94
+            # Try to do some basic validation. This is unfortunately
45cc94
+            # hardcoded.
45cc94
+            if key in ('all', 'debug', 'failures_only', 'verbose'):
45cc94
+                if value.lower() not in ('true', 'false'):
45cc94
+                    logging.error(
45cc94
+                        "%s is not a valid boolean in %s [%s]",
45cc94
+                        key, config_file, CONFIG_SECTION
45cc94
+                    )
45cc94
+                    return None
45cc94
+            elif key in ('indent',):
45cc94
+                if not isinstance(convert_string(value), int):
45cc94
+                    logging.error(
45cc94
+                        "%s is not a valid integer in %s [%s]",
45cc94
+                        key, config_file, CONFIG_SECTION
45cc94
+                    )
45cc94
+                    return None
45cc94
+            # Some rough type translation from strings
45cc94
+            config[key] = convert_string(value)
45cc94
 
45cc94
     return config
45cc94
diff --git a/src/ipahealthcheck/core/output.py b/src/ipahealthcheck/core/output.py
45cc94
index 61dffbe..945969f 100644
45cc94
--- a/src/ipahealthcheck/core/output.py
45cc94
+++ b/src/ipahealthcheck/core/output.py
45cc94
@@ -110,7 +110,7 @@ class JSON(Output):
45cc94
 
45cc94
     def __init__(self, options):
45cc94
         super(JSON, self).__init__(options)
45cc94
-        self.indent = int(options.indent)
45cc94
+        self.indent = options.indent
45cc94
 
45cc94
     def generate(self, data):
45cc94
         output = json.dumps(data, indent=self.indent)
45cc94
diff --git a/tests/test_config.py b/tests/test_config.py
45cc94
index 09c2188..655233c 100644
45cc94
--- a/tests/test_config.py
45cc94
+++ b/tests/test_config.py
45cc94
@@ -2,7 +2,7 @@
45cc94
 # Copyright (C) 2019 FreeIPA Contributors see COPYING for license
45cc94
 #
45cc94
 
45cc94
-from ipahealthcheck.core.config import read_config
45cc94
+from ipahealthcheck.core.config import read_config, convert_string
45cc94
 import tempfile
45cc94
 
45cc94
 
45cc94
@@ -60,3 +60,17 @@ def test_config_recursion():
45cc94
         config._Config__d['_Config__d']
45cc94
     except KeyError:
45cc94
         pass
45cc94
+
45cc94
+
45cc94
+def test_convert_string():
45cc94
+    for value in ("s", "string", "BiggerString"):
45cc94
+        assert convert_string(value) == value
45cc94
+
45cc94
+    for value in ("True", "true", True):
45cc94
+        assert convert_string(value) is True
45cc94
+
45cc94
+    for value in ("False", "false", False):
45cc94
+        assert convert_string(value) is False
45cc94
+
45cc94
+    for value in ("10", "99999", 807):
45cc94
+        assert convert_string(value) == int(value)
45cc94
diff --git a/tests/test_options.py b/tests/test_options.py
45cc94
index da1866f..00cdb7c 100644
45cc94
--- a/tests/test_options.py
45cc94
+++ b/tests/test_options.py
45cc94
@@ -40,6 +40,6 @@ def test_options_merge(mock_parse, mock_run, mock_service):
45cc94
 
45cc94
         # verify two valus that have defaults with our overriden values
45cc94
         assert run.options.output_type == 'human'
45cc94
-        assert run.options.indent == '5'
45cc94
+        assert run.options.indent == 5
45cc94
     finally:
45cc94
         os.remove(config_path)
45cc94
-- 
45cc94
2.31.1
45cc94