]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Subsequent configuration keys now override previous ones
authorJason Ish <jason.ish@emulex.com>
Thu, 21 Nov 2013 20:52:06 +0000 (14:52 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Dec 2013 10:21:52 +0000 (11:21 +0100)
instead of merging.

The exception is final values, for example, values like
default-log-dir that may be set on the command line.

src/conf-yaml-loader.c

index 9442caeac618fc9543b3991d6497f44250d437ae..a5e0d27e66cf4b8a03b661b642798014e389dae9 100644 (file)
@@ -237,9 +237,14 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq)
                                 Mangle(parent->val);
                         }
                     }
-                    ConfNode *n0 = ConfNodeLookupChild(parent, value);
-                    if (n0 != NULL) {
-                        node = n0;
+                    ConfNode *existing = ConfNodeLookupChild(parent, value);
+                    if (existing != NULL) {
+                        if (!existing->final) {
+                            SCLogInfo("Configuration node '%s' redefined.",
+                                existing->name);
+                            ConfNodePrune(existing);
+                        }
+                        node = existing;
                     }
                     else {
                         node = ConfNodeNew();
@@ -733,6 +738,88 @@ cleanup:
     return ret;
 }
 
+/**
+ * Test that a configuration section is overridden but subsequent
+ * occurrences.
+ */
+static int
+ConfYamlOverrideTest(void)
+{
+    char config[] =
+        "%YAML 1.1\n"
+        "---\n"
+        "some-log-dir: /var/log\n"
+        "some-log-dir: /tmp\n"
+        "\n"
+        "parent:\n"
+        "  child0:\n"
+        "    key: value\n"
+        "parent:\n"
+        "  child1:\n"
+        "    key: value\n"
+        ;
+    char *value;
+
+    ConfCreateContextBackup();
+    ConfInit();
+
+    if (ConfYamlLoadString(config, strlen(config)) != 0)
+        return 0;
+    if (!ConfGet("some-log-dir", &value))
+        return 0;
+    if (strcmp(value, "/tmp") != 0)
+        return 0;
+
+    /* Test that parent.child0 does not exist, but child1 does. */
+    if (ConfGetNode("parent.child0") != NULL)
+        return 0;
+    if (!ConfGet("parent.child1.key", &value))
+        return 0;
+    if (strcmp(value, "value") != 0)
+        return 0;
+
+    ConfDeInit();
+    ConfRestoreContextBackup();
+
+    return 1;
+}
+
+/**
+ * Test that a configuration parameter loaded from YAML doesn't
+ * override a 'final' value that may be set on the command line.
+ */
+static int
+ConfYamlOverrideFinalTest(void)
+{
+    ConfCreateContextBackup();
+    ConfInit();
+
+    char config[] =
+        "%YAML 1.1\n"
+        "---\n"
+        "default-log-dir: /var/log\n";
+
+    /* Set the log directory as if it was set on the command line. */
+    if (!ConfSetFinal("default-log-dir", "/tmp"))
+        return 0;
+    if (ConfYamlLoadString(config, strlen(config)) != 0)
+        return 0;
+
+    char *default_log_dir;
+
+    if (!ConfGet("default-log-dir", &default_log_dir))
+        return 0;
+    if (strcmp(default_log_dir, "/tmp") != 0) {
+        fprintf(stderr, "final value was reassigned\n");
+        return 0;
+    }
+
+    ConfDeInit();
+    ConfRestoreContextBackup();
+
+    return 1;
+}
+
 #endif /* UNITTESTS */
 
 void
@@ -746,5 +833,7 @@ ConfYamlRegisterTests(void)
     UtRegisterTest("ConfYamlSecondLevelSequenceTest",
         ConfYamlSecondLevelSequenceTest, 1);
     UtRegisterTest("ConfYamlFileIncludeTest", ConfYamlFileIncludeTest, 1);
+    UtRegisterTest("ConfYamlOverrideTest", ConfYamlOverrideTest, 1);
+    UtRegisterTest("ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest, 1);
 #endif /* UNITTESTS */
 }