Class: Infrataster::Contexts::LdapContext

Inherits:
BaseContext
  • Object
show all
Defined in:
lib/infrataster/contexts/ldap_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LdapContext

Returns a new instance of LdapContext.



8
9
10
11
12
13
14
# File 'lib/infrataster/contexts/ldap_context.rb', line 8

def initialize(*args)
  @test_attr = {
    :cn => "test",
    :objectclass => [ "top", "structural" ]
  }
  super(*args)
end

Instance Method Details

#add_result(dn = "cn=test,#{resource.basedn}", attr = @test_attr) ⇒ Object



22
23
24
25
26
27
# File 'lib/infrataster/contexts/ldap_context.rb', line 22

def add_result (dn = "cn=test,#{resource.basedn}", attr = @test_attr)
  conn = connect(server)
  conn.bind
  conn.add ({ :dn => dn, :attributes => attr })
  return conn.get_operation_result
end

#bindObject



16
17
18
19
20
# File 'lib/infrataster/contexts/ldap_context.rb', line 16

def bind
  conn = connect(server)
  conn.bind
  return conn.get_operation_result
end

#connect(server) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/infrataster/contexts/ldap_context.rb', line 39

def connect (server)
  # Default options
  options = { port: 389,
              username: '',
              password: '',
              basedn: 'dc=example,dc=com'
  }
  options = options.merge(server.options[:ldap]) if server.options[:ldap]

  server.forward_port(options[:port]) do |address, new_port|
    conn = Net::LDAP.new(
      :host => server.address,
      :port => new_port,
      :auth => {
        :method => :simple,
        :username => options[:username],
        :password => options[:password]
      }
    )
    return conn
  end
end

#search_result(filter) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/infrataster/contexts/ldap_context.rb', line 29

def search_result (filter)
  conn = connect(server)
  conn.bind
  r = conn.search(
    :filter => filter,
    :base => resource.basedn
   )
   conn.get_operation_result
end