2019-10-22 17:37:41 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2020-09-15 14:01:26 +00:00
|
|
|
"context"
|
2019-10-22 17:37:41 +00:00
|
|
|
"fmt"
|
2019-11-07 16:54:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-02-14 18:26:30 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2019-10-22 17:37:41 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/ldaputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PrepareTestContainer(t *testing.T, version string) (cleanup func(), cfg *ldaputil.ConfigEntry) {
|
2020-09-15 14:01:26 +00:00
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
2020-02-14 18:26:30 +00:00
|
|
|
// Currently set to "michelvocks" until https://github.com/rroemhild/docker-test-openldap/pull/14
|
|
|
|
// has been merged.
|
2020-09-15 14:01:26 +00:00
|
|
|
ImageRepo: "michelvocks/docker-test-openldap",
|
|
|
|
ImageTag: version,
|
|
|
|
ContainerName: "ldap",
|
|
|
|
Ports: []string{"389/tcp"},
|
2021-04-08 16:43:39 +00:00
|
|
|
// Env: []string{"LDAP_DEBUG_LEVEL=384"},
|
2020-09-15 14:01:26 +00:00
|
|
|
})
|
2019-10-22 17:37:41 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
t.Fatalf("could not start local LDAP docker container: %s", err)
|
2019-10-22 17:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cfg = new(ldaputil.ConfigEntry)
|
|
|
|
cfg.UserDN = "ou=people,dc=planetexpress,dc=com"
|
|
|
|
cfg.UserAttr = "cn"
|
2021-10-26 17:39:12 +00:00
|
|
|
cfg.UserFilter = "({{.UserAttr}}={{.Username}})"
|
2020-09-15 14:01:26 +00:00
|
|
|
cfg.BindDN = "cn=admin,dc=planetexpress,dc=com"
|
|
|
|
cfg.BindPassword = "GoodNewsEveryone"
|
|
|
|
cfg.GroupDN = "ou=people,dc=planetexpress,dc=com"
|
|
|
|
cfg.GroupAttr = "cn"
|
|
|
|
cfg.RequestTimeout = 60
|
|
|
|
|
|
|
|
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
connURL := fmt.Sprintf("ldap://%s:%d", host, port)
|
|
|
|
cfg.Url = connURL
|
2019-10-22 17:37:41 +00:00
|
|
|
logger := hclog.New(nil)
|
|
|
|
client := ldaputil.Client{
|
|
|
|
LDAP: ldaputil.NewLDAP(),
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := client.DialLDAP(cfg)
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-10-22 17:37:41 +00:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
if _, err := client.GetUserBindDN(cfg, conn, "Philip J. Fry"); err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-10-22 17:37:41 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
|
|
|
|
return docker.NewServiceURLParse(connURL)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not start local LDAP docker container: %s", err)
|
2019-10-22 17:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
return svc.Cleanup, cfg
|
2019-10-22 17:37:41 +00:00
|
|
|
}
|