Class: Telemetry::Snmp::Auth

Inherits:
Object
  • Object
show all
Includes:
AuthDefaults
Defined in:
lib/telemetry/snmp/auth.rb

Instance Method Summary collapse

Methods included from AuthDefaults

#admin_group, #attrs, #filter, #ldap_host, #ldap_port, #opts, #provider, #treebase, #users_group

Constructor Details

#initialize(username:, **opts) ⇒ Auth

Returns a new instance of Auth.



9
10
11
12
13
# File 'lib/telemetry/snmp/auth.rb', line 9

def initialize(username:, **opts)
  @username = username
  @details = {}
  @opts = opts
end

Instance Method Details

#auth_with_service(password) ⇒ Object



36
37
38
39
40
41
# File 'lib/telemetry/snmp/auth.rb', line 36

def auth_with_service(password)
  options = defaults
  options[:auth] = defaults_auth
  result = provider.new(options).bind_as(base: defaults[:base], attributes: attrs, filter: filter, password: password) # rubocop:disable Layout/LineLength
  process_result(result.first)
end

#auth_without_service(password) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/telemetry/snmp/auth.rb', line 43

def auth_without_service(password)
  options = { host: defaults[:host], port: defaults[:port] }
  options[:auth] = { password: password, username: @username, method: :simple }
  ldap = provider.new(options)
  @success = ldap.bind
  return unless @success

  process_result(search_user(ldap, @username))
end

#process_result(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/telemetry/snmp/auth.rb', line 15

def process_result(result)
  unless result.is_a? Net::LDAP::Entry
    @success = false
    return
  end
  @details[:username] = result.sAMAccountName.first
  @details[:email] = result.mail.first
  @details[:first] = result.givenName.first
  @details[:last] = result.sn.first
  @success = true
end

#search_user(ldap, username) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/telemetry/snmp/auth.rb', line 27

def search_user(ldap, username)
  user_filter = Net::LDAP::Filter.eq('sAMAccountName', username)

  ldap.search(base: treebase, filter: user_filter, attrs: attrs, return_result: false) do |entry|
    @details[:group_access] = entry.memberof.include?("CN=#{admin_group}, #{treebase}")
    return entry
  end
end