summaryrefslogtreecommitdiff
path: root/lib/chef_zero/endpoints/principal_endpoint.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef_zero/endpoints/principal_endpoint.rb')
-rw-r--r--lib/chef_zero/endpoints/principal_endpoint.rb85
1 files changed, 43 insertions, 42 deletions
diff --git a/lib/chef_zero/endpoints/principal_endpoint.rb b/lib/chef_zero/endpoints/principal_endpoint.rb
index 2dcec1b..90be311 100644
--- a/lib/chef_zero/endpoints/principal_endpoint.rb
+++ b/lib/chef_zero/endpoints/principal_endpoint.rb
@@ -25,60 +25,61 @@ module ChefZero
private
def get_principal_data(request, name)
- # If /organizations/ORG/users/NAME exists, use this user (only org members have precedence over clients). hey are an org member.
- get_org_users_data(request, name) ||
+ # If /organizations/ORG/users/NAME exists, use this user (only org
+ # members have precedence over clients).
+ get_org_user_data(request, name) ||
# If /organizations/ORG/clients/NAME exists, use the client.
- get_clients_data(request, name) ||
- # If there is no client with that name, check for a user (/users/NAME) and return that with
- # org_member = false.
- get_users_data(request, name)
+ get_client_data(request, name) ||
+ # If there is no client with that name, check for a user
+ # (/users/NAME) and return that with org_member = false.
+ get_user_data(request, name)
end
- def get_org_users_data(request, name)
- path = [ *request.rest_path[0..1], 'users', name ]
- return if get_data(request, path, :nil).nil?
-
- user_keys_json = get_data(request,
- [ 'user_keys', name, 'keys', DEFAULT_PUBLIC_KEY_NAME ],
- :data_store_exceptions
- )
-
- public_key = FFI_Yajl::Parser.parse(user_keys_json)['public_key']
+ def get_org_user_data(request, name)
+ user_path = request.rest_path.first(2) + [ 'users', name ]
+ return if get_data(request, user_path, :nil).nil?
+
+ # In single org. mode assume that we only support one user, "pivotal,"
+ # and there is no user_keys data for that user; use the default
+ # PUBLIC_KEY.
+ public_key =
+ if data_store.real_store.respond_to?(:single_org) && data_store.real_store.single_org
+ PUBLIC_KEY
+ else
+ user_keys_json = get_data(request,
+ [ 'user_keys', name, 'keys', DEFAULT_PUBLIC_KEY_NAME ],
+ :data_store_exceptions
+ )
+
+ FFI_Yajl::Parser.parse(user_keys_json)['public_key']
+ end
{ "type" => "user",
"org_member" => true,
- "public_key" => public_key
- }
+ "public_key" => public_key }
end
- def get_clients_data(request, name)
- path = [ *request.rest_path[0..1], 'clients', name ]
- json = get_data(request, path, :nil)
- return if json.nil?
+ def get_client_data(request, name)
+ base_path = request.rest_path.first(2)
+ client_path = base_path + [ 'clients', name ]
+ client_key_path = base_path + [ 'client_keys', name, 'keys', DEFAULT_PUBLIC_KEY_NAME ]
- public_key = FFI_Yajl::Parser.parse(json)['public_key']
-
- { "type" => "client",
- "org_member" => true,
- "public_key" => public_key || PUBLIC_KEY
- }
+ get_actor_data(request, client_path, client_key_path,
+ "type" => "client", "org_member" => true)
end
- def get_users_data(request, name)
- path = [ 'users', name ]
- return if get_data(request, path, :nil).nil?
-
- user_keys_json = get_data(request,
- [ 'user_keys', name, 'keys', DEFAULT_PUBLIC_KEY_NAME ],
- :data_store_exceptions
- )
-
- public_key = FFI_Yajl::Parser.parse(user_keys_json)['public_key']
+ def get_user_data(request, name)
+ user_path = [ 'users', name ]
+ user_key_path = [ 'user_keys', name, 'keys', DEFAULT_PUBLIC_KEY_NAME ]
+ get_actor_data(request, user_path, user_key_path,
+ "type" => "user", "org_member" => false)
+ end
- { "type" => "user",
- "org_member" => false,
- "public_key" => public_key
- }
+ def get_actor_data(request, actor_path, actor_key_path, attrs={})
+ return if get_data(request, actor_path, :nil).nil?
+ actor_key_json = get_data(request, actor_key_path, :data_store_exceptions)
+ public_key = FFI_Yajl::Parser.parse(actor_key_json)['public_key']
+ attrs.merge("public_key" => public_key)
end
end
end