diff options
author | danielsdeleo <dan@chef.io> | 2016-02-24 15:40:01 -0800 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2016-02-25 10:03:51 -0800 |
commit | c858f736c2a33a8bd061564cb784c4947d015824 (patch) | |
tree | c36ad04f7d807c6e3ef4c79231cda6bcd5225347 /lib | |
parent | 75e1d346dedbc40cd325c1f0dee2275bcace9b6a (diff) | |
download | chef-zero-c858f736c2a33a8bd061564cb784c4947d015824.tar.gz |
Implement APIv1 behaviors
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef_zero/endpoints/actor_default_key_endpoint.rb | 7 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/actor_endpoint.rb | 10 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/actors_endpoint.rb | 34 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/principal_endpoint.rb | 13 | ||||
-rw-r--r-- | lib/chef_zero/rest_request.rb | 9 |
5 files changed, 66 insertions, 7 deletions
diff --git a/lib/chef_zero/endpoints/actor_default_key_endpoint.rb b/lib/chef_zero/endpoints/actor_default_key_endpoint.rb index f63ffb9..3be1475 100644 --- a/lib/chef_zero/endpoints/actor_default_key_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_default_key_endpoint.rb @@ -21,6 +21,13 @@ module ChefZero def get(request) # 404 if actor doesn't exist actor_data = get_actor_data(request) + key_data = default_public_key_from_actor(actor_data) + + # 404 if the actor doesn't have a default key + if key_data["public_key"].nil? + raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}") + end + json_response(200, default_public_key_from_actor(actor_data)) end diff --git a/lib/chef_zero/endpoints/actor_endpoint.rb b/lib/chef_zero/endpoints/actor_endpoint.rb index 446c196..500f2bb 100644 --- a/lib/chef_zero/endpoints/actor_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_endpoint.rb @@ -8,6 +8,16 @@ module ChefZero # /organizations/ORG/users/NAME # /users/NAME class ActorEndpoint < RestObjectEndpoint + + def get(request) + result = super + user_data = FFI_Yajl::Parser.parse(result[2], :create_additions => false) + + user_data.delete("public_key") unless request.api_v0? + + json_response(200, user_data) + end + def delete(request) result = super diff --git a/lib/chef_zero/endpoints/actors_endpoint.rb b/lib/chef_zero/endpoints/actors_endpoint.rb index c6c676f..4221e93 100644 --- a/lib/chef_zero/endpoints/actors_endpoint.rb +++ b/lib/chef_zero/endpoints/actors_endpoint.rb @@ -41,19 +41,45 @@ module ChefZero # one. request_body = FFI_Yajl::Parser.parse(request.body, :create_additions => false) public_key = request_body['public_key'] - if !public_key + + skip_key_create = !request.api_v0? && !request_body["create_key"] + + if !public_key && !skip_key_create private_key, public_key = server.gen_key_pair request_body['public_key'] = public_key request.body = FFI_Yajl::Encoder.encode(request_body, :pretty => true) + elsif skip_key_create + request_body['public_key'] = nil + request.body = FFI_Yajl::Encoder.encode(request_body, :pretty => true) end result = super(request) if result[0] == 201 # If we generated a key, stuff it in the response. - response = FFI_Yajl::Parser.parse(result[2], :create_additions => false) - response['private_key'] = private_key if private_key - response['public_key'] = public_key unless request.rest_path[0] == 'users' + user_data = FFI_Yajl::Parser.parse(result[2], :create_additions => false) + + key_data = {} + key_data['private_key'] = private_key if private_key + key_data['public_key'] = public_key unless request.rest_path[0] == 'users' + + response = + if request.api_v0? + user_data.merge(key_data) + elsif skip_key_create && !public_key + user_data + else + actor_name = request_body["name"] || request_body["username"] || request_body["clientname"] + + relpath_to_default_key = [ actor_name, "keys", "default" ] + key_data["uri"] = build_uri(request.base_uri, request.rest_path + relpath_to_default_key) + key_data["public_key"] = public_key + key_data["name"] = "default" + key_data["expiration_date"] = "infinity" + user_data["chef_key"] = key_data + user_data + end + json_response(201, response) else result diff --git a/lib/chef_zero/endpoints/principal_endpoint.rb b/lib/chef_zero/endpoints/principal_endpoint.rb index b1f4efb..64c6986 100644 --- a/lib/chef_zero/endpoints/principal_endpoint.rb +++ b/lib/chef_zero/endpoints/principal_endpoint.rb @@ -30,13 +30,22 @@ module ChefZero end end if json - json_response(200, { + principal_data = { 'name' => name, 'type' => type, 'public_key' => FFI_Yajl::Parser.parse(json)['public_key'] || PUBLIC_KEY, 'authz_id' => '0'*32, 'org_member' => org_member - }) + } + + response_data = + if request.api_v0? + principal_data + else + { "principals" => [ principal_data ] } + end + + json_response(200, response_data) else error(404, 'Principal not found') end diff --git a/lib/chef_zero/rest_request.rb b/lib/chef_zero/rest_request.rb index 6b37c18..60738cf 100644 --- a/lib/chef_zero/rest_request.rb +++ b/lib/chef_zero/rest_request.rb @@ -2,6 +2,9 @@ require 'rack/request' module ChefZero class RestRequest + + ZERO = "0".freeze + def initialize(env, rest_base_prefix = []) @env = env @rest_base_prefix = rest_base_prefix @@ -19,7 +22,11 @@ module ChefZero end def api_version - @env['HTTP_X_OPS_SERVER_API_VERSION'] || 0 + @env['HTTP_X_OPS_SERVER_API_VERSION'] || ZERO + end + + def api_v0? + api_version == ZERO end def requestor |