From 89e4054db8b4e8e90da1c1183dcb9416960c0999 Mon Sep 17 00:00:00 2001 From: Tobias Wolter Date: Thu, 25 Aug 2022 16:54:07 +0200 Subject: [PATCH] Fix readonly handling The database `readonly` property expects a boolean, but the olc provider doesn't take care to parse the existing value into a boolean, thus leading to issues. Simply applies the same logic applied to `olcMirrorMode` for `olcReadOnly`. --- REFERENCE.md | 4 ++ lib/puppet/provider/openldap_database/olc.rb | 2 +- lib/puppet/type/openldap_database.rb | 2 + .../provider/openldap_database/olc_spec.rb | 55 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 spec/unit/puppet/provider/openldap_database/olc_spec.rb diff --git a/REFERENCE.md b/REFERENCE.md index 28d2e9d5..552cc26d 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -1386,8 +1386,12 @@ This option puts a replica database into "multiprovider" mode ##### `readonly` +Valid values: `true`, `false` + Puts the database into read-only mode. +Default value: `false` + ##### `rootdn` The distinguished name that is not subject to access control or administrative limit restrictions for operations on this database. diff --git a/lib/puppet/provider/openldap_database/olc.rb b/lib/puppet/provider/openldap_database/olc.rb index de46b562..46198c61 100644 --- a/lib/puppet/provider/openldap_database/olc.rb +++ b/lib/puppet/provider/openldap_database/olc.rb @@ -50,7 +50,7 @@ def self.instances when %r{^olcRelay: } relay = line.split[1] when %r{^olcReadOnly: }i - readonly = line.split[1] + readonly = line.split[1] == 'TRUE' ? :true : :false when %r{^olcSizeLimit: }i sizelimit = line.split[1] when %r{^olcDbMaxSize: }i diff --git a/lib/puppet/type/openldap_database.rb b/lib/puppet/type/openldap_database.rb index e55bc010..2ae94f9a 100644 --- a/lib/puppet/type/openldap_database.rb +++ b/lib/puppet/type/openldap_database.rb @@ -167,6 +167,8 @@ def should_to_s(_newvalue) newproperty(:readonly) do desc 'Puts the database into read-only mode.' + newvalues(:true, :false) + defaultto(:false) end newproperty(:sizelimit) do diff --git a/spec/unit/puppet/provider/openldap_database/olc_spec.rb b/spec/unit/puppet/provider/openldap_database/olc_spec.rb new file mode 100644 index 00000000..c1468c87 --- /dev/null +++ b/spec/unit/puppet/provider/openldap_database/olc_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Puppet::Type.type(:openldap_database).provider(:olc) do + let(:params) do + { + suffix: 'dc=example,dc=com', + backend: 'mdb', + readonly: false, + # provider: described_class.name, + } + end + + let(:resource) do + Puppet::Type.type(:openldap_database).new(params) + end + let(:provider) do + resource.provider + end + + before do + allow(described_class).to receive(:slapcat).with('(|(olcDatabase=monitor)(olcDatabase={0}config)(&(objectClass=olcDatabaseConfig)(|(objectClass=olcBdbConfig)(objectClass=olcHdbConfig)(objectClass=olcMdbConfig)(objectClass=olcMonitorConfig)(objectClass=olcRelayConfig)(objectClass=olcLDAPConfig))))').and_return(<<~SLAPCAT) + dn: olcDatabase={1}mdb,cn=config + olcDatabase: {1}mdb + olcReadOnly: FALSE + SLAPCAT + allow(provider).to receive(:slapcat) + allow(provider).to receive(:ldapmodify) + allow(provider).to receive(:ldapadd) + # allow(described_class).to receive(:slapcat) + # allow(described_class).to receive(:ldapmodify) + # allow(described_class).to receive(:ldapadd) + end + + describe 'when creating' do + context 'with readonly set to false' do + it 'parses olcReadOnly as false' do + provider.create + expect(described_class.instances.first.readonly).to eq :false + # expect(described_class.instances.first.readonly).to eq(:false) + end + end + + context 'with readonly set to true' do + let(:params) do + super().merge({ readonly: true }) + end + + it 'parses olcReadonly' do + expect(described_class.instances.first.readonly).to eq(:true) + end + end + end +end