summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/node/ldap.rb
blob: 5fd738511ee30ba5fe537b6c068802ef935d3e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
require 'puppet/node'
require 'puppet/indirector/ldap'

class Puppet::Node::Ldap < Puppet::Indirector::Ldap
  desc "Search in LDAP for node configuration information.  See
  the [LDAP Nodes](http://projects.puppetlabs.com/projects/puppet/wiki/Ldap_Nodes) page for more information.  This will first
  search for whatever the certificate name is, then (if that name
  contains a `.`) for the short name, then `default`."

  # The attributes that Puppet class information is stored in.
  def class_attributes
    # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com]
    x = Puppet[:ldapclassattrs].split(/\s*,\s*/)
  end

  # 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.
  def name2hash(name)
    info = nil
    ldapsearch(search_filter(name)) { |entry| info = entry2hash(entry) }
    info
  end

  # Look for our node in ldap.
  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

  # 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.
  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

  # The parent attribute, if we have one.
  def parent_attribute
    if pattr = Puppet[:ldapparentattr] and ! pattr.empty?
      pattr
    else
      nil
    end
  end

  # The attributes that Puppet will stack as array over the full
  # hierarchy.
  def stacked_attributes(dummy_argument=:work_arround_for_ruby_GC_bug)
    Puppet[:ldapstackedattrs].split(/\s*,\s*/)
  end

  # Convert the found entry into a simple hash.
  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

  # Default to all attributes.
  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

  # The ldap search filter to use.
  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

  private

  # Add our hash of ldap information to the node instance.
  def add_to_node(node, information)
    node.classes = information[:classes].uniq unless information[:classes].nil? or information[:classes].empty?
    node.parameters = information[:parameters] unless information[:parameters].nil? or information[:parameters].empty?
    node.environment = information[:environment] if information[:environment]
  end

  def convert_parameters(parameters)
    result = {}
    parameters.each do |param, value|
      if value.is_a?(Array)
        result[param] = value.collect { |v| convert(v) }
      else
        result[param] = convert(value)
      end
    end
    result
  end

  # Convert any values if necessary.
  def convert(value)
    case value
    when Integer, Fixnum, Bignum; value
    when "true"; true
    when "false"; false
    else
      value
    end
  end

  # Find information for our parent and merge it into the current info.
  def find_and_merge_parent(parent, information)
    parent_info = name2hash(parent) || raise(Puppet::Error.new("Could not find parent node '#{parent}'"))
    information[:classes] += parent_info[:classes]
    parent_info[:parameters].each do |param, value|
      # Specifically test for whether it's set, so false values are handled correctly.
      information[:parameters][param] = value unless information[:parameters].include?(param)
    end
    information[:environment] ||= parent_info[:environment]
    parent_info[:parent]
  end

  # Take a name and a hash, and return a node instance.
  def info2node(name, info)
    merge_parent(info) if info[:parent]

    node = Puppet::Node.new(name)

    add_to_node(node, info)

    node.fact_merge

    node
  end

  def merge_parent(info)
    parent_info = nil
    parent = info[:parent]

    # Preload the parent array with the node name.
    parents = [info[:name]]
    while parent
      raise ArgumentError, "Found loop in LDAP node parents; #{parent} appears twice" if parents.include?(parent)
      parents << parent
      parent = find_and_merge_parent(parent, info)
    end

    info
  end

  def get_classes_from_entry(entry)
    result = class_attributes.inject([]) do |array, attr|
      if values = entry.vals(attr)
        values.each do |v| array << v end
      end
      array
    end
    result.uniq
  end

  def get_parameters_from_entry(entry)
    stacked_params = stacked_attributes
    entry.to_hash.inject({}) do |hash, ary|
      unless stacked_params.include?(ary[0]) # don't add our stacked parameters to the main param list
        if ary[1].length == 1
          hash[ary[0]] = ary[1].shift
        else
          hash[ary[0]] = ary[1]
        end
      end
      hash
    end
  end

  def get_parent_from_entry(entry)
    pattr = parent_attribute

    return nil unless values = entry.vals(pattr)

    if values.length > 1
      raise Puppet::Error,
        "Node entry #{entry.dn} specifies more than one parent: #{values.inspect}"
    end
    return(values.empty? ? nil : values.shift)
  end

  def get_stacked_values_from_entry(entry)
    stacked_attributes.inject([]) do |result, attr|
      if values = entry.vals(attr)
        result += values
      end
      result
    end
  end
end