Class: ChefZero::Endpoints::ActorKeysEndpoint

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/actor_keys_endpoint.rb

Overview

/users/USER/keys /organizations/ORG/clients/CLIENT/keys

Constant Summary collapse

DEFAULT_PUBLIC_KEY_NAME =
"default".freeze
DATE_FORMAT =

e.g. 2015-12-24T21:00:00Z

"%FT%TZ".freeze

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestBase

#accepts?, #already_json_response, #build_uri, build_uri, #call, #check_api_version, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #get_data_or_else, #hashify_list, #head_request, #initialize, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, #populate_defaults, rfc2396_parser, #set_data, #text_response, #to_json

Constructor Details

This class inherits a constructor from ChefZero::RestBase

Instance Method Details

#get(request, alt_uri_root = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chef_zero/endpoints/actor_keys_endpoint.rb', line 11

def get(request, alt_uri_root = nil)
  path = data_path(request)

  # Get actor or 404 if it doesn't exist
  actor_json = get_data(request, actor_path(request))

  key_names = list_data_or_else(request, path, [])
  key_names.unshift(DEFAULT_PUBLIC_KEY_NAME) if actor_has_default_public_key?(actor_json)

  result = key_names.map do |key_name|
    list_key(request, [ *path, key_name ], alt_uri_root)
  end

  json_response(200, result)
end

#post(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef_zero/endpoints/actor_keys_endpoint.rb', line 27

def post(request)
  request_body = parse_json(request.body)

  # Try loading the client or user so a 404 is returned if it doesn't exist
  actor_json = get_data(request, actor_path(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"]

  if key_name == DEFAULT_PUBLIC_KEY_NAME
    store_actor_default_public_key!(request, actor_json, public_key)
  else
    store_actor_public_key!(request, key_name, public_key, request_body["expiration_date"])
  end

  response_body = { "uri" => key_uri(request, key_name) }
  response_body["private_key"] = private_key if generate_keys

  json_response(201, response_body,
    headers: { "Location" => response_body["uri"] })
end