Skip to content

Commit 8a754ed

Browse files
authored
Fix issue in which confine_to_keys must be an array of strings instead of regexp's (#77)
1 parent 95c611c commit 8a754ed

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/puppet/functions/azure_key_vault/lookup.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Puppet::Functions.create_function(:'azure_key_vault::lookup') do
44
dispatch :lookup_key do
55
param 'Variant[String, Numeric]', :secret_name
6-
param 'Struct[{vault_name => String, vault_api_version => String, metadata_api_version => String, confine_to_keys => Array[Regexp], Optional[key_replacement_token] => String}]', :options
6+
param 'Struct[{vault_name => String, vault_api_version => String, metadata_api_version => String, confine_to_keys => Array[String], Optional[key_replacement_token] => String}]', :options
77
param 'Puppet::LookupContext', :context
88
end
99

@@ -15,6 +15,12 @@ def lookup_key(secret_name, options, context)
1515
if confine_keys
1616
raise ArgumentError, 'confine_to_keys must be an array' unless confine_keys.is_a?(Array)
1717

18+
begin
19+
confine_keys = confine_keys.map { |r| Regexp.new(r) }
20+
rescue StandardError => e
21+
raise ArgumentError, "creating regexp failed with: #{e}"
22+
end
23+
1824
regex_key_match = Regexp.union(confine_keys)
1925

2026
unless secret_name[regex_key_match] == secret_name

spec/functions/azure_key_vault_lookup_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'vault_name' => 'vault_name',
77
'vault_api_version' => 'vault_api_version',
88
'metadata_api_version' => 'metadata_api_version',
9-
'confine_to_keys' => [%r{^.*sensitive_azure.*}],
9+
'confine_to_keys' => ['^.*sensitive_azure.*'],
1010
}
1111
end
1212
let(:lookup_context) do
@@ -81,7 +81,7 @@
8181
it 'errors when passing invalid regexes' do
8282
is_expected.to run.with_params(
8383
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['['] }), lookup_context
84-
).and_raise_error(ArgumentError, %r{'confine_to_keys' index 0 expects a Regexp value}i)
84+
).and_raise_error(ArgumentError, %r{creating regexp failed with}i)
8585
end
8686

8787
it 'returns the key if regex matches confine_to_keys' do
@@ -90,7 +90,7 @@
9090
expect(TragicCode::Azure).to receive(:get_access_token).and_return(access_token_value)
9191
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
9292
is_expected.to run.with_params(
93-
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => [%r{^.*sensitive_azure.*}] }), lookup_context
93+
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context
9494
).and_return(secret_value)
9595
end
9696

@@ -103,7 +103,7 @@
103103
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
104104

105105
is_expected.to run.with_params(
106-
'profile::windows::sqlserver::sensitive_sql_user_password', options.merge({ 'confine_to_keys' => [%r{^sensitive_azure.*$}] }), lookup_context
106+
'profile::windows::sqlserver::sensitive_sql_user_password', options.merge({ 'confine_to_keys' => ['^sensitive_azure.*$'] }), lookup_context
107107
)
108108
end
109109
end

0 commit comments

Comments
 (0)