require 'infrataster' require 'net-ldap' module Infrataster module Contexts class LdapContext < BaseContext def initialize(*args) @test_attr = { :cn => "test", :objectclass => [ "top", "structural" ] } super(*args) end def bind conn = connect(server) conn.bind return conn.get_operation_result end 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 def search_result (filter) conn = connect(server) conn.bind r = conn.search( :filter => filter, :base => resource.basedn ) conn.get_operation_result end 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 end end end