diff --git a/lib/puppet/parser/functions/ldapquery.rb b/lib/puppet/parser/functions/ldapquery.rb index 4ecb2cc..7c4b6f9 100644 --- a/lib/puppet/parser/functions/ldapquery.rb +++ b/lib/puppet/parser/functions/ldapquery.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Provides a query interface to an LDAP server # # @example simple query @@ -10,16 +12,13 @@ require_relative '../../../puppet_x/ldapquery' begin require 'net/ldap' -rescue +rescue StandardError Puppet.warn('Missing net/ldap gem required for ldapquery() function') end Puppet::Parser::Functions.newfunction(:ldapquery, type: :rvalue) do |args| - - if args.size > 3 - raise Puppet::ParseError, 'Too many arguments received in ldapquery()' - end + raise Puppet::ParseError, 'Too many arguments received in ldapquery()' if args.size > 3 filter, attributes, opts = args diff --git a/lib/puppet_x/ldapquery.rb b/lib/puppet_x/ldapquery.rb index c7f6993..41f8056 100644 --- a/lib/puppet_x/ldapquery.rb +++ b/lib/puppet_x/ldapquery.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Class: PuppetX::LDAPquery # @@ -19,11 +21,12 @@ module PuppetX return unless scope - if scope == 'sub' + case scope + when 'sub' @scope = Net::LDAP::SearchScope_WholeSubtree - elsif scope == 'base' + when 'base' @scope = Net::LDAP::SearchScope_BaseObject - elsif scope == 'single' + when 'single' @scope = Net::LDAP::SearchScope_SingleLevel else raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]' @@ -32,15 +35,13 @@ module PuppetX def ldap_config # Load the configuration variables from Puppet - required_vars = [ - :ldapserver, - :ldapport + required_vars = %i[ + ldapserver + ldapport ] required_vars.each do |r| - unless Puppet[r] - raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf" - end + raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf" unless Puppet[r] end port = Puppet[:ldapport] @@ -112,11 +113,11 @@ module PuppetX time_delta = format('%.3f', end_time - start_time) Puppet.debug("ldapquery(): Searching #{@base} for #{@attributes} using #{@filter} took #{time_delta} seconds and returned #{entries.length} results") - return entries + entries rescue Net::LDAP::LdapError => e Puppet.debug("There was an error searching LDAP #{e.message}") Puppet.debug('Returning false') - return false + false end end diff --git a/spec/unit/ldapquery_spec.rb b/spec/unit/ldapquery_spec.rb index 3d00e92..6b66600 100644 --- a/spec/unit/ldapquery_spec.rb +++ b/spec/unit/ldapquery_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'puppet_x/ldapquery' require 'net/ldap' @@ -20,7 +22,7 @@ describe 'PuppetX::LDAPquery' do it 'fails with no filter' do filter = '' attributes = ['uid'] - expect { PuppetX::LDAPquery.new(filter, attributes).results }.to raise_error + expect { PuppetX::LDAPquery.new(filter, attributes).results }.to raise_error # rubocop:disable RSpec/UnspecifiedException end it 'does not fail when using defaults in puppet.conf' do @@ -56,6 +58,7 @@ describe 'PuppetX::LDAPquery' do allow(l).to receive(:entries).and_return(entries) expect(l.results).to eq(wanted) end + it 'returns the attributes without new lines' do filter = '(uid=zach)' attributes = ['sshPublicKey']