2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
2021-12-08 18:12:07 +00:00
|
|
|
"errors"
|
2019-11-01 13:20:26 +00:00
|
|
|
"fmt"
|
2022-11-10 16:26:01 +00:00
|
|
|
"io"
|
2020-09-30 19:31:21 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-02-28 21:13:53 +00:00
|
|
|
"strings"
|
2020-09-30 19:31:21 +00:00
|
|
|
"sync"
|
2019-11-01 13:20:26 +00:00
|
|
|
|
2021-06-21 21:51:37 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-12-06 16:06:36 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2021-06-21 21:51:37 +00:00
|
|
|
vaultapi "github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2022-12-06 16:06:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-21 21:51:37 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2020-09-30 19:31:21 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-11-01 13:20:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// KeyTestCases is a list of the important CA key types that we should test
|
|
|
|
// against when signing. For now leaf keys are always EC P256 but CA can be EC
|
|
|
|
// (any NIST curve) or RSA (2048, 4096). Providers must be able to complete all
|
|
|
|
// signing operations with both types that includes:
|
|
|
|
// - Sign must be able to sign EC P256 leaf with all these types of CA key
|
|
|
|
// - CrossSignCA must be able to sign all these types of new CA key with all
|
|
|
|
// these types of old CA key.
|
|
|
|
// - SignIntermediate muse bt able to sign all the types of secondary
|
|
|
|
// intermediate CA key with all these types of primary CA key
|
2022-12-06 16:06:36 +00:00
|
|
|
var KeyTestCases = []KeyTestCase{
|
2019-11-01 13:20:26 +00:00
|
|
|
{
|
|
|
|
Desc: "Default Key Type (EC 256)",
|
|
|
|
KeyType: connect.DefaultPrivateKeyType,
|
|
|
|
KeyBits: connect.DefaultPrivateKeyBits,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Desc: "RSA 2048",
|
|
|
|
KeyType: "rsa",
|
|
|
|
KeyBits: 2048,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-06 16:06:36 +00:00
|
|
|
type KeyTestCase struct {
|
|
|
|
Desc string
|
|
|
|
KeyType string
|
|
|
|
KeyBits int
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
// CASigningKeyTypes is a struct with params for tests that sign one CA CSR with
|
|
|
|
// another CA key.
|
|
|
|
type CASigningKeyTypes struct {
|
|
|
|
Desc string
|
|
|
|
SigningKeyType string
|
|
|
|
SigningKeyBits int
|
|
|
|
CSRKeyType string
|
|
|
|
CSRKeyBits int
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASigningKeyTypeCases returns the cross-product of the important supported CA
|
|
|
|
// key types for generating table tests for CA signing tests (CrossSignCA and
|
|
|
|
// SignIntermediate).
|
|
|
|
func CASigningKeyTypeCases() []CASigningKeyTypes {
|
|
|
|
cases := make([]CASigningKeyTypes, 0, len(KeyTestCases)*len(KeyTestCases))
|
|
|
|
for _, outer := range KeyTestCases {
|
|
|
|
for _, inner := range KeyTestCases {
|
|
|
|
cases = append(cases, CASigningKeyTypes{
|
|
|
|
Desc: fmt.Sprintf("%s-%d signing %s-%d", outer.KeyType, outer.KeyBits,
|
|
|
|
inner.KeyType, inner.KeyBits),
|
|
|
|
SigningKeyType: outer.KeyType,
|
|
|
|
SigningKeyBits: outer.KeyBits,
|
|
|
|
CSRKeyType: inner.KeyType,
|
|
|
|
CSRKeyBits: inner.KeyBits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cases
|
|
|
|
}
|
2019-11-11 20:30:01 +00:00
|
|
|
|
|
|
|
// TestConsulProvider creates a new ConsulProvider, taking care to stub out it's
|
|
|
|
// Logger so that logging calls don't panic. If logging output is important
|
|
|
|
func TestConsulProvider(t testing.T, d ConsulProviderStateDelegate) *ConsulProvider {
|
2022-11-10 16:26:01 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{Output: io.Discard})
|
2021-07-12 18:04:34 +00:00
|
|
|
provider := &ConsulProvider{Delegate: d, logger: logger}
|
2019-11-11 20:30:01 +00:00
|
|
|
return provider
|
|
|
|
}
|
2020-09-30 19:31:21 +00:00
|
|
|
|
2020-10-09 12:02:18 +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.
|
|
|
|
func SkipIfVaultNotPresent(t testing.T) {
|
2022-02-28 21:13:53 +00:00
|
|
|
// Try to safeguard against tests that will never run in CI.
|
|
|
|
// This substring should match the pattern used by the
|
|
|
|
// test-connect-ca-providers CI job.
|
|
|
|
if !strings.Contains(t.Name(), "Vault") {
|
|
|
|
t.Fatalf("test name must contain Vault, otherwise CI will never run it")
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:02:18 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:31:21 +00:00
|
|
|
func NewTestVaultServer(t testing.T) *TestVaultServer {
|
|
|
|
vaultBinaryName := os.Getenv("VAULT_BINARY_NAME")
|
|
|
|
if vaultBinaryName == "" {
|
|
|
|
vaultBinaryName = "vault"
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := exec.LookPath(vaultBinaryName)
|
|
|
|
if err != nil || path == "" {
|
2022-12-06 16:06:36 +00:00
|
|
|
t.Fatalf("%q not found on $PATH", vaultBinaryName)
|
2020-09-30 19:31:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:27:59 +00:00
|
|
|
ports := freeport.GetN(t, 2)
|
2020-09-30 19:31:21 +00:00
|
|
|
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,
|
|
|
|
})
|
2022-12-06 16:06:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-30 19:31:21 +00:00
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"server",
|
|
|
|
"-dev",
|
|
|
|
"-dev-root-token-id",
|
|
|
|
token,
|
|
|
|
"-dev-listen-address",
|
|
|
|
clientAddr,
|
|
|
|
"-address",
|
|
|
|
clusterAddr,
|
2022-12-06 19:15:13 +00:00
|
|
|
// We pass '-dev-no-store-token' to avoid having multiple vaults oddly
|
|
|
|
// interact and fail like this:
|
|
|
|
//
|
2023-04-19 16:19:29 +00:00
|
|
|
// Error initializing Dev mode: rename /.vault-token.tmp /.vault-token: no such file or directory
|
2022-12-06 19:15:13 +00:00
|
|
|
//
|
|
|
|
"-dev-no-store-token",
|
2020-09-30 19:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(vaultBinaryName, args...)
|
2022-11-10 16:26:01 +00:00
|
|
|
cmd.Stdout = io.Discard
|
|
|
|
cmd.Stderr = io.Discard
|
2022-12-06 16:06:36 +00:00
|
|
|
require.NoError(t, cmd.Start())
|
2020-09-30 19:31:21 +00:00
|
|
|
|
|
|
|
testVault := &TestVaultServer{
|
2021-11-27 20:27:59 +00:00
|
|
|
RootToken: token,
|
|
|
|
Addr: "http://" + clientAddr,
|
|
|
|
cmd: cmd,
|
|
|
|
client: client,
|
2020-09-30 19:31:21 +00:00
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
2021-11-12 00:03:52 +00:00
|
|
|
if err := testVault.Stop(); err != nil {
|
2021-12-08 18:12:07 +00:00
|
|
|
t.Logf("failed to stop vault server: %v", err)
|
2021-11-12 00:03:52 +00:00
|
|
|
}
|
2020-09-30 19:31:21 +00:00
|
|
|
})
|
2021-11-12 00:03:52 +00:00
|
|
|
|
2022-12-06 16:06:36 +00:00
|
|
|
testVault.WaitUntilReady(t)
|
|
|
|
|
|
|
|
return testVault
|
2020-09-30 19:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestVaultServer struct {
|
|
|
|
RootToken string
|
|
|
|
Addr string
|
|
|
|
cmd *exec.Cmd
|
|
|
|
client *vaultapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var printedVaultVersion sync.Once
|
2023-05-03 20:30:37 +00:00
|
|
|
var vaultTestVersion string
|
2020-09-30 19:31:21 +00:00
|
|
|
|
|
|
|
func (v *TestVaultServer) Client() *vaultapi.Client {
|
|
|
|
return v.client
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2023-05-03 20:30:37 +00:00
|
|
|
vaultTestVersion = version
|
2020-09-30 19:31:21 +00:00
|
|
|
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 {
|
2021-12-08 18:12:07 +00:00
|
|
|
if err := v.cmd.Process.Signal(os.Interrupt); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
2020-09-30 19:31:21 +00:00
|
|
|
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 {
|
2022-12-06 16:06:36 +00:00
|
|
|
if strings.Contains(err.Error(), "exec: Wait was already called") {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 19:31:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
|
2021-07-01 00:48:29 +00:00
|
|
|
func requireTrailingNewline(t testing.T, leafPEM string) {
|
|
|
|
t.Helper()
|
|
|
|
if len(leafPEM) == 0 {
|
|
|
|
t.Fatalf("cert is empty")
|
|
|
|
}
|
|
|
|
if '\n' != rune(leafPEM[len(leafPEM)-1]) {
|
|
|
|
t.Fatalf("cert do not end with a new line")
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 16:06:36 +00:00
|
|
|
|
|
|
|
// The zero value implies unprivileged.
|
|
|
|
type VaultTokenAttributes struct {
|
|
|
|
RootPath, IntermediatePath string
|
|
|
|
|
|
|
|
ConsulManaged bool
|
|
|
|
VaultManaged bool
|
|
|
|
WithSudo bool
|
|
|
|
|
|
|
|
CustomRules string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *VaultTokenAttributes) DisplayName() string {
|
|
|
|
switch {
|
|
|
|
case a == nil:
|
|
|
|
return "unprivileged"
|
|
|
|
case a.CustomRules != "":
|
|
|
|
return "custom"
|
|
|
|
case a.ConsulManaged:
|
|
|
|
return "consul-managed"
|
|
|
|
case a.VaultManaged:
|
|
|
|
return "vault-managed"
|
|
|
|
default:
|
|
|
|
return "unprivileged"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *VaultTokenAttributes) Rules(t testing.T) string {
|
|
|
|
switch {
|
|
|
|
case a == nil:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
case a.CustomRules != "":
|
|
|
|
return a.CustomRules
|
|
|
|
|
|
|
|
case a.RootPath == "":
|
|
|
|
t.Fatal("missing required RootPath")
|
|
|
|
return "" // dead code
|
|
|
|
|
|
|
|
case a.IntermediatePath == "":
|
|
|
|
t.Fatal("missing required IntermediatePath")
|
|
|
|
return "" // dead code
|
|
|
|
|
|
|
|
case a.ConsulManaged:
|
|
|
|
// Consul Managed PKI Mounts
|
|
|
|
rules := fmt.Sprintf(`
|
|
|
|
path "sys/mounts" {
|
|
|
|
capabilities = [ "read" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
path "sys/mounts/%[1]s" {
|
|
|
|
capabilities = [ "create", "read", "update", "delete", "list" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
path "sys/mounts/%[2]s" {
|
|
|
|
capabilities = [ "create", "read", "update", "delete", "list" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Needed for Consul 1.11+
|
|
|
|
path "sys/mounts/%[2]s/tune" {
|
|
|
|
capabilities = [ "update" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
# vault token renewal
|
|
|
|
path "auth/token/renew-self" {
|
|
|
|
capabilities = [ "update" ]
|
|
|
|
}
|
|
|
|
path "auth/token/lookup-self" {
|
|
|
|
capabilities = [ "read" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
path "%[1]s/*" {
|
|
|
|
capabilities = [ "create", "read", "update", "delete", "list" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
path "%[2]s/*" {
|
|
|
|
capabilities = [ "create", "read", "update", "delete", "list" ]
|
|
|
|
}
|
|
|
|
`, a.RootPath, a.IntermediatePath)
|
|
|
|
|
|
|
|
if a.WithSudo {
|
|
|
|
rules += fmt.Sprintf(`
|
|
|
|
|
|
|
|
path "%[1]s/root/sign-self-issued" {
|
|
|
|
capabilities = [ "sudo", "update" ]
|
|
|
|
}
|
|
|
|
`, a.RootPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules
|
|
|
|
|
|
|
|
case a.VaultManaged:
|
|
|
|
// Vault-managed PKI root.
|
|
|
|
t.Fatal("TODO: implement this and use it in tests")
|
|
|
|
return ""
|
|
|
|
|
|
|
|
default:
|
|
|
|
// zero value
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateVaultTokenWithAttrs(t testing.T, client *vaultapi.Client, attr *VaultTokenAttributes) string {
|
|
|
|
policyName, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rules := attr.Rules(t)
|
|
|
|
|
|
|
|
token := createVaultTokenAndPolicy(t, client, policyName, rules)
|
|
|
|
// t.Logf("created vault token with scope %q: %s", attr.DisplayName(), token)
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
func createVaultTokenAndPolicy(t testing.T, client *vaultapi.Client, policyName, policyRules string) string {
|
|
|
|
require.NoError(t, client.Sys().PutPolicy(policyName, policyRules))
|
|
|
|
|
|
|
|
renew := true
|
|
|
|
tok, err := client.Auth().Token().Create(&vaultapi.TokenCreateRequest{
|
|
|
|
Policies: []string{policyName},
|
|
|
|
Renewable: &renew,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return tok.Auth.ClientToken
|
|
|
|
}
|