From 96a82ea1050e1dd360035b760c19faca5678251c Mon Sep 17 00:00:00 2001 From: Jordan Running Date: Tue, 26 Jan 2016 13:28:04 -0600 Subject: Rename UserKey(s)Endpoint to ActorKey(s)Endpoint, add client keys routes --- lib/chef_zero/endpoints/actor_key_endpoint.rb | 33 +++++++++++ lib/chef_zero/endpoints/actor_keys_endpoint.rb | 80 ++++++++++++++++++++++++++ lib/chef_zero/endpoints/user_key_endpoint.rb | 32 ----------- lib/chef_zero/endpoints/user_keys_endpoint.rb | 80 -------------------------- lib/chef_zero/server.rb | 10 ++-- spec/run_oc_pedant.rb | 6 +- 6 files changed, 123 insertions(+), 118 deletions(-) create mode 100644 lib/chef_zero/endpoints/actor_key_endpoint.rb create mode 100644 lib/chef_zero/endpoints/actor_keys_endpoint.rb delete mode 100644 lib/chef_zero/endpoints/user_key_endpoint.rb delete mode 100644 lib/chef_zero/endpoints/user_keys_endpoint.rb diff --git a/lib/chef_zero/endpoints/actor_key_endpoint.rb b/lib/chef_zero/endpoints/actor_key_endpoint.rb new file mode 100644 index 0000000..4cfd4b2 --- /dev/null +++ b/lib/chef_zero/endpoints/actor_key_endpoint.rb @@ -0,0 +1,33 @@ +require 'ffi_yajl' +require 'chef_zero/rest_base' + +module ChefZero + module Endpoints + # /users/USER/keys/NAME + # /organizations/ORG/clients/CLIENT/keys/NAME + class ActorKeyEndpoint < RestBase + def get(request) + path = [ "user_keys", *request.rest_path[1..-1] ] + already_json_response(200, get_data(request, path)) + end + + def delete(request) + path = [ "user_keys", *request.rest_path[1..-1] ] + + data = get_data(request, path) + delete_data(request, path) + + already_json_response(200, data) + end + + def put(request) + path = [ "user_keys", *request.rest_path[1..-1] ] + + # We grab the old data to trigger a 404 if it doesn't exist + get_data(request, path) + + set_data(request, path, request.body) + end + end + end +end diff --git a/lib/chef_zero/endpoints/actor_keys_endpoint.rb b/lib/chef_zero/endpoints/actor_keys_endpoint.rb new file mode 100644 index 0000000..405a927 --- /dev/null +++ b/lib/chef_zero/endpoints/actor_keys_endpoint.rb @@ -0,0 +1,80 @@ +require 'ffi_yajl' +require 'chef_zero/rest_base' + +module ChefZero + module Endpoints + # /users/USER/keys + # /organizations/ORG/clients/CLIENT/keys + class ActorKeysEndpoint < RestBase + DATE_FORMAT = "%FT%TZ" # e.g. 2015-12-24T21:00:00Z + + def get(request) + username = request.rest_path[1] + path = [ "user_keys", username, "keys" ] + + result = list_data(request, path).map do |key_name| + list_key(request, [ *path, key_name ]) + end + + json_response(200, result) + end + + def post(request) + username = request.rest_path[1] + request_body = FFI_Yajl::Parser.parse(request.body) + + validate_user!(request) + + generate_keys = request_body["public_key"].nil? + + if generate_keys + private_key, public_key = server.gen_key_pair + else + public_key = request_body['public_key'] + end + + key_name = request_body["name"] + path = [ "user_keys", username, "keys" ] + + data = FFI_Yajl::Encoder.encode( + "name" => key_name, + "public_key" => public_key, + "expiration_date" => request_body["expiration_date"] + ) + + create_data(request, path, key_name, data, :create_dir) + + response_body = { + "uri" => build_uri(request.base_uri, + [ "users", username, "keys", key_name ]) + } + response_body["private_key"] = private_key if generate_keys + + json_response(201, response_body, + headers: { "Location" => response_body["uri"] }) + end + + private + + def list_key(request, data_path) + data = FFI_Yajl::Parser.parse(get_data(request, data_path), create_additions: false) + uri = build_uri(request.base_uri, [ "users", *data_path[1..-1] ]) + + expiration_date = if data["expiration_date"] == "infinity" + Float::INFINITY + else + DateTime.strptime(data["expiration_date"], DATE_FORMAT) + end + + { "name" => data_path[-1], + "uri" => uri, + "expired" => DateTime.now > expiration_date } + end + + def validate_user!(request) + # Try loading the user so a 404 is returned if the user doesn't + get_data(request, request.rest_path[0, 2]) + end + end + end +end diff --git a/lib/chef_zero/endpoints/user_key_endpoint.rb b/lib/chef_zero/endpoints/user_key_endpoint.rb deleted file mode 100644 index 4e4872b..0000000 --- a/lib/chef_zero/endpoints/user_key_endpoint.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'ffi_yajl' -require 'chef_zero/rest_base' - -module ChefZero - module Endpoints - # /users/USER/keys/NAME - class UserKeyEndpoint < RestBase - def get(request) - path = [ "user_keys", *request.rest_path[1..-1] ] - already_json_response(200, get_data(request, path)) - end - - def delete(request) - path = [ "user_keys", *request.rest_path[1..-1] ] - - data = get_data(request, path) - delete_data(request, path) - - already_json_response(200, data) - end - - def put(request) - path = [ "user_keys", *request.rest_path[1..-1] ] - - # We grab the old data to trigger a 404 if it doesn't exist - get_data(request, path) - - set_data(request, path, request.body) - end - end - end -end diff --git a/lib/chef_zero/endpoints/user_keys_endpoint.rb b/lib/chef_zero/endpoints/user_keys_endpoint.rb deleted file mode 100644 index c6aea07..0000000 --- a/lib/chef_zero/endpoints/user_keys_endpoint.rb +++ /dev/null @@ -1,80 +0,0 @@ -require 'ffi_yajl' -require 'chef_zero/rest_base' - -module ChefZero - module Endpoints - # /users/USER/keys - - class UserKeysEndpoint < RestBase - DATE_FORMAT = "%FT%TZ" # e.g. 2015-12-24T21:00:00Z - - def get(request) - username = request.rest_path[1] - path = [ "user_keys", username, "keys" ] - - result = list_data(request, path).map do |key_name| - list_key(request, [ *path, key_name ]) - end - - json_response(200, result) - end - - def post(request) - username = request.rest_path[1] - request_body = FFI_Yajl::Parser.parse(request.body) - - validate_user!(request) - - generate_keys = request_body["public_key"].nil? - - if generate_keys - private_key, public_key = server.gen_key_pair - else - public_key = request_body['public_key'] - end - - key_name = request_body["name"] - path = [ "user_keys", username, "keys" ] - - data = FFI_Yajl::Encoder.encode( - "name" => key_name, - "public_key" => public_key, - "expiration_date" => request_body["expiration_date"] - ) - - create_data(request, path, key_name, data, :create_dir) - - response_body = { - "uri" => build_uri(request.base_uri, - [ "users", username, "keys", key_name ]) - } - response_body["private_key"] = private_key if generate_keys - - json_response(201, response_body, - headers: { "Location" => response_body["uri"] }) - end - - private - - def list_key(request, data_path) - data = FFI_Yajl::Parser.parse(get_data(request, data_path), create_additions: false) - uri = build_uri(request.base_uri, [ "users", *data_path[1..-1] ]) - - expiration_date = if data["expiration_date"] == "infinity" - Float::INFINITY - else - DateTime.strptime(data["expiration_date"], DATE_FORMAT) - end - - { "name" => data_path[-1], - "uri" => uri, - "expired" => DateTime.now > expiration_date } - end - - def validate_user!(request) - # Try loading the user so a 404 is returned if the user doesn't - get_data(request, request.rest_path[0, 2]) - end - end - end -end diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb index f0ad9b1..45d36c3 100644 --- a/lib/chef_zero/server.rb +++ b/lib/chef_zero/server.rb @@ -94,8 +94,8 @@ require 'chef_zero/endpoints/system_recovery_endpoint' require 'chef_zero/endpoints/user_association_requests_endpoint' require 'chef_zero/endpoints/user_association_requests_count_endpoint' require 'chef_zero/endpoints/user_association_request_endpoint' -require 'chef_zero/endpoints/user_key_endpoint' -require 'chef_zero/endpoints/user_keys_endpoint' +require 'chef_zero/endpoints/actor_key_endpoint' +require 'chef_zero/endpoints/actor_keys_endpoint' require 'chef_zero/endpoints/user_organizations_endpoint' require 'chef_zero/endpoints/file_store_file_endpoint' require 'chef_zero/endpoints/not_found_endpoint' @@ -539,8 +539,8 @@ module ChefZero [ "/users/*/association_requests", UserAssociationRequestsEndpoint.new(self) ], [ "/users/*/association_requests/count", UserAssociationRequestsCountEndpoint.new(self) ], [ "/users/*/association_requests/*", UserAssociationRequestEndpoint.new(self) ], - [ "/users/*/keys", UserKeysEndpoint.new(self) ], - [ "/users/*/keys/*", UserKeyEndpoint.new(self) ], + [ "/users/*/keys", ActorKeysEndpoint.new(self) ], + [ "/users/*/keys/*", ActorKeyEndpoint.new(self) ], [ "/users/*/organizations", UserOrganizationsEndpoint.new(self) ], [ "/authenticate_user", AuthenticateUserEndpoint.new(self) ], [ "/system_recovery", SystemRecoveryEndpoint.new(self) ], @@ -568,6 +568,8 @@ module ChefZero [ "/dummy", DummyEndpoint.new(self) ], [ "/organizations/*/clients", ActorsEndpoint.new(self) ], [ "/organizations/*/clients/*", ActorEndpoint.new(self) ], + [ "/organizations/*/clients/*/keys", ActorKeysEndpoint.new(self) ], + [ "/organizations/*/clients/*/keys/*", ActorKeyEndpoint.new(self) ], [ "/organizations/*/controls", ControlsEndpoint.new(self) ], [ "/organizations/*/cookbooks", CookbooksEndpoint.new(self) ], [ "/organizations/*/cookbooks/*", CookbookEndpoint.new(self) ], diff --git a/spec/run_oc_pedant.rb b/spec/run_oc_pedant.rb index 25b5ea9..25672fb 100644 --- a/spec/run_oc_pedant.rb +++ b/spec/run_oc_pedant.rb @@ -145,10 +145,12 @@ begin default_skips + chef_fs_skips + %w{ --skip-knife } end + pedant_args << "--focus-client-keys" + Pedant.setup(pedant_args) - fail_fast = %w()#--fail-fast) - #fail_fast = ["--fail-fast"] + # fail_fast = [] + fail_fast = ["--fail-fast"] result = RSpec::Core::Runner.run(Pedant.config.rspec_args + fail_fast) -- cgit v1.2.1