Skip to content

Commit 68c2e23

Browse files
authored
(GH-87) Stop logging secret not found in hiera lookup as warning (#88)
1 parent f08d331 commit 68c2e23

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

lib/puppet/functions/azure_key_vault/lookup.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ def lookup_key(secret_name, options, context)
4949
Puppet.warning(e.message)
5050
secret_value = nil
5151
end
52-
context.not_found if secret_value.nil?
53-
return if secret_value.nil?
52+
53+
if secret_value.nil?
54+
context.not_found
55+
return
56+
end
5457
context.cache(normalized_secret_name, secret_value)
5558
end
5659
end

lib/puppet/functions/azure_key_vault/secret.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def secret(cache, vault_name, secret_name, api_versions_hash, secret_version = '
3434
secret_version,
3535
)
3636

37+
raise Puppet::Error, "The secret named #{secret_name} could not be found in a vault named #{vault_name}" if secret_value.nil?
38+
3739
Puppet::Pops::Types::PSensitiveType::Sensitive.new(secret_value)
3840
end
3941
end

lib/puppet_x/tragiccode/azure.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def self.get_secret(vault_name, secret_name, vault_api_version, access_token, se
2727
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
2828
http.request(req)
2929
end
30+
return nil if res.is_a?(Net::HTTPNotFound)
3031
raise res.body unless res.is_a?(Net::HTTPSuccess)
3132
JSON.parse(res.body)['value']
3233
end

spec/functions/azure_key_vault_lookup_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,16 @@
106106
'profile::windows::sqlserver::sensitive_sql_user_password', options.merge({ 'confine_to_keys' => ['^sensitive_azure.*$'] }), lookup_context
107107
)
108108
end
109+
110+
it 'calls context.not_found when secret is not found in vault' do
111+
access_token_value = 'access_value'
112+
113+
expect(lookup_context).to receive(:not_found)
114+
expect(TragicCode::Azure).to receive(:get_access_token).and_return(access_token_value)
115+
expect(TragicCode::Azure).to receive(:get_secret).and_return(nil)
116+
117+
is_expected.to run.with_params(
118+
'profile::windows::sqlserver::sensitive_azure_sql_user_password', options.merge({ 'confine_to_keys' => ['^.*sensitive_azure.*'] }), lookup_context
119+
)
120+
end
109121
end

spec/functions/azure_key_vault_secret_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
context 'when getting the latest version of a secret' do
2424
it 'defaults to using an empty string as the latest version' do
2525
expect(TragicCode::Azure).to receive(:get_access_token).with(api_versions_hash['metadata_api_version']).and_return(access_token)
26-
expect(TragicCode::Azure).to receive(:get_secret).with(vault_name, secret_name, api_versions_hash['vault_api_version'], access_token, '')
26+
expect(TragicCode::Azure).to receive(:get_secret).with(vault_name, secret_name, api_versions_hash['vault_api_version'], access_token, '').and_return(secret_value)
2727

2828
is_expected.to run.with_params(vault_name, secret_name, api_versions_hash)
2929
end
@@ -32,12 +32,23 @@
3232
context 'when getting a specific version of a secret' do
3333
it 'uses the secret version when retreiving the secret' do
3434
expect(TragicCode::Azure).to receive(:get_access_token).with(api_versions_hash['metadata_api_version']).and_return(access_token)
35-
expect(TragicCode::Azure).to receive(:get_secret).with(vault_name, secret_name, api_versions_hash['vault_api_version'], access_token, secret_version)
35+
expect(TragicCode::Azure).to receive(:get_secret).with(vault_name, secret_name, api_versions_hash['vault_api_version'], access_token, secret_version).and_return(secret_value)
3636

3737
is_expected.to run.with_params(vault_name, secret_name, api_versions_hash, secret_version)
3838
end
3939
end
4040

41+
context 'when getting a secret that does not exist in the vault' do
42+
it 'throws an error' do
43+
expect(TragicCode::Azure).to receive(:get_access_token).with(api_versions_hash['metadata_api_version']).and_return(access_token)
44+
expect(TragicCode::Azure).to receive(:get_secret).with(vault_name, secret_name, api_versions_hash['vault_api_version'], access_token, secret_version).and_return(nil)
45+
46+
is_expected.to run.with_params(
47+
vault_name, secret_name, api_versions_hash, secret_version
48+
).and_raise_error(Puppet::Error, %r{The secret named #{secret_name} could not be found in a vault named #{vault_name}}i)
49+
end
50+
end
51+
4152
# rubocop:disable RSpec/NamedSubject
4253
it 'returns the secret' do
4354
expect(TragicCode::Azure).to receive(:get_access_token).with(api_versions_hash['metadata_api_version']).and_return(access_token)

0 commit comments

Comments
 (0)