Browse code

local.conf processing doesn't handle '=' in values

When attempting to add a libvirt section with a volume_drivers entry
to $NOVA_CONF, via a post-config block in the local.conf file, I
encountered problems; the value for this attribute takes the form

driver=python.import.path.to.driver

but the value actually populated in the $NOVA_CONF was truncated at the
equals.

Taking the iscsi driver setting specified in the official nova.conf
documentation as an example, if I have the following in my local.conf
file:

[[post-config|$NOVA_CONF]]
[libvirt]
volume_drivers = iscsi=nova.virt.libvirt.volume.LibvirtISCSIVolumeDriver

I will see that the generated $NOVA_CONF has the following:

[libvirt]
volume_driver = iscsi

This occurs because the existing handling for a post-config setion, as
implemented in merge_config_file(), splits the line on the equals sign,
and then uses the first and seconds elements of the resulting array as
attribute name and value respectively.

However when an equals occurs as part of the value this results in the
value being truncated at the first equals in the value.

The fix I've implemented, based upon review feedback, extracts the
contents of $0 before the first equals as the attr name, and extracts
the remainder after the equals as the value. Then it strips the leading
and trailing whitespaces from both as appropriate.

I've also added test5 to tests/test_config.sh to test for, and verify,
correct operation when this scenario is encountered. Similarly I've
added test6 to ensure that trailing spaces in values are stripped
correctly.

Change-Id: Id0cb1e6e1cece21bc5dbf427c4d756af86fbd927
Closes-Bug: #1374482

Fergal Mc Carthy authored on 2014/10/10 05:16:42
Showing 2 changed files
... ...
@@ -96,8 +96,17 @@ function merge_config_file {
96 96
             next
97 97
         }
98 98
         /^[^ \t]+/ {
99
-            split($0, d, " *= *")
100
-            print "iniset " configfile " " section " " d[1] " \x27" d[2] "\x27 "
99
+            # get offset of first '=' in $0
100
+            eq_idx = index($0, "=")
101
+            # extract attr & value from $0
102
+            attr = substr($0, 1, eq_idx - 1)
103
+            value = substr($0, eq_idx + 1)
104
+            # only need to strip trailing whitespace from attr
105
+            sub(/[ \t]*$/, "", attr)
106
+            # need to strip leading & trailing whitespace from value
107
+            sub(/^[ \t]*/, "", value)
108
+            sub(/[ \t]*$/, "", value)
109
+            print "iniset " configfile " " section " " attr " \x27" value "\x27"
101 110
         }
102 111
     ' | while read a; do eval "$a"; done
103 112
 
... ...
@@ -95,6 +95,15 @@ type=new
95 95
 [[test-quote|test-quote.conf]]
96 96
 [foo]
97 97
 foo="foo bar" "baz"
98
+
99
+[[test5|test-equals.conf]]
100
+[DEFAULT]
101
+drivers = driver=python.import.path.Driver
102
+
103
+[[test6|test-strip.conf]]
104
+[DEFAULT]
105
+# next line has trailing space
106
+attr = strip_trailing_space 
98 107
 EOF
99 108
 
100 109
 echo -n "get_meta_section_files: test0 doesn't exist: "
... ...
@@ -238,5 +247,25 @@ EXPECT_VAL="
238 238
 type = new"
239 239
 check_result "$VAL" "$EXPECT_VAL"
240 240
 
241
-rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf
241
+echo -n "merge_config_file test5 equals in value: "
242
+rm -f test-equals.conf
243
+merge_config_file test.conf test5 test-equals.conf
244
+VAL=$(cat test-equals.conf)
245
+# iniset adds a blank line if it creates the file...
246
+EXPECT_VAL="
247
+[DEFAULT]
248
+drivers = driver=python.import.path.Driver"
249
+check_result "$VAL" "$EXPECT_VAL"
250
+
251
+echo -n "merge_config_file test6 value stripped: "
252
+rm -f test-strip.conf
253
+merge_config_file test.conf test6 test-strip.conf
254
+VAL=$(cat test-strip.conf)
255
+# iniset adds a blank line if it creates the file...
256
+EXPECT_VAL="
257
+[DEFAULT]
258
+attr = strip_trailing_space"
259
+check_result "$VAL" "$EXPECT_VAL"
260
+
261
+rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf test-equals.conf test-strip.conf
242 262
 rm -rf test-etc