Class: Ping::LDAP

Inherits:
Ping
  • Object
show all
Defined in:
lib/net/ping/ldap.rb

Overview

The Ping::LDAP class encapsulates methods for LDAP pings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, timeout = 5) ⇒ LDAP

Creates and returns a new Ping::LDAP object. The default timeout is 5 seconds.

uri string is expected to be a full URI with scheme (ldap/ldaps) and optionally the port (else default port is assumed) e.g.

ldap://my.ldap.host.com
ldap://my.ldap.host.com:1389
ldaps://my.ldap.host.com
ldaps://my.ldap.host.com:6636

If a plain hostname is provided as the uri, a default port of 389 is assumed



40
41
42
43
# File 'lib/net/ping/ldap.rb', line 40

def initialize(uri = nil, timeout = 5)
  host, port = decode_uri(uri)
  super(host, port, timeout)
end

Instance Attribute Details

#encryptionObject

set/get the encryption method. By default nil, but may be set to :simple_tls



22
23
24
# File 'lib/net/ping/ldap.rb', line 22

def encryption
  @encryption
end

#passwordObject

Returns the value of attribute password.



17
18
19
# File 'lib/net/ping/ldap.rb', line 17

def password
  @password
end

#uriObject

uri contains the URI object for the request



11
12
13
# File 'lib/net/ping/ldap.rb', line 11

def uri
  @uri
end

#usernameObject

username and password may be set for ping using an authenticated LDAP bind



16
17
18
# File 'lib/net/ping/ldap.rb', line 16

def username
  @username
end

Instance Method Details

#configObject

constructs the LDAP configuration structure



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/net/ping/ldap.rb', line 62

def config
  {
    host: uri.host,
    port: uri.port,
    encryption: encryption
  }.merge(
    if username && password
      { auth: { method: :simple, username: username, password: password } }
    else
      { auth: { method: :anonymous } }
    end
  )
end

#decode_uri(value) ⇒ Object

method used to decode uri string



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/net/ping/ldap.rb', line 47

def decode_uri(value)
  @uri = URI.parse(value)
  if uri.scheme =~ /ldap/
    p = @port = uri.port
    h = @host = uri.host
    @encryption = uri.scheme == 'ldaps' ? :simple_tls : nil
  else
    h = value
    p = 389
  end
  [h, p]
end

#ping(host = nil) ⇒ Object Also known as: ping?, pingecho

perform ping, optionally providing the ping destination uri



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/net/ping/ldap.rb', line 78

def ping(host = nil)
  decode_uri(host) if host
  super(@host)

  bool = false

  start_time = Time.now

  begin
    Timeout.timeout(@timeout) do
      Net::LDAP.new(config).bind
    end
  rescue StandardError => e
    @exception = e.message
  else
    bool = true
  end

  # There is no duration if the ping failed
  @duration = Time.now - start_time if bool

  bool
end