diff options
author | AJ Christensen <aj@opscode.com> | 2010-01-05 16:41:50 +1300 |
---|---|---|
committer | AJ Christensen <aj@opscode.com> | 2010-01-05 16:43:54 +1300 |
commit | b5085cb45b5e4cd49c38c51387c000c463bc51e2 (patch) | |
tree | 330e2158f3e5cf1bf00896f0d95b744f21c6ea96 | |
parent | e4b741ddec40ad152ba5f11c2132a38dd106210e (diff) | |
download | chef-b5085cb45b5e4cd49c38c51387c000c463bc51e2.tar.gz |
CHEF-654: json_attribs fails against self signed.
Shift the JSON attribute fetching to chef::rest so we inherit Chef's
signature verification options.
-rw-r--r-- | chef/lib/chef/application/client.rb | 12 | ||||
-rw-r--r-- | chef/lib/chef/application/solo.rb | 12 | ||||
-rw-r--r-- | chef/spec/unit/application/client_spec.rb | 43 | ||||
-rw-r--r-- | chef/spec/unit/application/solo_spec.rb | 46 |
4 files changed, 90 insertions, 23 deletions
diff --git a/chef/lib/chef/application/client.rb b/chef/lib/chef/application/client.rb index a058f9ddc1..4b19335880 100644 --- a/chef/lib/chef/application/client.rb +++ b/chef/lib/chef/application/client.rb @@ -21,8 +21,7 @@ require 'chef/client' require 'chef/config' require 'chef/daemon' require 'chef/log' -require 'net/http' -require 'open-uri' +require 'chef/rest' class Chef::Application::Client < Chef::Application @@ -142,7 +141,13 @@ class Chef::Application::Client < Chef::Application if Chef::Config[:json_attribs] begin - json_io = open(Chef::Config[:json_attribs]) + json_io = case Chef::Config[:json_attribs] + when /^(http|https):\/\// + @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil) + @rest.get_rest(Chef::Config[:json_attribs], true).open + else + open(Chef::Config[:json_attribs]) + end rescue SocketError => error Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2) rescue Errno::ENOENT => error @@ -155,6 +160,7 @@ class Chef::Application::Client < Chef::Application begin @chef_client_json = JSON.parse(json_io.read) + json_io.close unless json_io.closed? rescue JSON::ParserError => error Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) end diff --git a/chef/lib/chef/application/solo.rb b/chef/lib/chef/application/solo.rb index 74e46aab35..43cece2c4c 100644 --- a/chef/lib/chef/application/solo.rb +++ b/chef/lib/chef/application/solo.rb @@ -20,8 +20,7 @@ require 'chef/client' require 'chef/config' require 'chef/daemon' require 'chef/log' -require 'net/http' -require 'open-uri' +require 'chef/rest' require 'fileutils' class Chef::Application::Solo < Chef::Application @@ -132,7 +131,13 @@ class Chef::Application::Solo < Chef::Application if Chef::Config[:json_attribs] begin - json_io = open(Chef::Config[:json_attribs]) + json_io = case Chef::Config[:json_attribs] + when /^(http|https):\/\// + @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil) + @rest.get_rest(Chef::Config[:json_attribs], true).open + else + open(Chef::Config[:json_attribs]) + end rescue SocketError => error Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2) rescue Errno::ENOENT => error @@ -145,6 +150,7 @@ class Chef::Application::Solo < Chef::Application begin @chef_solo_json = JSON.parse(json_io.read) + json_io.close unless json_io.closed? rescue JSON::ParserError => error Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) end diff --git a/chef/spec/unit/application/client_spec.rb b/chef/spec/unit/application/client_spec.rb index b64dff4fb3..509c6f3001 100644 --- a/chef/spec/unit/application/client_spec.rb +++ b/chef/spec/unit/application/client_spec.rb @@ -56,19 +56,46 @@ describe Chef::Application::Client, "reconfigure" do end describe "when the json_attribs configuration option is specified" do - before do - Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") - @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) - @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + + describe "and the json_attribs matches a HTTP regex" do + before do + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @rest = mock("Chef::REST", :get_rest => @json, :null_object => true) + + Chef::Config.stub!(:[]).with(:json_attribs).and_return("https://foo.com/foo.json") + Chef::REST.stub!(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + end + + it "should create a new Chef::REST" do + Chef::REST.should_receive(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest) + @app.reconfigure + end + + it "should perform a RESTful GET on the supplied URL" do + @rest.should_receive(:get_rest).with("https://foo.com/foo.json", true).and_return(@json) + @app.reconfigure + end end - - it "should parse the json out of the file" do - JSON.should_receive(:parse).with(@json.read) - @app.reconfigure + + describe "and the json_attribs does not match the HTTP regex" do + before do + Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + end + + it "should parse the json out of the file" do + JSON.should_receive(:parse).with(@json.read) + @app.reconfigure + end end describe "when parsing fails" do before do + Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError) Chef::Application.stub!(:fatal!).and_return(true) end diff --git a/chef/spec/unit/application/solo_spec.rb b/chef/spec/unit/application/solo_spec.rb index a717ec5d58..bf664d4e32 100644 --- a/chef/spec/unit/application/solo_spec.rb +++ b/chef/spec/unit/application/solo_spec.rb @@ -65,21 +65,47 @@ describe Chef::Application::Solo, "reconfigure" do end end - describe "when the json_attribs configuration option is specified" do - before do - Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") - @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) - @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + + describe "and the json_attribs matches a HTTP regex" do + before do + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @rest = mock("Chef::REST", :get_rest => @json, :null_object => true) + + Chef::Config.stub!(:[]).with(:json_attribs).and_return("https://foo.com/foo.json") + Chef::REST.stub!(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + end + + it "should create a new Chef::REST" do + Chef::REST.should_receive(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest) + @app.reconfigure + end + + it "should perform a RESTful GET on the supplied URL" do + @rest.should_receive(:get_rest).with("https://foo.com/foo.json", true).and_return(@json) + @app.reconfigure + end end - - it "should parse the json out of the file" do - JSON.should_receive(:parse).with(@json.read) - @app.reconfigure + + describe "and the json_attribs does not match the HTTP regex" do + before do + Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) + end + + it "should parse the json out of the file" do + JSON.should_receive(:parse).with(@json.read) + @app.reconfigure + end end describe "when parsing fails" do before do + Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json") + @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) + @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError) Chef::Application.stub!(:fatal!).and_return(true) end @@ -90,6 +116,8 @@ describe Chef::Application::Solo, "reconfigure" do end end end + + describe "when the recipe_url configuration option is specified" do before do |