diff options
Diffstat (limited to 'lib/chef_zero/endpoints')
22 files changed, 42 insertions, 26 deletions
diff --git a/lib/chef_zero/endpoints/acl_endpoint.rb b/lib/chef_zero/endpoints/acl_endpoint.rb index e389f2d..d5010f9 100644 --- a/lib/chef_zero/endpoints/acl_endpoint.rb +++ b/lib/chef_zero/endpoints/acl_endpoint.rb @@ -23,6 +23,7 @@ module ChefZero if !acl_path || !%w{read create update delete grant}.include?(perm) raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}") end + [acl_path, perm] end diff --git a/lib/chef_zero/endpoints/acls_endpoint.rb b/lib/chef_zero/endpoints/acls_endpoint.rb index 64840cd..48759c2 100644 --- a/lib/chef_zero/endpoints/acls_endpoint.rb +++ b/lib/chef_zero/endpoints/acls_endpoint.rb @@ -17,9 +17,10 @@ module ChefZero path = request.rest_path[0..-2] # Strip off _acl path = path[0..1] if path.size == 3 && path[0] == "organizations" && %w{organization organizations}.include?(path[2]) acl_path = ChefData::AclPath.get_acl_data_path(path) - if !acl_path + unless acl_path raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}") end + acls = FFI_Yajl::Parser.parse(get_data(request, acl_path)) acls = ChefData::DataNormalizer.normalize_acls(acls) if request.query_params["detail"] == "granular" diff --git a/lib/chef_zero/endpoints/actor_default_key_endpoint.rb b/lib/chef_zero/endpoints/actor_default_key_endpoint.rb index 1985dea..a13204b 100644 --- a/lib/chef_zero/endpoints/actor_default_key_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_default_key_endpoint.rb @@ -53,6 +53,7 @@ module ChefZero def actor_path(request) return request.rest_path[0..3] if request.rest_path[2] == "clients" + request.rest_path[0..1] end diff --git a/lib/chef_zero/endpoints/actor_endpoint.rb b/lib/chef_zero/endpoints/actor_endpoint.rb index 4cba31a..8c79f9a 100644 --- a/lib/chef_zero/endpoints/actor_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_endpoint.rb @@ -125,7 +125,7 @@ module ChefZero def rename_keys!(request, new_client_or_user_name) orig_keys_path = keys_path_base(request) new_keys_path = orig_keys_path.dup - .tap { |path| path[-2] = new_client_or_user_name } + .tap { |path| path[-2] = new_client_or_user_name } key_names = list_data_or_else(request, orig_keys_path, nil) return unless key_names # No keys to move diff --git a/lib/chef_zero/endpoints/actor_keys_endpoint.rb b/lib/chef_zero/endpoints/actor_keys_endpoint.rb index b3bab9a..89528b2 100644 --- a/lib/chef_zero/endpoints/actor_keys_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_keys_endpoint.rb @@ -50,7 +50,7 @@ module ChefZero response_body["private_key"] = private_key if generate_keys json_response(201, response_body, - headers: { "Location" => response_body["uri"] }) + headers: { "Location" => response_body["uri"] }) end private @@ -117,6 +117,7 @@ module ChefZero def actor_path(request) return request.rest_path[0..3] if client?(request) + request.rest_path[0..1] end diff --git a/lib/chef_zero/endpoints/authenticate_user_endpoint.rb b/lib/chef_zero/endpoints/authenticate_user_endpoint.rb index e54b8f1..44ed24e 100644 --- a/lib/chef_zero/endpoints/authenticate_user_endpoint.rb +++ b/lib/chef_zero/endpoints/authenticate_user_endpoint.rb @@ -19,6 +19,7 @@ module ChefZero if user["password"] != password raise RestErrorResponse.new(401, "Bad username or password") end + # Include only particular user data in the response user.keep_if { |key, value| %w{first_name last_name display_name email username}.include?(key) } json_response(200, { diff --git a/lib/chef_zero/endpoints/containers_endpoint.rb b/lib/chef_zero/endpoints/containers_endpoint.rb index e8708ed..2260765 100644 --- a/lib/chef_zero/endpoints/containers_endpoint.rb +++ b/lib/chef_zero/endpoints/containers_endpoint.rb @@ -15,7 +15,7 @@ module ChefZero data = parse_json(request.body) # if they don't match, id wins. container_name = data["id"] || data["containername"] - container_path_suffix = data["containerpath"].split("/").reject { |o| o.empty? } + container_path_suffix = data["containerpath"].split("/").reject(&:empty?) create_data(request, request.rest_path, container_name, to_json({}), :create_dir) json_response(201, { uri: build_uri(request.base_uri, request.rest_path + container_path_suffix + [container_name]) }) diff --git a/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb b/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb index 9f7b973..1c2d6a7 100644 --- a/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb @@ -60,8 +60,8 @@ module ChefZero def normalize(request, cookbook_artifact_data) cookbook = parse_json(cookbook_artifact_data) ChefData::DataNormalizer.normalize_cookbook(self, request.rest_path[0..1], - cookbook, request.rest_path[3], request.rest_path[4], - request.base_uri, request.method, true, api_version: request.api_version) + cookbook, request.rest_path[3], request.rest_path[4], + request.base_uri, request.method, true, api_version: request.api_version) end end end diff --git a/lib/chef_zero/endpoints/cookbook_version_endpoint.rb b/lib/chef_zero/endpoints/cookbook_version_endpoint.rb index 0ee5be7..5ec1aa4 100644 --- a/lib/chef_zero/endpoints/cookbook_version_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_version_endpoint.rb @@ -27,9 +27,10 @@ module ChefZero if request.query_params["force"] != "true" raise RestErrorResponse.new(409, "The cookbook #{name} at version #{version} is frozen. Use the 'force' option to override.") end + # For some reason, you are forever unable to modify "frozen?" on a frozen cookbook. request_body = FFI_Yajl::Parser.parse(request.body) - if !request_body["frozen?"] + unless request_body["frozen?"] request_body["frozen?"] = true request.body = FFI_Yajl::Encoder.encode(request_body, pretty: true) end @@ -92,6 +93,7 @@ module ChefZero rescue ChefZero::DataStore::DataNotFoundError # Not all chef versions support cookbook_artifacts raise unless cookbook_type == "cookbook_artifacts" + cookbooks = [] end cookbooks.each do |cookbook_name| diff --git a/lib/chef_zero/endpoints/cookbooks_base.rb b/lib/chef_zero/endpoints/cookbooks_base.rb index e515992..331f504 100644 --- a/lib/chef_zero/endpoints/cookbooks_base.rb +++ b/lib/chef_zero/endpoints/cookbooks_base.rb @@ -54,6 +54,7 @@ module ChefZero versions = [] cookbooks_list[name].sort_by { |version| Gem::Version.new(version.dup) }.reverse_each do |version| break if num_versions && versions.size >= num_versions + if constraint.satisfied_by?(Gem::Version.new(version.dup)) versions << version end @@ -66,6 +67,7 @@ module ChefZero cookbook["all_files"].inject([]) do |acc, file| part, name = file["name"].split("/") next acc unless part == "recipes" || File.extname(name) != ".rb" + if name == "default.rb" acc << cookbook_name else diff --git a/lib/chef_zero/endpoints/data_bags_endpoint.rb b/lib/chef_zero/endpoints/data_bags_endpoint.rb index 5b9dd14..f90b7d7 100644 --- a/lib/chef_zero/endpoints/data_bags_endpoint.rb +++ b/lib/chef_zero/endpoints/data_bags_endpoint.rb @@ -10,7 +10,7 @@ module ChefZero json = FFI_Yajl::Parser.parse(contents) name = identity_keys.map { |k| json[k] }.select { |v| v }.first if name.nil? - error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") + error(400, "Must specify #{identity_keys.map(&:inspect).join(" or ")} in JSON") elsif exists_data_dir?(request, request.rest_path[0..1] + ["data", name]) error(409, "Object already exists") else diff --git a/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb b/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb index 43ef4ce..8023d69 100644 --- a/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb +++ b/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb @@ -15,12 +15,14 @@ module ChefZero run_list = FFI_Yajl::Parser.parse(request.body)["run_list"] run_list.each do |run_list_entry| if run_list_entry =~ /(.+)::.+\@(.+)/ || run_list_entry =~ /(.+)\@(.+)/ - raise RestErrorResponse.new(412, "No such cookbook: #{$1}") if !cookbook_names.include?($1) - raise RestErrorResponse.new(412, "No such cookbook version for cookbook #{$1}: #{$2}") if !list_data(request, request.rest_path[0..1] + ["cookbooks", $1]).include?($2) + raise RestErrorResponse.new(412, "No such cookbook: #{$1}") unless cookbook_names.include?($1) + raise RestErrorResponse.new(412, "No such cookbook version for cookbook #{$1}: #{$2}") unless list_data(request, request.rest_path[0..1] + ["cookbooks", $1]).include?($2) + desired_versions[$1] = [ $2 ] else desired_cookbook = run_list_entry.split("::")[0] - raise RestErrorResponse.new(412, "No such cookbook: #{desired_cookbook}") if !cookbook_names.include?(desired_cookbook) + raise RestErrorResponse.new(412, "No such cookbook: #{desired_cookbook}") unless cookbook_names.include?(desired_cookbook) + desired_versions[desired_cookbook] = list_data(request, request.rest_path[0..1] + ["cookbooks", desired_cookbook]) end end @@ -35,7 +37,7 @@ module ChefZero # Depsolve! solved = depsolve(request, desired_versions.keys, desired_versions, environment_constraints) - if !solved + unless solved if @last_missing_dep && !cookbook_names.include?(@last_missing_dep) return raise RestErrorResponse.new(412, "No such cookbook: #{@last_missing_dep}") elsif @last_constraint_failure @@ -64,7 +66,7 @@ module ChefZero # If everything is already solve_for = unsolved[0] - return desired_versions if !solve_for + return desired_versions unless solve_for # Go through each desired version of this cookbook, starting with the latest, # until we find one we can solve successfully with @@ -81,10 +83,10 @@ module ChefZero cookbook_dependencies.each_pair do |dep_name, dep_constraint| # If the dep is not already in the list, add it to the list to solve # and bring in all environment-allowed cookbook versions to desired_versions - if !new_desired_versions.key?(dep_name) + unless new_desired_versions.key?(dep_name) new_unsolved += [dep_name] # If the dep is missing, we will try other versions of the cookbook that might not have the bad dep. - if !exists_data_dir?(request, request.rest_path[0..1] + ["cookbooks", dep_name]) + unless exists_data_dir?(request, request.rest_path[0..1] + ["cookbooks", dep_name]) @last_missing_dep = dep_name.to_s dep_not_found = true break @@ -110,7 +112,8 @@ module ChefZero end def filter_by_constraint(versions, cookbook_name, constraint) - return versions if !constraint + return versions unless constraint + constraint = Gem::Requirement.new(constraint) new_versions = versions[cookbook_name] new_versions = new_versions.select { |version| constraint.satisfied_by?(Gem::Version.new(version.dup)) } diff --git a/lib/chef_zero/endpoints/not_found_endpoint.rb b/lib/chef_zero/endpoints/not_found_endpoint.rb index 1c18f8b..160d4a2 100644 --- a/lib/chef_zero/endpoints/not_found_endpoint.rb +++ b/lib/chef_zero/endpoints/not_found_endpoint.rb @@ -4,7 +4,7 @@ module ChefZero module Endpoints class NotFoundEndpoint def call(request) - [404, { "Content-Type" => "application/json" }, FFI_Yajl::Encoder.encode({ "error" => ["Object not found: #{request.env['REQUEST_PATH']}"] }, pretty: true)] + [404, { "Content-Type" => "application/json" }, FFI_Yajl::Encoder.encode({ "error" => ["Object not found: #{request.env["REQUEST_PATH"]}"] }, pretty: true)] end end end diff --git a/lib/chef_zero/endpoints/organization_association_request_endpoint.rb b/lib/chef_zero/endpoints/organization_association_request_endpoint.rb index c73d816..65caf56 100644 --- a/lib/chef_zero/endpoints/organization_association_request_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_association_request_endpoint.rb @@ -11,6 +11,7 @@ module ChefZero if id !~ /(.+)-#{orgname}$/ raise HttpErrorResponse.new(404, "Invalid ID #{id}. Must be of the form username-#{orgname}") end + username = $1 path = request.rest_path[0..-2] + [username] delete_data(request, path) diff --git a/lib/chef_zero/endpoints/organization_endpoint.rb b/lib/chef_zero/endpoints/organization_endpoint.rb index 3c14548..d71d9ad 100644 --- a/lib/chef_zero/endpoints/organization_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_endpoint.rb @@ -19,8 +19,9 @@ module ChefZero save_org = FFI_Yajl::Encoder.encode(org, pretty: true) if new_org["name"] != request.rest_path[-1] # This is a rename - return error(400, "Cannot rename org #{request.rest_path[-1]} to #{new_org['name']}: rename not supported for orgs") + return error(400, "Cannot rename org #{request.rest_path[-1]} to #{new_org["name"]}: rename not supported for orgs") end + set_data(request, request.rest_path + [ "org" ], save_org) json_response(200, { "uri" => (build_uri(request.base_uri, request.rest_path)).to_s, diff --git a/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb b/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb index a34da32..b69cfe3 100644 --- a/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb +++ b/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb @@ -48,7 +48,7 @@ module ChefZero # If the policy revision being submitted does not exist, create it. # Storage: /organizations/ORG/policies/POLICY/revisions/REVISION policyfile_path = request.rest_path[0..1] + ["policies", policy_name, "revisions", revision_id] - if !exists_data?(request, policyfile_path) + unless exists_data?(request, policyfile_path) create_data(request, policyfile_path[0..-2], revision_id, request.body, :create_dir) end diff --git a/lib/chef_zero/endpoints/rest_list_endpoint.rb b/lib/chef_zero/endpoints/rest_list_endpoint.rb index 135f0e9..28af8d4 100644 --- a/lib/chef_zero/endpoints/rest_list_endpoint.rb +++ b/lib/chef_zero/endpoints/rest_list_endpoint.rb @@ -26,7 +26,7 @@ module ChefZero contents = request.body key = get_key(contents) if key.nil? - error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") + error(400, "Must specify #{identity_keys.map(&:inspect).join(" or ")} in JSON") else create_data(request, request.rest_path, key, contents) json_response(201, { "uri" => (build_uri(request.base_uri, request.rest_path + [key])).to_s }) diff --git a/lib/chef_zero/endpoints/rest_object_endpoint.rb b/lib/chef_zero/endpoints/rest_object_endpoint.rb index 5d6ae40..4fc2ef1 100644 --- a/lib/chef_zero/endpoints/rest_object_endpoint.rb +++ b/lib/chef_zero/endpoints/rest_object_endpoint.rb @@ -70,6 +70,7 @@ module ChefZero # Does this request change the value of the identity key? def is_rename?(request) return false unless (key = identity_key_value(request)) + key != request.rest_path[-1] end end diff --git a/lib/chef_zero/endpoints/sandbox_endpoint.rb b/lib/chef_zero/endpoints/sandbox_endpoint.rb index d7eb4de..aedb665 100644 --- a/lib/chef_zero/endpoints/sandbox_endpoint.rb +++ b/lib/chef_zero/endpoints/sandbox_endpoint.rb @@ -9,7 +9,7 @@ module ChefZero def put(request) existing_sandbox = FFI_Yajl::Parser.parse(get_data(request)) existing_sandbox["checksums"].each do |checksum| - if !exists_data?(request, request.rest_path[0..1] + ["file_store", "checksums", checksum]) + unless exists_data?(request, request.rest_path[0..1] + ["file_store", "checksums", checksum]) raise RestErrorResponse.new(503, "Checksum not uploaded: #{checksum}") end end diff --git a/lib/chef_zero/endpoints/search_endpoint.rb b/lib/chef_zero/endpoints/search_endpoint.rb index e2f30ce..c341074 100644 --- a/lib/chef_zero/endpoints/search_endpoint.rb +++ b/lib/chef_zero/endpoints/search_endpoint.rb @@ -30,7 +30,7 @@ module ChefZero if path.size > 0 value = search_value path.each do |path_part| - value = value[path_part] if !value.nil? + value = value[path_part] unless value.nil? end data[key] = value else @@ -180,7 +180,7 @@ module ChefZero dest when Hash source.each do |src_key, src_value| - if dest.kind_of?(Hash) + if dest.is_a?(Hash) if dest[src_key] dest[src_key] = deep_merge!(src_value, dest[src_key]) else # dest[src_key] doesn't exist so we take whatever source has @@ -191,7 +191,7 @@ module ChefZero end end when Array - if dest.kind_of?(Array) + if dest.is_a?(Array) dest |= source else dest = source diff --git a/lib/chef_zero/endpoints/system_recovery_endpoint.rb b/lib/chef_zero/endpoints/system_recovery_endpoint.rb index 0ad2293..0f77a8c 100644 --- a/lib/chef_zero/endpoints/system_recovery_endpoint.rb +++ b/lib/chef_zero/endpoints/system_recovery_endpoint.rb @@ -10,13 +10,13 @@ module ChefZero name = request_json["username"] password = request_json["password"] user = get_data(request, request.rest_path[0..-2] + ["users", name], :nil) - if !user + unless user raise RestErrorResponse.new(403, "Nonexistent user") end user = FFI_Yajl::Parser.parse(user) user = ChefData::DataNormalizer.normalize_user(user, name, [ "username" ], server.options[:osc_compat]) - if !user["recovery_authentication_enabled"] + unless user["recovery_authentication_enabled"] raise RestErrorResponse.new(403, "Only users with recovery_authentication_enabled=true may use /system_recovery to log in") end if user["password"] != password diff --git a/lib/chef_zero/endpoints/user_association_request_endpoint.rb b/lib/chef_zero/endpoints/user_association_request_endpoint.rb index 580ba49..70dd189 100644 --- a/lib/chef_zero/endpoints/user_association_request_endpoint.rb +++ b/lib/chef_zero/endpoints/user_association_request_endpoint.rb @@ -11,6 +11,7 @@ module ChefZero if id !~ /^#{username}-(.+)/ raise RestErrorResponse.new(400, "Association request #{id} is invalid. Must be #{username}-orgname.") end + orgname = $1 json = FFI_Yajl::Parser.parse(request.body) |