From 4d929523450102efe7e3fad4ede01a3de2dab9d1 Mon Sep 17 00:00:00 2001 From: Thomas Goirand Date: Tue, 11 May 2021 12:51:24 +0200 Subject: [PATCH] Add a swift_drive_audit_config puppet type This patch adds what's necessary to configure drive-audit.conf Change-Id: I9bee191412ead21f8fd765b8839b6d63f628bb9c --- .../swift_drive_audit_config/ini_setting.rb | 10 +++ lib/puppet/type/swift_drive_audit_config.rb | 53 ++++++++++++++ manifests/deps.pp | 6 ++ .../ini_setting_spec.rb | 72 +++++++++++++++++++ .../type/swift_drive_audit_config_spec.rb | 19 +++++ 5 files changed, 160 insertions(+) create mode 100644 lib/puppet/provider/swift_drive_audit_config/ini_setting.rb create mode 100644 lib/puppet/type/swift_drive_audit_config.rb create mode 100644 spec/unit/provider/swift_drive_audit_config/ini_setting_spec.rb create mode 100644 spec/unit/type/swift_drive_audit_config_spec.rb diff --git a/lib/puppet/provider/swift_drive_audit_config/ini_setting.rb b/lib/puppet/provider/swift_drive_audit_config/ini_setting.rb new file mode 100644 index 00000000..7c588e54 --- /dev/null +++ b/lib/puppet/provider/swift_drive_audit_config/ini_setting.rb @@ -0,0 +1,10 @@ +Puppet::Type.type(:swift_drive_audit_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:openstack_config).provider(:ini_setting) +) do + + def self.file_path + '/etc/swift/drive-audit.conf' + end + +end diff --git a/lib/puppet/type/swift_drive_audit_config.rb b/lib/puppet/type/swift_drive_audit_config.rb new file mode 100644 index 00000000..b988203f --- /dev/null +++ b/lib/puppet/type/swift_drive_audit_config.rb @@ -0,0 +1,53 @@ +Puppet::Type.newtype(:swift_drive_audit_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from /etc/swift/drive-audit.conf' + newvalues(/\S+\/\S+/) + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + newvalues(/^[\S ]*$/) + + def is_to_s( currentvalue ) + if resource.secret? + return '[old secret redacted]' + else + return currentvalue + end + end + + def should_to_s( newvalue ) + if resource.secret? + return '[new secret redacted]' + else + return newvalue + end + end + end + + newparam(:secret, :boolean => true) do + desc 'Whether to hide the value from Puppet logs. Defaults to `false`.' + + newvalues(:true, :false) + + defaultto false + end + + newparam(:ensure_absent_val) do + desc 'A value that is specified as the value property will behave as if ensure => absent was specified' + defaultto('') + end + + autorequire(:anchor) do + ['swift::install::end'] + end + +end diff --git a/manifests/deps.pp b/manifests/deps.pp index 9e546e51..12a827a0 100644 --- a/manifests/deps.pp +++ b/manifests/deps.pp @@ -59,6 +59,12 @@ class swift::deps { -> Swift_container_uwsgi_config<||> ~> Anchor['swift::config::end'] + # drive-audit.conf is not used by swift services, so any change in the file + # should not trigger restarting services. + Anchor['swift::config::begin'] + -> Swift_drive_audit_config<||> + -> Anchor['swift::config::end'] + # Support packages need to be installed in the install phase, but we don't # put them in the chain above because we don't want any false dependencies # between packages with the swift-package tag and the swift-support-package diff --git a/spec/unit/provider/swift_drive_audit_config/ini_setting_spec.rb b/spec/unit/provider/swift_drive_audit_config/ini_setting_spec.rb new file mode 100644 index 00000000..40cc2483 --- /dev/null +++ b/spec/unit/provider/swift_drive_audit_config/ini_setting_spec.rb @@ -0,0 +1,72 @@ +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'openstacklib', + 'lib') +) + +require 'spec_helper' + +provider_class = Puppet::Type.type(:swift_drive_audit_config).provider(:ini_setting) + +describe provider_class do + + it 'should default to the default setting when no other one is specified' do + resource = Puppet::Type::Swift_drive_audit_config.new( + { + :name => 'DEFAULT/foo', + :value => 'bar' + } + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('DEFAULT') + expect(provider.setting).to eq('foo') + end + + it 'should allow setting to be set explicitly' do + resource = Puppet::Type::Swift_drive_audit_config.new( + { + :name => 'dude/foo', + :value => 'bar' + } + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('dude') + expect(provider.setting).to eq('foo') + end + + it 'should ensure absent when is specified as a value' do + resource = Puppet::Type::Swift_drive_audit_config.new( + {:name => 'dude/foo', :value => ''} + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end + + it 'should ensure absent when value matches ensure_absent_val' do + resource = Puppet::Type::Swift_drive_audit_config.new( + {:name => 'dude/foo', :value => 'foo', :ensure_absent_val => 'foo' } + ) + provider = provider_class.new(resource) + provider.exists? + expect(resource[:ensure]).to eq :absent + end + +end diff --git a/spec/unit/type/swift_drive_audit_config_spec.rb b/spec/unit/type/swift_drive_audit_config_spec.rb new file mode 100644 index 00000000..20882c10 --- /dev/null +++ b/spec/unit/type/swift_drive_audit_config_spec.rb @@ -0,0 +1,19 @@ +require 'puppet' +require 'puppet/type/swift_drive_audit_config' + +describe 'Puppet::Type.type(:swift_drive_audit_config)' do + before :each do + @swift_drive_audit_config = Puppet::Type.type(:swift_drive_audit_config).new(:name => 'DEFAULT/foo', :value => 'bar') + end + + it 'should autorequire the package that install the file' do + catalog = Puppet::Resource::Catalog.new + anchor = Puppet::Type.type(:anchor).new(:name => 'swift::install::end') + catalog.add_resource anchor, @swift_drive_audit_config + dependency = @swift_drive_audit_config.autorequire + expect(dependency.size).to eq(1) + expect(dependency[0].target).to eq(@swift_drive_audit_config) + expect(dependency[0].source).to eq(anchor) + end + +end