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

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