Skip to content
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

Hide sensitive parameters #216

Merged
merged 4 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/octocatalog-diff/catalog-diff/differ.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'diffy'
require 'digest'
require 'hashdiff'
require 'json'
require 'set'
Expand Down Expand Up @@ -263,7 +264,7 @@ def filter_and_cleanup(catalog_resources)

# Handle parameters
if k == 'parameters'
cleansed_param = cleanse_parameters_hash(v)
cleansed_param = cleanse_parameters_hash(v, resource.fetch('sensitive_parameters', []))
hsh[k] = cleansed_param unless cleansed_param.nil? || cleansed_param.empty?
elsif k == 'tags'
# The order of tags is unimportant. Sort this array to avoid false diffs if order changes.
Expand Down Expand Up @@ -456,10 +457,18 @@ def ignored?(diff)

# Cleanse parameters of filtered attributes.
# @param parameters_hash [Hash] Hash of parameters
# @param sensitive_parameters [Array] Array of sensitive parameters
# @return [Hash] Cleaned parameters hash (original input hash is not altered)
def cleanse_parameters_hash(parameters_hash)
def cleanse_parameters_hash(parameters_hash, sensitive_parameters)
result = parameters_hash.dup

# hides sensitive params. We still need to know if there's a going to
# be a diff, so we hash the value.
sensitive_parameters.each do |p|
md5 = Digest::MD5.hexdigest Marshal.dump(result[p])
result[p] = 'Sensitive [md5sum ' + md5 + ']'
end

# 'before' and 'require' handle internal Puppet ordering but do not affect what
# happens on the target machine. Don't consider these for the purpose of catalog diff.
result.delete('before')
Expand Down
24 changes: 24 additions & 0 deletions spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,30 @@
result = testobj.catalog1
expect(result.first['title']).to eq('/etc/foo')
end

it 'should hide sensitive parameters' do
json_hash = {
'document_type' => 'Catalog',
'data' => {
'name' => 'rspec-node.github.net',
'tags' => [],
'resources' => [
{
'type' => 'File',
'title' => 'verysecretfile',
'parameters' => {
'content' => 'secret1'
},
'sensitive_parameters' => ['content']
}
]
}
}
catalog = OctocatalogDiff::Catalog.create(json: JSON.generate(json_hash))
testobj = OctocatalogDiff::CatalogDiff::Differ.new(@options, catalog, @empty_puppet_catalog)
result = testobj.catalog1
expect(result.first['parameters']['content']).to eq('Sensitive [md5sum e52d98c459819a11775936d8dfbb7929]')
end
end

describe '#diff' do
Expand Down