Skip to content

Commit ee2fe03

Browse files
authored
(GH-95) Wrap hiera return in puppet sensitive data type (#96)
1 parent b5a157e commit ee2fe03

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lib/puppet/functions/azure_key_vault/lookup.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
param 'Variant[String, Numeric]', :secret_name
66
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
8+
return_type 'Variant[Sensitive, Undef]'
89
end
910

1011
def lookup_key(secret_name, options, context)
@@ -31,7 +32,7 @@ def lookup_key(secret_name, options, context)
3132

3233
normalized_secret_name = TragicCode::Azure.normalize_object_name(secret_name, options['key_replacement_token'] || '-')
3334
context.explain { "Using normalized KeyVault secret key for lookup: #{normalized_secret_name}" }
34-
return context.cached_value(normalized_secret_name) if context.cache_has_key(normalized_secret_name)
35+
return Puppet::Pops::Types::PSensitiveType::Sensitive.new(context.cached_value(normalized_secret_name)) if context.cache_has_key(normalized_secret_name)
3536
access_token = context.cached_value('access_token')
3637
if access_token.nil?
3738
access_token = TragicCode::Azure.get_access_token(options['metadata_api_version'])
@@ -54,6 +55,6 @@ def lookup_key(secret_name, options, context)
5455
context.not_found
5556
return
5657
end
57-
context.cache(normalized_secret_name, secret_value)
58+
Puppet::Pops::Types::PSensitiveType::Sensitive.new(context.cache(normalized_secret_name, secret_value))
5859
end
5960
end

lib/puppet/functions/azure_key_vault/secret.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Retrieves secrets from Azure's Key Vault.
44
Puppet::Functions.create_function(:'azure_key_vault::secret', Puppet::Functions::InternalFunction) do
5-
# @param vault_name Name of the vault in your Azure subcription.
5+
# @param vault_name Name of the vault in your Azure subscription.
66
# @param secret_name Name of the secret to be retrieved.
77
# @param api_versions_hash A Hash of the exact versions of the metadata_api_version and vault_api_version to use.
88
# @param secret_version The version of the secret you want to retrieve. This parameter is optional and if not passed the default behavior is to retrieve the latest version.
@@ -13,6 +13,7 @@
1313
required_param 'String', :secret_name
1414
required_param 'Hash', :api_versions_hash
1515
optional_param 'String', :secret_version
16+
return_type 'Sensitive[String]'
1617
end
1718

1819
def secret(cache, vault_name, secret_name, api_versions_hash, secret_version = '')

spec/functions/azure_key_vault_lookup_spec.rb

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232
'profile::windows::sqlserver::sensitive_azure_sql_user_password', { 'key1' => 'value1' }, lookup_context
3333
).and_raise_error(ArgumentError)
3434
end
35+
36+
# rubocop:disable RSpec/NamedSubject
3537
it 'uses the cache' do
3638
expect(lookup_context).to receive(:cache_has_key).with('profile--windows--sqlserver--sensitive-azure-sql-user-password').and_return(true)
3739
expect(lookup_context).to receive(:cached_value).with('profile--windows--sqlserver--sensitive-azure-sql-user-password').and_return('value')
38-
is_expected.to run.with_params(
39-
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context
40-
).and_return('value')
40+
41+
expect(subject.execute('profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context).unwrap).to eq 'value'
4142
end
43+
# rubocop:enable RSpec/NamedSubject
44+
45+
# rubocop:disable RSpec/NamedSubject
4246
it 'caches the access token after a cache miss' do
4347
access_token_value = 'access_value'
4448
secret_value = 'secret_value'
@@ -48,10 +52,10 @@
4852
expect(lookup_context).to receive(:cache).with('access_token', access_token_value).ordered
4953
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
5054
expect(lookup_context).to receive(:cache).and_return(secret_value).ordered
51-
is_expected.to run.with_params(
52-
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context
53-
).and_return(secret_value)
55+
56+
expect(subject.execute('profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context).unwrap).to eq secret_value
5457
end
58+
# rubocop:enable RSpec/NamedSubject
5559

5660
it 'call context.not_found for the lookup_options key' do
5761
expect(lookup_context).to receive(:not_found)
@@ -60,17 +64,18 @@
6064
)
6165
end
6266

67+
# rubocop:disable RSpec/NamedSubject
6368
it 'uses - as the default key_replacement_token' do
6469
secret_name = 'profile::windows::sqlserver::sensitive_azure_sql_user_password'
6570
access_token_value = 'access_value'
6671
secret_value = 'secret_value'
6772
expect(TragicCode::Azure).to receive(:normalize_object_name).with(secret_name, '-')
6873
expect(TragicCode::Azure).to receive(:get_access_token).and_return(access_token_value)
6974
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
70-
is_expected.to run.with_params(
71-
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context
72-
).and_return(secret_value)
75+
76+
expect(subject.execute('profile::windows::sqlserver::sensitive_azure_sql_user_password', options, lookup_context).unwrap).to eq secret_value
7377
end
78+
# rubocop:enable RSpec/NamedSubject
7479

7580
it 'errors when confine_to_keys is no array' do
7681
is_expected.to run.with_params(
@@ -84,15 +89,17 @@
8489
).and_raise_error(ArgumentError, %r{creating regexp failed with}i)
8590
end
8691

92+
# rubocop:disable RSpec/NamedSubject
8793
it 'returns the key if regex matches confine_to_keys' do
8894
access_token_value = 'access_value'
8995
secret_value = 'secret_value'
9096
expect(TragicCode::Azure).to receive(:get_access_token).and_return(access_token_value)
9197
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
92-
is_expected.to run.with_params(
93-
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context
94-
).and_return(secret_value)
98+
99+
expect(subject.execute('profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context).unwrap)
100+
.to eq secret_value
95101
end
102+
# rubocop:enable RSpec/NamedSubject
96103

97104
it 'does not return the key if regex does not match confine_to_keys' do
98105
access_token_value = 'access_value'
@@ -118,4 +125,16 @@
118125
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context
119126
)
120127
end
128+
129+
# rubocop:disable RSpec/NamedSubject
130+
it 'returns the secret wrapped in the sensitive data type' do
131+
access_token_value = 'access_value'
132+
secret_value = 'secret_value'
133+
expect(TragicCode::Azure).to receive(:get_access_token).and_return(access_token_value)
134+
expect(TragicCode::Azure).to receive(:get_secret).and_return(secret_value)
135+
136+
expect(subject.execute('profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context))
137+
.to be_an_instance_of(Puppet::Pops::Types::PSensitiveType::Sensitive)
138+
end
139+
# rubocop:enable RSpec/NamedSubject
121140
end

0 commit comments

Comments
 (0)