Skip to content

Commit

Permalink
Update S0022 check to Ingest .lic files
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronoftheages committed Jun 26, 2024
1 parent 1186753 commit 40e8f64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/facter/pe_status_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

Expand Down
12 changes: 12 additions & 0 deletions spec/acceptance/pe_status_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ 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 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')
Expand Down

0 comments on commit 40e8f64

Please sign in to comment.