Files
threshold/testing/controller/controller.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

232 lines
6.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package controller
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/boundary/internal/cmd/config"
"github.com/hashicorp/boundary/internal/daemon/controller"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
_ "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets/tcp"
)
type option struct {
tcOptions *controller.TestControllerOpts
setWithConfigFile bool
setWithConfigText bool
setDisableAuthMethodCreation bool
setDisableDatabaseCreation bool
setDisableDatabaseDestruction bool
setDefaultPasswordAuthMethodId bool
setDefaultOidcAuthMethodId bool
setDefaultLdapAuthMethodId bool
setDefaultLoginName bool
setDefaultPassword bool
setRootKms bool
setWorkerAuthKms bool
setRecoveryKms bool
setDatabaseUrl bool
setEnableTemplatedDatabase bool
}
type Option func(*option) error
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
}
}
opts.tcOptions.DisableDatabaseTemplate = !opts.setEnableTemplatedDatabase
if opts.setWithConfigFile && opts.setWithConfigText {
return nil, fmt.Errorf("Cannot provide both WithConfigFile and WithConfigText")
}
var setDbParams bool
if opts.setDefaultPasswordAuthMethodId ||
opts.setDefaultOidcAuthMethodId ||
opts.setDefaultLdapAuthMethodId ||
opts.setDefaultLoginName ||
opts.setDefaultPassword {
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")
}
}
return opts.tcOptions, nil
}
// WithConfigFile provides the given ConfigFile to the built TestController.
// This option cannot be used if WithConfigText is used.
func WithConfigFile(f string) Option {
return func(c *option) error {
if c.setWithConfigFile {
return fmt.Errorf("WithConfigFile provided more than once.")
}
c.setWithConfigFile = true
cfg, err := config.Load(context.Background(), []string{f}, "")
if err != nil {
return err
}
c.tcOptions.Config = cfg
return nil
}
}
// WithConfigText configures the TestController sets up the Controller using the
// provided config text. This option cannot be used if WithConfigFile is used.
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
}
}
// DisableAuthMethodCreation skips creating a default auth method
func DisableAuthMethodCreation() Option {
return func(c *option) error {
c.setDisableAuthMethodCreation = true
c.tcOptions.DisableAuthMethodCreation = true
return nil
}
}
// DisableDatabaseCreation skips creating a database in docker and allows one to
// be provided through a tcOptions.
func DisableDatabaseCreation() Option {
return func(c *option) error {
c.setDisableDatabaseCreation = true
c.tcOptions.DisableDatabaseCreation = true
return nil
}
}
// 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
}
}
func WithDefaultPasswordAuthMethodId(id string) Option {
return func(c *option) error {
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
return nil
}
}
func WithDefaultLdapAuthMethodId(id string) Option {
return func(c *option) error {
c.setDefaultLdapAuthMethodId = true
c.tcOptions.DefaultLdapAuthMethodId = id
return nil
}
}
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
}
}
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
}
}
func WithRecoveryKms(wrapper wrapping.Wrapper) Option {
return func(c *option) error {
c.setRecoveryKms = true
c.tcOptions.RecoveryKms = wrapper
return nil
}
}
func WithDatabaseUrl(url string) Option {
return func(c *option) error {
c.setDatabaseUrl = true
c.tcOptions.DatabaseUrl = url
return nil
}
}
// 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
}
}
type TestController struct {
*controller.TestController
}
// 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.
func NewTestController(t *testing.T, opt ...Option) *TestController {
conf, err := getOpts(opt...)
if err != nil {
t.Fatalf("Couldn't create TestController: %v", err)
}
tc := controller.NewTestController(t, conf)
return &TestController{TestController: tc}
}