Skip to content

Commit fd88d9b

Browse files
Merge pull request #15 from go-ldap/ldap-v3
Porting to LDAP v3
2 parents 3491d58 + fd8c14d commit fd88d9b

11 files changed

+115
-78
lines changed

.travis.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
language: go
22
go:
3-
- 1.6
4-
- 1.7
3+
- 1.11
4+
- 1.13
5+
- 1.14
56
- tip
67
matrix:
78
fast_finish: true
89
allow_failures:
910
- go: tip
1011
go_import_path: github.com/go-ldap/ldif
1112
install:
12-
- go get gopkg.in/asn1-ber.v1
13-
- go get gopkg.in/ldap.v2
13+
- go get github.com/go-ldap/ldap/v3
1414
- go get code.google.com/p/go.tools/cmd/cover || go get golang.org/x/tools/cmd/cover
15-
- go get github.com/golang/lint/golint || true
15+
- go get golang.org/x/lint/golint || true
16+
- go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
1617
- go build -v ./...
1718
script:
1819
- make test

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fmt:
2626

2727
# Only run on go1.5+
2828
vet:
29-
go tool vet -atomic -bool -copylocks -nilfunc -printf -shadow -rangeloops -unreachable -unsafeptr -unusedresult .
29+
go vet -vettool=$(which shadow) -atomic -bool -copylocks -nilfunc -printf -rangeloops -unreachable -unsafeptr -unusedresult .
3030

3131
# https://github.com/golang/lint
3232
# go get github.com/golang/lint/golint

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Utilities for working with ldif data. This implements most of RFC 2849.
55
## Change Entries
66

77
Support for moddn / modrdn changes is missing (in Unmarshal and
8-
Marshal) - gopkg.in/ldap.v2 does not support it currently
8+
Marshal) - github.com/go-ldap/ldap/v3 does not support it currently
99

1010
## Controls
1111

apply.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"log"
66

7-
"gopkg.in/ldap.v2"
7+
"github.com/go-ldap/ldap/v3"
88
)
99

1010
// Apply sends the LDIF entries to the server and does the changes as
@@ -19,7 +19,7 @@ func (l *LDIF) Apply(conn ldap.Client, continueOnErr bool) error {
1919
for _, entry := range l.Entries {
2020
switch {
2121
case entry.Entry != nil:
22-
add := ldap.NewAddRequest(entry.Entry.DN)
22+
add := ldap.NewAddRequest(entry.Entry.DN, entry.Add.Controls)
2323
for _, attr := range entry.Entry.Attributes {
2424
add.Attribute(attr.Name, attr.Values)
2525
}

changes_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func TestLDIFParseRFC2849Example6(t *testing.T) {
8181
if l.Entries[3].Modify == nil {
8282
t.Errorf("last entry not a modify request")
8383
}
84-
if l.Entries[3].Modify.DeleteAttributes[0].Type != "description" {
84+
if l.Entries[3].Modify.Changes[1].Modification.Type != "description" {
8585
t.Errorf("RFC 2849 example 6: no deletion of description in last entry")
8686
}
87-
if l.Entries[2].Modify.ReplaceAttributes[0].Type != "telephonenumber" &&
88-
l.Entries[2].Modify.ReplaceAttributes[0].Vals[1] != "+1 408 555 5678" {
87+
if l.Entries[2].Modify.Changes[2].Modification.Type != "telephonenumber" &&
88+
l.Entries[2].Modify.Changes[2].Modification.Vals[1] != "+1 408 555 5678" {
8989
t.Errorf("RFC 2849 example 6: no replacing of telephonenumber")
9090
}
9191
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/go-ldap/ldif
2+
3+
go 1.14
4+
5+
require (
6+
github.com/go-asn1-ber/asn1-ber v1.4.1 // indirect
7+
github.com/go-ldap/ldap/v3 v3.1.7
8+
)

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
2+
github.com/go-asn1-ber/asn1-ber v1.4.1 h1:qP/QDxOtmMoJVgXHCXNzDpA0+wkgYB2x5QoLMVOciyw=
3+
github.com/go-asn1-ber/asn1-ber v1.4.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
4+
github.com/go-ldap/ldap/v3 v3.1.7 h1:aHjuWTgZsnxjMgqzx0JHwNqz4jBYZTcNarbPFkW1Oww=
5+
github.com/go-ldap/ldap/v3 v3.1.7/go.mod h1:5Zun81jBTabRaI8lzN7E1JjyEl1g6zI6u9pd8luAK4Q=

ldif.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strconv"
1414
"strings"
1515

16-
"gopkg.in/ldap.v2"
16+
"github.com/go-ldap/ldap/v3"
1717
)
1818

1919
// Entry is one entry in the LDIF
@@ -220,7 +220,7 @@ func (l *LDIF) parseEntry(lines []string) (entry *Entry, err error) {
220220
return nil, err
221221
}
222222
// FIXME: controls for add - see https://github.com/go-ldap/ldap/issues/81
223-
add := ldap.NewAddRequest(dn)
223+
add := ldap.NewAddRequest(dn, controls)
224224
for attr, vals := range attrs {
225225
add.Attribute(attr, vals)
226226
}
@@ -234,7 +234,7 @@ func (l *LDIF) parseEntry(lines []string) (entry *Entry, err error) {
234234

235235
case "modify":
236236
// FIXME: controls for modify - see https://github.com/go-ldap/ldap/issues/81
237-
mod := ldap.NewModifyRequest(dn)
237+
mod := ldap.NewModifyRequest(dn, controls)
238238
var op, attribute string
239239
var values []string
240240
if lines[len(lines)-1] != "-" {

ldif_test.go

+33-17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/go-ldap/ldap/v3"
89
"github.com/go-ldap/ldif"
910
)
1011

@@ -286,69 +287,84 @@ func TestParseModify(t *testing.T) {
286287
}
287288

288289
//DeleteAttributes
289-
attributes := l.Entries[0].Modify.DeleteAttributes
290+
attributes := countChangeType("delete", l.Entries[0].Modify.Changes)
290291
if actual := len(attributes); actual != 2 {
291292
t.Error("Expected", 2, "Actual", actual)
292293
}
293294

294-
if actual := attributes[0].Type; actual != "mail" {
295+
if actual := attributes[0].Modification.Type; actual != "mail" {
295296
t.Error("Expected", "mail", "Actual", actual)
296297
}
297298

298-
if actual := len(attributes[0].Vals); actual != 0 {
299+
if actual := len(attributes[0].Modification.Vals); actual != 0 {
299300
t.Error("Expected", 0, "Actual", actual)
300301
}
301302

302-
if actual := attributes[1].Type; actual != "telephoneNumber" {
303+
if actual := attributes[1].Modification.Type; actual != "telephoneNumber" {
303304
t.Error("Expected", "telephoneNumber", "Actual", actual)
304305
}
305306

306-
if actual := len(attributes[1].Vals); actual != 1 {
307+
if actual := len(attributes[1].Modification.Vals); actual != 1 {
307308
t.Error("Expected", 1, "Actual", actual)
308309
}
309310

310-
if actual := attributes[1].Vals[0]; actual != "123 456 789 - 0" {
311+
if actual := attributes[1].Modification.Vals[0]; actual != "123 456 789 - 0" {
311312
t.Error("Expected", "123 456 789 - 0", "Actual", actual)
312313
}
313314

314-
//ReplaceAttributes
315-
attributes = l.Entries[0].Modify.ReplaceAttributes
315+
// //ReplaceAttributes
316+
attributes = countChangeType("replace", l.Entries[0].Modify.Changes)
316317
if actual := len(attributes); actual != 1 {
317318
t.Error("Expected", 1, "Actual", actual)
318319
}
319320

320-
if actual := attributes[0].Type; actual != "sn" {
321+
if actual := attributes[0].Modification.Type; actual != "sn" {
321322
t.Error("Expected", "sn", "Actual", actual)
322323
}
323324

324-
if actual := len(attributes[0].Vals); actual != 1 {
325+
if actual := len(attributes[0].Modification.Vals); actual != 1 {
325326
t.Error("Expected", 1, "Actual", actual)
326327
}
327328

328-
if actual := attributes[0].Vals[0]; actual != "One" {
329+
if actual := attributes[0].Modification.Vals[0]; actual != "One" {
329330
t.Error("Expected", "One", "Actual", actual)
330331
}
331332

332-
//AddAttributes
333-
attributes = l.Entries[0].Modify.AddAttributes
333+
// //AddAttributes
334+
attributes = countChangeType("add", l.Entries[0].Modify.Changes)
334335
if actual := len(attributes); actual != 1 {
335336
t.Error("Expected", 1, "Actual", actual)
336337
}
337338

338-
if actual := attributes[0].Type; actual != "givenName" {
339+
if actual := attributes[0].Modification.Type; actual != "givenName" {
339340
t.Error("Expected", "givenName", "Actual", actual)
340341
}
341342

342-
if actual := len(attributes[0].Vals); actual != 2 {
343+
if actual := len(attributes[0].Modification.Vals); actual != 2 {
343344
t.Error("Expected", 2, "Actual", actual)
344345
}
345346

346-
if actual := attributes[0].Vals[0]; actual != "Some1" {
347+
if actual := attributes[0].Modification.Vals[0]; actual != "Some1" {
347348
t.Error("Expected", "Some1", "Actual", actual)
348349
}
349350

350-
if actual := attributes[0].Vals[1]; actual != "Some2" {
351+
if actual := attributes[0].Modification.Vals[1]; actual != "Some2" {
351352
t.Error("Expected", "Some2", "Actual", actual)
352353
}
354+
}
353355

356+
// countChangeType is a helper function to minimise test changes migrating to ldapv3
357+
func countChangeType(typ string, ch []ldap.Change) []ldap.Change {
358+
var t = map[string]uint{
359+
"add": 0,
360+
"delete": 1,
361+
"replace": 2,
362+
}
363+
c := []ldap.Change{}
364+
for _, i := range ch {
365+
if i.Operation == t[typ] {
366+
c = append(c, i)
367+
}
368+
}
369+
return c
354370
}

marshal.go

+45-39
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"encoding/base64"
55
"errors"
66
"fmt"
7-
"gopkg.in/ldap.v2"
87
"io"
8+
9+
"github.com/go-ldap/ldap/v3"
910
)
1011

1112
var foldWidth = 76
@@ -70,50 +71,55 @@ func Marshal(l *LDIF) (data string, err error) {
7071
}
7172
data += foldLine("dn: "+e.Modify.DN, fw) + "\n"
7273
data += "changetype: modify\n"
73-
for _, mod := range e.Modify.AddAttributes {
74-
if len(mod.Vals) == 0 {
75-
return "", errors.New("changetype 'modify', op 'add' requires non empty value list")
76-
}
74+
for _, mod := range e.Modify.Changes {
75+
switch mod.Operation {
76+
// add operation - https://tools.ietf.org/html/rfc4511#section-4.6
77+
case 0:
78+
if len(mod.Modification.Vals) == 0 {
79+
return "", errors.New("changetype 'modify', op 'add' requires non empty value list")
80+
}
7781

78-
data += "add: " + mod.Type + "\n"
79-
for _, v := range mod.Vals {
80-
ev, t := encodeValue(v)
81-
col := ": "
82-
if t {
83-
col = ":: "
82+
data += "add: " + mod.Modification.Type + "\n"
83+
for _, v := range mod.Modification.Vals {
84+
ev, t := encodeValue(v)
85+
col := ": "
86+
if t {
87+
col = ":: "
88+
}
89+
data += foldLine(mod.Modification.Type+col+ev, fw) + "\n"
8490
}
85-
data += foldLine(mod.Type+col+ev, fw) + "\n"
86-
}
87-
data += "-\n"
88-
}
89-
for _, mod := range e.Modify.DeleteAttributes {
90-
data += "delete: " + mod.Type + "\n"
91-
for _, v := range mod.Vals {
92-
ev, t := encodeValue(v)
93-
col := ": "
94-
if t {
95-
col = ":: "
91+
data += "-\n"
92+
// delete operation - https://tools.ietf.org/html/rfc4511#section-4.6
93+
case 1:
94+
data += "delete: " + mod.Modification.Type + "\n"
95+
for _, v := range mod.Modification.Vals {
96+
ev, t := encodeValue(v)
97+
col := ": "
98+
if t {
99+
col = ":: "
100+
}
101+
data += foldLine(mod.Modification.Type+col+ev, fw) + "\n"
96102
}
97-
data += foldLine(mod.Type+col+ev, fw) + "\n"
98-
}
99-
data += "-\n"
100-
}
101-
for _, mod := range e.Modify.ReplaceAttributes {
102-
if len(mod.Vals) == 0 {
103-
return "", errors.New("changetype 'modify', op 'replace' requires non empty value list")
104-
}
105-
data += "replace: " + mod.Type + "\n"
106-
for _, v := range mod.Vals {
107-
ev, t := encodeValue(v)
108-
col := ": "
109-
if t {
110-
col = ":: "
103+
data += "-\n"
104+
// replace operation - https://tools.ietf.org/html/rfc4511#section-4.6
105+
case 2:
106+
if len(mod.Modification.Vals) == 0 {
107+
return "", errors.New("changetype 'modify', op 'replace' requires non empty value list")
111108
}
112-
data += foldLine(mod.Type+col+ev, fw) + "\n"
109+
data += "replace: " + mod.Modification.Type + "\n"
110+
for _, v := range mod.Modification.Vals {
111+
ev, t := encodeValue(v)
112+
col := ": "
113+
if t {
114+
col = ":: "
115+
}
116+
data += foldLine(mod.Modification.Type+col+ev, fw) + "\n"
117+
}
118+
data += "-\n"
119+
default:
120+
return "", fmt.Errorf("invalid type %s in modify request", mod.Modification.Type)
113121
}
114-
data += "-\n"
115122
}
116-
117123
default:
118124
hasEntry = true
119125
if hasChange {

marshal_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package ldif_test
22

33
import (
44
"bytes"
5-
"github.com/go-ldap/ldif"
6-
"gopkg.in/ldap.v2"
75
"testing"
6+
7+
"github.com/go-ldap/ldap/v3"
8+
"github.com/go-ldap/ldif"
89
)
910

1011
var personLDIF = `dn: uid=someone,ou=people,dc=example,dc=org
@@ -146,6 +147,9 @@ description:: VGhlIFBlw7ZwbGUgw5ZyZ2FuaXphdGlvbg==
146147
func TestMarshalMod(t *testing.T) {
147148
modLDIF := `dn: uid=someone,ou=people,dc=example,dc=org
148149
changetype: modify
150+
replace: sn
151+
sn: One
152+
-
149153
add: givenName
150154
givenName: Some
151155
-
@@ -154,12 +158,9 @@ delete: mail
154158
delete: telephoneNumber
155159
telephoneNumber: 123 456 789 - 0
156160
-
157-
replace: sn
158-
sn: One
159-
-
160161
161162
`
162-
mod := ldap.NewModifyRequest("uid=someone,ou=people,dc=example,dc=org")
163+
mod := ldap.NewModifyRequest("uid=someone,ou=people,dc=example,dc=org", []ldap.Control{})
163164
mod.Replace("sn", []string{"One"})
164165
mod.Add("givenName", []string{"Some"})
165166
mod.Delete("mail", []string{})
@@ -190,7 +191,7 @@ cn: Someone
190191
191192
192193
`
193-
add := ldap.NewAddRequest("uid=someone,ou=people,dc=example,dc=org")
194+
add := ldap.NewAddRequest("uid=someone,ou=people,dc=example,dc=org", []ldap.Control{})
194195
for _, a := range entries[1].Attributes {
195196
add.Attribute(a.Name, a.Values)
196197
}

0 commit comments

Comments
 (0)