-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: master
Are you sure you want to change the base?
Vault kv2 #37
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -92,22 +106,29 @@ def lookup_generic(key, scope) | |
end | ||
|
||
return nil if secret.nil? | ||
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
|
||
|
There was a problem hiding this comment.
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:and then reference
sd
instead ofsecret.data
everywhere, without changing any of the existing logic?