Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(SUP-4929) Update to S0022 for handling license files with .key and .lic file #230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 !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

Expand Down
32 changes: 32 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,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')
Expand Down
Loading