github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/ldaplib/ldap_docker_test.go (about) 1 package ldaplib 2 3 import ( 4 "fmt" 5 "os" 6 "path" 7 "time" 8 9 "github.com/tommi2day/gomodules/test" 10 11 "github.com/go-ldap/ldap/v3" 12 "github.com/ory/dockertest/v3" 13 "github.com/ory/dockertest/v3/docker" 14 "github.com/tommi2day/gomodules/common" 15 ) 16 17 const Ldaprepo = "docker.io/bitnami/openldap" 18 const LdaprepoTag = "2.6.7" 19 const LdapcontainerTimeout = 120 20 21 var ldapcontainerName string 22 var ldapContainer *dockertest.Resource 23 24 // prepareContainer create an OpenLdap Docker Container 25 func prepareLdapContainer() (container *dockertest.Resource, err error) { 26 if os.Getenv("SKIP_LDAP") != "" { 27 err = fmt.Errorf("skipping LDAP Container in CI environment") 28 return 29 } 30 ldapcontainerName = os.Getenv("LDAP_CONTAINER_NAME") 31 if ldapcontainerName == "" { 32 ldapcontainerName = "ldaplib-openldap" 33 } 34 35 var pool *dockertest.Pool 36 pool, err = common.GetDockerPool() 37 if err != nil { 38 return 39 } 40 vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX") 41 repoString := vendorImagePrefix + Ldaprepo 42 43 fmt.Printf("Try to start docker container for %s:%s\n", repoString, LdaprepoTag) 44 fmt.Println(path.Join(test.TestDir, "docker", "ldap", "certs") + ":/opt/bitnami/openldap/certs:ro") 45 container, err = pool.RunWithOptions(&dockertest.RunOptions{ 46 Repository: repoString, 47 Tag: LdaprepoTag, 48 Env: []string{ 49 50 "LDAP_PORT_NUMBER=1389", 51 "LDAP_LDAPS_PORT_NUMBER=1636", 52 "BITNAMI_DEBUG=true", 53 "LDAP_ROOT=" + LdapBaseDn, 54 "LDAP_ADMIN_USERNAME=admin", 55 "LDAP_ADMIN_PASSWORD=" + LdapAdminPassword, 56 "LDAP_CONFIG_ADMIN_ENABLED=yes", 57 "LDAP_CONFIG_ADMIN_USERNAME=config", 58 "LDAP_CONFIG_ADMIN_PASSWORD=" + LdapConfigPassword, 59 // "LDAP_SKIP_DEFAULT_TREE=yes", 60 // "LDAP_CUSTOM_LDIF_DIR=/bootstrap/ldif", 61 "LDAP_CUSTOM_SCHEMA_DIR=/bootstrap/schema", 62 "LDAP_ADD_SCHEMAS=yes", 63 "LDAP_EXTRA_SCHEMAS=cosine,inetorgperson,nis", 64 "LDAP_ALLOW_ANON_BINDING=yes", 65 "LDAP_ENABLE_TLS=yes", 66 "LDAP_TLS_CERT_FILE=/opt/bitnami/openldap/certs/ldap.example.local-full.crt", 67 "LDAP_TLS_KEY_FILE=/opt/bitnami/openldap/certs/ldap.example.local.key", 68 "LDAP_TLS_CA_FILE=/opt/bitnami/openldap/certs/ca.crt", 69 "LDAP_TLS_VERIFY_CLIENTS=never", 70 }, 71 72 Mounts: []string{ 73 test.TestDir + "/docker/ldap/ldif:/bootstrap/ldif:ro", 74 test.TestDir + "/docker/ldap/schema:/bootstrap/schema:ro", 75 test.TestDir + "/docker/ldap/entrypoint:/docker-entrypoint-initdb.d", 76 test.TestDir + "/docker/ldap/certs:/opt/bitnami/openldap/certs:ro", 77 }, 78 79 Hostname: ldapcontainerName, 80 Name: ldapcontainerName, 81 }, func(config *docker.HostConfig) { 82 // set AutoRemove to true so that stopped container goes away by itself 83 config.AutoRemove = true 84 config.RestartPolicy = docker.RestartPolicy{Name: "no"} 85 }) 86 87 if err != nil { 88 err = fmt.Errorf("error starting ldap docker container: %v", err) 89 return 90 } 91 92 pool.MaxWait = LdapcontainerTimeout * time.Second 93 myhost, myport := common.GetContainerHostAndPort(container, "1389/tcp") 94 dialURL := fmt.Sprintf("ldap://%s:%d", myhost, myport) 95 fmt.Printf("Wait to successfully connect to Ldap with %s (max %ds)...\n", dialURL, LdapcontainerTimeout) 96 start := time.Now() 97 var l *ldap.Conn 98 if err = pool.Retry(func() error { 99 l, err = ldap.DialURL(dialURL) 100 return err 101 }); err != nil { 102 fmt.Printf("Could not connect to LDAP Container: %s", err) 103 return 104 } 105 /* 106 if err = pool.Retry(func() error { 107 err = l.Bind("cn=admin,"+LdapBaseDn, LdapAdminPassword) 108 return err 109 }); err != nil { 110 fmt.Printf("Could not login to LDAP Container: %s", err) 111 return 112 } 113 */ 114 _ = l.Close() 115 elapsed := time.Since(start) 116 fmt.Printf("LDAP Container is available after %s\n", elapsed.Round(time.Millisecond)) 117 // wait 15s to init container 118 time.Sleep(15 * time.Second) 119 err = nil 120 return 121 }