Files
terraform-provider-threshold/internal/provider/provider_test.go
T
Jeff MalnickandLouis Ruch 4f0509c55d Add credential library resource for Vault (#114)
* Integrate with Vault to retrieve and manage per session credentials

Co-authored-by: Louis Ruch <louisruch@gmail.com>
2021-06-22 21:07:07 -07:00

140 lines
3.1 KiB
Go

package provider
import (
"crypto/rand"
"encoding/base64"
"fmt"
"strings"
"testing"
"github.com/hashicorp/boundary/testing/controller"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-kms-wrapping/wrappers/aead"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
tcLoginName = "testuser"
tcPassword = "passpass"
tcPAUM = "ampw_0000000000"
tcConfig = []controller.Option{
controller.WithDefaultPasswordAuthMethodId(tcPAUM),
controller.WithDefaultLoginName(tcLoginName),
controller.WithDefaultPassword(tcPassword),
}
tcRecoveryKey = "7xtkEoS5EXPbgynwd+dDLHopaCqK8cq0Rpep4eooaTs="
)
func providerFactories(p **schema.Provider) map[string]func() (*schema.Provider, error) {
// TODO: eventually rework this to real factories...
*p = New()
return map[string]func() (*schema.Provider, error){
"boundary": func() (*schema.Provider, error) {
return *p, nil
},
}
}
func testWrapper(t *testing.T, key string) wrapping.Wrapper {
var keyBytes []byte
switch key {
case "":
keyBytes = make([]byte, 32)
n, err := rand.Read(keyBytes)
if err != nil {
t.Fatal(err)
}
if n != 32 {
t.Fatal(n)
}
key = base64.StdEncoding.EncodeToString(keyBytes)
default:
var err error
keyBytes, err = base64.StdEncoding.DecodeString(key)
if err != nil {
t.Fatal(err)
}
}
wrapper := aead.NewWrapper(nil)
_, err := wrapper.SetConfig(map[string]string{
"key_id": key,
})
if err != nil {
t.Fatal(err)
}
if err := wrapper.SetAESGCMKeyBytes(keyBytes); err != nil {
t.Fatal(err)
}
return wrapper
}
func testConfig(url string, res ...string) string {
provider := fmt.Sprintf(`
provider "boundary" {
addr = "%s"
auth_method_id = "%s"
password_auth_method_login_name = "%s"
password_auth_method_password = "%s"
}`, url, tcPAUM, tcLoginName, tcPassword)
c := []string{provider}
c = append(c, res...)
return strings.Join(c, "\n")
}
func testConfigWithToken(url, token string, res ...string) string {
provider := fmt.Sprintf(`
provider "boundary" {
addr = "%s"
token = "%s"
}`, url, token)
c := []string{provider}
c = append(c, res...)
return strings.Join(c, "\n")
}
func testConfigWithRecovery(url string, res ...string) string {
provider := fmt.Sprintf(`
provider "boundary" {
addr = "%s"
auth_method_id = "%s"
password_auth_method_login_name = "%s"
password_auth_method_password = "%s"
recovery_kms_hcl = <<DOC
kms "aead" {
purpose = ["recovery", "config"]
aead_type = "aes-gcm"
key = "7xtkEoS5EXPbgynwd+dDLHopaCqK8cq0Rpep4eooaTs="
key_id = "global_recovery"
}
DOC
}`, url, tcPAUM, tcLoginName, tcPassword)
c := []string{provider}
c = append(c, res...)
return strings.Join(c, "\n")
}
func importStep(name string, ignore ...string) resource.TestStep {
step := resource.TestStep{
ResourceName: name,
ImportState: true,
ImportStateVerify: true,
}
if len(ignore) > 0 {
step.ImportStateVerifyIgnore = ignore
}
return step
}
func TestProvider(t *testing.T) {
if err := New().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}