summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/data_handler
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/chef_fs/data_handler')
-rw-r--r--lib/chef/chef_fs/data_handler/acl_data_handler.rb4
-rw-r--r--lib/chef/chef_fs/data_handler/client_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/client_key_data_handler.rb4
-rw-r--r--lib/chef/chef_fs/data_handler/container_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/cookbook_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb13
-rw-r--r--lib/chef/chef_fs/data_handler/data_handler_base.rb22
-rw-r--r--lib/chef/chef_fs/data_handler/environment_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/group_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/node_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/organization_data_handler.rb9
-rw-r--r--lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb4
-rw-r--r--lib/chef/chef_fs/data_handler/organization_members_data_handler.rb2
-rw-r--r--lib/chef/chef_fs/data_handler/policy_data_handler.rb8
-rw-r--r--lib/chef/chef_fs/data_handler/policy_group_data_handler.rb4
-rw-r--r--lib/chef/chef_fs/data_handler/role_data_handler.rb6
-rw-r--r--lib/chef/chef_fs/data_handler/user_data_handler.rb4
17 files changed, 61 insertions, 55 deletions
diff --git a/lib/chef/chef_fs/data_handler/acl_data_handler.rb b/lib/chef/chef_fs/data_handler/acl_data_handler.rb
index 6c8833004a..e64f3d245c 100644
--- a/lib/chef/chef_fs/data_handler/acl_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/acl_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
@@ -13,7 +13,7 @@ class Chef
"delete" => {},
"grant" => {},
})
- result.keys.each do |key|
+ result.each_key do |key|
result[key] = normalize_hash(result[key], { "actors" => [], "groups" => [] })
result[key]["actors"] = result[key]["actors"].sort
result[key]["groups"] = result[key]["groups"].sort
diff --git a/lib/chef/chef_fs/data_handler/client_data_handler.rb b/lib/chef/chef_fs/data_handler/client_data_handler.rb
index 5e120035ac..3e5c4f1b84 100644
--- a/lib/chef/chef_fs/data_handler/client_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/client_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/api_client"
+require_relative "data_handler_base"
+require_relative "../../api_client"
class Chef
module ChefFS
@@ -25,7 +25,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/client_key_data_handler.rb b/lib/chef/chef_fs/data_handler/client_key_data_handler.rb
index 6276413bcf..fc81a658cf 100644
--- a/lib/chef/chef_fs/data_handler/client_key_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/client_key_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/api_client"
+require_relative "data_handler_base"
+require_relative "../../api_client"
class Chef
module ChefFS
diff --git a/lib/chef/chef_fs/data_handler/container_data_handler.rb b/lib/chef/chef_fs/data_handler/container_data_handler.rb
index 04973b5135..25bdf73b04 100644
--- a/lib/chef/chef_fs/data_handler/container_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/container_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
@@ -12,7 +12,7 @@ class Chef
end
def preserve_key?(key)
- return key == "containername"
+ key == "containername"
end
# Verify that the JSON hash for this type has a key that matches its name.
@@ -24,7 +24,7 @@ class Chef
def verify_integrity(object, entry)
base_name = remove_dot_json(entry.name)
if object["containername"] != base_name
- yield("Name in #{entry.path_for_printing} must be '#{base_name}' (is '#{object['containername']}')")
+ yield("Name in #{entry.path_for_printing} must be '#{base_name}' (is '#{object["containername"]}')")
end
end
diff --git a/lib/chef/chef_fs/data_handler/cookbook_data_handler.rb b/lib/chef/chef_fs/data_handler/cookbook_data_handler.rb
index 156c1eef4e..ee1480c62e 100644
--- a/lib/chef/chef_fs/data_handler/cookbook_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/cookbook_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/cookbook/metadata"
+require_relative "data_handler_base"
+require_relative "../../cookbook/metadata"
class Chef
module ChefFS
@@ -24,7 +24,7 @@ class Chef
end
def preserve_key?(key)
- return key == "cookbook_name" || key == "version"
+ %w{cookbook_name version}.include?(key)
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb b/lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb
index c6b6449d52..4f9a7ae151 100644
--- a/lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb
@@ -1,10 +1,12 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/data_bag_item"
+require_relative "data_handler_base"
+require_relative "../../data_bag_item"
class Chef
module ChefFS
module DataHandler
class DataBagItemDataHandler < DataHandlerBase
+ RESERVED_NAMES = /^(node|role|environment|client)$/.freeze
+
def normalize(data_bag_item, entry)
# If it's wrapped with raw_data, unwrap it.
if data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
@@ -35,7 +37,7 @@ class Chef
end
def preserve_key?(key)
- return key == "id"
+ key == "id"
end
def chef_class
@@ -43,6 +45,7 @@ class Chef
end
# Verify that the JSON hash for this type has a key that matches its name.
+ # Also check that the data bag name is not a reserved search index name.
#
# @param object [Object] JSON hash of the object
# @param entry [Chef::ChefFS::FileSystem::BaseFSObject] filesystem object we are verifying
@@ -51,7 +54,9 @@ class Chef
def verify_integrity(object, entry)
base_name = remove_dot_json(entry.name)
if object["raw_data"]["id"] != base_name
- yield("ID in #{entry.path_for_printing} must be '#{base_name}' (is '#{object['raw_data']['id']}')")
+ yield("ID in #{entry.path_for_printing} must be '#{base_name}' (is '#{object["raw_data"]["id"]}')")
+ elsif RESERVED_NAMES.match?(entry.parent.name)
+ yield("Data bag name ('#{entry.parent.name}') must not match #{RESERVED_NAMES.inspect}")
end
end
diff --git a/lib/chef/chef_fs/data_handler/data_handler_base.rb b/lib/chef/chef_fs/data_handler/data_handler_base.rb
index 3668f77dd5..d51e54e8ab 100644
--- a/lib/chef/chef_fs/data_handler/data_handler_base.rb
+++ b/lib/chef/chef_fs/data_handler/data_handler_base.rb
@@ -56,19 +56,22 @@ class Chef
# 2. Put the actual values in the order of the defaults
# 3. Move any other values to the end
#
- # == Example
- #
+ # @example
# normalize_hash({x: 100, c: 2, a: 1}, { a: 10, b: 20, c: 30})
# -> { a: 1, b: 20, c: 2, x: 100}
#
def normalize_hash(object, defaults)
# Make a normalized result in the specified order for diffing
result = {}
- defaults.each_pair do |key, default|
- result[key] = object.has_key?(key) ? object[key] : default
+ defaults.each_pair do |key, value|
+ result[key] = object.is_a?(Hash) && object.key?(key) ? object[key] : value
end
- object.each_pair do |key, value|
- result[key] = value if !result.has_key?(key)
+ if object.is_a?(Hash)
+ object.each_pair do |key, value|
+ result[key] = value unless result.key?(key)
+ end
+ else
+ Chef::Log.warn "Encountered invalid object during normalization. Using these defaults #{defaults}"
end
result
end
@@ -111,7 +114,7 @@ class Chef
def from_ruby(path)
r = chef_class.new
r.from_file(path)
- r.to_hash
+ r.to_h
end
#
@@ -140,8 +143,7 @@ class Chef
# the keys specified in "keys"; anything else must be emitted by the
# caller.
#
- # == Example
- #
+ # @example
# to_ruby_keys({"name" => "foo", "environment" => "desert", "foo": "bar"}, [ "name", "environment" ])
# ->
# 'name "foo"
@@ -195,7 +197,7 @@ class Chef
def verify_integrity(object, entry)
base_name = remove_file_extension(entry.name)
if object["name"] != base_name
- yield("Name must be '#{base_name}' (is '#{object['name']}')")
+ yield("Name must be '#{base_name}' (is '#{object["name"]}')")
end
end
diff --git a/lib/chef/chef_fs/data_handler/environment_data_handler.rb b/lib/chef/chef_fs/data_handler/environment_data_handler.rb
index 68f6daee9a..e3aa242fc9 100644
--- a/lib/chef/chef_fs/data_handler/environment_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/environment_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/environment"
+require_relative "data_handler_base"
+require_relative "../../environment"
class Chef
module ChefFS
@@ -18,7 +18,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/group_data_handler.rb b/lib/chef/chef_fs/data_handler/group_data_handler.rb
index 7f38784826..3fffec5d11 100644
--- a/lib/chef/chef_fs/data_handler/group_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/group_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/api_client"
+require_relative "data_handler_base"
+require_relative "../../api_client"
class Chef
module ChefFS
@@ -41,7 +41,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/node_data_handler.rb b/lib/chef/chef_fs/data_handler/node_data_handler.rb
index 36a7bf545b..c4698afd2b 100644
--- a/lib/chef/chef_fs/data_handler/node_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/node_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/node"
+require_relative "data_handler_base"
+require_relative "../../node"
class Chef
module ChefFS
@@ -22,7 +22,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/organization_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_data_handler.rb
index 0facd5d55d..01d227ffaf 100644
--- a/lib/chef/chef_fs/data_handler/organization_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/organization_data_handler.rb
@@ -1,22 +1,21 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
module DataHandler
class OrganizationDataHandler < DataHandlerBase
def normalize(organization, entry)
- result = normalize_hash(organization, {
+ normalize_hash(organization, {
"name" => entry.org,
"full_name" => entry.org,
"org_type" => "Business",
"clientname" => "#{entry.org}-validator",
"billing_plan" => "platform-free",
})
- result
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
# Verify that the JSON hash for this type has a key that matches its name.
@@ -27,7 +26,7 @@ class Chef
# @yieldparam [s<string>] error message
def verify_integrity(object, entry)
if entry.org != object["name"]
- yield("Name must be '#{entry.org}' (is '#{object['name']}')")
+ yield("Name must be '#{entry.org}' (is '#{object["name"]}')")
end
end
end
diff --git a/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb
index c5a5f873c5..b3be2a962f 100644
--- a/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/organization_invites_data_handler.rb
@@ -1,11 +1,11 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
module DataHandler
class OrganizationInvitesDataHandler < DataHandlerBase
def normalize(invites, entry)
- invites.map { |invite| invite.is_a?(Hash) ? invite["username"] : invite }.sort.uniq
+ invites.map { |invite| invite.is_a?(Hash) ? invite["username"] : invite }.compact.sort.uniq
end
def minimize(invites, entry)
diff --git a/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb b/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb
index 8e452a413c..944d3fa0a5 100644
--- a/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/organization_members_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
diff --git a/lib/chef/chef_fs/data_handler/policy_data_handler.rb b/lib/chef/chef_fs/data_handler/policy_data_handler.rb
index fa7bbe9101..8ae749af73 100644
--- a/lib/chef/chef_fs/data_handler/policy_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/policy_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
@@ -28,18 +28,18 @@ class Chef
# Verify that the JSON hash for this type has a key that matches its name.
#
- # @param object [Object] JSON hash of the object
+ # @param object_data [Object] JSON hash of the object
# @param entry [Chef::ChefFS::FileSystem::BaseFSObject] filesystem object we are verifying
# @yield [s] callback to handle errors
# @yieldparam [s<string>] error message
def verify_integrity(object_data, entry)
name, revision = name_and_revision(entry.name)
if object_data["name"] != name
- yield("Object name '#{object_data['name']}' doesn't match entry '#{name}'.")
+ yield("Object name '#{object_data["name"]}' doesn't match entry '#{name}'.")
end
if object_data["revision_id"] != revision
- yield("Object revision ID '#{object_data['revision_id']}' doesn't match entry '#{revision}'.")
+ yield("Object revision ID '#{object_data["revision_id"]}' doesn't match entry '#{revision}'.")
end
end
end
diff --git a/lib/chef/chef_fs/data_handler/policy_group_data_handler.rb b/lib/chef/chef_fs/data_handler/policy_group_data_handler.rb
index f7aa92373c..bb0d4efb1c 100644
--- a/lib/chef/chef_fs/data_handler/policy_group_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/policy_group_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
@@ -17,7 +17,7 @@ class Chef
# Verify that the JSON hash for this type has a key that matches its name.
#
- # @param object [Object] JSON hash of the object
+ # @param object_data [Object] JSON hash of the object
# @param entry [Chef::ChefFS::FileSystem::BaseFSObject] filesystem object we are verifying
# @yield [s] callback to handle errors
# @yieldparam [s<string>] error message
diff --git a/lib/chef/chef_fs/data_handler/role_data_handler.rb b/lib/chef/chef_fs/data_handler/role_data_handler.rb
index b09c146a5d..1007a29c8a 100644
--- a/lib/chef/chef_fs/data_handler/role_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/role_data_handler.rb
@@ -1,5 +1,5 @@
-require "chef/chef_fs/data_handler/data_handler_base"
-require "chef/role"
+require_relative "data_handler_base"
+require_relative "../../role"
class Chef
module ChefFS
@@ -24,7 +24,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
def chef_class
diff --git a/lib/chef/chef_fs/data_handler/user_data_handler.rb b/lib/chef/chef_fs/data_handler/user_data_handler.rb
index c2df4db49d..50a0c0682d 100644
--- a/lib/chef/chef_fs/data_handler/user_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/user_data_handler.rb
@@ -1,4 +1,4 @@
-require "chef/chef_fs/data_handler/data_handler_base"
+require_relative "data_handler_base"
class Chef
module ChefFS
@@ -19,7 +19,7 @@ class Chef
end
def preserve_key?(key)
- return key == "name"
+ key == "name"
end
# There is no chef_class for users, nor does to_ruby work.