From bc7a09e154310855b89b249891655f9dcbf78fc1 Mon Sep 17 00:00:00 2001 From: jonathannewman <jonathan.newman@puppetlabs.com> Date: Fri, 29 Mar 2024 16:38:27 -0700 Subject: [PATCH] (bug) ensure avoid nil dereference See https://github.com/puppetlabs/facter/issues/2696 This paches the two cloud providers accesses to metadata to avoid nil dereferneces which have been seen in production. --- lib/facter/facts/linux/cloud/provider.rb | 9 ++++++--- lib/facter/facts/windows/cloud/provider.rb | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/facter/facts/linux/cloud/provider.rb b/lib/facter/facts/linux/cloud/provider.rb index 58d061b88c..38af049cde 100644 --- a/lib/facter/facts/linux/cloud/provider.rb +++ b/lib/facter/facts/linux/cloud/provider.rb @@ -9,11 +9,14 @@ class Provider def call_the_resolver provider = case Facter::Util::Facts::Posix::VirtualDetector.platform when 'hyperv' - 'azure' unless Facter::Resolvers::Az.resolve(:metadata).empty? + metadata = Facter::Resolvers::Az.resolve(:metadata) + 'azure' unless metadata.nil? || metadata.empty? when 'kvm', 'xen' - 'aws' unless Facter::Resolvers::Ec2.resolve(:metadata).empty? + metadata = Facter::Resolvers::Ec2.resolve(:metadata) + 'aws' unless metadata.nil? || metadata.empty? when 'gce' - 'gce' unless Facter::Resolvers::Gce.resolve(:metadata).empty? + metadata = Facter::Resolvers::Gce.resolve(:metadata) + 'gce' unless metadata.nil? || metadata.empty? end Facter::ResolvedFact.new(FACT_NAME, provider) diff --git a/lib/facter/facts/windows/cloud/provider.rb b/lib/facter/facts/windows/cloud/provider.rb index 9fb7a52174..985b604722 100644 --- a/lib/facter/facts/windows/cloud/provider.rb +++ b/lib/facter/facts/windows/cloud/provider.rb @@ -10,11 +10,14 @@ def call_the_resolver virtual = Facter::Resolvers::Windows::Virtualization.resolve(:virtual) provider = case virtual when 'hyperv' - 'azure' unless Facter::Resolvers::Az.resolve(:metadata).empty? + metadata = Facter::Resolvers::Az.resolve(:metadata) + 'azure' unless metadata.nil? || metadata.empty? when 'kvm', 'xen' - 'aws' unless Facter::Resolvers::Ec2.resolve(:metadata).empty? + metadata = Facter::Resolvers::Ec2.resolve(:metadata) + 'aws' unless metadata.nil? || metadata.empty? when 'gce' - 'gce' unless Facter::Resolvers::Gce.resolve(:metadata).empty? + metadata = Facter::Resolvers::Gce.resolve(:metadata) + 'gce' unless metadata.nil? || metadata.empty? end Facter::ResolvedFact.new(FACT_NAME, provider)