summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/chef_fs/file_system/acl_dir.rb4
-rw-r--r--lib/chef/chef_fs/file_system/acls_dir.rb4
-rw-r--r--lib/chef/chef_fs/file_system/chef_server_root_dir.rb8
-rw-r--r--lib/chef/chef_fs/file_system/cookbook_dir.rb6
-rw-r--r--lib/chef/chef_fs/file_system/cookbook_file.rb4
-rw-r--r--lib/chef/chef_fs/file_system/cookbook_subdir.rb4
-rw-r--r--lib/chef/chef_fs/file_system/cookbooks_dir.rb9
-rw-r--r--lib/chef/chef_fs/file_system/data_bags_dir.rb2
-rw-r--r--lib/chef/chef_fs/file_system/nodes_dir.rb2
-rw-r--r--lib/chef/chef_fs/file_system/rest_list_dir.rb6
-rw-r--r--lib/chef/chef_fs/file_system/rest_list_entry.rb10
-rw-r--r--lib/chef/chef_fs/raw_request.rb83
-rw-r--r--lib/chef/http/json_to_model_inflater.rb20
-rw-r--r--lib/chef/knife/raw.rb17
-rw-r--r--lib/chef/rest.rb12
15 files changed, 80 insertions, 111 deletions
diff --git a/lib/chef/chef_fs/file_system/acl_dir.rb b/lib/chef/chef_fs/file_system/acl_dir.rb
index c2354d478d..96708de8a0 100644
--- a/lib/chef/chef_fs/file_system/acl_dir.rb
+++ b/lib/chef/chef_fs/file_system/acl_dir.rb
@@ -58,6 +58,10 @@ class Chef
def rest
parent.rest
end
+
+ def rest_json
+ parent.rest_json
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/acls_dir.rb b/lib/chef/chef_fs/file_system/acls_dir.rb
index 938bf73fb2..a4d39a63a5 100644
--- a/lib/chef/chef_fs/file_system/acls_dir.rb
+++ b/lib/chef/chef_fs/file_system/acls_dir.rb
@@ -62,6 +62,10 @@ class Chef
def rest
parent.rest
end
+
+ def rest_json
+ parent.rest_json
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb
index 98f90e3a68..f74fc8e463 100644
--- a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb
@@ -57,6 +57,14 @@ class Chef
end
def rest
+ Chef::REST.new(chef_server_url, chef_username, chef_private_key, { :raw_output => true })
+ end
+
+ def rest_json
+ Chef::REST.new(chef_server_url, chef_username, chef_private_key, { :inflate_json_class => false })
+ end
+
+ def rest_normal
Chef::REST.new(chef_server_url, chef_username, chef_private_key)
end
diff --git a/lib/chef/chef_fs/file_system/cookbook_dir.rb b/lib/chef/chef_fs/file_system/cookbook_dir.rb
index 5751328b02..79540067f7 100644
--- a/lib/chef/chef_fs/file_system/cookbook_dir.rb
+++ b/lib/chef/chef_fs/file_system/cookbook_dir.rb
@@ -172,6 +172,10 @@ class Chef
parent.rest
end
+ def rest_json
+ parent.rest_json
+ end
+
def chef_object
# We cheat and cache here, because it seems like a good idea to keep
# the cookbook view consistent with the directory structure.
@@ -191,7 +195,7 @@ class Chef
old_retry_count = Chef::Config[:http_retry_count]
begin
Chef::Config[:http_retry_count] = 0
- @chef_object ||= Chef::CookbookVersion.json_create(Chef::ChefFS::RawRequest.raw_json(rest, api_path))
+ @chef_object ||= Chef::CookbookVersion.json_create(rest_json.get(api_path))
ensure
Chef::Config[:http_retry_count] = old_retry_count
end
diff --git a/lib/chef/chef_fs/file_system/cookbook_file.rb b/lib/chef/chef_fs/file_system/cookbook_file.rb
index e05c4aa614..552f360173 100644
--- a/lib/chef/chef_fs/file_system/cookbook_file.rb
+++ b/lib/chef/chef_fs/file_system/cookbook_file.rb
@@ -59,6 +59,10 @@ class Chef
parent.rest
end
+ def rest_json
+ parent.rest_json
+ end
+
def compare_to(other)
other_value = nil
if other.respond_to?(:checksum)
diff --git a/lib/chef/chef_fs/file_system/cookbook_subdir.rb b/lib/chef/chef_fs/file_system/cookbook_subdir.rb
index 73c709e01e..33f0072378 100644
--- a/lib/chef/chef_fs/file_system/cookbook_subdir.rb
+++ b/lib/chef/chef_fs/file_system/cookbook_subdir.rb
@@ -48,6 +48,10 @@ class Chef
def rest
parent.rest
end
+
+ def rest_json
+ parent.rest_json
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/cookbooks_dir.rb b/lib/chef/chef_fs/file_system/cookbooks_dir.rb
index 73684450b3..2358408585 100644
--- a/lib/chef/chef_fs/file_system/cookbooks_dir.rb
+++ b/lib/chef/chef_fs/file_system/cookbooks_dir.rb
@@ -18,7 +18,6 @@
require 'chef/chef_fs/file_system/rest_list_dir'
require 'chef/chef_fs/file_system/cookbook_dir'
-require 'chef/chef_fs/raw_request'
require 'chef/chef_fs/file_system/operation_failed_error'
require 'chef/chef_fs/file_system/cookbook_frozen_error'
require 'chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir'
@@ -54,13 +53,13 @@ class Chef
@children ||= begin
if Chef::Config[:versioned_cookbooks]
result = []
- Chef::ChefFS::RawRequest.raw_json(rest, "#{api_path}/?num_versions=all").each_pair do |cookbook_name, cookbooks|
+ rest_json.get("#{api_path}/?num_versions=all").each_pair do |cookbook_name, cookbooks|
cookbooks['versions'].each do |cookbook_version|
result << CookbookDir.new("#{cookbook_name}-#{cookbook_version['version']}", self, :exists => true)
end
end
else
- result = Chef::ChefFS::RawRequest.raw_json(rest, api_path).keys.map { |cookbook_name| CookbookDir.new(cookbook_name, self, :exists => true) }
+ result = rest_json.get(api_path).keys.map { |cookbook_name| CookbookDir.new(cookbook_name, self, :exists => true) }
end
result.sort_by(&:name)
end
@@ -107,7 +106,7 @@ class Chef
cookbook_to_upload.freeze_version if options[:freeze]
# Instantiate a new uploader based on the proxy loader
- uploader = Chef::CookbookUploader.new(cookbook_to_upload, proxy_cookbook_path, :force => options[:force], :rest => rest)
+ uploader = Chef::CookbookUploader.new(cookbook_to_upload, proxy_cookbook_path, :force => options[:force], :rest => root.rest_normal)
with_actual_cookbooks_dir(temp_cookbooks_path) do
upload_cookbook!(uploader)
@@ -129,7 +128,7 @@ class Chef
def upload_unversioned_cookbook(other, options)
cookbook_to_upload = other.chef_object
cookbook_to_upload.freeze_version if options[:freeze]
- uploader = Chef::CookbookUploader.new(cookbook_to_upload, other.parent.file_path, :force => options[:force], :rest => rest)
+ uploader = Chef::CookbookUploader.new(cookbook_to_upload, other.parent.file_path, :force => options[:force], :rest => root.rest_normal)
with_actual_cookbooks_dir(other.parent.file_path) do
upload_cookbook!(uploader)
diff --git a/lib/chef/chef_fs/file_system/data_bags_dir.rb b/lib/chef/chef_fs/file_system/data_bags_dir.rb
index d0404c284b..bdae077586 100644
--- a/lib/chef/chef_fs/file_system/data_bags_dir.rb
+++ b/lib/chef/chef_fs/file_system/data_bags_dir.rb
@@ -34,7 +34,7 @@ class Chef
def children
begin
- @children ||= Chef::ChefFS::RawRequest.raw_json(rest, api_path).keys.sort.map do |entry|
+ @children ||= rest_json.get(api_path).keys.sort.map do |entry|
DataBagDir.new(entry, self, true)
end
rescue Timeout::Error => e
diff --git a/lib/chef/chef_fs/file_system/nodes_dir.rb b/lib/chef/chef_fs/file_system/nodes_dir.rb
index 82683e81ac..9f4466b16f 100644
--- a/lib/chef/chef_fs/file_system/nodes_dir.rb
+++ b/lib/chef/chef_fs/file_system/nodes_dir.rb
@@ -32,7 +32,7 @@ class Chef
# Identical to RestListDir.children, except supports environments
def children
begin
- @children ||= Chef::ChefFS::RawRequest.raw_json(rest, env_api_path).keys.sort.map do |key|
+ @children ||= rest_json.get(env_api_path).keys.sort.map do |key|
_make_child_entry("#{key}.json", true)
end
rescue Timeout::Error => e
diff --git a/lib/chef/chef_fs/file_system/rest_list_dir.rb b/lib/chef/chef_fs/file_system/rest_list_dir.rb
index 7e4a33813b..05ee031954 100644
--- a/lib/chef/chef_fs/file_system/rest_list_dir.rb
+++ b/lib/chef/chef_fs/file_system/rest_list_dir.rb
@@ -45,7 +45,7 @@ class Chef
def children
begin
- @children ||= Chef::ChefFS::RawRequest.raw_json(rest, api_path).keys.sort.map do |key|
+ @children ||= rest_json.get(api_path).keys.sort.map do |key|
_make_child_entry("#{key}.json", true)
end
rescue Timeout::Error => e
@@ -106,6 +106,10 @@ class Chef
parent.rest
end
+ def rest_json
+ parent.rest_json
+ end
+
def _make_child_entry(name, exists = nil)
RestListEntry.new(name, self, exists)
end
diff --git a/lib/chef/chef_fs/file_system/rest_list_entry.rb b/lib/chef/chef_fs/file_system/rest_list_entry.rb
index 6e6ad12438..420dac9c72 100644
--- a/lib/chef/chef_fs/file_system/rest_list_entry.rb
+++ b/lib/chef/chef_fs/file_system/rest_list_entry.rb
@@ -19,7 +19,6 @@
require 'chef/chef_fs/file_system/base_fs_object'
require 'chef/chef_fs/file_system/not_found_error'
require 'chef/chef_fs/file_system/operation_failed_error'
-require 'chef/chef_fs/raw_request'
require 'chef/role'
require 'chef/node'
@@ -86,7 +85,8 @@ class Chef
def _read_hash
begin
- json = Chef::ChefFS::RawRequest.raw_request(rest, api_path)
+ # Minimize the value (get rid of defaults) so the results don't look terrible
+ minimize_value(rest_json.get(api_path))
rescue Timeout::Error => e
raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "Timeout reading: #{e}"
rescue Net::HTTPServerException => e
@@ -96,8 +96,6 @@ class Chef
raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "HTTP error reading: #{e}"
end
end
- # Minimize the value (get rid of defaults) so the results don't look terrible
- minimize_value(JSON.parse(json, :create_additions => false))
end
def chef_object
@@ -145,6 +143,10 @@ class Chef
parent.rest
end
+ def rest_json
+ parent.rest_json
+ end
+
def write(file_contents)
begin
object = JSON.parse(file_contents, :create_additions => false)
diff --git a/lib/chef/chef_fs/raw_request.rb b/lib/chef/chef_fs/raw_request.rb
deleted file mode 100644
index e017589fa4..0000000000
--- a/lib/chef/chef_fs/raw_request.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-class Chef
- module ChefFS
- module RawRequest
- def self.raw_json(chef_rest, api_path)
- api_request(chef_rest, :GET, chef_rest.create_url(api_path), {}, nil, :parse_json => true)
- end
-
- def self.raw_request(chef_rest, api_path)
- api_request(chef_rest, :GET, chef_rest.create_url(api_path))
- end
-
- def self.api_request(chef_rest, method, url, headers={}, data=nil, options = {})
- json_body = data || nil
- # json_body = data ? Chef::JSONCompat.to_json(data) : nil
- # Force encoding to binary to fix SSL related EOFErrors
- # cf. http://tickets.opscode.com/browse/CHEF-2363
- # http://redmine.ruby-lang.org/issues/5233
- # json_body.force_encoding(Encoding::BINARY) if json_body.respond_to?(:force_encoding)
- headers = build_headers(chef_rest, method, url, headers, json_body)
-
- chef_rest.retriable_rest_request(method, url, json_body, headers) do |rest_request|
- response = rest_request.call {|r| r.read_body}
-
- response_body = chef_rest.decompress_body(response)
-
- if response.kind_of?(Net::HTTPSuccess)
- if options[:parse_json] && response['content-type'] =~ /json/
- JSON.parse(response_body, :create_additions => false)
- else
- response_body
- end
- elsif redirect_location = redirected_to(response)
- if [:GET, :HEAD].include?(method)
- chef_rest.follow_redirect do
- api_request(chef_rest, method, chef_rest.create_url(redirect_location), headers, nil, options)
- end
- else
- raise Exceptions::InvalidRedirect, "#{method} request was redirected from #{url} to #{redirect_location}. Only GET and HEAD support redirects."
- end
- else
- # have to decompress the body before making an exception for it. But the body could be nil.
- response.body.replace(chef_rest.decompress_body(response)) if response.body.respond_to?(:replace)
-
- if response['content-type'] =~ /json/
- exception = response_body
- msg = "HTTP Request Returned #{response.code} #{response.message}: "
- msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s)
- Chef::Log.info(msg)
- end
- response.error!
- end
- end
- end
-
- private
-
- # Copied so that it does not automatically inflate an object
- # This is also used by knife raw_essentials
-
- ACCEPT_ENCODING = "Accept-Encoding".freeze
- ENCODING_GZIP_DEFLATE = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3".freeze
-
- def self.redirected_to(response)
- return nil unless response.kind_of?(Net::HTTPRedirection)
- # Net::HTTPNotModified is undesired subclass of Net::HTTPRedirection so test for this
- return nil if response.kind_of?(Net::HTTPNotModified)
- response['location']
- end
-
- def self.build_headers(chef_rest, method, url, headers={}, json_body=nil, raw=nil)
- # headers = @default_headers.merge(headers)
- #headers['Accept'] = "application/json" unless raw
- headers['Accept'] = "application/json" unless raw
- headers['Content-Type'] = 'application/json' if json_body
- headers['Content-Length'] = json_body.bytesize.to_s if json_body
- headers[Chef::REST::RESTRequest::ACCEPT_ENCODING] = Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE
- headers.merge!(chef_rest.authentication_headers(method, url, json_body)) if chef_rest.sign_requests?
- headers.merge!(Chef::Config[:custom_http_headers]) if Chef::Config[:custom_http_headers]
- headers
- end
- end
- end
-end
diff --git a/lib/chef/http/json_to_model_inflater.rb b/lib/chef/http/json_to_model_inflater.rb
index 0b46054510..31cd18a6b6 100644
--- a/lib/chef/http/json_to_model_inflater.rb
+++ b/lib/chef/http/json_to_model_inflater.rb
@@ -26,6 +26,9 @@ class Chef
class JSONToModelInflater
def initialize(opts={})
+ @raw_input = opts[:raw_input]
+ @raw_output = opts[:raw_output]
+ @inflate_json_class = opts.has_key?(:inflate_json_class) ? opts[:inflate_json_class] : true
end
def handle_request(method, url, headers={}, data=false)
@@ -34,7 +37,11 @@ class Chef
# Accept to the desired value before middlewares get called.
headers['Accept'] ||= "application/json"
headers["Content-Type"] = 'application/json' if data
- json_body = data ? Chef::JSONCompat.to_json(data) : nil
+ if @raw_input
+ json_body = data || nil
+ else
+ json_body = data ? Chef::JSONCompat.to_json(data) : nil
+ end
# Force encoding to binary to fix SSL related EOFErrors
# cf. http://tickets.opscode.com/browse/CHEF-2363
# http://redmine.ruby-lang.org/issues/5233
@@ -47,7 +54,16 @@ class Chef
# needed to keep conditional get stuff working correctly.
return [http_response, rest_request, return_value] if return_value == false
if http_response['content-type'] =~ /json/
- [http_response, rest_request, Chef::JSONCompat.from_json(http_response.body.chomp)]
+ if @raw_output
+ return_value = http_response.body.to_s
+ else
+ if @inflate_json_class
+ return_value = Chef::JSONCompat.from_json(http_response.body.chomp)
+ else
+ return_value = Chef::JSONCompat.from_json(http_response.body.chomp, :create_additions => false)
+ end
+ end
+ [http_response, rest_request, return_value]
else
Chef::Log.warn("Expected JSON response, but got content-type '#{http_response['content-type']}'")
return [http_response, rest_request, http_response.body.to_s]
diff --git a/lib/chef/knife/raw.rb b/lib/chef/knife/raw.rb
index 5f357c6f1e..0a491d7759 100644
--- a/lib/chef/knife/raw.rb
+++ b/lib/chef/knife/raw.rb
@@ -9,7 +9,6 @@ class Chef
require 'json'
require 'chef/rest'
require 'chef/config'
- require 'chef/chef_fs/raw_request'
end
option :method,
@@ -45,13 +44,19 @@ class Chef
if config[:input]
data = IO.read(config[:input])
end
- chef_rest = Chef::REST.new(Chef::Config[:chef_server_url])
begin
method = config[:method].to_sym
- url = chef_rest.create_url(name_args[0])
- result = Chef::ChefFS::RawRequest.api_request(chef_rest, method, url, {}, data, :parse_json => config[:pretty])
- if result.is_a?(Hash) || result.is_a?(Array)
- result = Chef::JSONCompat.to_json_pretty(result)
+ if config[:pretty]
+ chef_rest = Chef::REST.new(Chef::Config[:chef_server_url], Chef::Config[:node_name], Chef::Config[:client_key], :raw_input => true, :inflate_json_class => false)
+ url = chef_rest.create_url(name_args[0])
+ result = chef_rest.request(method, url, {}, data)
+ unless result.is_a?(String)
+ result = Chef::JSONCompat.to_json_pretty(result)
+ end
+ else
+ chef_rest = Chef::REST.new(Chef::Config[:chef_server_url], Chef::Config[:node_name], Chef::Config[:client_key], :raw_input => true, :raw_output => true)
+ url = chef_rest.create_url(name_args[0])
+ result = chef_rest.request(method, url, {}, data)
end
output result
rescue Timeout::Error => e
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index d27a139104..236e4cbac2 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -58,10 +58,13 @@ class Chef
options[:signing_key_filename] = signing_key_filename
super(url, options)
- @chef_json_inflater = JSONToModelInflater.new(options)
- @cookie_manager = CookieManager.new(options)
@decompressor = Decompressor.new(options)
@authenticator = Authenticator.new(options)
+
+ @middlewares << JSONToModelInflater.new(options)
+ @middlewares << CookieManager.new(options)
+ @middlewares << @decompressor
+ @middlewares << @authenticator
end
def signing_key_filename
@@ -117,11 +120,6 @@ class Chef
streaming_request(create_url(path), headers) {|tmp_file| yield tmp_file }
end
- # Chef::REST doesn't define middleware in the normal way for backcompat reasons, so it's hardcoded here.
- def middlewares
- [@chef_json_inflater, @cookie_manager, @decompressor, @authenticator]
- end
-
alias :api_request :request
alias :raw_http_request :send_http_request