Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS12 Support #117

Merged
merged 16 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Chef Zero CHANGELOG
===================

$ 5.0 (pending)

* updates to be compatible with current Chef Server 12 behaviors
* update oc-chef-pedant to 2.0
* remove chef-pedant support

# 4.2.3

* [PR#143](https://github.com/chef/chef-zero/pull/143): Fix server_scope: :context
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gemspec

gem 'rest-client', :github => 'chef/rest-client'

gem 'chef-pedant', :github => 'chef/chef-pedant', :tag => '1.0.46'
gem 'oc-chef-pedant', :github => 'chef/chef-server'

gem 'chef', :github => 'opscode/chef', :tag => '12.2.1'
gem 'chef', :github => 'chef/chef', :tag => '12.4.1'

4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ task :spec do
system('rspec spec/*_spec.rb')
end

desc "run pedant"
desc "run oc pedant"
task :pedant do
require File.expand_path('spec/run_pedant')
require File.expand_path('spec/run_oc_pedant')
end

desc "run oc pedant"
Expand Down
2 changes: 1 addition & 1 deletion chef-zero.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.add_dependency 'mixlib-log', '~> 1.3'
s.add_dependency 'hashie', '~> 2.0'
s.add_dependency 'uuidtools', '~> 2.1'
s.add_dependency 'ffi-yajl', '>= 1.1', '< 3.0'
s.add_dependency 'ffi-yajl', '~> 2.2'
s.add_dependency 'rack'

s.add_development_dependency 'rake'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/latest-chef.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gemspec :path => "../"

gem 'chef', :github => 'opscode/chef'
gem 'chef', :github => 'chef/chef'
7 changes: 0 additions & 7 deletions gemfiles/latest-pedant.gemfile

This file was deleted.

7 changes: 3 additions & 4 deletions gemfiles/oc-chef-pedant.gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
source 'https://rubygems.org'
gemspec :path => '../'

gem 'rest-client', :github => 'opscode/rest-client', :branch => 'lcg/1.6.7-version-lying'
gem 'chef-pedant', :github => 'opscode/chef-pedant', :ref => '81f3b4ecbc09d04950f2819b38a6a8f906ada2a7'
gem 'oc-chef-pedant', :git => '[email protected]:opscode/oc-chef-pedant', :ref => '3c0eb31f1e49aa947b81ad51387b7a68adbc5f91'
gem 'chef', :github => 'opscode/chef'
gem 'rest-client', :github => 'chef/rest-client', :branch => 'lcg/1.6.7-version-lying'
gem 'oc-chef-pedant', :github => 'chef/oc-chef-pedant', :tag => '2.0.0'
gem 'chef', :github => 'chef/chef'
6 changes: 3 additions & 3 deletions lib/chef_zero/chef_data/data_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def self.normalize_acls(acls)
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"
Expand Down
2 changes: 1 addition & 1 deletion lib/chef_zero/chef_data/default_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def exists?(path)
'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' => {}
}
Expand Down
10 changes: 8 additions & 2 deletions lib/chef_zero/endpoints/actor_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ def put(request)
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super confusing branching logic - I would expect to see actor_endpoint_spec.rb updated. Unless all the testing is through pedant?

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)
Expand All @@ -77,7 +83,7 @@ def put(request)
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
Expand Down
22 changes: 22 additions & 0 deletions lib/chef_zero/endpoints/node_identifiers_endpoint.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@ module Endpoints
# /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
Expand Down
13 changes: 9 additions & 4 deletions lib/chef_zero/endpoints/organization_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ def put(request)
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

Expand Down
29 changes: 29 additions & 0 deletions lib/chef_zero/endpoints/organization_user_base.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 1 addition & 23 deletions lib/chef_zero/endpoints/organization_user_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,7 @@ def delete(request)
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than removing this I think we need to move it and slightly change this code. The actual endpoint that does this is:

POST /organizations/ORGNAME/users

with a body of {"username": "USERTOADD"}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the Chef Server support both endpoints? POST /organizations/ORGNAME/users and POST /organizations/ORGNAME/users/USER? If we want chef-zero to emulated CS12 I would think we want want it to do whatever CS12 does. I'm not sure when the endpoint was changed so I don't know when the old one was deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current chef server only support POST /organization/ORGNAME/users/. I don't think there is a version that supports POST /organizations/ORGNAME/users/USER but I haven't look extensively.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this in andrewjamesbrown@0447018 which is part of #146

end
end
end
8 changes: 6 additions & 2 deletions lib/chef_zero/endpoints/organization_users_endpoint.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/chef_zero/endpoints/organizations_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def get(request)
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
Expand All @@ -43,8 +46,12 @@ def post(request)
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
})
Expand Down
14 changes: 8 additions & 6 deletions lib/chef_zero/endpoints/search_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ module Endpoints
# /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 = {}
Expand Down Expand Up @@ -45,10 +47,10 @@ def post(request)

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'
Expand Down Expand Up @@ -92,7 +94,7 @@ def expand_for_indexing(value, index, id)
end
end

def search(request)
def search(request, orgname = nil)
# Extract parameters
index = request.rest_path[3]
query_string = request.query_params['q'] || '*:*'
Expand All @@ -104,7 +106,7 @@ def search(request)
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 = []
Expand Down
2 changes: 1 addition & 1 deletion lib/chef_zero/endpoints/user_organizations_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get(request)
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
Expand Down
2 changes: 2 additions & 0 deletions lib/chef_zero/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
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'
Expand Down Expand Up @@ -540,6 +541,7 @@ def open_source_endpoints
[ "/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) ],
Expand Down
Loading