rubocop: autofix

This commit is contained in:
Tim Meusel 2022-03-11 23:21:17 +01:00
parent f73013318b
commit d3d643b1b2
No known key found for this signature in database
GPG Key ID: 04D659E6BF1C4CC0
3 changed files with 20 additions and 17 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Provides a query interface to an LDAP server # Provides a query interface to an LDAP server
# #
# @example simple query # @example simple query
@ -10,16 +12,13 @@ require_relative '../../../puppet_x/ldapquery'
begin begin
require 'net/ldap' require 'net/ldap'
rescue rescue StandardError
Puppet.warn('Missing net/ldap gem required for ldapquery() function') Puppet.warn('Missing net/ldap gem required for ldapquery() function')
end end
Puppet::Parser::Functions.newfunction(:ldapquery, Puppet::Parser::Functions.newfunction(:ldapquery,
type: :rvalue) do |args| type: :rvalue) do |args|
raise Puppet::ParseError, 'Too many arguments received in ldapquery()' if args.size > 3
if args.size > 3
raise Puppet::ParseError, 'Too many arguments received in ldapquery()'
end
filter, attributes, opts = args filter, attributes, opts = args

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Class: PuppetX::LDAPquery # Class: PuppetX::LDAPquery
# #
@ -19,11 +21,12 @@ module PuppetX
return unless scope return unless scope
if scope == 'sub' case scope
when 'sub'
@scope = Net::LDAP::SearchScope_WholeSubtree @scope = Net::LDAP::SearchScope_WholeSubtree
elsif scope == 'base' when 'base'
@scope = Net::LDAP::SearchScope_BaseObject @scope = Net::LDAP::SearchScope_BaseObject
elsif scope == 'single' when 'single'
@scope = Net::LDAP::SearchScope_SingleLevel @scope = Net::LDAP::SearchScope_SingleLevel
else else
raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]' raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]'
@ -32,15 +35,13 @@ module PuppetX
def ldap_config def ldap_config
# Load the configuration variables from Puppet # Load the configuration variables from Puppet
required_vars = [ required_vars = %i[
:ldapserver, ldapserver
:ldapport ldapport
] ]
required_vars.each do |r| required_vars.each do |r|
unless Puppet[r] raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf" unless Puppet[r]
raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf"
end
end end
port = Puppet[:ldapport] port = Puppet[:ldapport]
@ -112,11 +113,11 @@ module PuppetX
time_delta = format('%.3f', end_time - start_time) 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") 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 rescue Net::LDAP::LdapError => e
Puppet.debug("There was an error searching LDAP #{e.message}") Puppet.debug("There was an error searching LDAP #{e.message}")
Puppet.debug('Returning false') Puppet.debug('Returning false')
return false false
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
require 'puppet_x/ldapquery' require 'puppet_x/ldapquery'
require 'net/ldap' require 'net/ldap'
@ -20,7 +22,7 @@ describe 'PuppetX::LDAPquery' do
it 'fails with no filter' do it 'fails with no filter' do
filter = '' filter = ''
attributes = ['uid'] 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 end
it 'does not fail when using defaults in puppet.conf' do 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) allow(l).to receive(:entries).and_return(entries)
expect(l.results).to eq(wanted) expect(l.results).to eq(wanted)
end end
it 'returns the attributes without new lines' do it 'returns the attributes without new lines' do
filter = '(uid=zach)' filter = '(uid=zach)'
attributes = ['sshPublicKey'] attributes = ['sshPublicKey']