From 5d4977c9060ebccac3270d5ff003b26c01870890 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Mon, 10 Feb 2025 14:11:05 -0500 Subject: [PATCH 1/2] openssl_version: Adjust regex to match more openssl version strings --- lib/facter/openssl_version.rb | 2 +- spec/unit/openssl_version_spec.rb | 54 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/facter/openssl_version.rb b/lib/facter/openssl_version.rb index c5d9c913..e9aeaf03 100644 --- a/lib/facter/openssl_version.rb +++ b/lib/facter/openssl_version.rb @@ -4,7 +4,7 @@ setcode do if Facter::Util::Resolution.which('openssl') openssl_version = Facter::Util::Resolution.exec('openssl version 2>&1') - matches = %r{^OpenSSL ([\w.-]+)(\s+FIPS)?( +)([\d.]+)( +)([\w.]+)( +)([\d.]+)}.match(openssl_version) + matches = %r{^OpenSSL ([\w.]+)[ -]*(fips|FIPS)? +([\d.]+) +([\w.]+) +([\d.]+) *(\([\w:. ]+\))?}.match(openssl_version) matches[1] if matches end end diff --git a/spec/unit/openssl_version_spec.rb b/spec/unit/openssl_version_spec.rb index 95ddbd2f..60d057ec 100644 --- a/spec/unit/openssl_version_spec.rb +++ b/spec/unit/openssl_version_spec.rb @@ -45,7 +45,7 @@ end it { - expect(Facter.value(:openssl_version)).to eq('1.0.1e-fips') + expect(Facter.value(:openssl_version)).to eq('1.0.1e') } end end @@ -88,6 +88,58 @@ } end end + + describe 'openssl_version oracle7' do + context 'with value' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.0.2k-fips 26 Jan 2017') + end + + it { + expect(Facter.value(:openssl_version)).to eq('1.0.2k') + } + end + end + + describe 'openssl_version oracle8' do + context 'with value' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1k FIPS 25 Mar 2021') + end + + it { + expect(Facter.value(:openssl_version)).to eq('1.1.1k') + } + end + end + + describe 'openssl_version debian11' do + context 'with value' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1w 11 Sep 2023') + end + + it { + expect(Facter.value(:openssl_version)).to eq('1.1.1w') + } + end + end + + describe 'openssl_version debian12' do + context 'with value' do + before do + allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 3.0.15 3 Sep 2024 (Library: OpenSSL 3.0.15 3 Sep 2024)') + end + + it { + expect(Facter.value(:openssl_version)).to eq('3.0.15') + } + end + end end end end From 99bd40e30b6fa737499bae3407427c22a8e0d710 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Mon, 10 Feb 2025 16:15:53 -0500 Subject: [PATCH 2/2] tests: DRY out openssl_version fact tests --- spec/unit/openssl_version_spec.rb | 173 ++++++++++++++---------------- 1 file changed, 80 insertions(+), 93 deletions(-) diff --git a/spec/unit/openssl_version_spec.rb b/spec/unit/openssl_version_spec.rb index 60d057ec..f8c3db39 100644 --- a/spec/unit/openssl_version_spec.rb +++ b/spec/unit/openssl_version_spec.rb @@ -3,6 +3,73 @@ require 'spec_helper' require 'facter' +fact_matrix = { + 'debian-11-x86_64' => { + return_string: 'OpenSSL 1.1.1w 11 Sep 2023', + version_string: '1.1.1w', + }, + 'debian-12-x86_64' => { + return_string: 'OpenSSL 3.0.15 3 Sep 2024 (Library: OpenSSL 3.0.15 3 Sep 2024)', + version_string: '3.0.15', + }, + 'ubuntu-20.04-x86_64' => { + return_string: 'OpenSSL 1.1.1f 31 Mar 2020', + version_string: '1.1.1f', + }, + 'ubuntu-22.04-x86_64' => { + return_string: 'OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)', + version_string: '3.0.2', + }, + 'ubuntu-24.04-x86_64' => { + return_string: 'OpenSSL 3.0.13 30 Jan 2024 (Library: OpenSSL 3.0.13 30 Jan 2024)', + version_string: '3.0.13', + }, + 'redhat-8-legacy' => { + return_string: 'OpenSSL 1.1.1c FIPS 28 May 2019', + version_string: '1.1.1c', + }, + 'redhat-8-x86_64' => { + return_string: 'OpenSSL 1.1.1k FIPS 25 Mar 2021', + version_string: '1.1.1k', + }, + 'redhat-9-x86_64' => { + return_string: 'OpenSSL 9.9.9zzz FIPS 1 Jan 2099', + version_string: '9.9.9zzz', + }, + 'oraclelinux-8-x86_64' => { + return_string: 'OpenSSL 1.1.1k FIPS 25 Mar 2021', + version_string: '1.1.1k', + }, + 'oraclelinux-9-x86_64' => { + return_string: 'OpenSSL 3.2.2 4 Jun 2024 (Library: OpenSSL 3.2.2 4 Jun 2024)', + version_string: '3.2.2', + }, + 'rocky-8-x86_64' => { + return_string: 'OpenSSL 1.1.1k FIPS 25 Mar 2021', + version_string: '1.1.1k', + }, + 'rocky-9-x86_64' => { + return_string: 'OpenSSL 3.2.2 4 Jun 2024 (Library: OpenSSL 3.2.2 4 Jun 2024)', + version_string: '3.2.2', + }, + 'almalinux-8-x86_64' => { + return_string: 'OpenSSL 1.1.1k FIPS 25 Mar 2021', + version_string: '1.1.1k', + }, + 'almalinux-9-x86_64' => { + return_string: 'OpenSSL 3.2.2 4 Jun 2024 (Library: OpenSSL 3.2.2 4 Jun 2024)', + version_string: '3.2.2', + }, + 'centos-9-x86_64' => { + return_string: 'OpenSSL 1.0.2g 1 Mar 2016', + version_string: '1.0.2g', + }, + 'legacy' => { + return_string: 'OpenSSL 0.9.8zg 14 July 2015', + version_string: '0.9.8zg', + }, +} + describe Facter.fact(:openssl_version) do on_supported_os.each do |os, facts| context "on #{os}" do @@ -16,11 +83,11 @@ context 'with value' do before do allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 0.9.8zg 14 July 2015') + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return(fact_matrix[os][:return_string]) end it { - expect(Facter.value(:openssl_version)).to eq('0.9.8zg') + expect(Facter.value(:openssl_version)).to eq(fact_matrix[os][:version_string]) } end @@ -36,107 +103,27 @@ } end end + end + end - describe 'openssl_version rhel' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.0.1e-fips 11 Feb 2013') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.0.1e') - } - end - end - - describe 'openssl_version centos' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.0.2g 1 Mar 2016') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.0.2g') - } - end - end - - describe 'openssl_version rhel8' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1c FIPS 28 May 2019') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.1.1c') - } - end - end - - describe 'openssl_version rhel8 latest' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1k FIPS 25 Mar 2021') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.1.1k') - } - end - end - - describe 'openssl_version oracle7' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.0.2k-fips 26 Jan 2017') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.0.2k') - } - end - end - - describe 'openssl_version oracle8' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1k FIPS 25 Mar 2021') - end - - it { - expect(Facter.value(:openssl_version)).to eq('1.1.1k') - } - end - end - - describe 'openssl_version debian11' do - context 'with value' do - before do - allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 1.1.1w 11 Sep 2023') - end + filler_facts = on_supported_os['redhat-9-x86_64'] + ['legacy', 'redhat-8-legacy'].each do |special_case| + context "on #{special_case}" do + let(:facts) { filler_facts } - it { - expect(Facter.value(:openssl_version)).to eq('1.1.1w') - } - end + before do + Facter.clear end - describe 'openssl_version debian12' do + describe 'openssl_version' do context 'with value' do before do allow(Facter::Util::Resolution).to receive(:which).with('openssl').and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return('OpenSSL 3.0.15 3 Sep 2024 (Library: OpenSSL 3.0.15 3 Sep 2024)') + allow(Facter::Util::Resolution).to receive(:exec).with('openssl version 2>&1').and_return(fact_matrix[special_case][:return_string]) end it { - expect(Facter.value(:openssl_version)).to eq('3.0.15') + expect(Facter.value(:openssl_version)).to eq(fact_matrix[special_case][:version_string]) } end end