Class: Puppet::Node::Ldap

Inherits:
Indirector::Ldap show all
Defined in:
lib/vendor/puppet/indirector/node/ldap.rb

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Indirector::Ldap

#connection, #ldapsearch, #process, #search_base

Methods inherited from Indirector::Terminus

abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type

Methods included from Util::InstanceLoader

#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Instance Method Details

#class_attributesObject

The attributes that Puppet class information is stored in.



11
12
13
14
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 11

def class_attributes
  # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com]
  x = Puppet[:ldapclassattrs].split(/\s*,\s*/)
end

#entry2hash(entry, fqdn = false) ⇒ Object

Convert the found entry into a simple hash.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 79

def entry2hash(entry, fqdn = false)
  result = {}

  cn  = entry.dn[     /cn\s*=\s*([^,\s]+)/i,1]
  dcs = entry.dn.scan(/dc\s*=\s*([^,\s]+)/i)
  result[:name]    = fqdn ? ([cn]+dcs).join('.') : cn
  result[:parent] = get_parent_from_entry(entry) if parent_attribute
  result[:classes] = get_classes_from_entry(entry)
  result[:stacked] = get_stacked_values_from_entry(entry)
  result[:parameters] = get_parameters_from_entry(entry)

  result[:environment] = result[:parameters]["environment"] if result[:parameters]["environment"]

  result[:stacked_parameters] = {}

  if result[:stacked]
    result[:stacked].each do |value|
      param = value.split('=', 2)
      result[:stacked_parameters][param[0]] = param[1]
    end
  end

  if result[:stacked_parameters]
    result[:stacked_parameters].each do |param, value|
      result[:parameters][param] = value unless result[:parameters].include?(param)
    end
  end

  result[:parameters] = convert_parameters(result[:parameters])

  result
end

#find(request) ⇒ Object

Look for our node in ldap.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 29

def find(request)
  names = [request.key]
  names << request.key.sub(/\..+/, '') if request.key.include?(".") # we assume it's an fqdn
  names << "default"

  node = nil
  names.each do |name|
    next unless info = name2hash(name)

    break if node = info2node(request.key, info)
  end

  node
end

#name2hash(name) ⇒ Object

Separate this out so it’s relatively atomic. It’s tempting to call process instead of name2hash() here, but it ends up being difficult to test because all exceptions get caught by ldapsearch. LAK:NOTE Unfortunately, the ldap support is too stupid to throw anything but LDAP::ResultError, even on bad connections, so we are rough handed with our error handling.



22
23
24
25
26
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 22

def name2hash(name)
  info = nil
  ldapsearch(search_filter(name)) { |entry| info = entry2hash(entry) }
  info
end

#parent_attributeObject

The parent attribute, if we have one.



64
65
66
67
68
69
70
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 64

def parent_attribute
  if pattr = Puppet[:ldapparentattr] and ! pattr.empty?
    pattr
  else
    nil
  end
end

#search(request) ⇒ Object

Find more than one node. LAK:NOTE This is a bit of a clumsy API, because the ‘search’ method currently requires a key. It seems appropriate in some cases but not others, and I don’t really know how to get rid of it as a requirement but allow it when desired.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 47

def search(request)
  if classes = request.options[:class]
    classes = [classes] unless classes.is_a?(Array)
    filter = "(&(objectclass=puppetClient)(puppetclass=" + classes.join(")(puppetclass=") + "))"
  else
    filter = "(objectclass=puppetClient)"
  end

  infos = []
  ldapsearch(filter) { |entry| infos << entry2hash(entry, request.options[:fqdn]) }

  return infos.collect do |info|
    info2node(info[:name], info)
  end
end

#search_attributesObject

Default to all attributes.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 113

def search_attributes
  ldapattrs = Puppet[:ldapattrs]

  # results in everything getting returned
  return nil if ldapattrs == "all"

  search_attrs = class_attributes + ldapattrs.split(/\s*,\s*/)

  if pattr = parent_attribute
    search_attrs << pattr
  end

  search_attrs
end

#search_filter(name) ⇒ Object

The ldap search filter to use.



129
130
131
132
133
134
135
136
137
138
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 129

def search_filter(name)
  filter = Puppet[:ldapstring]

  if filter.include? "%s"
    # Don't replace the string in-line, since that would hard-code our node
    # info.
    filter = filter.gsub('%s', name)
  end
  filter
end

#stacked_attributes(dummy_argument = :work_arround_for_ruby_GC_bug) ⇒ Object

The attributes that Puppet will stack as array over the full hierarchy.



74
75
76
# File 'lib/vendor/puppet/indirector/node/ldap.rb', line 74

def stacked_attributes(dummy_argument=:work_arround_for_ruby_GC_bug)
  Puppet[:ldapstackedattrs].split(/\s*,\s*/)
end