From 6d521dc74bde0387fe99850d01190539a5290bb2 Mon Sep 17 00:00:00 2001 From: Aria Li Date: Wed, 30 Oct 2024 11:50:06 -0700 Subject: [PATCH] (PUP-12083) Update soft limit warning for fact value & name length This commit updates the warning for exceeding the fact value length soft limit to include the fact name and updates the warning for exceeding the fact name length soft limit to give the fact name with dots & include the length that was used to evaluate if the length exceeds the limit. --- lib/puppet/configurer.rb | 28 +++++++++++++++++----------- spec/unit/configurer_spec.rb | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/puppet/configurer.rb b/lib/puppet/configurer.rb index 617cd482cdc..f322d29b795 100644 --- a/lib/puppet/configurer.rb +++ b/lib/puppet/configurer.rb @@ -135,36 +135,37 @@ def warn_number_of_facts(size, max_number) Puppet.warning _("The current total number of fact values: %{size} exceeds the fact values limit: %{max_size}") % { size: size, max_size: max_number } end - def warn_fact_name_length(name, max_length) - Puppet.warning _("Fact %{name} with length: '%{length}' exceeds the length limit: %{limit}") % { name: name, length: name.to_s.bytesize, limit: max_length } + def warn_fact_name_length(name, max_length, fact_name_length) + Puppet.warning _("Fact %{name} with length: '%{length}' exceeds the length limit: %{limit}") % { name: name, length: fact_name_length, limit: max_length } end def warn_number_of_top_level_facts(size, max_number) Puppet.warning _("The current number of top level facts: %{size} exceeds the top facts limit: %{max_size}") % { size: size, max_size: max_number } end - def warn_fact_value_length(value, max_length) - Puppet.warning _("Fact value '%{value}' with the value length: '%{length}' exceeds the value length limit: %{max_length}") % { value: value, length: value.to_s.bytesize, max_length: max_length } + def warn_fact_value_length(name, value, max_length) + Puppet.warning _("Fact '%{name}' with value '%{value}' with the value length: '%{length}' exceeds the value length limit: %{max_length}") % { name: name, value: value, length: value.to_s.bytesize, max_length: max_length } end def warn_fact_payload_size(payload, max_size) Puppet.warning _("Payload with the current size of: '%{payload}' exceeds the payload size limit: %{max_size}") % { payload: payload, max_size: max_size } end - def check_fact_name_length(name, number_of_dots) + def check_fact_name_length(fact_path, number_of_dots) max_length = Puppet[:fact_name_length_soft_limit] return if max_length.zero? + name_without_dots = fact_path.join() # rough byte size estimations of fact path as a postgresql btree index - size_as_btree_index = 8 + (number_of_dots * 2) + name.to_s.bytesize - warn_fact_name_length(name, max_length) if size_as_btree_index > max_length + size_as_btree_index = 8 + (number_of_dots * 2) + name_without_dots.to_s.bytesize + warn_fact_name_length(fact_path.join('.'), max_length, size_as_btree_index) if size_as_btree_index > max_length end - def check_fact_values_length(values) + def check_fact_values_length(name, values) max_length = Puppet[:fact_value_length_soft_limit] return if max_length.zero? - warn_fact_value_length(values, max_length) if values.to_s.bytesize > max_length + warn_fact_value_length(name, values, max_length) if values.to_s.bytesize > max_length end def check_top_level_number_limit(size) @@ -204,8 +205,8 @@ def parse_fact_name_and_value_limits(object, path = []) path.pop end else - check_fact_name_length(path.join(), path.size) - check_fact_values_length(object) + check_fact_name_length(path, path.size) # second param is number of dots in fact name + check_fact_values_length(path.join('.'), object) @number_of_facts += 1 end end @@ -757,3 +758,8 @@ def download_plugins(remote_environment_for_plugins) end end end + +#This commit updates the warning for exceeding the fact value length soft limit +#to include the fact name and updates the warning for exceeding the fact name +#length soft limit to include the length that was used to evaluate if the length +#exceeds the limit. \ No newline at end of file diff --git a/spec/unit/configurer_spec.rb b/spec/unit/configurer_spec.rb index 289b6ddc8da..0172dc342da 100644 --- a/spec/unit/configurer_spec.rb +++ b/spec/unit/configurer_spec.rb @@ -213,7 +213,7 @@ } Puppet::Node::Facts.indirection.save(facts) - expect(Puppet).to receive(:warning).with(/Fact value '.+' with the value length: '[1-9]*' exceeds the value length limit: [1-9]*/).twice + expect(Puppet).to receive(:warning).with(/Fact '.+\..+' with value '.+' with the value length: '[1-9]*' exceeds the value length limit: [1-9]*/).twice configurer.run end @@ -288,16 +288,24 @@ end it "should warn the user when the fact name length limits are exceeded" do - Puppet[:fact_name_length_soft_limit] = 1 + Puppet[:fact_name_length_soft_limit] = 30 Puppet[:fact_value_length_soft_limit] = 0 Puppet[:top_level_facts_soft_limit] = 0 Puppet[:number_of_facts_soft_limit] = 0 Puppet[:payload_soft_limit] = 0 - facts.values = {'my_new_fact_name' => 'my_new_fact_value'} + facts.values = { 'processors' => { + 'cores' => 1, + 'count' => 2, + 'isa' => "i386", + 'models' => [ + "CPU1 @ 2.80GHz" + ], + 'physicalcount' => 4 } + } Puppet::Node::Facts.indirection.save(facts) - expect(Puppet).to receive(:warning).with(/Fact .+ with length: '[1-9]*' exceeds the length limit: [1-9]*/) + expect(Puppet).to receive(:warning).with(/Fact .+\..+ with length: '[1-9]*' exceeds the length limit: [1-9]*/).twice configurer.run end