Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple requests to Chef server #365

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/chef-vault/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ def remove(key)
end

def secret
if @keys.include?(@node_name) && !@keys[@node_name].nil?
data_bag_key = @keys[@node_name]
if !data_bag_key.nil?
private_key = OpenSSL::PKey::RSA.new(File.open(@client_key_path).read)
begin
private_key.private_decrypt(Base64.decode64(@keys[@node_name]))
private_key.private_decrypt(Base64.decode64(data_bag_key))
rescue OpenSSL::PKey::RSAError
raise ChefVault::Exceptions::SecretDecryption,
"#{data_bag}/#{id} is encrypted for you, but your private key failed to decrypt the contents. "\
Expand Down
5 changes: 5 additions & 0 deletions lib/chef-vault/item_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ def initialize(vault, name)
@raw_data["search_query"] = []
@raw_data["mode"] = "default"
@cache = {} # write-back cache for keys
@tmpcache = {}
end

def [](key)
# return if cache contains client key
return @tmpcache[key] if @tmpcache.key?(key)
# return options immediately
return @raw_data[key] if %w{id admins clients search_query mode}.include?(key)

Expand All @@ -47,6 +50,7 @@ def [](key)
# check if the key is saved in sparse mode
skey = sparse_key(sparse_id(key)) if sparse?
if skey
@tmpcache[key] = skey[key]
skey[key]
else
# fallback to raw data
Expand Down Expand Up @@ -89,6 +93,7 @@ def clear_encrypted

def delete(chef_key)
@cache[chef_key.name] = false
@tmpcache = {}
raw_data[chef_key.type].delete(chef_key.name)
raw_data.delete(chef_key.name)
end
Expand Down