2018-06-13 08:40:03 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
2019-11-01 13:20:26 +00:00
|
|
|
"crypto/x509"
|
2018-06-14 21:20:10 +00:00
|
|
|
"fmt"
|
2018-06-13 08:40:03 +00:00
|
|
|
"io/ioutil"
|
2019-09-23 17:04:40 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-03-01 18:24:32 +00:00
|
|
|
"sync"
|
2018-06-13 08:40:03 +00:00
|
|
|
"testing"
|
2018-06-14 21:20:10 +00:00
|
|
|
"time"
|
2018-06-13 08:40:03 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2019-01-08 16:09:22 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-09-23 17:04:40 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2018-06-14 17:56:17 +00:00
|
|
|
vaultapi "github.com/hashicorp/vault/api"
|
2018-06-13 08:40:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-01-08 16:09:22 +00:00
|
|
|
func TestVaultCAProvider_VaultTLSConfig(t *testing.T) {
|
|
|
|
config := &structs.VaultCAProviderConfig{
|
|
|
|
CAFile: "/capath/ca.pem",
|
|
|
|
CAPath: "/capath/",
|
|
|
|
CertFile: "/certpath/cert.pem",
|
|
|
|
KeyFile: "/certpath/key.pem",
|
|
|
|
TLSServerName: "server.name",
|
|
|
|
TLSSkipVerify: true,
|
|
|
|
}
|
|
|
|
tlsConfig := vaultTLSConfig(config)
|
|
|
|
require := require.New(t)
|
|
|
|
require.Equal(config.CAFile, tlsConfig.CACert)
|
|
|
|
require.Equal(config.CAPath, tlsConfig.CAPath)
|
|
|
|
require.Equal(config.CertFile, tlsConfig.ClientCert)
|
|
|
|
require.Equal(config.KeyFile, tlsConfig.ClientKey)
|
|
|
|
require.Equal(config.TLSServerName, tlsConfig.TLSServerName)
|
|
|
|
require.Equal(config.TLSSkipVerify, tlsConfig.Insecure)
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:36:22 +00:00
|
|
|
func TestVaultCAProvider_SecondaryActiveIntermediate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
skipIfVaultNotPresent(t)
|
|
|
|
|
|
|
|
provider, testVault := testVaultProviderWithConfig(t, false, nil)
|
|
|
|
defer testVault.Stop()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cert, err := provider.ActiveIntermediate()
|
|
|
|
require.Empty(cert)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
func TestVaultCAProvider_Bootstrap(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-23 21:02:13 +00:00
|
|
|
skipIfVaultNotPresent(t)
|
2019-09-23 17:04:40 +00:00
|
|
|
|
|
|
|
provider, testVault := testVaultProvider(t)
|
|
|
|
defer testVault.Stop()
|
|
|
|
client := testVault.client
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
certFunc func() (string, error)
|
|
|
|
backendPath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
certFunc: provider.ActiveRoot,
|
|
|
|
backendPath: "pki-root/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
certFunc: provider.ActiveIntermediate,
|
|
|
|
backendPath: "pki-intermediate/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the root and intermediate certs match the ones in the vault backends
|
|
|
|
for _, tc := range cases {
|
|
|
|
cert, err := tc.certFunc()
|
|
|
|
require.NoError(err)
|
2018-06-14 17:56:17 +00:00
|
|
|
req := client.NewRequest("GET", "/v1/"+tc.backendPath+"ca/pem")
|
2018-06-13 08:40:03 +00:00
|
|
|
resp, err := client.RawRequest(req)
|
|
|
|
require.NoError(err)
|
|
|
|
bytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(cert, string(bytes))
|
|
|
|
|
2018-06-14 17:56:17 +00:00
|
|
|
// Should be a valid CA cert
|
2018-06-13 08:40:03 +00:00
|
|
|
parsed, err := connect.ParseCert(cert)
|
|
|
|
require.NoError(err)
|
2018-06-14 17:56:17 +00:00
|
|
|
require.True(parsed.IsCA)
|
2018-06-14 21:20:10 +00:00
|
|
|
require.Len(parsed.URIs, 1)
|
2019-11-11 17:11:54 +00:00
|
|
|
require.Equal(fmt.Sprintf("spiffe://%s.consul", provider.clusterID), parsed.URIs[0].String())
|
2018-06-14 21:20:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
func assertCorrectKeyType(t *testing.T, want, certPEM string) {
|
|
|
|
t.Helper()
|
2018-06-14 21:20:10 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
cert, err := connect.ParseCert(certPEM)
|
|
|
|
require.NoError(t, err)
|
2018-06-14 21:20:10 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
switch want {
|
|
|
|
case "ec":
|
|
|
|
require.Equal(t, x509.ECDSA, cert.PublicKeyAlgorithm)
|
|
|
|
case "rsa":
|
|
|
|
require.Equal(t, x509.RSA, cert.PublicKeyAlgorithm)
|
|
|
|
default:
|
|
|
|
t.Fatal("test doesn't support key type")
|
2018-06-14 21:20:10 +00:00
|
|
|
}
|
2019-11-01 13:20:26 +00:00
|
|
|
}
|
2018-06-14 21:20:10 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
func TestVaultCAProvider_SignLeaf(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-06-14 21:20:10 +00:00
|
|
|
|
2020-03-23 21:02:13 +00:00
|
|
|
skipIfVaultNotPresent(t)
|
2018-06-14 21:20:10 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
for _, tc := range KeyTestCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.Desc, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
provider, testVault := testVaultProviderWithConfig(t, true, map[string]interface{}{
|
|
|
|
"LeafCertTTL": "1h",
|
|
|
|
"PrivateKeyType": tc.KeyType,
|
|
|
|
"PrivateKeyBits": tc.KeyBits,
|
|
|
|
})
|
|
|
|
defer testVault.Stop()
|
|
|
|
|
|
|
|
spiffeService := &connect.SpiffeIDService{
|
|
|
|
Host: "node1",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Service: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPEM, err := provider.ActiveRoot()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.KeyType, rootPEM)
|
|
|
|
|
|
|
|
intPEM, err := provider.ActiveIntermediate()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.KeyType, intPEM)
|
|
|
|
|
|
|
|
// Generate a leaf cert for the service.
|
|
|
|
var firstSerial uint64
|
|
|
|
{
|
|
|
|
raw, _ := connect.TestCSR(t, spiffeService)
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(raw)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
cert, err := provider.Sign(csr)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
parsed, err := connect.ParseCert(cert)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(parsed.URIs[0], spiffeService.URI())
|
|
|
|
firstSerial = parsed.SerialNumber.Uint64()
|
|
|
|
|
|
|
|
// Ensure the cert is valid now and expires within the correct limit.
|
|
|
|
now := time.Now()
|
|
|
|
require.True(parsed.NotAfter.Sub(now) < time.Hour)
|
|
|
|
require.True(parsed.NotBefore.Before(now))
|
|
|
|
|
|
|
|
// Make sure we can validate the cert as expected.
|
|
|
|
require.NoError(connect.ValidateLeaf(rootPEM, cert, []string{intPEM}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new cert for another service and make sure
|
|
|
|
// the serial number is unique.
|
|
|
|
spiffeService.Service = "bar"
|
|
|
|
{
|
|
|
|
raw, _ := connect.TestCSR(t, spiffeService)
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(raw)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
cert, err := provider.Sign(csr)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
parsed, err := connect.ParseCert(cert)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(parsed.URIs[0], spiffeService.URI())
|
|
|
|
require.NotEqual(firstSerial, parsed.SerialNumber.Uint64())
|
|
|
|
|
|
|
|
// Ensure the cert is valid now and expires within the correct limit.
|
|
|
|
require.True(time.Until(parsed.NotAfter) < time.Hour)
|
|
|
|
require.True(parsed.NotBefore.Before(time.Now()))
|
|
|
|
|
|
|
|
// Make sure we can validate the cert as expected.
|
|
|
|
require.NoError(connect.ValidateLeaf(rootPEM, cert, []string{intPEM}))
|
|
|
|
}
|
|
|
|
})
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 23:25:53 +00:00
|
|
|
|
|
|
|
func TestVaultCAProvider_CrossSignCA(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-23 21:02:13 +00:00
|
|
|
skipIfVaultNotPresent(t)
|
2019-09-23 17:04:40 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
tests := CASigningKeyTypeCases()
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.Desc, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
if tc.SigningKeyType != tc.CSRKeyType {
|
|
|
|
// See https://github.com/hashicorp/vault/issues/7709
|
|
|
|
t.Skip("Vault doesn't support cross-signing different key types yet.")
|
|
|
|
}
|
|
|
|
provider1, testVault1 := testVaultProviderWithConfig(t, true, map[string]interface{}{
|
|
|
|
"LeafCertTTL": "1h",
|
|
|
|
"PrivateKeyType": tc.SigningKeyType,
|
|
|
|
"PrivateKeyBits": tc.SigningKeyBits,
|
|
|
|
})
|
|
|
|
defer testVault1.Stop()
|
|
|
|
|
|
|
|
{
|
|
|
|
rootPEM, err := provider1.ActiveRoot()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.SigningKeyType, rootPEM)
|
|
|
|
|
|
|
|
intPEM, err := provider1.ActiveIntermediate()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.SigningKeyType, intPEM)
|
|
|
|
}
|
|
|
|
|
|
|
|
provider2, testVault2 := testVaultProviderWithConfig(t, true, map[string]interface{}{
|
|
|
|
"LeafCertTTL": "1h",
|
|
|
|
"PrivateKeyType": tc.CSRKeyType,
|
|
|
|
"PrivateKeyBits": tc.CSRKeyBits,
|
|
|
|
})
|
|
|
|
defer testVault2.Stop()
|
|
|
|
|
|
|
|
{
|
|
|
|
rootPEM, err := provider2.ActiveRoot()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.CSRKeyType, rootPEM)
|
|
|
|
|
|
|
|
intPEM, err := provider2.ActiveIntermediate()
|
|
|
|
require.NoError(err)
|
|
|
|
assertCorrectKeyType(t, tc.CSRKeyType, intPEM)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCrossSignProviders(t, provider1, provider2)
|
|
|
|
})
|
|
|
|
}
|
2018-06-15 23:25:53 +00:00
|
|
|
}
|
2018-09-13 20:09:07 +00:00
|
|
|
|
|
|
|
func TestVaultProvider_SignIntermediate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-23 21:02:13 +00:00
|
|
|
skipIfVaultNotPresent(t)
|
2019-09-23 17:04:40 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
tests := CASigningKeyTypeCases()
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.Desc, func(t *testing.T) {
|
|
|
|
provider1, testVault1 := testVaultProviderWithConfig(t, true, map[string]interface{}{
|
|
|
|
"LeafCertTTL": "1h",
|
|
|
|
"PrivateKeyType": tc.SigningKeyType,
|
|
|
|
"PrivateKeyBits": tc.SigningKeyBits,
|
|
|
|
})
|
|
|
|
defer testVault1.Stop()
|
|
|
|
|
|
|
|
provider2, testVault2 := testVaultProviderWithConfig(t, false, map[string]interface{}{
|
|
|
|
"LeafCertTTL": "1h",
|
|
|
|
"PrivateKeyType": tc.CSRKeyType,
|
|
|
|
"PrivateKeyBits": tc.CSRKeyBits,
|
|
|
|
})
|
|
|
|
defer testVault2.Stop()
|
|
|
|
|
|
|
|
testSignIntermediateCrossDC(t, provider1, provider2)
|
|
|
|
})
|
|
|
|
}
|
2018-09-13 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVaultProvider_SignIntermediateConsul(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-23 21:02:13 +00:00
|
|
|
skipIfVaultNotPresent(t)
|
2018-09-13 20:09:07 +00:00
|
|
|
|
2018-09-14 23:08:54 +00:00
|
|
|
// primary = Vault, secondary = Consul
|
2019-09-23 17:04:40 +00:00
|
|
|
t.Run("pri=vault,sec=consul", func(t *testing.T) {
|
|
|
|
provider1, testVault1 := testVaultProviderWithConfig(t, true, nil)
|
|
|
|
defer testVault1.Stop()
|
2018-09-13 20:09:07 +00:00
|
|
|
|
2018-09-14 23:08:54 +00:00
|
|
|
conf := testConsulCAConfig()
|
|
|
|
delegate := newMockDelegate(t, conf)
|
2019-11-11 20:30:01 +00:00
|
|
|
provider2 := TestConsulProvider(t, delegate)
|
2019-11-18 14:22:19 +00:00
|
|
|
cfg := testProviderConfig(conf)
|
|
|
|
cfg.IsPrimary = false
|
|
|
|
cfg.Datacenter = "dc2"
|
|
|
|
require.NoError(t, provider2.Configure(cfg))
|
2018-09-14 23:08:54 +00:00
|
|
|
|
|
|
|
testSignIntermediateCrossDC(t, provider1, provider2)
|
2019-09-23 17:04:40 +00:00
|
|
|
})
|
2018-09-14 23:08:54 +00:00
|
|
|
|
|
|
|
// primary = Consul, secondary = Vault
|
2019-09-23 17:04:40 +00:00
|
|
|
t.Run("pri=consul,sec=vault", func(t *testing.T) {
|
2018-09-14 23:08:54 +00:00
|
|
|
conf := testConsulCAConfig()
|
|
|
|
delegate := newMockDelegate(t, conf)
|
2019-11-11 20:30:01 +00:00
|
|
|
provider1 := TestConsulProvider(t, delegate)
|
2019-11-18 14:22:19 +00:00
|
|
|
require.NoError(t, provider1.Configure(testProviderConfig(conf)))
|
2019-09-23 17:04:40 +00:00
|
|
|
require.NoError(t, provider1.GenerateRoot())
|
2018-09-14 23:08:54 +00:00
|
|
|
|
2020-01-21 20:55:21 +00:00
|
|
|
// Ensure that we don't configure vault to try and mint leafs that
|
|
|
|
// outlive their CA during the test (which hard fails in vault).
|
|
|
|
intermediateCertTTL := getIntermediateCertTTL(t, conf)
|
|
|
|
leafCertTTL := intermediateCertTTL - 4*time.Hour
|
|
|
|
|
|
|
|
overrideConf := map[string]interface{}{
|
|
|
|
"LeafCertTTL": []uint8(leafCertTTL.String()),
|
|
|
|
}
|
|
|
|
|
|
|
|
provider2, testVault2 := testVaultProviderWithConfig(t, false, overrideConf)
|
2019-09-23 17:04:40 +00:00
|
|
|
defer testVault2.Stop()
|
2018-09-14 23:08:54 +00:00
|
|
|
|
|
|
|
testSignIntermediateCrossDC(t, provider1, provider2)
|
2019-09-23 17:04:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:55:21 +00:00
|
|
|
func getIntermediateCertTTL(t *testing.T, caConf *structs.CAConfiguration) time.Duration {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.NotNil(t, caConf)
|
|
|
|
require.NotNil(t, caConf.Config)
|
|
|
|
|
|
|
|
iface, ok := caConf.Config["IntermediateCertTTL"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ttlBytes, ok := iface.([]uint8)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ttlString := string(ttlBytes)
|
|
|
|
|
|
|
|
dur, err := time.ParseDuration(ttlString)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return dur
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:04:40 +00:00
|
|
|
func testVaultProvider(t *testing.T) (*VaultProvider, *testVaultServer) {
|
|
|
|
return testVaultProviderWithConfig(t, true, nil)
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:22:19 +00:00
|
|
|
func testVaultProviderWithConfig(t *testing.T, isPrimary bool, rawConf map[string]interface{}) (*VaultProvider, *testVaultServer) {
|
2019-09-23 17:04:40 +00:00
|
|
|
testVault, err := runTestVault()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2018-09-14 23:08:54 +00:00
|
|
|
}
|
2019-09-23 17:04:40 +00:00
|
|
|
|
|
|
|
testVault.WaitUntilReady(t)
|
|
|
|
|
|
|
|
conf := map[string]interface{}{
|
|
|
|
"Address": testVault.addr,
|
|
|
|
"Token": testVault.rootToken,
|
|
|
|
"RootPKIPath": "pki-root/",
|
|
|
|
"IntermediatePKIPath": "pki-intermediate/",
|
|
|
|
// Tests duration parsing after msgpack type mangling during raft apply.
|
|
|
|
"LeafCertTTL": []uint8("72h"),
|
|
|
|
}
|
|
|
|
for k, v := range rawConf {
|
|
|
|
conf[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := &VaultProvider{}
|
|
|
|
|
2019-11-18 14:22:19 +00:00
|
|
|
cfg := ProviderConfig{
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IsPrimary: true,
|
|
|
|
RawConfig: conf,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isPrimary {
|
|
|
|
cfg.IsPrimary = false
|
|
|
|
cfg.Datacenter = "dc2"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := provider.Configure(cfg); err != nil {
|
2019-09-23 17:04:40 +00:00
|
|
|
testVault.Stop()
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-11-18 14:22:19 +00:00
|
|
|
if isPrimary {
|
2019-09-23 17:04:40 +00:00
|
|
|
if err = provider.GenerateRoot(); err != nil {
|
|
|
|
testVault.Stop()
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := provider.GenerateIntermediate(); err != nil {
|
|
|
|
testVault.Stop()
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return provider, testVault
|
|
|
|
}
|
|
|
|
|
2020-03-24 19:16:13 +00:00
|
|
|
// skipIfVaultNotPresent skips the test if the vault binary is not in PATH.
|
|
|
|
//
|
|
|
|
// These tests may be skipped in CI. They are run as part of a separate
|
|
|
|
// integration test suite.
|
2020-03-23 21:02:13 +00:00
|
|
|
func skipIfVaultNotPresent(t *testing.T) {
|
2019-09-23 17:04:40 +00:00
|
|
|
vaultBinaryName := os.Getenv("VAULT_BINARY_NAME")
|
|
|
|
if vaultBinaryName == "" {
|
|
|
|
vaultBinaryName = "vault"
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := exec.LookPath(vaultBinaryName)
|
|
|
|
if err != nil || path == "" {
|
|
|
|
t.Skipf("%q not found on $PATH - download and install to run this test", vaultBinaryName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTestVault() (*testVaultServer, error) {
|
|
|
|
vaultBinaryName := os.Getenv("VAULT_BINARY_NAME")
|
|
|
|
if vaultBinaryName == "" {
|
|
|
|
vaultBinaryName = "vault"
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := exec.LookPath(vaultBinaryName)
|
|
|
|
if err != nil || path == "" {
|
|
|
|
return nil, fmt.Errorf("%q not found on $PATH", vaultBinaryName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := freeport.MustTake(2)
|
|
|
|
returnPortsFn := func() {
|
|
|
|
freeport.Return(ports)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
clientAddr = fmt.Sprintf("127.0.0.1:%d", ports[0])
|
|
|
|
clusterAddr = fmt.Sprintf("127.0.0.1:%d", ports[1])
|
|
|
|
)
|
|
|
|
|
|
|
|
const token = "root"
|
|
|
|
|
|
|
|
client, err := vaultapi.NewClient(&vaultapi.Config{
|
|
|
|
Address: "http://" + clientAddr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
returnPortsFn()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"server",
|
|
|
|
"-dev",
|
|
|
|
"-dev-root-token-id",
|
|
|
|
token,
|
|
|
|
"-dev-listen-address",
|
|
|
|
clientAddr,
|
|
|
|
"-address",
|
|
|
|
clusterAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(vaultBinaryName, args...)
|
|
|
|
cmd.Stdout = ioutil.Discard
|
|
|
|
cmd.Stderr = ioutil.Discard
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
returnPortsFn()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testVaultServer{
|
|
|
|
rootToken: token,
|
|
|
|
addr: "http://" + clientAddr,
|
|
|
|
cmd: cmd,
|
|
|
|
client: client,
|
|
|
|
returnPortsFn: returnPortsFn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testVaultServer struct {
|
|
|
|
rootToken string
|
|
|
|
addr string
|
|
|
|
cmd *exec.Cmd
|
|
|
|
client *vaultapi.Client
|
|
|
|
|
|
|
|
// returnPortsFn will put the ports claimed for the test back into the
|
|
|
|
returnPortsFn func()
|
|
|
|
}
|
|
|
|
|
2020-03-24 19:16:13 +00:00
|
|
|
var printedVaultVersion sync.Once
|
|
|
|
|
2019-09-23 17:04:40 +00:00
|
|
|
func (v *testVaultServer) WaitUntilReady(t *testing.T) {
|
|
|
|
var version string
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
resp, err := v.client.Sys().Health()
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !resp.Initialized {
|
|
|
|
r.Fatalf("vault server is not initialized")
|
|
|
|
}
|
|
|
|
if resp.Sealed {
|
|
|
|
r.Fatalf("vault server is sealed")
|
|
|
|
}
|
|
|
|
version = resp.Version
|
|
|
|
})
|
|
|
|
printedVaultVersion.Do(func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "[INFO] agent/connect/ca: testing with vault server version: %s\n", version)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *testVaultServer) Stop() error {
|
|
|
|
// There was no process
|
|
|
|
if v.cmd == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.cmd.Process != nil {
|
|
|
|
if err := v.cmd.Process.Signal(os.Interrupt); err != nil {
|
|
|
|
return fmt.Errorf("failed to kill vault server: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the process to exit to be sure that the data dir can be
|
|
|
|
// deleted on all platforms.
|
|
|
|
if err := v.cmd.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.returnPortsFn != nil {
|
|
|
|
v.returnPortsFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-09-13 20:09:07 +00:00
|
|
|
}
|