-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathopenldap.go
174 lines (151 loc) · 5.16 KB
/
openldap.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
package openldap
import (
"context"
"errors"
"fmt"
"io"
"net"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
defaultUser = "admin"
defaultPassword = "adminpassword"
defaultRoot = "dc=example,dc=org"
defaultAdminDn = "cn=admin,dc=example,dc=org"
)
// OpenLDAPContainer represents the OpenLDAP container type used in the module
type OpenLDAPContainer struct {
testcontainers.Container
adminUsername string
adminPassword string
rootDn string
}
// ConnectionString returns the connection string for the OpenLDAP container
func (c *OpenLDAPContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
containerPort, err := c.MappedPort(ctx, "1389/tcp")
if err != nil {
return "", err
}
host, err := c.Host(ctx)
if err != nil {
return "", err
}
connStr := fmt.Sprintf("ldap://%s", net.JoinHostPort(host, containerPort.Port()))
return connStr, nil
}
// LoadLdif loads an ldif file into the OpenLDAP container
func (c *OpenLDAPContainer) LoadLdif(ctx context.Context, ldif []byte) error {
err := c.CopyToContainer(ctx, ldif, "/tmp/ldif.ldif", 0o644)
if err != nil {
return err
}
code, output, err := c.Exec(ctx, []string{"ldapadd", "-H", "ldap://localhost:1389", "-x", "-D", fmt.Sprintf("cn=%s,%s", c.adminUsername, c.rootDn), "-w", c.adminPassword, "-f", "/tmp/ldif.ldif"})
if err != nil {
return err
}
if code != 0 {
data, _ := io.ReadAll(output)
return errors.New(string(data))
}
return nil
}
// WithAdminUsername sets the initial admin username to be created when the container starts
// It is used in conjunction with WithAdminPassword to set a username and its password.
// It will create the specified user with admin power.
func WithAdminUsername(username string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["LDAP_ADMIN_USERNAME"] = username
return nil
}
}
// WithAdminPassword sets the initial admin password of the user to be created when the container starts
// It is used in conjunction with WithAdminUsername to set a username and its password.
// It will set the admin password for OpenLDAP.
func WithAdminPassword(password string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["LDAP_ADMIN_PASSWORD"] = password
return nil
}
}
// WithRoot sets the root of the OpenLDAP instance
func WithRoot(root string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["LDAP_ROOT"] = root
return nil
}
}
// WithInitialLdif sets the initial ldif file to be loaded into the OpenLDAP container
func WithInitialLdif(ldif string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Files = append(req.Files, testcontainers.ContainerFile{
HostFilePath: ldif,
ContainerFilePath: "/initial_ldif.ldif",
FileMode: 0o644,
})
req.LifecycleHooks = append(req.LifecycleHooks, testcontainers.ContainerLifecycleHooks{
PostReadies: []testcontainers.ContainerHook{
func(ctx context.Context, container testcontainers.Container) error {
username := req.Env["LDAP_ADMIN_USERNAME"]
rootDn := req.Env["LDAP_ROOT"]
password := req.Env["LDAP_ADMIN_PASSWORD"]
code, output, err := container.Exec(ctx, []string{"ldapadd", "-H", "ldap://localhost:1389", "-x", "-D", fmt.Sprintf("cn=%s,%s", username, rootDn), "-w", password, "-f", "/initial_ldif.ldif"})
if err != nil {
return err
}
if code != 0 {
data, _ := io.ReadAll(output)
return errors.New(string(data))
}
return nil
},
},
})
return nil
}
}
// Deprecated: use Run instead
// RunContainer creates an instance of the OpenLDAP container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*OpenLDAPContainer, error) {
return Run(ctx, "bitnami/openldap:2.6.6", opts...)
}
// Run creates an instance of the OpenLDAP container type
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OpenLDAPContainer, error) {
req := testcontainers.ContainerRequest{
Image: img,
Env: map[string]string{
"LDAP_ADMIN_USERNAME": defaultUser,
"LDAP_ADMIN_PASSWORD": defaultPassword,
"LDAP_ROOT": defaultRoot,
},
ExposedPorts: []string{"1389/tcp"},
WaitingFor: wait.ForAll(
wait.ForLog("** Starting slapd **"),
wait.ForListeningPort("1389/tcp"),
),
LifecycleHooks: []testcontainers.ContainerLifecycleHooks{
{
PostReadies: []testcontainers.ContainerHook{},
},
},
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &OpenLDAPContainer{
Container: container,
adminUsername: req.Env["LDAP_ADMIN_USERNAME"],
adminPassword: req.Env["LDAP_ADMIN_PASSWORD"],
rootDn: req.Env["LDAP_ROOT"],
}, nil
}