diff options
author | Tyler Ball <tyler-ball@users.noreply.github.com> | 2015-08-12 12:12:16 -0600 |
---|---|---|
committer | Tyler Ball <tyler-ball@users.noreply.github.com> | 2015-08-12 12:12:16 -0600 |
commit | 3116c8cc30232103065e4bdf85e043ca20312197 (patch) | |
tree | a291011c455b191e98d32ef3db1246d2e99cf926 /lib | |
parent | 28fe2928469885b0138de4d4270c6eccac8ab482 (diff) | |
parent | 67b0d4b3ae451ffa79d4c358e9a0c281822d4743 (diff) | |
download | chef-zero-3116c8cc30232103065e4bdf85e043ca20312197.tar.gz |
Merge pull request #117 from chef/mp/merge-pedants
CS12 Support
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef_zero/chef_data/data_normalizer.rb | 6 | ||||
-rw-r--r-- | lib/chef_zero/chef_data/default_creator.rb | 2 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/actor_endpoint.rb | 10 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/node_identifiers_endpoint.rb | 22 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_association_requests_endpoint.rb | 17 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_endpoint.rb | 13 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_user_base.rb | 29 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_user_endpoint.rb | 24 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_users_endpoint.rb | 8 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organizations_endpoint.rb | 7 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/search_endpoint.rb | 14 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/user_organizations_endpoint.rb | 2 | ||||
-rw-r--r-- | lib/chef_zero/server.rb | 2 |
13 files changed, 101 insertions, 55 deletions
diff --git a/lib/chef_zero/chef_data/data_normalizer.rb b/lib/chef_zero/chef_data/data_normalizer.rb index ab45d76..404db03 100644 --- a/lib/chef_zero/chef_data/data_normalizer.rb +++ b/lib/chef_zero/chef_data/data_normalizer.rb @@ -14,11 +14,11 @@ module ChefZero acls end - def self.normalize_client(client, name) + def self.normalize_client(client, name, orgname = nil) client['name'] ||= name - client['admin'] ||= false - client['admin'] = !!client['admin'] + client['clientname'] ||= name client['public_key'] ||= PUBLIC_KEY + client['orgname'] ||= orgname client['validator'] ||= false client['validator'] = !!client['validator'] client['json_class'] ||= "Chef::ApiClient" diff --git a/lib/chef_zero/chef_data/default_creator.rb b/lib/chef_zero/chef_data/default_creator.rb index 60b495a..d1a0118 100644 --- a/lib/chef_zero/chef_data/default_creator.rb +++ b/lib/chef_zero/chef_data/default_creator.rb @@ -160,7 +160,7 @@ module ChefZero 'users' => {}, 'org' => {}, - 'containers' => %w(clients containers cookbooks data environments groups nodes roles sandboxes), + 'containers' => %w(clients containers cookbooks data environments groups nodes roles sandboxes policies cookbook_artifacts), 'groups' => %w(admins billing-admins clients users), 'association_requests' => {} } diff --git a/lib/chef_zero/endpoints/actor_endpoint.rb b/lib/chef_zero/endpoints/actor_endpoint.rb index 342619c..1572ac1 100644 --- a/lib/chef_zero/endpoints/actor_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_endpoint.rb @@ -65,7 +65,13 @@ module ChefZero else response = FFI_Yajl::Parser.parse(result[2], :create_additions => false) end - response['private_key'] = private_key if private_key + + if request.rest_path[2] == 'clients' + response['private_key'] = private_key ? private_key : false + else + response['private_key'] = private_key if private_key + end + response.delete('public_key') if !updating_public_key && request.rest_path[2] == 'users' response.delete('password') json_response(result[0], response) @@ -77,7 +83,7 @@ module ChefZero def populate_defaults(request, response_json) response = FFI_Yajl::Parser.parse(response_json, :create_additions => false) if request.rest_path[2] == 'clients' - response = ChefData::DataNormalizer.normalize_client(response, request.rest_path[3]) + response = ChefData::DataNormalizer.normalize_client(response,request.rest_path[3], request.rest_path[1]) else response = ChefData::DataNormalizer.normalize_user(response, request.rest_path[3], identity_keys, server.options[:osc_compat], request.method) end diff --git a/lib/chef_zero/endpoints/node_identifiers_endpoint.rb b/lib/chef_zero/endpoints/node_identifiers_endpoint.rb new file mode 100644 index 0000000..9f89a98 --- /dev/null +++ b/lib/chef_zero/endpoints/node_identifiers_endpoint.rb @@ -0,0 +1,22 @@ +require 'ffi_yajl' +require 'chef_zero/rest_base' +require 'uuidtools' + +module ChefZero + module Endpoints + # /organizations/NAME/nodes/NAME/_identifiers + class NodeIdentifiersEndpoint < RestBase + def get(request) + if get_data(request, request.rest_path[0..3]) + result = { + :id => UUIDTools::UUID.parse_raw(request.rest_path[0..4].to_s).to_s.gsub('-',''), + :authz_id => '0'*32, + :org_id => UUIDTools::UUID.parse_raw(request.rest_path[0..1].to_s).to_s.gsub('-','') } + json_response(200, result) + else + raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}") + end + end + end + end +end diff --git a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb index 608d548..e1b6a7e 100644 --- a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb @@ -6,23 +6,14 @@ module ChefZero # /organizations/ORG/association_requests class OrganizationAssociationRequestsEndpoint < RestBase def post(request) - json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) - username = json['user'] - orgname = request.rest_path[1] - id = "#{username}-#{orgname}" - - if exists_data?(request, [ 'organizations', orgname, 'users', username ]) - raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") - end - - create_data(request, request.rest_path, username, '{}') - json_response(201, { "uri" => build_uri(request.base_uri, request.rest_path + [ id ]) }) + ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'user') end def get(request) orgname = request.rest_path[1] - result = list_data(request).map { |username| { "id" => "#{username}-#{orgname}", 'username' => username } } - json_response(200, result) + ChefZero::Endpoints::OrganizationUserBase.get(self, request) do |username| + { "id" => "#{username}-#{orgname}", 'username' => username } + end end end end diff --git a/lib/chef_zero/endpoints/organization_endpoint.rb b/lib/chef_zero/endpoints/organization_endpoint.rb index bc5c3f0..a5512db 100644 --- a/lib/chef_zero/endpoints/organization_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_endpoint.rb @@ -16,18 +16,23 @@ module ChefZero new_org.each do |key, value| org[key] = value end - org = FFI_Yajl::Encoder.encode(org, :pretty => true) + 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") end - set_data(request, request.rest_path + [ 'org' ], org) - json_response(200, "uri" => "#{build_uri(request.base_uri, request.rest_path)}") + set_data(request, request.rest_path + [ 'org' ], save_org) + json_response(200, { + "uri" => "#{build_uri(request.base_uri, request.rest_path)}", + "name" => org['name'], + "org_type" => org['org_type'], + "full_name" => org['full_name'] + }) end def delete(request) org = get_data(request, request.rest_path + [ 'org' ]) - delete_data_dir(request, request.rest_path) + delete_data_dir(request, request.rest_path, :recursive) already_json_response(200, populate_defaults(request, org)) end diff --git a/lib/chef_zero/endpoints/organization_user_base.rb b/lib/chef_zero/endpoints/organization_user_base.rb new file mode 100644 index 0000000..aaa2e3a --- /dev/null +++ b/lib/chef_zero/endpoints/organization_user_base.rb @@ -0,0 +1,29 @@ +require 'ffi_yajl' +require 'chef_zero/rest_base' + +module ChefZero + module Endpoints + module OrganizationUserBase + + def self.get(obj, request, &block) + result = obj.list_data(request).map(&block) + obj.json_response(200, result) + end + + def self.post(obj, request, key) + json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) + username = json[key] + orgname = request.rest_path[1] + id = "#{username}-#{orgname}" + + if obj.exists_data?(request, [ 'organizations', orgname, 'users', username ]) + raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") + end + + obj.create_data(request, request.rest_path, username, '{}') + obj.json_response(201, { "uri" => obj.build_uri(request.base_uri, request.rest_path + [ id ]) }) + end + + end + end +end diff --git a/lib/chef_zero/endpoints/organization_user_endpoint.rb b/lib/chef_zero/endpoints/organization_user_endpoint.rb index ef922d4..8fd97d4 100644 --- a/lib/chef_zero/endpoints/organization_user_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_user_endpoint.rb @@ -20,29 +20,7 @@ module ChefZero json_response(200, ChefData::DataNormalizer.normalize_user(user, request.rest_path[3], ['username'], server.options[:osc_compat])) end - def post(request) - orgname = request.rest_path[1] - username = request.rest_path[3] - - users = get_data(request, [ 'organizations', orgname, 'groups', 'users' ]) - users = FFI_Yajl::Parser.parse(users, :create_additions => false) - - create_data(request, [ 'organizations', orgname, 'users' ], username, '{}') - - # /organizations/ORG/association_requests/USERNAME-ORG - begin - delete_data(request, [ 'organizations', orgname, 'association_requests', username], :data_store_exceptions) - rescue DataStore::DataNotFoundError - end - - # Add the user to the users group if it isn't already there - if !users['users'] || !users['users'].include?(username) - users['users'] ||= [] - users['users'] |= [ username ] - set_data(request, [ 'organizations', orgname, 'groups', 'users' ], FFI_Yajl::Encoder.encode(users, :pretty => true)) - end - json_response(200, {}) - end + # Note: post to a named org user is not permitted, alllow invalid method handling (405) end end end diff --git a/lib/chef_zero/endpoints/organization_users_endpoint.rb b/lib/chef_zero/endpoints/organization_users_endpoint.rb index ed70cf6..e914820 100644 --- a/lib/chef_zero/endpoints/organization_users_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_users_endpoint.rb @@ -1,13 +1,17 @@ require 'ffi_yajl' require 'chef_zero/rest_base' +require 'chef_zero/endpoints/organization_user_base' module ChefZero module Endpoints # /organizations/ORG/users class OrganizationUsersEndpoint < RestBase + def post(request) + ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'username') + end + def get(request) - result = list_data(request).map { |username| { "user" => { "username" => username } } } - json_response(200, result) + ChefZero::Endpoints::OrganizationUserBase.get(self, request) { |username| { "user" => { "username" => username } } } end end end diff --git a/lib/chef_zero/endpoints/organizations_endpoint.rb b/lib/chef_zero/endpoints/organizations_endpoint.rb index 88816e8..41bf03b 100644 --- a/lib/chef_zero/endpoints/organizations_endpoint.rb +++ b/lib/chef_zero/endpoints/organizations_endpoint.rb @@ -17,8 +17,11 @@ module ChefZero def post(request) contents = FFI_Yajl::Parser.parse(request.body, :create_additions => false) name = contents['name'] + full_name = contents['full_name'] if name.nil? error(400, "Must specify 'name' in JSON") + elsif full_name.nil? + error(400, "Must specify 'full_name' in JSON") elsif exists_data_dir?(request, request.rest_path + [ name ]) error(409, "Organization already exists") else @@ -43,8 +46,12 @@ module ChefZero set_data(request, validator_path, validator) end + json_response(201, { "uri" => "#{build_uri(request.base_uri, org_path)}", + "name" => name, + "org_type" => org["org_type"], + "full_name" => full_name, "clientname" => validator_name, "private_key" => private_key }) diff --git a/lib/chef_zero/endpoints/search_endpoint.rb b/lib/chef_zero/endpoints/search_endpoint.rb index 3a093be..a9ad2bf 100644 --- a/lib/chef_zero/endpoints/search_endpoint.rb +++ b/lib/chef_zero/endpoints/search_endpoint.rb @@ -10,13 +10,15 @@ module ChefZero # /search/INDEX class SearchEndpoint < RestBase def get(request) - results = search(request) + orgname = request.rest_path[1] + results = search(request, orgname) results['rows'] = results['rows'].map { |name,uri,value,search_value| value } json_response(200, results) end def post(request) - full_results = search(request) + orgname = request.rest_path[1] + full_results = search(request, orgname) keys = FFI_Yajl::Parser.parse(request.body, :create_additions => false) partial_results = full_results['rows'].map do |name, uri, doc, search_value| data = {} @@ -45,10 +47,10 @@ module ChefZero private - def search_container(request, index) + def search_container(request, index, orgname) relative_parts, normalize_proc = case index when 'client' - [ ['clients'], Proc.new { |client, name| ChefData::DataNormalizer.normalize_client(client, name) } ] + [ ['clients'], Proc.new { |client, name| ChefData::DataNormalizer.normalize_client(client, name, orgname) } ] when 'node' [ ['nodes'], Proc.new { |node, name| ChefData::DataNormalizer.normalize_node(node, name) } ] when 'environment' @@ -92,7 +94,7 @@ module ChefZero end end - def search(request) + def search(request, orgname = nil) # Extract parameters index = request.rest_path[3] query_string = request.query_params['q'] || '*:*' @@ -104,7 +106,7 @@ module ChefZero rows = rows.to_i if rows # Get the search container - container, expander = search_container(request, index) + container, expander = search_container(request, index, orgname) # Search! result = [] diff --git a/lib/chef_zero/endpoints/user_organizations_endpoint.rb b/lib/chef_zero/endpoints/user_organizations_endpoint.rb index b6decb9..3eb13c4 100644 --- a/lib/chef_zero/endpoints/user_organizations_endpoint.rb +++ b/lib/chef_zero/endpoints/user_organizations_endpoint.rb @@ -13,7 +13,7 @@ module ChefZero result = result.map do |orgname| org = get_data(request, [ 'organizations', orgname, 'org' ]) org = FFI_Yajl::Parser.parse(org, :create_additions => false) - ChefData::DataNormalizer.normalize_organization(org, orgname) + { "organization" => ChefData::DataNormalizer.normalize_organization(org, orgname) } end json_response(200, result) end diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb index 9cf7b39..672f795 100644 --- a/lib/chef_zero/server.rb +++ b/lib/chef_zero/server.rb @@ -61,6 +61,7 @@ require 'chef_zero/endpoints/environment_recipes_endpoint' require 'chef_zero/endpoints/environment_role_endpoint' require 'chef_zero/endpoints/license_endpoint' require 'chef_zero/endpoints/node_endpoint' +require 'chef_zero/endpoints/node_identifiers_endpoint' require 'chef_zero/endpoints/organizations_endpoint' require 'chef_zero/endpoints/organization_endpoint' require 'chef_zero/endpoints/organization_association_requests_endpoint' @@ -540,6 +541,7 @@ module ChefZero [ "/organizations/*/environments/*/roles/*", EnvironmentRoleEndpoint.new(self) ], [ "/organizations/*/nodes", RestListEndpoint.new(self) ], [ "/organizations/*/nodes/*", NodeEndpoint.new(self) ], + [ "/organizations/*/nodes/*/_identifiers", NodeIdentifiersEndpoint.new(self) ], [ "/organizations/*/policies/*/*", PoliciesEndpoint.new(self) ], [ "/organizations/*/principals/*", PrincipalEndpoint.new(self) ], [ "/organizations/*/roles", RestListEndpoint.new(self) ], |