summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSarah Michaelson <sarah.michaelson@gmail.com>2015-09-16 07:14:23 -0400
committerBryan McLellan <btm@chef.io>2015-11-25 12:42:36 -0500
commit8766e9395c3dd3f7e5b8e458d69352a9b8179bdd (patch)
tree38b35c5c167dce833c81b03e8a389af0d0f0b707
parent199ec5d16985b6717e0a3b23594ca90c4c50e9c3 (diff)
downloadchef-8766e9395c3dd3f7e5b8e458d69352a9b8179bdd.tar.gz
GH-1909 Add validation for chef_server_url
-rw-r--r--chef-config/lib/chef-config/config.rb13
-rw-r--r--chef-config/spec/unit/config_spec.rb6
2 files changed, 17 insertions, 2 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index 4e9355192a..e6748897ae 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -76,12 +76,21 @@ module ChefConfig
default :formatters, []
+ def self.is_valid_url? uri
+ url = uri.to_s.strip
+ /^http:\/\// =~ url || /^https:\/\// =~ url || /^chefzero:/ =~ url
+ end
# Override the config dispatch to set the value of multiple server options simultaneously
#
# === Parameters
# url<String>:: String to be set for all of the chef-server-api URL's
#
- configurable(:chef_server_url).writes_value { |url| url.to_s.strip }
+ configurable(:chef_server_url).writes_value do |uri|
+ unless is_valid_url? uri
+ raise ConfigurationError, "#{url} is an invalid chef_server_url."
+ end
+ uri.to_s.strip
+ end
# When you are using ActiveSupport, they monkey-patch 'daemonize' into Kernel.
# So while this is basically identical to what method_missing would do, we pull
@@ -303,7 +312,7 @@ module ChefConfig
default :host, 'localhost'
default :port, 8889.upto(9999) # Will try ports from 8889-9999 until one works
end
- default :chef_server_url, "https://localhost:443"
+ default :chef_server_url, "https://localhost:443"
default(:chef_server_root) do
# if the chef_server_url is a path to an organization, aka
diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb
index 8e9a499a1a..4af5d4b7c7 100644
--- a/chef-config/spec/unit/config_spec.rb
+++ b/chef-config/spec/unit/config_spec.rb
@@ -60,6 +60,12 @@ RSpec.describe ChefConfig::Config do
expect(ChefConfig::Config.chef_server_url).to eq("https://junglist.gen.nz")
end
end
+
+ context "when the url is invalid" do
+ it "raises an exception" do
+ expect { ChefConfig::Config.chef_server_url = "127.0.0.1" }.to raise_error(ChefConfig::ConfigurationError)
+ end
+ end
end
describe "when configuring formatters" do