Skip to content

Commit

Permalink
Issue 39: Fall back to clients api if admin not found in users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Moser, Kevin authored and Moser, Kevin committed Oct 4, 2013
1 parent 772f024 commit cd0f1ba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
4 changes: 2 additions & 2 deletions 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
Expand Down
57 changes: 37 additions & 20 deletions lib/chef-vault/item.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit cd0f1ba

Please sign in to comment.