2026-01-28 11:05:07 -06:00
|
|
|
// Copyright IBM Corp. 2020, 2025
|
2021-11-28 12:20:46 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-05-19 14:54:35 -07:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-29 09:01:33 -04:00
|
|
|
"context"
|
2020-05-19 14:54:35 -07:00
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/cmd/config"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller"
|
2022-03-16 17:04:07 -04:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2021-12-22 22:53:52 -05:00
|
|
|
|
2022-05-09 12:45:10 -07:00
|
|
|
_ "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets/tcp"
|
2020-05-19 14:54:35 -07:00
|
|
|
)
|
|
|
|
|
|
2021-11-28 12:20:46 -08:00
|
|
|
type option struct {
|
|
|
|
|
tcOptions *controller.TestControllerOpts
|
|
|
|
|
setWithConfigFile bool
|
|
|
|
|
setWithConfigText bool
|
|
|
|
|
setDisableAuthMethodCreation bool
|
|
|
|
|
setDisableDatabaseCreation bool
|
|
|
|
|
setDisableDatabaseDestruction bool
|
|
|
|
|
setDefaultPasswordAuthMethodId bool
|
|
|
|
|
setDefaultOidcAuthMethodId bool
|
2023-04-28 11:35:54 -04:00
|
|
|
setDefaultLdapAuthMethodId bool
|
2021-11-28 12:20:46 -08:00
|
|
|
setDefaultLoginName bool
|
|
|
|
|
setDefaultPassword bool
|
|
|
|
|
setRootKms bool
|
|
|
|
|
setWorkerAuthKms bool
|
|
|
|
|
setRecoveryKms bool
|
|
|
|
|
setDatabaseUrl bool
|
|
|
|
|
setEnableTemplatedDatabase bool
|
2023-09-15 14:14:12 -04:00
|
|
|
setEnableEventing bool
|
2021-11-28 12:20:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*option) error
|
|
|
|
|
|
2020-05-19 14:54:35 -07:00
|
|
|
func getOpts(opt ...Option) (*controller.TestControllerOpts, error) {
|
|
|
|
|
opts := &option{
|
|
|
|
|
tcOptions: &controller.TestControllerOpts{},
|
|
|
|
|
}
|
|
|
|
|
for _, o := range opt {
|
|
|
|
|
if err := o(opts); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-28 12:20:46 -08:00
|
|
|
opts.tcOptions.DisableDatabaseTemplate = !opts.setEnableTemplatedDatabase
|
2020-05-19 14:54:35 -07:00
|
|
|
if opts.setWithConfigFile && opts.setWithConfigText {
|
|
|
|
|
return nil, fmt.Errorf("Cannot provide both WithConfigFile and WithConfigText")
|
|
|
|
|
}
|
2020-08-26 23:27:46 -04:00
|
|
|
var setDbParams bool
|
2021-11-28 12:20:46 -08:00
|
|
|
if opts.setDefaultPasswordAuthMethodId ||
|
|
|
|
|
opts.setDefaultOidcAuthMethodId ||
|
2023-04-28 11:35:54 -04:00
|
|
|
opts.setDefaultLdapAuthMethodId ||
|
2021-11-28 12:20:46 -08:00
|
|
|
opts.setDefaultLoginName ||
|
|
|
|
|
opts.setDefaultPassword {
|
2020-08-26 23:27:46 -04:00
|
|
|
setDbParams = true
|
|
|
|
|
}
|
|
|
|
|
if opts.setDisableAuthMethodCreation {
|
|
|
|
|
if setDbParams {
|
|
|
|
|
return nil, fmt.Errorf("Cannot both disable auth method creation and provide auth method parameters")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if opts.setDisableDatabaseCreation {
|
|
|
|
|
if setDbParams {
|
|
|
|
|
return nil, fmt.Errorf("Cannot both disable database creation and provide auth method parameters")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 14:54:35 -07:00
|
|
|
return opts.tcOptions, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// WithConfigFile provides the given ConfigFile to the built TestController.
|
|
|
|
|
// This option cannot be used if WithConfigText is used.
|
2020-05-19 14:54:35 -07:00
|
|
|
func WithConfigFile(f string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
if c.setWithConfigFile {
|
|
|
|
|
return fmt.Errorf("WithConfigFile provided more than once.")
|
|
|
|
|
}
|
|
|
|
|
c.setWithConfigFile = true
|
2022-09-29 09:01:33 -04:00
|
|
|
cfg, err := config.Load(context.Background(), []string{f}, "")
|
2020-05-19 14:54:35 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
c.tcOptions.Config = cfg
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// WithConfigText configures the TestController sets up the Controller using the
|
|
|
|
|
// provided config text. This option cannot be used if WithConfigFile is used.
|
2020-05-19 14:54:35 -07:00
|
|
|
func WithConfigText(ct string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
if c.setWithConfigText {
|
|
|
|
|
return fmt.Errorf("WithConfigText provided more than once.")
|
|
|
|
|
}
|
|
|
|
|
c.setWithConfigText = true
|
|
|
|
|
cfg, err := config.Parse(ct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
c.tcOptions.Config = cfg
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 23:27:46 -04:00
|
|
|
// DisableAuthMethodCreation skips creating a default auth method
|
|
|
|
|
func DisableAuthMethodCreation() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDisableAuthMethodCreation = true
|
|
|
|
|
c.tcOptions.DisableAuthMethodCreation = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:16:59 -05:00
|
|
|
// DisableDatabaseCreation skips creating a database in docker and allows one to
|
|
|
|
|
// be provided through a tcOptions.
|
2020-05-19 14:54:35 -07:00
|
|
|
func DisableDatabaseCreation() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDisableDatabaseCreation = true
|
|
|
|
|
c.tcOptions.DisableDatabaseCreation = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 10:10:06 -04:00
|
|
|
// DisableDatabaseCreation skips creating a database in docker and allows one to
|
|
|
|
|
// be provided through a tcOptions.
|
|
|
|
|
func DisableDatabaseDestruction() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDisableDatabaseDestruction = true
|
|
|
|
|
c.tcOptions.DisableDatabaseDestruction = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 19:43:36 -05:00
|
|
|
func WithDefaultPasswordAuthMethodId(id string) Option {
|
2020-08-12 08:27:19 -07:00
|
|
|
return func(c *option) error {
|
2021-05-26 19:43:36 -05:00
|
|
|
c.setDefaultPasswordAuthMethodId = true
|
|
|
|
|
c.tcOptions.DefaultPasswordAuthMethodId = id
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithDefaultOidcAuthMethodId(id string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultOidcAuthMethodId = true
|
|
|
|
|
c.tcOptions.DefaultOidcAuthMethodId = id
|
2020-08-12 08:27:19 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 11:35:54 -04:00
|
|
|
func WithDefaultLdapAuthMethodId(id string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultLdapAuthMethodId = true
|
|
|
|
|
c.tcOptions.DefaultLdapAuthMethodId = id
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 08:27:19 -07:00
|
|
|
func WithDefaultLoginName(ln string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultLoginName = true
|
|
|
|
|
c.tcOptions.DefaultLoginName = ln
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithDefaultPassword(pw string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDefaultPassword = true
|
|
|
|
|
c.tcOptions.DefaultPassword = pw
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 09:20:09 -04:00
|
|
|
func WithRootKms(wrapper wrapping.Wrapper) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setRootKms = true
|
|
|
|
|
c.tcOptions.RootKms = wrapper
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithWorkerAuthKms(wrapper wrapping.Wrapper) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setWorkerAuthKms = true
|
|
|
|
|
c.tcOptions.WorkerAuthKms = wrapper
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 21:53:51 -04:00
|
|
|
func WithRecoveryKms(wrapper wrapping.Wrapper) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setRecoveryKms = true
|
|
|
|
|
c.tcOptions.RecoveryKms = wrapper
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 09:15:54 -04:00
|
|
|
func WithDatabaseUrl(url string) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setDatabaseUrl = true
|
|
|
|
|
c.tcOptions.DatabaseUrl = url
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:20:46 -08:00
|
|
|
// WithEnableTemplatedDatabase enables usage of a local database test instance
|
|
|
|
|
// with a template. Generally outside tests will probably want to use the
|
|
|
|
|
// non-templated database, so this defaults to enabling instead of disabling.
|
|
|
|
|
func WithEnableTemplatedDatabase(enable bool) Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setEnableTemplatedDatabase = enable
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 14:14:12 -04:00
|
|
|
func WithEnableEventing() Option {
|
|
|
|
|
return func(c *option) error {
|
|
|
|
|
c.setEnableEventing = true
|
|
|
|
|
c.tcOptions.EnableEventing = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 12:20:46 -08:00
|
|
|
type TestController struct {
|
|
|
|
|
*controller.TestController
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 14:54:35 -07:00
|
|
|
// NewTestController blocks until a new TestController is created, returns the url for the TestController and a function
|
|
|
|
|
// that can be called to tear down the controller after it has been used for testing.
|
2020-05-21 12:12:08 -04:00
|
|
|
func NewTestController(t *testing.T, opt ...Option) *TestController {
|
2020-05-19 14:54:35 -07:00
|
|
|
conf, err := getOpts(opt...)
|
|
|
|
|
if err != nil {
|
2020-05-20 19:11:39 -04:00
|
|
|
t.Fatalf("Couldn't create TestController: %v", err)
|
2020-05-19 14:54:35 -07:00
|
|
|
}
|
|
|
|
|
tc := controller.NewTestController(t, conf)
|
2020-05-21 12:12:08 -04:00
|
|
|
return &TestController{TestController: tc}
|
|
|
|
|
}
|