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

vault_secret: Save lease ID to the nested attribute #56

Merged
merged 2 commits into from
May 11, 2016
Merged
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
12 changes: 9 additions & 3 deletions libraries/vault_secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class VaultSecret < Chef::Resource
# The number of attempts to try & read a Vault secret.
# @return [Fixnum]
attribute(:attempts, kind_of: Fixnum, default: 2)
# The run state reference where the secret value will be saved,
# e.q. node.run_state['run_state_reference']
# @return [String]
attribute(:run_state_reference, kind_of: String, default: nil)
# @see https://github.com/hashicorp/vault-ruby
attribute(:address, kind_of: String, required: true)
attribute(:token, kind_of: String)
Expand Down Expand Up @@ -56,7 +60,8 @@ def config
notifying_block do
run_context.include_recipe 'hashicorp-vault::gems'

lease_id = node[new_resource.path]
node.default_unless['hashicorp-vault']['leases'] = []
lease_id = node['hashicorp-vault']['leases'][new_resource.path]

begin
client = Vault::Client.new(new_resource.config)
Expand Down Expand Up @@ -87,9 +92,10 @@ def config
return
end

node.set[new_resource.path] = secret.lease_id if secret.renewable?
node.set['hashicorp-vault']['leases'][new_resource.path] = secret.lease_id if secret.renewable?
# Store secret in-memory for the rest of the Chef run
node.run_state[new_resource.path] = secret
reference = new_resource.run_state_reference || new_resource.path
node.run_state[reference] = secret
new_resource.updated_by_last_action(true)
rescue Vault::HTTPError => e
Chef::Log.warn("Failed to read #{new_resource.path}.\n" + e.message)
Expand Down