From 012a00bc37c5dc78463a179d5d00cbfb3173fb9f Mon Sep 17 00:00:00 2001 From: Aaron McCracken Date: Wed, 26 Jun 2024 17:08:56 +0100 Subject: [PATCH] Update S0022 check to Ingest .lic files --- lib/facter/pe_status_check.rb | 57 ++++++++++++++----------- spec/acceptance/pe_status_check_spec.rb | 32 ++++++++++++++ 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/lib/facter/pe_status_check.rb b/lib/facter/pe_status_check.rb index 743bf266..c66a8d7f 100644 --- a/lib/facter/pe_status_check.rb +++ b/lib/facter/pe_status_check.rb @@ -237,39 +237,48 @@ # Also takes into account if the license type is Perpetual next unless ['primary'].include?(Facter.value('pe_status_check_role')) + # Check for suite license file + suite_license_file = '/etc/puppetlabs/suite-license.lic' + + # Check for license key file license_file = '/etc/puppetlabs/license.key' - if File.exist?(license_file) + + if File.exist?(suite_license_file) + # Presence of suite-license.lic file satisfies check + validity = true + elsif !validity && File.exist?(license_file) begin - license_type = File.readlines(license_file).grep(%r{license_type:}).first - if license_type.nil? - validity = true - elsif license_type.include? 'Perpetual' - validity = true - elsif license_type.include? 'Subscription' - require 'date' - begin - end_date = Date.parse(File.readlines(license_file).grep(%r{end:}).first) - today_date = Date.today - daysexp = (end_date - today_date).to_i - validity = ((today_date <= end_date) && (daysexp >= 90)) ? true : false - rescue StandardError => e - Facter.warn("Error in fact 'pe_status_check.S0022' when checking license end date: #{e.message}") - Facter.debug(e.backtrace) - # license file has missing or invalid end date + license_type = File.readlines(license_file).grep(%r{license_type:}).first + if license_type.nil? + validity = true + elsif license_type.include?('Perpetual') + validity = true + elsif license_type.include?('Subscription') + require 'date' + begin + end_date = Date.parse(File.readlines(license_file).grep(%r{end:}).first) + today_date = Date.today + daysexp = (end_date - today_date).to_i + validity = (today_date <= end_date) && (daysexp >= 90) + rescue StandardError => e + Facter.warn("Error in fact 'pe_status_check.S0022' when checking license end date: #{e.message}") + Facter.debug(e.backtrace) + # License file has missing or invalid end date + validity = false + end + else + # License file has invalid license_type validity = false - end - else - # license file has invalid license_type - validity = false - end + end rescue StandardError => e Facter.warn("Error in fact 'pe_status_check.S0022' when checking license type: #{e.message}") validity = false - end + end else - # license file doesn't exist + # Neither suite-license.lic nor license.key exist validity = false end + { S0022: validity } end diff --git a/spec/acceptance/pe_status_check_spec.rb b/spec/acceptance/pe_status_check_spec.rb index 3b01491e..b51c85b7 100644 --- a/spec/acceptance/pe_status_check_spec.rb +++ b/spec/acceptance/pe_status_check_spec.rb @@ -230,6 +230,38 @@ class {'pe_status_check': expect(result.stdout).to match(%r{false}) run_shell('mv -f /tmp/license.key /etc/puppetlabs/license.key') end + it 'if S0022 conditions when license.key is not present but suite-license is present to be true' do + run_shell('touch /etc/puppetlabs/suite-license.lic && mv /etc/puppetlabs/license.key /tmp/license.key') + result = run_shell('facter -p pe_status_check.S0022') + expect(result.stdout).to match(%r{true}) + run_shell('mv -f /tmp/license.key /etc/puppetlabs/license.key') + end + it 'if S0022 conditions when both license files are not present to be false' do + run_shell('mv /etc/puppetlabs/license.key /tmp/license.key && mv /etc/puppetlabs/suite-license.lic /tmp/suite-license.lic') + result = run_shell('facter -p pe_status_check.S0022') + expect(result.stdout).to match(%r{false}) + run_shell('mv -f /tmp/license.key /etc/puppetlabs/license.key && mv -f /tmp/suite-license.lic /etc/puppetlabs/suite-license.lic') + end + it 'if S0022 conditions when both license files are present and one is invalid' do + myexpiredlicensefile = <<~EOF + ####################### + # Begin License File # + ####################### + # PUPPET ENTERPRISE LICENSE - Puppet Labs + to: test + nodes: 100 + license_type: Subscription + support_type: PE Premium + start: 2014-02-10 + end: 2022-03-13 + ##################### + # End License File # + ##################### + EOF + write_file(myexpiredlicensefile, '/etc/puppetlabs/license.key') + result = run_shell('facter -p pe_status_check.S0022') + expect(result.stdout).to match(%r{true}) + end it 'if S0024 conditions for false are met' do run_shell('touch -d "30 minutes ago" /opt/puppetlabs/server/data/puppetdb/stockpile/discard/test.file') result = run_shell('facter -p pe_status_check.S0024')