Skip to content

Commit ceffcb1

Browse files
committed
fix paging LDAP does not work with a map
1 parent 79aa4f8 commit ceffcb1

File tree

9 files changed

+214
-168
lines changed

9 files changed

+214
-168
lines changed

config/dynamic/directory/config.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package directory
33
import (
44
"fmt"
55
"mokapi/config/dynamic"
6+
"mokapi/sortedmap"
67
"mokapi/version"
78
"net/url"
89
"path/filepath"
@@ -19,7 +20,7 @@ type Config struct {
1920
Info Info
2021
Address string
2122
SizeLimit int64
22-
Entries map[string]Entry
23+
Entries *sortedmap.LinkedHashMap[string, Entry]
2324
Files []string
2425

2526
root map[string][]string
@@ -42,13 +43,14 @@ type Entry struct {
4243
}
4344

4445
func (c *Config) Parse(config *dynamic.Config, reader dynamic.Reader) error {
45-
c.Entries = map[string]Entry{}
46+
c.Entries = &sortedmap.LinkedHashMap[string, Entry]{}
4647
// copy from old format
4748
for k, v := range c.oldEntries {
48-
c.Entries[k] = v
49+
c.Entries.Set(k, v)
4950
}
5051

51-
for _, entry := range c.Entries {
52+
for it := c.Entries.Iter(); it.Next(); {
53+
entry := it.Value()
5254
err := entry.Parse(config, reader)
5355
if err != nil {
5456
return err
@@ -98,7 +100,7 @@ func (c *Config) Parse(config *dynamic.Config, reader dynamic.Reader) error {
98100
}
99101
}
100102

101-
root, ok := c.Entries[""]
103+
root, ok := c.Entries.Get("")
102104
if !ok {
103105
root = Entry{
104106
Attributes: map[string][]string{
@@ -142,7 +144,7 @@ func (c *Config) Parse(config *dynamic.Config, reader dynamic.Reader) error {
142144
if v, ok = c.root["NamingContexts"]; ok {
143145
root.Attributes["namingContexts"] = v
144146
}
145-
c.Entries[""] = root
147+
c.Entries.Set("", root)
146148

147149
return nil
148150
}

config/dynamic/directory/config_ldif_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,9 @@ func TestConfig_LDIF(t *testing.T) {
282282
}},
283283
test: func(t *testing.T, c *Config, err error) {
284284
require.NoError(t, err)
285-
require.Len(t, c.Entries, 2)
286-
require.Contains(t, c.Entries, "dc=mokapi,dc=io")
287-
require.Equal(t, "dc=mokapi,dc=io", c.Entries["dc=mokapi,dc=io"].Dn)
288-
require.Equal(t, []string{"bar"}, c.Entries["dc=mokapi,dc=io"].Attributes["foo"])
285+
require.Equal(t, 2, c.Entries.Len())
286+
require.Equal(t, "dc=mokapi,dc=io", c.Entries.Lookup("dc=mokapi,dc=io").Dn)
287+
require.Equal(t, []string{"bar"}, c.Entries.Lookup("dc=mokapi,dc=io").Attributes["foo"])
289288
},
290289
},
291290
{
@@ -297,9 +296,8 @@ func TestConfig_LDIF(t *testing.T) {
297296
}},
298297
test: func(t *testing.T, c *Config, err error) {
299298
require.NoError(t, err)
300-
require.Len(t, c.Entries, 2)
301-
require.Contains(t, c.Entries, "dc=mokapi,dc=io")
302-
require.Equal(t, []string{"bar", "yuh"}, c.Entries["dc=mokapi,dc=io"].Attributes["foo"])
299+
require.Equal(t, 2, c.Entries.Len())
300+
require.Equal(t, []string{"bar", "yuh"}, c.Entries.Lookup("dc=mokapi,dc=io").Attributes["foo"])
303301
},
304302
},
305303
{
@@ -311,9 +309,8 @@ func TestConfig_LDIF(t *testing.T) {
311309
}},
312310
test: func(t *testing.T, c *Config, err error) {
313311
require.NoError(t, err)
314-
require.Len(t, c.Entries, 2)
315-
require.Contains(t, c.Entries, "dc=mokapi,dc=io")
316-
require.Len(t, c.Entries["dc=mokapi,dc=io"].Attributes, 0)
312+
require.Equal(t, 2, c.Entries.Len())
313+
require.Len(t, c.Entries.Lookup("dc=mokapi,dc=io").Attributes, 0)
317314
},
318315
},
319316
{
@@ -325,9 +322,8 @@ func TestConfig_LDIF(t *testing.T) {
325322
}},
326323
test: func(t *testing.T, c *Config, err error) {
327324
require.NoError(t, err)
328-
require.Len(t, c.Entries, 2)
329-
require.Contains(t, c.Entries, "dc=mokapi,dc=io")
330-
require.Equal(t, []string{"yuh"}, c.Entries["dc=mokapi,dc=io"].Attributes["foo"])
325+
require.Equal(t, 2, c.Entries.Len())
326+
require.Equal(t, []string{"yuh"}, c.Entries.Lookup("dc=mokapi,dc=io").Attributes["foo"])
331327
},
332328
},
333329
{
@@ -338,9 +334,8 @@ func TestConfig_LDIF(t *testing.T) {
338334
}},
339335
test: func(t *testing.T, c *Config, err error) {
340336
require.NoError(t, err)
341-
require.Len(t, c.Entries, 1)
342-
require.Contains(t, c.Entries, "")
343-
require.Equal(t, []string{"foo"}, c.Entries[""].Attributes["vendorName"])
337+
require.Equal(t, 1, c.Entries.Len())
338+
require.Equal(t, []string{"foo"}, c.Entries.Lookup("").Attributes["vendorName"])
344339
},
345340
},
346341
}

config/dynamic/directory/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestConfig(t *testing.T) {
2727
input: `{"ldap": "1.0", "info": { "name": "foo", "description": "bar" } }`,
2828
test: func(t *testing.T, cfg *Config, err error) {
2929
require.NoError(t, err)
30-
root := cfg.Entries[""]
30+
root := cfg.Entries.Lookup("")
3131
require.Equal(t, []string{"foo"}, root.Attributes["dsServiceName"])
3232
require.Equal(t, []string{"bar"}, root.Attributes["description"])
3333
},

config/dynamic/directory/directory_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"mokapi/engine/enginetest"
88
"mokapi/ldap"
99
"mokapi/ldap/ldaptest"
10+
"mokapi/sortedmap"
1011
"testing"
1112
)
1213

1314
var testConfig = &directory.Config{
1415
Info: directory.Info{Name: "foo"},
15-
Entries: map[string]directory.Entry{
16+
Entries: convert(map[string]directory.Entry{
1617
"": {
1718
Dn: "dc=foo,dc=com",
1819
Attributes: map[string][]string{
@@ -35,7 +36,8 @@ var testConfig = &directory.Config{
3536
"number": {"10"},
3637
},
3738
},
38-
}}
39+
}),
40+
}
3941

4042
func TestDirectory_ServeBind(t *testing.T) {
4143
testcases := []struct {
@@ -122,8 +124,9 @@ func TestDirectory_ServeSearch(t *testing.T) {
122124
{
123125
name: "base object",
124126
config: &directory.Config{Info: directory.Info{Name: "foo"},
125-
Entries: map[string]directory.Entry{
126-
"": {Dn: "", Attributes: map[string][]string{"foo": {"bar"}}}},
127+
Entries: convert(map[string]directory.Entry{
128+
"": {Dn: "", Attributes: map[string][]string{"foo": {"bar"}}},
129+
}),
127130
},
128131
fn: func(t *testing.T, h ldap.Handler) {
129132
rr := ldaptest.NewRecorder()
@@ -143,7 +146,7 @@ func TestDirectory_ServeSearch(t *testing.T) {
143146
{
144147
name: "objectclass=* scope=ScopeBaseObject",
145148
config: &directory.Config{Info: directory.Info{Name: "foo"},
146-
Entries: map[string]directory.Entry{
149+
Entries: convert(map[string]directory.Entry{
147150
"": {
148151
Dn: "dc=foo,dc=com",
149152
Attributes: map[string][]string{
@@ -153,7 +156,7 @@ func TestDirectory_ServeSearch(t *testing.T) {
153156
"not": {
154157
Dn: "not",
155158
},
156-
}},
159+
})},
157160
fn: func(t *testing.T, h ldap.Handler) {
158161
rr := ldaptest.NewRecorder()
159162
h.ServeLDAP(rr, ldaptest.NewRequest(0, &ldap.SearchRequest{
@@ -484,14 +487,14 @@ func TestDirectory_ServeSearch(t *testing.T) {
484487
{
485488
name: "options (cn;lang-en=John Doe)",
486489
config: &directory.Config{
487-
Entries: map[string]directory.Entry{
490+
Entries: convert(map[string]directory.Entry{
488491
"user2": {
489492
Dn: "cn=john,dc=foo,dc=com",
490493
Attributes: map[string][]string{
491494
"cn;lang-en": {"John Doe"},
492495
},
493496
},
494-
}},
497+
})},
495498
fn: func(t *testing.T, h ldap.Handler) {
496499
rr := ldaptest.NewRecorder()
497500
h.ServeLDAP(rr, ldaptest.NewRequest(0, &ldap.SearchRequest{
@@ -510,20 +513,20 @@ func TestDirectory_ServeSearch(t *testing.T) {
510513
{
511514
name: "paging",
512515
config: &directory.Config{
513-
Entries: map[string]directory.Entry{
514-
"user1": {
516+
Entries: convertArray([]directory.Entry{
517+
{
515518
Dn: "cn=john,dc=foo,dc=com",
516519
Attributes: map[string][]string{
517520
"cn": {"John Doe"},
518521
},
519522
},
520-
"user2": {
523+
{
521524
Dn: "cn=carol,dc=foo,dc=com",
522525
Attributes: map[string][]string{
523526
"cn": {"Carol Doe"},
524527
},
525528
},
526-
}},
529+
})},
527530
fn: func(t *testing.T, h ldap.Handler) {
528531
ctx := ldap.NewPagingFromContext(context.Background())
529532

@@ -583,3 +586,11 @@ func hasResult(results []ldap.SearchResult, dn string) bool {
583586
}
584587
return false
585588
}
589+
590+
func convertArray(entries []directory.Entry) *sortedmap.LinkedHashMap[string, directory.Entry] {
591+
r := &sortedmap.LinkedHashMap[string, directory.Entry]{}
592+
for _, entry := range entries {
593+
r.Set(entry.Dn, entry)
594+
}
595+
return r
596+
}

config/dynamic/directory/ldif.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"fmt"
66
"mokapi/config/dynamic"
7+
"mokapi/sortedmap"
78
"net/url"
89
"strings"
910
)
@@ -13,7 +14,7 @@ type Ldif struct {
1314
}
1415

1516
type LdifRecord interface {
16-
Apply(entries map[string]Entry) error
17+
Apply(entries *sortedmap.LinkedHashMap[string, Entry]) error
1718
append(name string, value string)
1819
}
1920

@@ -136,14 +137,14 @@ type AddRecord struct {
136137
Attributes map[string][]string
137138
}
138139

139-
func (add *AddRecord) Apply(entries map[string]Entry) error {
140-
if _, ok := entries[add.Dn]; ok {
140+
func (add *AddRecord) Apply(entries *sortedmap.LinkedHashMap[string, Entry]) error {
141+
if _, ok := entries.Get(add.Dn); ok {
141142
return fmt.Errorf("record dn='%s' already exists", add.Dn)
142143
}
143-
entries[add.Dn] = Entry{
144+
entries.Set(add.Dn, Entry{
144145
Dn: add.Dn,
145146
Attributes: add.Attributes,
146-
}
147+
})
147148
return nil
148149
}
149150

@@ -158,7 +159,7 @@ type DeleteRecord struct {
158159
Dn string
159160
}
160161

161-
func (del *DeleteRecord) Apply(entries map[string]Entry) error {
162+
func (del *DeleteRecord) Apply(_ *sortedmap.LinkedHashMap[string, Entry]) error {
162163
return nil
163164
}
164165

@@ -175,11 +176,11 @@ type ModifyAction struct {
175176
Attributes map[string][]string
176177
}
177178

178-
func (mod *ModifyRecord) Apply(entries map[string]Entry) error {
179-
if _, ok := entries[mod.Dn]; !ok {
179+
func (mod *ModifyRecord) Apply(entries *sortedmap.LinkedHashMap[string, Entry]) error {
180+
e, ok := entries.Get(mod.Dn)
181+
if !ok {
180182
return fmt.Errorf("apply change record failed: entry '%v' not found", mod.Dn)
181183
}
182-
e := entries[mod.Dn]
183184

184185
for _, action := range mod.Actions {
185186
switch action.Type {

config/dynamic/directory/patch.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func (c *Config) patchEntries(patch *Config) {
5353
return
5454
}
5555

56-
for k, p := range patch.Entries {
57-
if v, ok := c.Entries[k]; ok {
58-
v.patch(p)
59-
c.Entries[k] = v
56+
for it := patch.Entries.Iter(); it.Next(); {
57+
if v, ok := c.Entries.Get(it.Key()); ok {
58+
v.patch(it.Value())
59+
c.Entries.Set(it.Key(), v)
6060
} else {
61-
c.Entries[k] = p
61+
c.Entries.Set(it.Key(), it.Value())
6262
}
6363
}
6464
}

0 commit comments

Comments
 (0)