From cd0f1ba5b32c61dc8ef174fcca4d6dd3415ac59a Mon Sep 17 00:00:00 2001 From: "Moser, Kevin" Date: Fri, 4 Oct 2013 09:40:11 -0700 Subject: [PATCH] Issue 39: Fall back to clients api if admin not found in users endpoint --- Changelog.md | 4 +-- lib/chef-vault/item.rb | 57 +++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Changelog.md b/Changelog.md index 42095f8..500d8fc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,8 +1,8 @@ ## Planned (Unreleased) -## v2.0.3 +## v2.1.0 * Update README to correct typos -* Add a file-content parameter to allow reading in a file to encrypt to deal with \n conversion on the fly +* Modify admin loading to fall back to clients endpoint if not found in users endpoint ## Released diff --git a/lib/chef-vault/item.rb b/lib/chef-vault/item.rb index 4c7a390..6d02927 100644 --- a/lib/chef-vault/item.rb +++ b/lib/chef-vault/item.rb @@ -44,16 +44,7 @@ def clients(search=nil, action=:add) case action when :add - begin - keys.add(ChefVault::ChefPatch::ApiClient.load(node.name), @secret, "clients") - rescue Net::HTTPServerException => http_error - if http_error.response.code == "404" - raise ChefVault::Exceptions::ClientNotFound, - "#{node.name} is not a valid chef client and/or node" - else - raise http_error - end - end + keys.add(load_client(node.name), @secret, "clients") when :delete keys.delete(node.name, "clients") else @@ -77,16 +68,7 @@ def admins(admins=nil, action=:add) admin.strip! case action when :add - begin - keys.add(ChefVault::ChefPatch::User.load(admin), @secret, "admins") - rescue Net::HTTPServerException => http_error - if http_error.response.code == "404" - raise ChefVault::Exceptions::AdminNotFound, - "#{admin} is not a valid chef admin" - else - raise http_error - end - end + keys.add(load_admin(admin), @secret, "admins") when :delete keys.delete(admin, "admins") else @@ -244,4 +226,39 @@ def reload_raw_data @raw_data end + + def load_admin(admin) + begin + admin = ChefVault::ChefPatch::User.load(admin) + rescue Net::HTTPServerException => http_error + if http_error.response.code == "404" + begin + puts "WARNING: #{admin} not found in users, trying clients." + admin = load_client(admin) + rescue ChefVault::Exceptions::ClientNotFound + raise ChefVault::Exceptions::AdminNotFound, + "FATAL: Could not find #{admin} in users or clients!" + end + else + raise http_error + end + end + + admin + end + + def load_client(client) + begin + client = ChefVault::ChefPatch::ApiClient.load(client) + rescue Net::HTTPServerException => http_error + if http_error.response.code == "404" + raise ChefVault::Exceptions::ClientNotFound, + "#{client} is not a valid chef client and/or node" + else + raise http_error + end + end + + client + end end