vault

package module
v0.0.0-...-404ae92 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithInitCommand

func WithInitCommand(commands ...string) testcontainers.CustomizeRequestOption

WithInitCommand is an option function that adds a set of initialization commands to the Vault's configuration

func WithToken

func WithToken(token string) testcontainers.CustomizeRequestOption

WithToken is a container option function that sets the root token for the Vault

Types

type VaultContainer

type VaultContainer struct {
	testcontainers.Container
}

VaultContainer represents the vault container type used in the module

func RunContainer

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

RunContainer creates an instance of the vault container type

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/samkhawase/testcontainers-go/modules/vault"
)

func main() {
	// runVaultContainer {
	ctx := context.Background()

	vaultContainer, err := vault.RunContainer(ctx)
	if err != nil {
		log.Fatalf("failed to start container: %s", err)
	}

	// Clean up the container
	defer func() {
		if err := vaultContainer.Terminate(ctx); err != nil {
			log.Fatalf("failed to terminate container: %s", err)
		}
	}()
	// }

	state, err := vaultContainer.State(ctx)
	if err != nil {
		log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
	}

	fmt.Println(state.Running)

}
Output:

true
Example (WithInitCommand)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/samkhawase/testcontainers-go/modules/vault"
)

func main() {
	// runVaultContainerWithInitCommand {
	ctx := context.Background()

	vaultContainer, err := vault.RunContainer(ctx, vault.WithToken("MyToKeN"), vault.WithInitCommand(
		"auth enable approle",                         // Enable the approle auth method
		"secrets disable secret",                      // Disable the default secret engine
		"secrets enable -version=1 -path=secret kv",   // Enable the kv secret engine at version 1
		"write --force auth/approle/role/myrole",      // Create a role
		"write secret/testing top_secret=password123", // Create a secret
	))
	if err != nil {
		log.Fatalf("failed to start container: %s", err)
	}

	// Clean up the container
	defer func() {
		if err := vaultContainer.Terminate(ctx); err != nil {
			log.Fatalf("failed to terminate container: %s", err)
		}
	}()
	// }

	state, err := vaultContainer.State(ctx)
	if err != nil {
		log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
	}

	fmt.Println(state.Running)

}
Output:

true
Example (WithToken)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/samkhawase/testcontainers-go/exec"
	"github.com/samkhawase/testcontainers-go/modules/vault"
)

func main() {
	// runVaultContainerWithToken {
	ctx := context.Background()

	vaultContainer, err := vault.RunContainer(ctx, vault.WithToken("MyToKeN"))
	if err != nil {
		log.Fatalf("failed to start container: %s", err)
	}

	// Clean up the container
	defer func() {
		if err := vaultContainer.Terminate(ctx); err != nil {
			log.Fatalf("failed to terminate container: %s", err)
		}
	}()
	// }

	state, err := vaultContainer.State(ctx)
	if err != nil {
		log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
	}

	fmt.Println(state.Running)

	cmds := []string{
		"vault", "kv", "put", "secret/test", "value=123",
	}
	exitCode, _, err := vaultContainer.Exec(ctx, cmds, exec.Multiplexed())
	if err != nil {
		log.Fatalf("failed to execute command: %s", err)
	}

	fmt.Println(exitCode)

}
Output:

true
0

func (*VaultContainer) HttpHostAddress

func (v *VaultContainer) HttpHostAddress(ctx context.Context) (string, error)

HttpHostAddress returns the http host address of Vault. It returns a string with the format http://<host>:<port>

Jump to

Keyboard shortcuts

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