Class: Puppet::Util::Ldap::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/ldap/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, user: nil, password: nil, reset: nil, ssl: nil) ⇒ Connection

Returns a new instance of Connection.

Raises:



38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet/util/ldap/connection.rb', line 38

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/puppet/util/ldap/connection.rb', line 8

def connection
  @connection
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def password
  @password
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def port
  @port
end

#resetObject

Returns the value of attribute reset.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def reset
  @reset
end

#sslObject

Returns the value of attribute ssl.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def ssl
  @ssl
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/puppet/util/ldap/connection.rb', line 6

def user
  @user
end

Class Method Details

.instanceObject

Return a default connection, using our default settings.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/util/ldap/connection.rb', line 11

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

Instance Method Details

#closeObject



34
35
36
# File 'lib/puppet/util/ldap/connection.rb', line 34

def close
  connection.unbind if connection.bound?
end

#nameObject

Create a per-connection unique name.



50
51
52
# File 'lib/puppet/util/ldap/connection.rb', line 50

def name
  [host, port, user, password, ssl].collect(&:to_s).join("/")
end

#reset?Boolean

Should we reset the connection?

Returns:

  • (Boolean)


55
56
57
# File 'lib/puppet/util/ldap/connection.rb', line 55

def reset?
  reset
end

#startObject

Start our ldap connection.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/util/ldap/connection.rb', line 60

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