openldap

package module
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAdminPassword

func WithAdminPassword(password string) testcontainers.CustomizeRequestOption

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 WithAdminUsername

func WithAdminUsername(username string) testcontainers.CustomizeRequestOption

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 WithInitialLdif

func WithInitialLdif(ldif string) testcontainers.CustomizeRequestOption

WithInitialLdif sets the initial ldif file to be loaded into the OpenLDAP container

func WithRoot

func WithRoot(root string) testcontainers.CustomizeRequestOption

WithRoot sets the root of the OpenLDAP instance

Types

type OpenLDAPContainer

type OpenLDAPContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

OpenLDAPContainer represents the OpenLDAP container type used in the module

func Run added in v0.32.0

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OpenLDAPContainer, error)

Run creates an instance of the OpenLDAP container type

Example
// runOpenLDAPContainer {
ctx := context.Background()

openldapContainer, err := openldap.Run(ctx, "bitnami/openldap:2.6.6")
defer func() {
	if err := testcontainers.TerminateContainer(openldapContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := openldapContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
// connectToOpenLdap {
ctx := context.Background()

openldapContainer, err := openldap.Run(ctx, "bitnami/openldap:2.6.6")
defer func() {
	if err := testcontainers.TerminateContainer(openldapContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connectionString, err := openldapContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

client, err := ldap.DialURL(connectionString)
if err != nil {
	log.Printf("failed to connect to LDAP server: %s", err)
	return
}
defer client.Close()

// First bind with a read only user
err = client.Bind("cn=admin,dc=example,dc=org", "adminpassword")
if err != nil {
	log.Printf("failed to bind to LDAP server: %s", err)
	return
}

// Search for the given username
searchRequest := ldap.NewSearchRequest(
	"dc=example,dc=org",
	ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
	"(&(objectClass=organizationalPerson)(uid=user01))",
	[]string{"dn"},
	nil,
)

sr, err := client.Search(searchRequest)
if err != nil {
	log.Printf("failed to search LDAP server: %s", err)
	return
}

if len(sr.Entries) != 1 {
	log.Print("User does not exist or too many entries returned")
	return
}

fmt.Println(sr.Entries[0].DN)
Output:

cn=user01,ou=users,dc=example,dc=org

func RunContainer deprecated

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*OpenLDAPContainer, error)

Deprecated: use Run instead RunContainer creates an instance of the OpenLDAP container type

func (*OpenLDAPContainer) ConnectionString

func (c *OpenLDAPContainer) ConnectionString(ctx context.Context, args ...string) (string, error)

ConnectionString returns the connection string for the OpenLDAP container

func (*OpenLDAPContainer) LoadLdif

func (c *OpenLDAPContainer) LoadLdif(ctx context.Context, ldif []byte) error

LoadLdif loads an ldif file into the OpenLDAP container

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL