Skip to content

Commit

Permalink
Update apache_conf regular expression to exclude whitespace. (#2416)
Browse files Browse the repository at this point in the history
* Update apache_conf test to check for ServerAlias values.

Signed-off-by: Miah Johnson <miah@chia-pet.org>

* Add ServerAlias key and values to mock apache conf which includes
trailing whitespace.

Signed-off-by: Miah Johnson <miah@chia-pet.org>

* Updated test to reflect all ServerAlias values being put into a single
array item. This is expected as we do not override the key_values
default setting of '1' when passing the raw configuration to
SimpleConfig.

Signed-off-by: Miah Johnson <miah@chia-pet.org>

* Update the regular expression to include a conditional with positive
lookahead that checks if the line ends with one or more spaces. If the
lookahead succeeds we non-greedily capture, and when it fails we
greedily capture.

Signed-off-by: Miah Johnson <miah@chia-pet.org>
  • Loading branch information
miah authored and chris-rock committed Dec 22, 2017
1 parent d86ebee commit 685ba1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/resources/apache_conf.rb
Expand Up @@ -78,10 +78,17 @@ def read_content
raw_conf = read_file(to_read[0])
@content += raw_conf

# parse include file parameters
# An explaination of the below regular expression.
# Creates two capture groups.
# The first group captures the first group of non-whitespace character
# surrounded whitespace characters.
# The second group contains a conditional with a positive lookahead
# (does the line end with one or more spaces?). If the lookahead succeeds
# a non-greedy capture takes place, if it fails then a greedy capture takes place.
# The regex is terminated by an expression that matches zero or more spaces.
params = SimpleConfig.new(
raw_conf,
assignment_regex: /^\s*(\S+)\s+(.*)\s*$/,
assignment_regex: /^\s*(\S+)\s+((?=.*\s+$).*?|.*)\s*$/,
multiple_values: true,
).params
@params.merge!(params)
Expand Down
1 change: 1 addition & 0 deletions test/unit/mock/files/apache2.conf
@@ -1,5 +1,6 @@
# This is the main Apache server configuration file. It contains comments.
ServerRoot "/etc/apache2"
ServerAlias inspec.test www.inspec.test io.inspec.test

User ${APACHE_RUN_USER}
Include ports.conf
Expand Down
1 change: 1 addition & 0 deletions test/unit/resources/apache_conf_test.rb
Expand Up @@ -12,6 +12,7 @@
_(resource.params).must_be_kind_of Hash
_(resource.content).must_be_kind_of String
_(resource.params('ServerRoot')).must_equal ['"/etc/apache2"']
_(resource.params('ServerAlias')).must_equal ['inspec.test www.inspec.test io.inspec.test']
_(resource.params('Listen').sort).must_equal ['443', '80']
# sourced using a linked file in conf-enabled/
_(resource.params('ServerSignature')).must_equal ['Off']
Expand Down

0 comments on commit 685ba1b

Please sign in to comment.