Skip to content

Commit d2c2304

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

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 32 additions & 0 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
@@ -31,6 +50,19 @@ def save(request)
3150
package_inventory = inventory['packages'] if inventory.respond_to?(:keys)
3251
facts.values.delete('_puppet_inventory_1')
3352

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+
)
65+
3466
payload_value = {
3567
"certname" => facts.name,
3668
"values" => facts.values,

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ 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+
:fact_names_blacklist => "",
23+
:fact_names_blacklist_regex => ""
2224
}
2325

2426
config_file ||= File.join(Puppet[:confdir], "puppetdb.conf")
@@ -71,7 +73,9 @@ def self.load(config_file = nil)
7173
:submit_only_server_urls,
7274
:command_broadcast,
7375
:sticky_read_failover,
74-
:verify_client_certificate].include?(k))
76+
:verify_client_certificate,
77+
:fact_names_blacklist,
78+
:fact_names_blacklist_regex].include?(k))
7579
end
7680

7781
parsed_urls = config_hash[:server_urls].split(",").map {|s| s.strip}
@@ -108,6 +112,10 @@ def self.load(config_file = nil)
108112
"or equal to the number of server_urls (#{config_hash[:server_urls].length})"
109113
end
110114

115+
config_hash[:fact_names_blacklist] = config_hash[:fact_names_blacklist].split(",").map {|s| s.strip}
116+
117+
config_hash[:fact_names_blacklist_regex] = config_hash[:fact_names_blacklist_regex].split(",").map {|s| s.strip}
118+
111119
self.new(config_hash)
112120
rescue => detail
113121
Puppet.log_exception detail, "Could not configure PuppetDB terminuses: #{detail.message}", {level: :warning}
@@ -160,6 +168,15 @@ def verify_client_certificate
160168
config[:verify_client_certificate]
161169
end
162170

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

165182
# @!attribute [r] count

0 commit comments

Comments
 (0)