-
Notifications
You must be signed in to change notification settings - Fork 580
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
Support sensitive values in to_json_pretty
#1418
Open
alexjfisher
wants to merge
1
commit into
puppetlabs:main
Choose a base branch
from
alexjfisher:why_so_sensitive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary Unwraps any sensitives in data and returns a sensitive | ||
# | ||
# It's not uncommon to have Sensitive strings as values within a hash or array. | ||
# Before passing the data to a type property or another function, it's useful | ||
# to be able to `unwrap` these values first. This function does this. If | ||
# sensitive data was included in the data, the whole result is then rewrapped | ||
# as Sensitive. | ||
# | ||
# Optionally, this function can be passed a block. When a block is given, it will | ||
# be run with the unwrapped data, but before the final rewrapping. This is useful | ||
# to provide transparent rewrapping to other functions in stdlib especially. | ||
# | ||
# This is analogous to the way `epp` transparently handles sensitive parameters. | ||
Puppet::Functions.create_function(:'stdlib::rewrap_sensitive_data') do | ||
# @param data The data | ||
# @param block A lambda that will be run after the data has been unwrapped, but before it is rewrapped, (if it contained sensitives) | ||
# @return Returns the rewrapped data | ||
dispatch :rewrap_sensitive_data do | ||
param 'Any', :data | ||
optional_block_param 'Callable[Any]', :block | ||
return_type 'Any' | ||
end | ||
|
||
def rewrap_sensitive_data(data) | ||
@contains_sensitive = false | ||
|
||
unwrapped = deep_unwrap(data) | ||
|
||
result = block_given? ? yield(unwrapped) : unwrapped | ||
|
||
if @contains_sensitive | ||
Puppet::Pops::Types::PSensitiveType::Sensitive.new(result) | ||
else | ||
result | ||
end | ||
end | ||
|
||
def deep_unwrap(obj) | ||
case obj | ||
when Hash | ||
obj.each_with_object({}) do |(key, value), result| | ||
if key.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive) | ||
# This situation is probably fairly unlikely in reality, but easy enough to support | ||
@contains_sensitive = true | ||
key = key.unwrap | ||
end | ||
result[key] = deep_unwrap(value) | ||
end | ||
when Array | ||
obj.map { |element| deep_unwrap(element) } | ||
when Puppet::Pops::Types::PSensitiveType::Sensitive | ||
@contains_sensitive = true | ||
deep_unwrap(obj.unwrap) | ||
else | ||
obj | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,8 +67,10 @@ def to_json_pretty(data, skip_undef = false, opts = nil) | |
end | ||
|
||
data = data.compact if skip_undef && (data.is_a?(Array) || Hash) | ||
# Call ::JSON to ensure it references the JSON library from Ruby's standard library | ||
# instead of a random JSON namespace that might be in scope due to user code. | ||
JSON.pretty_generate(data, opts) << "\n" | ||
call_function('stdlib::rewrap_sensitive_data', data) do |unwrapped_data| | ||
# Call ::JSON to ensure it references the JSON library from Ruby's standard library | ||
# instead of a random JSON namespace that might be in scope due to user code. | ||
::JSON.pretty_generate(unwrapped_data, opts) << "\n" | ||
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. Leading colons put back in to match existing comment. See #1307 (comment) |
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'stdlib::rewrap_sensitive_data' do | ||
it { is_expected.not_to be_nil } | ||
|
||
context 'when called with data containing no sensitive elements' do | ||
it { is_expected.to run.with_params({}).and_return({}) } | ||
it { is_expected.to run.with_params([]).and_return([]) } | ||
it { is_expected.to run.with_params('a_string').and_return('a_string') } | ||
it { is_expected.to run.with_params(42).and_return(42) } | ||
it { is_expected.to run.with_params(true).and_return(true) } | ||
it { is_expected.to run.with_params(false).and_return(false) } | ||
|
||
it { is_expected.to run.with_params({ 'foo' => 'bar' }).and_return({ 'foo' => 'bar' }) } | ||
end | ||
|
||
context 'when called with a hash containing a sensitive string' do | ||
it 'unwraps the sensitive string and returns a sensitive hash' do | ||
is_expected.to run.with_params( | ||
{ | ||
'username' => 'my_user', | ||
'password' => sensitive('hunter2') | ||
}, | ||
).and_return(sensitive( | ||
{ | ||
'username' => 'my_user', | ||
'password' => 'hunter2' | ||
}, | ||
)) | ||
end | ||
end | ||
|
||
context 'when called with data containing lots of sensitive elements (including nested in arrays, and sensitive hashes etc)' do | ||
it 'recursively unwraps everything and marks the whole result as sensitive' do | ||
is_expected.to run.with_params( | ||
{ | ||
'a' => sensitive('bar'), | ||
'b' => [ | ||
1, | ||
2, | ||
:undef, | ||
true, | ||
false, | ||
{ | ||
'password' => sensitive('secret'), | ||
'weird_example' => sensitive({ 'foo' => sensitive(42) }) # A sensitive hash containing a sensitive Int as the value to a hash contained in an array which is the value of a hash key... | ||
}, | ||
], | ||
'c' => :undef, | ||
'd' => [], | ||
'e' => true, | ||
'f' => false, | ||
}, | ||
).and_return(sensitive( | ||
{ | ||
'a' => 'bar', | ||
'b' => [ | ||
1, | ||
2, | ||
:undef, | ||
true, | ||
false, | ||
{ | ||
'password' => 'secret', | ||
'weird_example' => { 'foo' => 42 } | ||
}, | ||
], | ||
'c' => :undef, | ||
'd' => [], | ||
'e' => true, | ||
'f' => false, | ||
}, | ||
)) | ||
end | ||
end | ||
|
||
context 'when a hash _key_ is sensitive' do | ||
it 'unwraps the key' do | ||
is_expected.to run.with_params( | ||
{ | ||
sensitive('key') => 'value', | ||
}, | ||
).and_return(sensitive( | ||
{ | ||
'key' => 'value', | ||
}, | ||
)) | ||
end | ||
end | ||
|
||
context 'when called with a block' do | ||
context 'that upcases hash values' do | ||
it do | ||
is_expected.to run | ||
.with_params({ 'secret' => sensitive('hunter2') }) | ||
.with_lambda { |data| data.transform_values { |value| value.upcase } } | ||
.and_return(sensitive({ 'secret' => 'HUNTER2' })) | ||
end | ||
end | ||
context 'that converts data to yaml' do | ||
it do | ||
is_expected.to run | ||
.with_params({ 'secret' => sensitive('hunter2') }) | ||
.with_lambda { |data| data.to_yaml } | ||
.and_return(sensitive("---\nsecret: hunter2\n")) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should the yielded
unwrapped
actually return aSensitive
. Or in other words, should it beyield(wrapped)
instead?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.
No, I think this is correct. The block, (if provided), needs to be handed the unwrapped data so that it can mutate it as desired (eg. serialising to yaml or json), before this function then wraps the result back up with sensitive.
I've added the first spec tests that hopefully illustrates this a bit better.