Skip to content

Commit c6ab55d

Browse files
author
Tim Hinderliter
committed
Merge branch 'PL-538' into pl-master
2 parents 7c0d183 + 572e68c commit c6ab55d

102 files changed

Lines changed: 331 additions & 210 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎chef-server-api/app/controllers/application.rb‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require "chef/mixin/checksum"
2323
require "chef/cookbook_loader"
2424
require "mixlib/authentication/signatureverification"
25+
require "chef/json"
2526

2627
class Application < Merb::Controller
2728

@@ -102,11 +103,11 @@ def redirect_back_or_default(default)
102103
def access_denied
103104
raise Unauthorized, "You must authenticate first!"
104105
end
105-
106+
106107
def get_available_recipes
107108
all_cookbooks_list = Chef::CookbookVersion.cdb_list(true)
108109
available_recipes = all_cookbooks_list.sort{ |a,b| a.name.to_s <=> b.name.to_s }.inject([]) do |result, element|
109-
element.recipes.sort.each do |r|
110+
element.recipes.sort.each do |r|
110111
if r =~ /^(.+)::default$/
111112
result << $1
112113
else
@@ -118,5 +119,19 @@ def get_available_recipes
118119
available_recipes
119120
end
120121

122+
# Fix CHEF-1292/PL-538; cause Merb to pass the max nesting constant into
123+
# obj.to_json, which it calls by default based on the original request's
124+
# accept headers and the type passed into Merb::Controller.display
125+
#
126+
# TODO: tim, 2010-11-24: would be nice to instead have Merb call
127+
# Chef::JSON.to_json, instead of obj.to_json, but changing that
128+
# behavior is convoluted in Merb. This override is assuming that
129+
# Merb is eventually calling obj.to_json, which takes the :max_nesting
130+
# option.
131+
override! :display
132+
def display(obj)
133+
super(obj, nil, {:max_nesting => Chef::JSON::JSON_MAX_NESTING})
134+
end
135+
121136
end
122137

‎chef-server-api/config/router.rb‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,37 @@
102102

103103
match('/').to(:controller => 'main', :action =>'index').name(:top)
104104

105+
# Need to monkey patch Merb so that it inflates JSON input with a higher
106+
# recursion depth allowed (the default is 19). See CHEF-1292/PL-538.
107+
module Merb
108+
class Request
109+
# ==== Returns
110+
# Hash:: Parameters from body if this is a JSON request.
111+
#
112+
# ==== Notes
113+
# If the JSON object parses as a Hash, it will be merged with the
114+
# parameters hash. If it parses to anything else (such as an Array, or
115+
# if it inflates to an Object) it will be accessible via the inflated_object
116+
# parameter.
117+
#
118+
# :api: private
119+
def json_params
120+
@json_params ||= begin
121+
if Merb::Const::JSON_MIME_TYPE_REGEXP.match(content_type)
122+
begin
123+
# Call Chef's JSON utility instead of the default in Merb,
124+
# JSON.parse.
125+
jobj = Chef::JSON.from_json(raw_post)
126+
jobj = jobj.to_mash if jobj.is_a?(Hash)
127+
rescue JSON::ParserError
128+
jobj = Mash.new
129+
end
130+
jobj.is_a?(Hash) ? jobj : { :inflated_object => jobj }
131+
end
132+
end
133+
end
134+
135+
end
136+
end
137+
105138
end

‎chef-server-webui/app/controllers/databag_items.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def update
3838
begin
3939
@databag_item = Chef::DataBagItem.new
4040
@databag_item.data_bag params[:databag_id]
41-
@databag_item.raw_data = JSON.parse(params[:json_data])
41+
@databag_item.raw_data = Chef::JSON.from_json(params[:json_data])
4242
raise ArgumentError, "Updating id is not allowed" unless @databag_item.raw_data['id'] == params[:id] #to be consistent with other objects, changing id is not allowed.
4343
@databag_item.save
4444
redirect(url(:databag_databag_items, :databag_id => params[:databag_id], :id => @databag_item.name), :message => { :notice => "Updated Databag Item #{@databag_item.name}" })
@@ -61,7 +61,7 @@ def create
6161
@databag_name = params[:databag_id]
6262
@databag_item = Chef::DataBagItem.new
6363
@databag_item.data_bag @databag_name
64-
@databag_item.raw_data = JSON.parse(params[:json_data])
64+
@databag_item.raw_data = Chef::JSON.from_json(params[:json_data])
6565
@databag_item.create
6666
redirect(url(:databag_databag_items, :databag_id => @databag_name), :message => { :notice => "Databag item created successfully" })
6767
rescue => e

‎chef-server-webui/app/controllers/nodes.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ def create
8989
begin
9090
@node = Chef::Node.new
9191
@node.name params[:name]
92-
@node.normal_attrs = JSON.parse(params[:attributes])
92+
@node.normal_attrs = Chef::JSON.from_json(params[:attributes])
9393
@node.run_list.reset!(params[:for_node] ? params[:for_node] : [])
9494
raise ArgumentError, "Node name cannot be blank" if (params[:name].nil? || params[:name].length==0)
9595
@node.create
9696
redirect(url(:nodes), :message => { :notice => "Created Node #{@node.name}" })
9797
rescue => e
9898
Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
99-
@node.normal_attrs = JSON.parse(params[:attributes])
99+
@node.normal_attrs = Chef::JSON.from_json(params[:attributes])
100100
@available_recipes = get_available_recipes
101101
@available_roles = Chef::Role.list.keys.sort
102102
@node.run_list params[:for_node]
@@ -110,7 +110,7 @@ def update
110110
begin
111111
@node = Chef::Node.load(params[:id])
112112
@node.run_list.reset!(params[:for_node] ? params[:for_node] : [])
113-
@node.normal_attrs = JSON.parse(params[:attributes])
113+
@node.normal_attrs = Chef::JSON.from_json(params[:attributes])
114114
@node.save
115115
@_message = { :notice => "Updated Node" }
116116
render :show

‎chef-server-webui/app/controllers/roles.rb‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ def create
9090
@role.name(params[:name])
9191
@role.run_list(params[:for_role] ? params[:for_role] : [])
9292
@role.description(params[:description]) if params[:description] != ''
93-
@role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != ''
94-
@role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != ''
93+
@role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != ''
94+
@role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != ''
9595
@role.create
9696
redirect(url(:roles), :message => { :notice => "Created Role #{@role.name}" })
9797
rescue => e
9898
Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
9999
@available_recipes = get_available_recipes
100100
@available_roles = Chef::Role.list.keys.sort
101101
@role = Chef::Role.new
102-
@role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != ''
103-
@role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != ''
102+
@role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != ''
103+
@role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != ''
104104
@run_list = Chef::RunList.new.reset!(Array(params[:for_role]))
105105
@_message = { :error => "Could not create role" }
106106
render :new
@@ -113,8 +113,8 @@ def update
113113
@role = Chef::Role.load(params[:id])
114114
@role.run_list(params[:for_role] ? params[:for_role] : [])
115115
@role.description(params[:description]) if params[:description] != ''
116-
@role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != ''
117-
@role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != ''
116+
@role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != ''
117+
@role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != ''
118118
@role.save
119119
@_message = { :notice => "Updated Role" }
120120
render :show
@@ -124,8 +124,8 @@ def update
124124
@available_roles = Chef::Role.list.keys.sort
125125
@run_list = Chef::RunList.new.reset!( Array(params[:for_role]))
126126
Chef::Log.error(@run_list.inspect)
127-
@role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != ''
128-
@role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != ''
127+
@role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != ''
128+
@role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != ''
129129
@_message = {:error => "Could not update role #{params[:id]}"}
130130
render :edit
131131
end

‎chef/lib/chef/api_client.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
require 'chef/certificate'
2525
require 'chef/index_queue'
2626
require 'extlib'
27-
require 'json'
27+
require 'chef/json'
2828

2929
class Chef
3030
class ApiClient

‎chef/lib/chef/application/client.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def reconfigure
181181
end
182182

183183
begin
184-
@chef_client_json = JSON.parse(json_io.read)
184+
@chef_client_json = Chef::JSON.from_json(json_io.read)
185185
json_io.close unless json_io.closed?
186186
rescue JSON::ParserError => error
187187
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)

‎chef/lib/chef/application/solo.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def reconfigure
145145
end
146146

147147
begin
148-
@chef_solo_json = JSON.parse(json_io.read)
148+
@chef_solo_json = Chef::JSON.from_json(json_io.read)
149149
json_io.close unless json_io.closed?
150150
rescue JSON::ParserError => error
151151
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)

‎chef/lib/chef/cookbook/metadata.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ def from_hash(o)
430430
end
431431

432432
def self.from_json(string)
433-
o = JSON.parse(string)
433+
o = Chef::JSON.from_json(string)
434434
self.from_hash(o)
435435
end
436436

437437
def from_json(string)
438-
o = JSON.parse(string)
438+
o = Chef::JSON.from_json(string)
439439
from_hash(o)
440440
end
441441

‎chef/lib/chef/couchdb.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
require 'chef/rest'
2222
require 'chef/log'
2323
require 'digest/sha2'
24-
require 'json'
24+
require 'chef/json'
2525

2626
# We want to fail on create if uuidtools isn't installed
2727
begin

0 commit comments

Comments
 (0)