open-vault/vault/external_tests/api/api_integration_test.go

82 lines
2.6 KiB
Go
Raw Normal View History

package api
import (
2017-09-02 22:48:48 +00:00
"encoding/base64"
"testing"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
2017-09-02 22:48:48 +00:00
"github.com/hashicorp/vault/audit"
auditFile "github.com/hashicorp/vault/builtin/audit/file"
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
2017-09-02 22:48:48 +00:00
"github.com/hashicorp/vault/builtin/logical/database"
"github.com/hashicorp/vault/builtin/logical/pki"
"github.com/hashicorp/vault/builtin/logical/transit"
2018-11-07 01:21:24 +00:00
"github.com/hashicorp/vault/helper/builtinplugins"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
)
2017-09-02 22:48:48 +00:00
// testVaultServer creates a test vault cluster and returns a configured API
// client and closer function.
func testVaultServer(t testing.TB) (*api.Client, func()) {
t.Helper()
client, _, closer := testVaultServerUnseal(t)
return client, closer
}
2017-09-02 22:48:48 +00:00
// testVaultServerUnseal creates a test vault cluster and returns a configured
// API client, list of unseal keys (as strings), and a closer function.
func testVaultServerUnseal(t testing.TB) (*api.Client, []string, func()) {
t.Helper()
return testVaultServerCoreConfig(t, &vault.CoreConfig{
DisableMlock: true,
DisableCache: true,
Logger: log.NewNullLogger(),
2017-09-02 22:48:48 +00:00
CredentialBackends: map[string]logical.Factory{
"userpass": credUserpass.Factory,
},
AuditBackends: map[string]audit.Factory{
"file": auditFile.Factory,
},
LogicalBackends: map[string]logical.Factory{
"database": database.Factory,
"generic-leased": vault.LeasedPassthroughBackendFactory,
"pki": pki.Factory,
"transit": transit.Factory,
},
2018-11-07 01:21:24 +00:00
BuiltinRegistry: builtinplugins.Registry,
2017-09-02 22:48:48 +00:00
})
}
2017-09-02 22:48:48 +00:00
// testVaultServerCoreConfig creates a new vault cluster with the given core
// configuration. This is a lower-level test helper.
func testVaultServerCoreConfig(t testing.TB, coreConfig *vault.CoreConfig) (*api.Client, []string, func()) {
t.Helper()
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: http.Handler,
})
cluster.Start()
2017-09-02 22:48:48 +00:00
// Make it easy to get access to the active
2017-07-11 03:47:03 +00:00
core := cluster.Cores[0].Core
vault.TestWaitActive(t, core)
2017-09-02 22:48:48 +00:00
// Get the client already setup for us!
2017-07-11 03:47:03 +00:00
client := cluster.Cores[0].Client
client.SetToken(cluster.RootToken)
2017-09-02 22:48:48 +00:00
// Convert the unseal keys to base64 encoded, since these are how the user
// will get them.
unsealKeys := make([]string, len(cluster.BarrierKeys))
for i := range unsealKeys {
unsealKeys[i] = base64.StdEncoding.EncodeToString(cluster.BarrierKeys[i])
}
return client, unsealKeys, func() { defer cluster.Cleanup() }
}