Sha256: 7177124194df669137d3f0008625e6412f43b2854f8eb8335f7a17c2a871b6b2

Contents?: true

Size: 1.86 KB

Versions: 20

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true
require_relative '../../../puppet/util/ldap'

class Puppet::Util::Ldap::Connection
  attr_accessor :host, :port, :user, :password, :reset, :ssl

  attr_reader :connection

  # Return a default connection, using our default settings.
  def self.instance
    ssl = if Puppet[:ldaptls]
      :tls
        elsif Puppet[:ldapssl]
          true
        else
          false
        end

    options = {}
    options[:ssl] = ssl
    user = Puppet.settings[:ldapuser]
    if user && user != ""
      options[:user] = user
      pass = Puppet.settings[:ldappassword]
      if pass && pass != ""
        options[:password] = pass
      end
    end

    new(Puppet[:ldapserver], Puppet[:ldapport], options)
  end

  def close
    connection.unbind if connection.bound?
  end

  def initialize(host, port, user: nil, password: nil, reset: nil, ssl: nil)
    raise Puppet::Error, _("Could not set up LDAP Connection: Missing ruby/ldap libraries") unless Puppet.features.ldap?

    @host = host
    @port = port
    @user = user
    @password = password
    @reset = reset
    @ssl = ssl
  end

  # Create a per-connection unique name.
  def name
    [host, port, user, password, ssl].collect { |p| p.to_s }.join("/")
  end

  # Should we reset the connection?
  def reset?
    reset
  end

  # Start our ldap connection.
  def start
      case ssl
      when :tls
        @connection = LDAP::SSLConn.new(host, port, true)
      when true
        @connection = LDAP::SSLConn.new(host, port)
      else
        @connection = LDAP::Conn.new(host, port)
      end
      @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
      @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON)
      @connection.simple_bind(user, password)
  rescue => detail
      raise Puppet::Error, _("Could not connect to LDAP: %{detail}") % { detail: detail }, detail.backtrace
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-8.3.0 lib/puppet/util/ldap/connection.rb
puppet-8.3.0-x86-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.3.0-x64-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.3.0-universal-darwin lib/puppet/util/ldap/connection.rb
puppet-8.3.1 lib/puppet/util/ldap/connection.rb
puppet-8.3.1-x86-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.3.1-x64-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.3.1-universal-darwin lib/puppet/util/ldap/connection.rb
puppet-8.2.0 lib/puppet/util/ldap/connection.rb
puppet-8.2.0-x86-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.2.0-x64-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.2.0-universal-darwin lib/puppet/util/ldap/connection.rb
puppet-8.1.0 lib/puppet/util/ldap/connection.rb
puppet-8.1.0-x86-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.1.0-x64-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.1.0-universal-darwin lib/puppet/util/ldap/connection.rb
puppet-8.0.1 lib/puppet/util/ldap/connection.rb
puppet-8.0.1-x86-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.0.1-x64-mingw32 lib/puppet/util/ldap/connection.rb
puppet-8.0.1-universal-darwin lib/puppet/util/ldap/connection.rb