Merge pull request #4215 from inspec/ja/fix-nginx-empty-parse

nginx_conf: Fix commented/empty file parsing
This commit is contained in:
Miah Johnson 2019-06-18 17:55:20 -07:00 committed by GitHub
commit ca4981d0d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 2 deletions

View file

@ -63,6 +63,11 @@ module Inspec::Resources
def parse_nginx(path) def parse_nginx(path)
return nil if inspec.os.windows? return nil if inspec.os.windows?
content = read_content(path) content = read_content(path)
# Don't attempt to parse file if it contains only comments or is empty
# https://regexper.com/#%2F%5E%5Cs*%23%7C%5E%24%2F
return {} if content.lines.reject { |l| l =~ /^\s*#|^$/ }.empty?
data = NginxConfig.parse(content) data = NginxConfig.parse(content)
resolve_references(data, File.dirname(path)) resolve_references(data, File.dirname(path))
rescue StandardError => _ rescue StandardError => _

View file

@ -118,6 +118,8 @@ class MockLoader
"/etc/nginx/nginx.conf" => mockfile.call("nginx.conf"), "/etc/nginx/nginx.conf" => mockfile.call("nginx.conf"),
"/etc/nginx/proxy.conf" => mockfile.call("nginx_proxy.conf"), "/etc/nginx/proxy.conf" => mockfile.call("nginx_proxy.conf"),
"/etc/nginx/conf/mime.types" => mockfile.call("nginx_mime.types"), "/etc/nginx/conf/mime.types" => mockfile.call("nginx_mime.types"),
"/etc/nginx/conf.d/comments_only.conf" => mockfile.call("nginx_confd_comments_only.conf"),
"/etc/nginx/conf.d/empty.conf" => mockfile.call("nginx_confd_empty.conf"),
"/etc/nginx/conf.d/foobar.conf" => mockfile.call("nginx_confd_foobar.conf"), "/etc/nginx/conf.d/foobar.conf" => mockfile.call("nginx_confd_foobar.conf"),
"/etc/nginx/conf.d/multiple.conf" => mockfile.call("nginx_confd_multiple.conf"), "/etc/nginx/conf.d/multiple.conf" => mockfile.call("nginx_confd_multiple.conf"),
"/etc/nginx/quotes.d/example.conf" => mockfile.call("nginx_quotesd_example.conf"), "/etc/nginx/quotes.d/example.conf" => mockfile.call("nginx_quotesd_example.conf"),
@ -156,7 +158,6 @@ class MockLoader
"/fakepath/fakefile" => emptyfile.call, "/fakepath/fakefile" => emptyfile.call,
"C:/fakepath/fakefile" => emptyfile.call, "C:/fakepath/fakefile" => emptyfile.call,
"/etc/cron.d/crondotd" => mockfile.call("crondotd"), "/etc/cron.d/crondotd" => mockfile.call("crondotd"),
"/missing_file" => emptyfile.call,
} }
# create all mock commands # create all mock commands

View file

@ -1,2 +1,4 @@
/etc/nginx/conf.d/comments_only.conf
/etc/nginx/conf.d/empty.conf
/etc/nginx/conf.d/foobar.conf /etc/nginx/conf.d/foobar.conf
/etc/nginx/conf.d/multiple.conf /etc/nginx/conf.d/multiple.conf

View file

@ -0,0 +1,33 @@
# This file is empty save for comments
#
# HTTPS server configuration
#
#server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
#}

View file

@ -10,7 +10,19 @@ describe "Inspec::Resources::NginxConf" do
let(:nginx_conf) { MockLoader.new(:ubuntu1404).load_resource("nginx_conf") } let(:nginx_conf) { MockLoader.new(:ubuntu1404).load_resource("nginx_conf") }
it "doesnt fail with a missing file" do it "doesnt fail with a missing file" do
nginx_conf = MockLoader.new(:ubuntu1404).load_resource("nginx_conf", "/missing_file") # This path is not mocked because we cannot mock File.exist?
# ...As far as I know
nginx_conf = MockLoader.new(:ubuntu1404).load_resource("nginx_conf", "/this/path/does/not/exist")
_(nginx_conf.params).must_equal({})
end
it "does not fail with an empty file" do
nginx_conf = MockLoader.new(:ubuntu1404).load_resource("nginx_conf", "/etc/nginx/conf.d/empty.conf")
_(nginx_conf.params).must_equal({})
end
it "does not fail with a file that all lines are commented out" do
nginx_conf = MockLoader.new(:ubuntu1404).load_resource("nginx_conf", "/etc/nginx/conf.d/comments_only.conf")
_(nginx_conf.params).must_equal({}) _(nginx_conf.params).must_equal({})
end end
@ -26,6 +38,8 @@ describe "Inspec::Resources::NginxConf" do
/etc/nginx/nginx.conf /etc/nginx/nginx.conf
/etc/nginx/conf/mime.types /etc/nginx/conf/mime.types
/etc/nginx/proxy.conf /etc/nginx/proxy.conf
/etc/nginx/conf.d/comments_only.conf
/etc/nginx/conf.d/empty.conf
/etc/nginx/conf.d/foobar.conf /etc/nginx/conf.d/foobar.conf
/etc/nginx/conf.d/multiple.conf /etc/nginx/conf.d/multiple.conf
/etc/nginx/quotes.d/example.conf /etc/nginx/quotes.d/example.conf