Skip to content

Commit d0d6715

Browse files
committed
Add configuration options to filter facts out in puppetdb termini
This patch works as well for structured facts, not like puppetlabs#3998
1 parent 3e29283 commit d0d6715

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

puppet/lib/puppet/indirector/facts/puppetdb.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ def get_trusted_info(node)
1616
trusted.to_h
1717
end
1818

19+
def filter_facts(obj, blacklist, blacklist_regexps, path = [])
20+
regexps = blacklist_regexps.map { |re| Regexp.new(re) }
21+
Puppet.warning("Received obj with values: #{obj.inspect}")
22+
case obj
23+
when Hash
24+
obj.each_with_object({}) do |(k, v), h|
25+
full_path = (path + [k]).join('.')
26+
excluded = blacklist.include?(full_path) || regexps.any? { |re| full_path =~ re }
27+
Puppet.warning("Fact filter: checking '#{full_path}'#{excluded ? ' [EXCLUDED]' : ''}")
28+
next if excluded
29+
h[k] = filter_facts(v, blacklist, blacklist_regexps, path + [k])
30+
end
31+
when Array
32+
obj.map.with_index { |v, i| filter_facts(v, blacklist, blacklist_regexps, path + [i.to_s]) }
33+
else
34+
obj
35+
end
36+
end
37+
1938
def save(request)
2039
profile("facts#save", [:puppetdb, :facts, :save, request.key]) do
2140
current_time = Time.now
@@ -29,7 +48,20 @@ def save(request)
2948

3049
inventory = facts.values['_puppet_inventory_1']
3150
package_inventory = inventory['packages'] if inventory.respond_to?(:keys)
32-
facts.values.delete('_puppet_inventory_1')
51+
facts.values.delete('_puppet_inventory_1'))
52+
53+
fact_names_blacklist = Puppet::Util::Puppetdb.config.fact_names_blacklist
54+
55+
fact_names_blacklist.each{|blacklisted_fact_name|
56+
facts.values.delete(blacklisted_fact_name)
57+
}
58+
59+
fact_names_blacklist_regexps = Puppet::Util::Puppetdb.config.fact_names_blacklist_regex
60+
facts.values = filter_facts(
61+
facts.values,
62+
fact_names_blacklist,
63+
fact_names_blacklist_regexps
64+
)
3365

3466
payload_value = {
3567
"certname" => facts.name,
@@ -155,4 +187,4 @@ def headers
155187
"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
156188
}
157189
end
158-
end
190+
end

puppet/lib/puppet/util/puppetdb/config.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def self.load(config_file = nil)
1818
:submit_only_server_urls => "",
1919
:command_broadcast => false,
2020
:sticky_read_failover => false,
21-
:verify_client_certificate => true
21+
:verify_client_certificate => true,
22+
:verify_client_certificate => true,
23+
:fact_names_blacklist => "",
24+
:fact_names_blacklist_regex => ""
2225
}
2326

2427
config_file ||= File.join(Puppet[:confdir], "puppetdb.conf")
@@ -71,7 +74,9 @@ def self.load(config_file = nil)
7174
:submit_only_server_urls,
7275
:command_broadcast,
7376
:sticky_read_failover,
74-
:verify_client_certificate].include?(k))
77+
:verify_client_certificate,
78+
:fact_names_blacklist,
79+
:fact_names_blacklist_regex].include?(k))
7580
end
7681

7782
parsed_urls = config_hash[:server_urls].split(",").map {|s| s.strip}
@@ -108,6 +113,10 @@ def self.load(config_file = nil)
108113
"or equal to the number of server_urls (#{config_hash[:server_urls].length})"
109114
end
110115

116+
config_hash[:fact_names_blacklist] = config_hash[:fact_names_blacklist].split(",").map {|s| s.strip}
117+
118+
config_hash[:fact_names_blacklist_regex] = config_hash[:fact_names_blacklist_regex].split(",").map {|s| s.strip}
119+
111120
self.new(config_hash)
112121
rescue => detail
113122
Puppet.log_exception detail, "Could not configure PuppetDB terminuses: #{detail.message}", {level: :warning}
@@ -160,6 +169,15 @@ def verify_client_certificate
160169
config[:verify_client_certificate]
161170
end
162171

172+
def fact_names_blacklist
173+
config[:fact_names_blacklist]
174+
end
175+
176+
def fact_names_blacklist_regex
177+
config[:fact_names_blacklist_regex]
178+
end
179+
180+
163181
# @!group Private instance methods
164182

165183
# @!attribute [r] count

0 commit comments

Comments
 (0)