summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/node/ldap.rb
blob: 01010a2af202149c6e26c1652ca451eea30c4f00 (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
require 'puppet/node'
require 'puppet/indirector/ldap'

class Puppet::Node::Ldap < Puppet::Indirector::Ldap
    desc "Search in LDAP for node configuration information.  See
    the `LdapNodes`:trac: 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) }
        return info
    end

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

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

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

        return 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) }

        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
        Puppet[:ldapstackedattrs].split(/\s*,\s*/)
    end

    # Convert the found entry into a simple hash.
    def entry2hash(entry)
        result = {}
        result[:name] = entry.dn.split(',')[0].split("=")[1]
        if pattr = parent_attribute
            if values = entry.vals(pattr)
                if values.length > 1
                    raise Puppet::Error,
                        "Node entry %s specifies more than one parent: %s" % [entry.dn, values.inspect]
                end
                unless values.empty?
                    result[:parent] = values.shift
                end
            end
        end

        result[:classes] = []
        class_attributes.each { |attr|
            if values = entry.vals(attr)
                values.each do |v| result[:classes] << v end
            end
        }
        result[:classes].uniq!

        result[:stacked] = []
        stacked_params = stacked_attributes
        stacked_params.each { |attr|
            if values = entry.vals(attr)
                result[:stacked] = result[:stacked] + values
            end
        }
        

        result[:parameters] = 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

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

    # Find information for our parent and merge it into the current info.
    def find_and_merge_parent(parent, information)
        unless parent_info = name2hash(parent)
            raise Puppet::Error.new("Could not find parent node '%s'" % parent)
        end
        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
            if parents.include?(parent)
                raise ArgumentError, "Found loop in LDAP node parents; %s appears twice" % parent
            end
            parents << parent
            parent = find_and_merge_parent(parent, info)
        end

        return info
    end
end