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 kv2 #37

Open
wants to merge 5 commits into
base: master
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ The hiera lookup for `foo` will return a Hash:

{"value"=>"bar","other"=>"baz"}

### Vault KV engine version - optional

Since version 0.10.0 Vault supports kv secrets versioning so-called KV version 2. By default version 1 support is enabled. To configure module to work with version 2 secrets specify the :kv_version setting e.g.

:vault:
:kv_version: 2

Make sure to enable versioning for all secrets in Vault:

vault kv enable-versioning secret/foo

**NOTE:** It is not possible to lookup through v1 and v2 secrets simultaneously. Use the only type.

### Single Value - optional

If you use just a single field to store data, eg. "value" - you can request that just this is returned as a string, instead of a hash.
Expand Down
2 changes: 1 addition & 1 deletion hiera-vault.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'rubygems/package_task'

spec = Gem::Specification.new do |gem|
gem.name = "hiera-vault"
gem.version = "0.2.2"
gem.version = "0.2.3"
gem.license = "Apache-2.0"
gem.summary = "Module for using vault as a hiera backend"
gem.email = "[email protected]"
Expand Down
31 changes: 26 additions & 5 deletions lib/hiera/backend/vault_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def initialize()
@vault = nil
Hiera.warn("[hiera-vault] Skipping backend. Configuration error: #{e}")
end

# Check vault kv version
if (@config[:kv_version]).nil?
@api_path = ""
Hiera.debug("[hiera-vault] kv engine version not set using default: 1")
elsif @config[:kv_version] == 1
@api_path = ""
Hiera.debug("[hiera-vault] Using kv engine version: #{@config[:kv_version]}")
elsif @config[:kv_version] == 2
@api_path = "data/"
Hiera.debug("[hiera-vault] Using kv engine version: #{@config[:kv_version]}")
else
Hiera.warn("[hiera-vault] Not supported kv engine version: #{@config[:kv_version]}")
end
end

def lookup(key, scope, order_override, resolution_type)
Expand All @@ -58,7 +72,7 @@ def lookup(key, scope, order_override, resolution_type)
path = Backend.parse_string(mount, scope, { 'key' => key })
Backend.datasources(scope, order_override) do |source|
Hiera.debug("Looking in path #{path}/#{source}/")
new_answer = lookup_generic("#{path}/#{source}/#{key}", scope)
new_answer = lookup_generic("#{path}/#{@api_path}#{source}/#{key}", scope)
#Hiera.debug("[hiera-vault] Answer: #{new_answer}:#{new_answer.class}")
next if new_answer.nil?
case resolution_type
Expand Down Expand Up @@ -92,22 +106,29 @@ def lookup_generic(key, scope)
end

return nil if secret.nil?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of sprinkling if @config[:kv_version] == 2 throughout the following, couldn't you just create a variable here to represents secret.data in the kv1 case, and secret.data[:data] in the kv2 case, and work off of that from here on out? (This is already a pretty ugly collection of conditionals, seems a shame to make it worse.) ie:

sd = @config[:kv_version] == 2 ? secret.data[:data] : secret.data

and then reference sd instead of secret.data everywhere, without changing any of the existing logic?

Hiera.debug("[hiera-vault] Read secret: #{key}")
if @config[:default_field] and (@config[:default_field_behavior] == 'ignore' or (secret.data.has_key?(@config[:default_field].to_sym) and secret.data.length == 1))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default fields also need to handle the kv2 case (there should be a secret.data[:data].has_key?(@config[:default_field] case in here somewhere).

return nil if not secret.data.has_key?(@config[:default_field].to_sym)
# Return just our default_field
data = secret.data[@config[:default_field].to_sym]
if @config[:kv_version] == 2
data = secret.data[:data][@config[:default_field].to_sym]
else
data = secret.data[@config[:default_field].to_sym]
end
if @config[:default_field_parse] == 'json'
begin
data = JSON.parse(data)
data = JSON.parse(data[:data])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is correct? You may want to test this.

rescue JSON::ParserError => e
Hiera.debug("[hiera-vault] Could not parse string as json: #{e}")
end
end
else
# Turn secret's hash keys into strings
data = secret.data.inject({}) { |h, (k, v)| h[k.to_s] = v; h }
if @config[:kv_version] == 2
data = secret.data[:data].inject({}) { |h, (k, v)| h[k.to_s] = v; h }
else
data = secret.data.inject({}) { |h, (k, v)| h[k.to_s] = v; h }
end
end
#Hiera.debug("[hiera-vault] Data: #{data}:#{data.class}")

Expand Down