forked from Pryz/terraform-provider-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ldap_object.go
478 lines (424 loc) · 14.9 KB
/
resource_ldap_object.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package main
import (
"bytes"
"log"
"fmt"
"strings"
"os"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"gopkg.in/ldap.v2"
)
func resourceLDAPObject() *schema.Resource {
return &schema.Resource{
Create: resourceLDAPObjectCreate,
Read: resourceLDAPObjectRead,
Update: resourceLDAPObjectUpdate,
Delete: resourceLDAPObjectDelete,
Exists: resourceLDAPObjectExists,
Importer: &schema.ResourceImporter{
State: resourceLDAPObjectImport,
},
Schema: map[string]*schema.Schema{
"dn": &schema.Schema{
Type: schema.TypeString,
Description: "The Distinguished Name (DN) of the object, as the concatenation of its RDN (unique among siblings) and its parent's DN.",
Required: true,
ForceNew: true,
},
"object_classes": &schema.Schema{
Type: schema.TypeSet,
Description: "The set of classes this object conforms to (e.g. organizationalUnit, inetOrgPerson).",
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Required: true,
},
"attributes": &schema.Schema{
Type: schema.TypeSet,
Description: "The map of attributes of this object; each attribute can be multi-valued.",
Set: attributeHash,
MinItems: 0,
Elem: &schema.Schema{
Type: schema.TypeMap,
Description: "The list of values for a given attribute.",
MinItems: 1,
MaxItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "The individual value for the given attribute.",
},
},
Optional: true,
},
},
}
}
func resourceLDAPObjectImport(d *schema.ResourceData, meta interface{}) (imported []*schema.ResourceData, err error) {
d.Set("dn", d.Id())
err = readLDAPObjectImpl(d, meta, false)
if path := os.Getenv("TF_LDAP_IMPORTER_PATH"); path != "" {
log.Printf("[DEBUG] ldap_object::import - dumping imported object to %q", path)
if _, err := os.Stat(path); os.IsNotExist(err) {
// the export file does not exist
if file, err := os.Create(path); err == nil {
defer file.Close()
id := d.Id()
tokens := strings.Split(id, ",")
if len(tokens) > 0 {
tokens = strings.Split(tokens[0], "=")
if len(tokens) >= 1 {
id = tokens[1]
//resource "ldap_object" "a123456" {
file.WriteString(fmt.Sprintf("resource \"ldap_object\" %q {\n", id))
// dn = "uid=a123456,dc=example,dc=com"
file.WriteString(fmt.Sprintf(" dn = %q\n", d.Id()))
// object_classes = ["inetOrgPerson", "posixAccount"]
classes := []string{}
for _, class := range d.Get("object_classes").(*schema.Set).List() {
//classes[i] = fmt.Sprintf("\"%s\"", class)
classes = append(classes, fmt.Sprintf("%q", class))
}
file.WriteString(fmt.Sprintf(" object_classes = [ %s ]\n", strings.Join(classes, ", ")))
if attributes, ok := d.GetOk("attributes"); ok {
attributes := attributes.(*schema.Set).List()
if len(attributes) > 0 {
// attributes = [
file.WriteString(" attributes = [\n")
for _, attribute := range attributes {
for name, value := range attribute.(map[string]interface{}) {
// { sn = "Doe" },
file.WriteString(fmt.Sprintf(" { %s = %q },\n", name, value.(string)))
}
}
// ]
file.WriteString(" ]\n")
}
}
file.WriteString("}\n")
}
}
}
}
}
imported = append(imported, d)
return
}
func resourceLDAPObjectExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
conn := meta.(*ldap.Conn)
dn := d.Get("dn").(string)
log.Printf("[DEBUG] ldap_object::exists - checking if %q exists", dn)
// search by primary key (that is, set the DN as base DN and use a "base
// object" scope); no attributes are retrieved since we are onòy checking
// for existence; all objects have an "objectClass" attribute, so the filter
// is a "match all"
request := ldap.NewSearchRequest(
dn,
ldap.ScopeBaseObject,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectClass=*)",
nil,
nil,
)
_, err := conn.Search(request)
if err != nil {
if err, ok := err.(*ldap.Error); ok {
if err.ResultCode == 32 { // no such object
log.Printf("[WARN] ldap_object::exists - lookup for %q returned no value: deleted on server?", dn)
return false, nil
}
}
log.Printf("[DEBUG] ldap_object::exists - lookup for %q returned an error %v", dn, err)
return false, err
}
log.Printf("[DEBUG] ldap_object::exists - object %q exists", dn)
return true, nil
}
func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
dn := d.Get("dn").(string)
log.Printf("[DEBUG] ldap_object::create - creating a new object under %q", dn)
request := ldap.NewAddRequest(dn)
// retrieve classe from HCL
objectClasses := []string{}
for _, oc := range (d.Get("object_classes").(*schema.Set)).List() {
log.Printf("[DEBUG] ldap_object::create - object %q has class: %q", dn, oc.(string))
objectClasses = append(objectClasses, oc.(string))
}
request.Attribute("objectClass", objectClasses)
// if there is a non empty list of attributes, loop though it and
// create a new map collecting attribute names and its value(s); we need to
// do this because we could not model the attributes as a map[string][]string
// due to an appareent limitation in HCL; we have a []map[string]string, so
// we loop through the list and accumulate values when they share the same
// key, then we use these as attributes in the LDAP client.
if v, ok := d.GetOk("attributes"); ok {
attributes := v.(*schema.Set).List()
if len(attributes) > 0 {
log.Printf("[DEBUG] ldap_object::create - object %q has %d attributes", dn, len(attributes))
m := make(map[string][]string)
for _, attribute := range attributes {
log.Printf("[DEBUG] ldap_object::create - %q has attribute of type %T", dn, attribute)
// each map should only have one entry (see resource declaration)
for name, value := range attribute.(map[string]interface{}) {
log.Printf("[DEBUG] ldap_object::create - %q has attribute[%v] => %v (%T)", dn, name, value, value)
m[name] = append(m[name], value.(string))
}
}
// now loop through the map and add attributes with theys value(s)
for name, values := range m {
request.Attribute(name, values)
}
}
}
err := client.Add(request)
if err != nil {
return err
}
log.Printf("[DEBUG] ldap_object::create - object %q added to LDAP server", dn)
d.SetId(dn)
return resourceLDAPObjectRead(d, meta)
}
func resourceLDAPObjectRead(d *schema.ResourceData, meta interface{}) error {
return readLDAPObjectImpl(d, meta, true)
}
func resourceLDAPObjectUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
log.Printf("[DEBUG] ldap_object::update - performing update on %q", d.Id())
request := ldap.NewModifyRequest(d.Id())
// handle objectClasses
if d.HasChange("object_classes") {
classes := []string{}
for _, oc := range (d.Get("object_classes").(*schema.Set)).List() {
classes = append(classes, oc.(string))
}
log.Printf("[DEBUG] ldap_object::update - updating classes of %q, new value: %v", d.Id(), classes)
request.ReplaceAttributes = []ldap.PartialAttribute{
ldap.PartialAttribute{
Type: "objectClass",
Vals: classes,
},
}
}
if d.HasChange("attributes") {
o, n := d.GetChange("attributes")
log.Printf("[DEBUG] ldap_object::update - \n%s", printAttributes("old attributes map", o))
log.Printf("[DEBUG] ldap_object::update - \n%s", printAttributes("new attributes map", n))
added, changed, removed := computeDeltas(o.(*schema.Set), n.(*schema.Set))
if len(added) > 0 {
log.Printf("[DEBUG] ldap_object::update - %d attributes added", len(added))
request.AddAttributes = added
}
if len(changed) > 0 {
log.Printf("[DEBUG] ldap_object::update - %d attributes changed", len(changed))
if request.ReplaceAttributes == nil {
request.ReplaceAttributes = changed
} else {
request.ReplaceAttributes = append(request.ReplaceAttributes, changed...)
}
}
if len(removed) > 0 {
log.Printf("[DEBUG] ldap_object::update - %d attributes removed", len(removed))
request.DeleteAttributes = removed
}
}
err := client.Modify(request)
if err != nil {
log.Printf("[ERROR] ldap_object::update - error modifying LDAP object %q with values %v", d.Id(), err)
return err
}
return resourceLDAPObjectRead(d, meta)
}
func resourceLDAPObjectDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ldap.Conn)
dn := d.Get("dn").(string)
log.Printf("[DEBUG] ldap_object::delete - removing %q", dn)
request := ldap.NewDelRequest(dn, nil)
err := client.Del(request)
if err != nil {
log.Printf("[ERROR] ldap_object::delete - error removing %q: %v", dn, err)
return err
}
log.Printf("[DEBUG] ldap_object::delete - %q removed", dn)
return nil
}
func readLDAPObjectImpl(d *schema.ResourceData, meta interface{}, updateState bool) error {
client := meta.(*ldap.Conn)
dn := d.Get("dn").(string)
log.Printf("[DEBUG] ldap_object::read - looking for object %q", dn)
// when searching by DN, you don't need t specify the base DN a search
// filter a "subtree" scope: just put the DN (i.e. the primary key) as the
// base DN with a "base object" scope, and the returned object will be the
// entry, if it exists
request := ldap.NewSearchRequest(
dn,
ldap.ScopeBaseObject,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectclass=*)",
[]string{"*"},
nil,
)
sr, err := client.Search(request)
if err != nil {
if err, ok := err.(*ldap.Error); ok {
if err.ResultCode == 32 && updateState { // no such object
log.Printf("[WARN] ldap_object::read - object not found, removing %q from state because it no longer exists in LDAP", dn)
d.SetId("")
return nil
}
}
log.Printf("[DEBUG] ldap_object::read - lookup for %q returned an error %v", dn, err)
return err
}
log.Printf("[DEBUG] ldap_object::read - query for %q returned %v", dn, sr)
d.SetId(dn)
d.Set("object_classes", sr.Entries[0].GetAttributeValues("objectClass"))
// now deal with attributes
set := &schema.Set{
F: attributeHash,
}
for _, attribute := range sr.Entries[0].Attributes {
log.Printf("[DEBUG] ldap_object::read - treating attribute %q of %q (%d values: %v)", attribute.Name, dn, len(attribute.Values), attribute.Values)
if attribute.Name == "objectClass" {
// skip: we don't treat object classes as ordinary attributes
log.Printf("[DEBUG] ldap_object::read - skipping attribute %q of %q", attribute.Name, dn)
continue
}
if len(attribute.Values) == 1 {
// we don't treat the RDN as an ordinary attribute
a := fmt.Sprintf("%s=%s", attribute.Name, attribute.Values[0])
if strings.HasPrefix(dn, a) {
log.Printf("[DEBUG] ldap_object::read - skipping RDN %q of %q", a, dn)
continue
}
}
log.Printf("[DEBUG] ldap_object::read - adding attribute %q to %q (%d values)", attribute.Name, dn, len(attribute.Values))
// now add each value as an individual entry into the object, because
// we do not handle name => []values, and we have a set of maps each
// holding a single entry name => value; multiple maps may share the
// same key.
for _, value := range attribute.Values {
log.Printf("[DEBUG] ldap_object::read - for %q, setting %q => %q", dn, attribute.Name, value)
set.Add(map[string]interface{}{
attribute.Name: value,
})
}
}
if err := d.Set("attributes", set); err != nil {
log.Printf("[WARN] ldap_object::read - error setting LDAP attributes for %q : %v", dn, err)
return err
}
return nil
}
// computes the hash of the map representing an attribute in the attributes set
func attributeHash(v interface{}) int {
m := v.(map[string]interface{})
var buffer bytes.Buffer
buffer.WriteString("map {")
for k, v := range m {
buffer.WriteString(fmt.Sprintf("%q := %q;", k, v.(string)))
}
buffer.WriteRune('}')
text := buffer.String()
hash := hashcode.String(text)
return hash
}
func printAttributes(prefix string, attributes interface{}) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s: {\n", prefix))
if attributes, ok := attributes.(*schema.Set); ok {
for _, attribute := range attributes.List() {
for k, v := range attribute.(map[string]interface{}) {
buffer.WriteString(fmt.Sprintf(" %q: %q\n", k, v.(string)))
}
}
buffer.WriteRune('}')
}
return buffer.String()
}
func computeDeltas(os, ns *schema.Set) (added, changed, removed []ldap.PartialAttribute) {
rk := NewSet() // names of removed attributes
for _, v := range os.Difference(ns).List() {
for k := range v.(map[string]interface{}) {
rk.Add(k)
}
}
ak := NewSet() // names of added attributes
for _, v := range ns.Difference(os).List() {
for k := range v.(map[string]interface{}) {
ak.Add(k)
}
}
kk := NewSet() // names of kept attributes
for _, v := range ns.Intersection(os).List() {
for k := range v.(map[string]interface{}) {
kk.Add(k)
}
}
ck := NewSet() // names of changed attributes
// loop over remove attributes' names
for _, k := range rk.List() {
if !ak.Contains(k) && !kk.Contains(k) {
// one value under this name has been removed, no other value has
// been added back, and there is no further value under the same
// name among those that were untouched; this means that it has
// been dropped and must go among the RemovedAttributes
log.Printf("[DEBUG} ldap_object::deltas - dropping attribute %q", k)
removed = append(removed, ldap.PartialAttribute{
Type: k,
Vals: []string{},
})
} else {
ck.Add(k)
}
}
for _, k := range ak.List() {
if !rk.Contains(k) && !kk.Contains(k) {
// this is the first value under this name: no value is being
// removed and no value is being kept; so we're adding this new
// attribute to the LDAP object (AddedAttributes), getting all
// the values under this name from the new set
values := []string{}
for _, m := range ns.List() {
for mk, mv := range m.(map[string]interface{}) {
if k == mk {
values = append(values, mv.(string))
}
}
}
added = append(added, ldap.PartialAttribute{
Type: k,
Vals: values,
})
log.Printf("[DEBUG} ldap_object::deltas - adding new attribute %q with values %v", k, values)
} else {
ck.Add(k)
}
}
// now loop over changed attributes and
for _, k := range ck.List() {
// the attributes in this set have been changed, in that a new value has
// been added or removed and it was not the last/first one; so we're
// adding this new attribute to the LDAP object (ModifiedAttributes),
// getting all the values under this name from the new set
values := []string{}
for _, m := range ns.List() {
for mk, mv := range m.(map[string]interface{}) {
if k == mk {
values = append(values, mv.(string))
}
}
}
changed = append(added, ldap.PartialAttribute{
Type: k,
Vals: values,
})
log.Printf("[DEBUG} ldap_object::deltas - changing attribute %q with values %v", k, values)
}
return
}