Skip to content

Commit 0b15564

Browse files
committed
Added Attr to list
1 parent 924a073 commit 0b15564

File tree

8 files changed

+42
-17
lines changed

8 files changed

+42
-17
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/golang-jwt/jwt/v5 v5.2.2
1111
github.com/mutablelogic/go-client v1.0.12
1212
github.com/stretchr/testify v1.10.0
13+
github.com/yinyin/go-ldap-schema-parser v0.0.0-20190716182935-542aadd3dcb5
1314
)
1415

1516
require (

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 h1:BP4M0CvQ
5252
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
5353
github.com/go-ldap/ldap/v3 v3.4.11 h1:4k0Yxweg+a3OyBLjdYn5OKglv18JNvfDykSoI8bW0gU=
5454
github.com/go-ldap/ldap/v3 v3.4.11/go.mod h1:bY7t0FLK8OAVpp/vV6sSlpz3EQDGcQwc8pF0ujLgKvM=
55+
github.com/go-ldap/ldif v0.0.0-20180918085934-3491d58cdb60/go.mod h1:blBiFTfuR1Jrw4xZ7t3xuNObLzzBG+ce+5W/bEYwJq0=
5556
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
5657
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
5758
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
@@ -163,6 +164,8 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA
163164
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
164165
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
165166
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
167+
github.com/yinyin/go-ldap-schema-parser v0.0.0-20190716182935-542aadd3dcb5 h1:siJ/5leB7JENBScgD/qG8JAGiS/2Q76qxCPK81icczU=
168+
github.com/yinyin/go-ldap-schema-parser v0.0.0-20190716182935-542aadd3dcb5/go.mod h1:Hb9db5nLRb/cT+dBKUrukgT3Z9mbtrpF3o2g8+sw7ic=
166169
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
167170
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
168171
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
@@ -238,9 +241,11 @@ google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
238241
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
239242
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
240243
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
244+
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
241245
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
242246
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
243247
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
248+
gopkg.in/ldap.v2 v2.5.1/go.mod h1:oI0cpe/D7HRtBQl8aTg+ZmzFUAvu4lsv3eLXMLGFxWk=
244249
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
245250
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
246251
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/ldap/client/opts.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func WithFilter(v *string) Opt {
5353
return OptSet("filter", types.PtrString(v))
5454
}
5555

56+
// Set LDAP attributes
57+
func WithAttr(v ...string) Opt {
58+
return func(o *opt) error {
59+
if len(v) == 0 {
60+
o.Del("attr")
61+
} else {
62+
o.Values["attr"] = v
63+
}
64+
return nil
65+
}
66+
}
67+
5668
func OptSet(k, v string) Opt {
5769
return func(o *opt) error {
5870
if v == "" {

pkg/ldap/cmd/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ObjectListCommand struct {
2626

2727
func (cmd ObjectListCommand) Run(ctx server.Cmd) error {
2828
return run(ctx, func(ctx context.Context, provider *client.Client) error {
29-
queues, err := provider.ListObjects(ctx, client.WithFilter(cmd.Filter), client.WithOffsetLimit(cmd.Offset, cmd.Limit))
29+
queues, err := provider.ListObjects(ctx, client.WithFilter(cmd.Filter), client.WithAttr(cmd.Attr...), client.WithOffsetLimit(cmd.Offset, cmd.Limit))
3030
if err != nil {
3131
return err
3232
}

pkg/ldap/config/config.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import (
1515
// TYPES
1616

1717
type Config struct {
18-
Url *url.URL `env:"LDAP_URL" help:"LDAP connection URL"` // Connection URL
19-
Password string `env:"LDAP_PASSWORD" help:"Password"` // Password
20-
BaseDN string `env:"LDAP_BASE_DN" help:"Base DN"` // Base DN
21-
Router server.HTTPRouter `kong:"-"` // HTTP Router
18+
Url *url.URL `env:"LDAP_URL" help:"LDAP connection URL"` // Connection URL
19+
User string `env:"LDAP_USER" help:"User"` // User
20+
Password string `env:"LDAP_PASSWORD" help:"Password"` // Password
21+
BaseDN string `env:"LDAP_BASE_DN" help:"Base DN"` // Base DN
22+
SkipVerify bool `env:"LDAP_SKIPVERIFY" help:"Skip TLS certificate verify"` // Skip verify
23+
Router server.HTTPRouter `kong:"-"` // HTTP Router
2224
}
2325

2426
var _ server.Plugin = Config{}
@@ -28,16 +30,20 @@ var _ server.Plugin = Config{}
2830

2931
func (c Config) New(ctx context.Context) (server.Task, error) {
3032
// Add options
31-
opts := []ldap.Opt{}
33+
opts := []ldap.Opt{
34+
ldap.WithUser(c.User),
35+
ldap.WithPassword(c.Password),
36+
ldap.WithBaseDN(c.BaseDN),
37+
}
3238
if c.Url != nil {
3339
opts = append(opts, ldap.WithUrl(c.Url.String()))
3440
}
35-
if c.Password != "" {
36-
opts = append(opts, ldap.WithPassword(c.Password))
37-
}
3841
if c.BaseDN != "" {
3942
opts = append(opts, ldap.WithBaseDN(c.BaseDN))
4043
}
44+
if c.SkipVerify {
45+
opts = append(opts, ldap.WithSkipVerify())
46+
}
4147

4248
// Create a new LDAP manager
4349
manager, err := ldap.NewManager(opts...)

pkg/ldap/manager.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,11 @@ func (manager *Manager) List(ctx context.Context, request schema.ObjectListReque
244244
limit = min(types.PtrUint64(request.Limit), limit)
245245
}
246246

247-
// Set filter and attributes
247+
// Set filter
248248
filter := "(objectclass=*)"
249249
if request.Filter != nil {
250250
filter = types.PtrString(request.Filter)
251251
}
252-
// TODO
253-
attrs := []string{}
254252

255253
// Perform the search through paging, skipping the first N entries
256254
var list schema.ObjectList
@@ -260,7 +258,7 @@ func (manager *Manager) List(ctx context.Context, request schema.ObjectListReque
260258
}
261259
list.Count = list.Count + 1
262260
return nil
263-
}, attrs...); err != nil {
261+
}, request.Attr...); err != nil {
264262
return nil, err
265263
}
266264

@@ -325,7 +323,7 @@ func (manager *Manager) list(ctx context.Context, scope int, dn, filter string,
325323
}
326324

327325
// Get an object by DN
328-
func (manager *Manager) Get(ctx context.Context, dn string) (*schema.Object, error) {
326+
func (manager *Manager) Get(ctx context.Context, dn string, attrs ...string) (*schema.Object, error) {
329327
manager.Lock()
330328
defer manager.Unlock()
331329

@@ -334,7 +332,8 @@ func (manager *Manager) Get(ctx context.Context, dn string) (*schema.Object, err
334332
return nil, httpresponse.ErrInternalError.With("Not connected")
335333
}
336334

337-
return manager.get(ctx, ldap.ScopeBaseObject, dn, "(objectclass=*)")
335+
// Get the object
336+
return manager.get(ctx, ldap.ScopeBaseObject, dn, "(objectclass=*)", attrs...)
338337
}
339338

340339
func (manager *Manager) get(ctx context.Context, scope int, dn, filter string, attrs ...string) (*schema.Object, error) {

pkg/ldap/manager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
const (
18+
// https://github.com/shadowbq/FreeIPA-Configuration
1819
User, Pass = "uid=admin,cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org", "Secret123"
1920
URL = "ldaps://ipa.demo1.freeipa.org/"
2021
BaseDN = "dc=demo1,dc=freeipa,dc=org"
@@ -35,9 +36,9 @@ func TestMain(m *testing.M) {
3536
opts = append(opts, ldap.WithBaseDN(dn))
3637
}
3738
} else {
39+
// Use FreeIPA instance
3840
opts = append(opts, ldap.WithUrl(URL), ldap.WithUser(User), ldap.WithPassword(Pass), ldap.WithBaseDN(BaseDN), ldap.WithSkipVerify())
3941
}
40-
4142
exitCode := m.Run()
4243
os.Exit(exitCode)
4344
}

pkg/ldap/schema/object.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type Object struct {
2121

2222
type ObjectListRequest struct {
2323
pg.OffsetLimit
24-
Filter *string `json:"filter,omitempty"`
24+
Filter *string `json:"filter,omitempty" help:"Filter"`
25+
Attr []string `json:"attr,omitempty" help:"Attributes to return"`
2526
}
2627

2728
type ObjectList struct {

0 commit comments

Comments
 (0)