2015-03-13 18:11:59 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2015-07-10 22:18:02 +00:00
|
|
|
"bytes"
|
2017-07-31 15:28:06 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2016-01-15 15:55:35 +00:00
|
|
|
"crypto/rand"
|
2015-11-19 01:26:03 +00:00
|
|
|
"crypto/sha256"
|
2016-08-15 13:42:42 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-07-31 15:28:06 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/base64"
|
2016-08-15 13:42:42 +00:00
|
|
|
"encoding/pem"
|
2015-07-10 15:56:14 +00:00
|
|
|
"fmt"
|
2017-04-07 22:50:03 +00:00
|
|
|
"io"
|
2017-07-31 15:28:06 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
mathrand "math/rand"
|
2015-07-10 22:18:02 +00:00
|
|
|
"net"
|
2016-08-15 13:42:42 +00:00
|
|
|
"net/http"
|
2017-04-07 22:50:03 +00:00
|
|
|
"os"
|
2015-07-10 22:18:02 +00:00
|
|
|
"os/exec"
|
2017-04-07 22:50:03 +00:00
|
|
|
"path/filepath"
|
2017-05-24 00:36:20 +00:00
|
|
|
"sync"
|
2015-09-02 19:56:58 +00:00
|
|
|
"time"
|
2015-03-13 18:11:59 +00:00
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
2017-01-17 20:43:10 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2017-02-24 15:45:29 +00:00
|
|
|
"golang.org/x/net/http2"
|
2015-07-10 22:18:02 +00:00
|
|
|
|
2017-02-24 15:45:29 +00:00
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
2016-01-09 02:21:02 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2017-02-24 15:45:29 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2015-04-02 01:36:13 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2016-08-19 20:45:17 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logformat"
|
2017-07-31 15:28:06 +00:00
|
|
|
"github.com/hashicorp/vault/helper/reload"
|
2015-11-19 01:26:03 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
2015-04-01 03:24:51 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2015-03-13 18:11:59 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
2017-08-01 18:07:08 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2017-08-03 17:24:27 +00:00
|
|
|
|
|
|
|
physInmem "github.com/hashicorp/vault/physical/inmem"
|
2015-03-13 18:11:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains a number of methods that are useful for unit
|
|
|
|
// tests within other packages.
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
const (
|
|
|
|
testSharedPublicKey = `
|
2015-07-10 23:27:21 +00:00
|
|
|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9i+hFxZHGo6KblVme4zrAcJstR6I0PTJozW286X4WyvPnkMYDQ5mnhEYC7UWCvjoTWbPEXPX7NjhRtwQTGD67bV+lrxgfyzK1JZbUXK4PwgKJvQD+XyyWYMzDgGSQY61KUSqCxymSm/9NZkPU3ElaQ9xQuTzPpztM4ROfb8f2Yv6/ZESZsTo0MTAkp8Pcy+WkioI/uJ1H7zqs0EA4OMY4aDJRu0UtP4rTVeYNEAuRXdX+eH4aW3KMvhzpFTjMbaJHJXlEeUm2SaX5TNQyTOvghCeQILfYIL/Ca2ij8iwCmulwdV6eQGfd4VDu40PvSnmfoaE38o6HaPnX0kUcnKiT
|
2015-07-10 22:18:02 +00:00
|
|
|
`
|
|
|
|
testSharedPrivateKey = `
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIEogIBAAKCAQEAvYvoRcWRxqOim5VZnuM6wHCbLUeiND0yaM1tvOl+Fsrz55DG
|
|
|
|
A0OZp4RGAu1Fgr46E1mzxFz1+zY4UbcEExg+u21fpa8YH8sytSWW1FyuD8ICib0A
|
|
|
|
/l8slmDMw4BkkGOtSlEqgscpkpv/TWZD1NxJWkPcULk8z6c7TOETn2/H9mL+v2RE
|
|
|
|
mbE6NDEwJKfD3MvlpIqCP7idR+86rNBAODjGOGgyUbtFLT+K01XmDRALkV3V/nh+
|
|
|
|
GltyjL4c6RU4zG2iRyV5RHlJtkml+UzUMkzr4IQnkCC32CC/wmtoo/IsAprpcHVe
|
|
|
|
nkBn3eFQ7uND70p5n6GhN/KOh2j519JFHJyokwIDAQABAoIBAHX7VOvBC3kCN9/x
|
|
|
|
+aPdup84OE7Z7MvpX6w+WlUhXVugnmsAAVDczhKoUc/WktLLx2huCGhsmKvyVuH+
|
|
|
|
MioUiE+vx75gm3qGx5xbtmOfALVMRLopjCnJYf6EaFA0ZeQ+NwowNW7Lu0PHmAU8
|
|
|
|
Z3JiX8IwxTz14DU82buDyewO7v+cEr97AnERe3PUcSTDoUXNaoNxjNpEJkKREY6h
|
|
|
|
4hAY676RT/GsRcQ8tqe/rnCqPHNd7JGqL+207FK4tJw7daoBjQyijWuB7K5chSal
|
|
|
|
oPInylM6b13ASXuOAOT/2uSUBWmFVCZPDCmnZxy2SdnJGbsJAMl7Ma3MUlaGvVI+
|
|
|
|
Tfh1aQkCgYEA4JlNOabTb3z42wz6mz+Nz3JRwbawD+PJXOk5JsSnV7DtPtfgkK9y
|
|
|
|
6FTQdhnozGWShAvJvc+C4QAihs9AlHXoaBY5bEU7R/8UK/pSqwzam+MmxmhVDV7G
|
|
|
|
IMQPV0FteoXTaJSikhZ88mETTegI2mik+zleBpVxvfdhE5TR+lq8Br0CgYEA2AwJ
|
|
|
|
CUD5CYUSj09PluR0HHqamWOrJkKPFPwa+5eiTTCzfBBxImYZh7nXnWuoviXC0sg2
|
|
|
|
AuvCW+uZ48ygv/D8gcz3j1JfbErKZJuV+TotK9rRtNIF5Ub7qysP7UjyI7zCssVM
|
|
|
|
kuDd9LfRXaB/qGAHNkcDA8NxmHW3gpln4CFdSY8CgYANs4xwfercHEWaJ1qKagAe
|
|
|
|
rZyrMpffAEhicJ/Z65lB0jtG4CiE6w8ZeUMWUVJQVcnwYD+4YpZbX4S7sJ0B8Ydy
|
|
|
|
AhkSr86D/92dKTIt2STk6aCN7gNyQ1vW198PtaAWH1/cO2UHgHOy3ZUt5X/Uwxl9
|
|
|
|
cex4flln+1Viumts2GgsCQKBgCJH7psgSyPekK5auFdKEr5+Gc/jB8I/Z3K9+g4X
|
|
|
|
5nH3G1PBTCJYLw7hRzw8W/8oALzvddqKzEFHphiGXK94Lqjt/A4q1OdbCrhiE68D
|
|
|
|
My21P/dAKB1UYRSs9Y8CNyHCjuZM9jSMJ8vv6vG/SOJPsnVDWVAckAbQDvlTHC9t
|
|
|
|
O98zAoGAcbW6uFDkrv0XMCpB9Su3KaNXOR0wzag+WIFQRXCcoTvxVi9iYfUReQPi
|
|
|
|
oOyBJU/HMVvBfv4g+OVFLVgSwwm6owwsouZ0+D/LasbuHqYyqYqdyPJQYzWA2Y+F
|
|
|
|
+B6f4RoPdSXj24JHPg/ioRxjaj094UXJxua2yfkcecGNEuBQHSs=
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2015-03-13 18:11:59 +00:00
|
|
|
// TestCore returns a pure in-memory, uninitialized core for testing.
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCore(t testing.T) *Core {
|
2016-04-25 19:39:04 +00:00
|
|
|
return TestCoreWithSeal(t, nil)
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:52:49 +00:00
|
|
|
// TestCoreNewSeal returns a pure in-memory, uninitialized core with
|
|
|
|
// the new seal configuration.
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreNewSeal(t testing.T) *Core {
|
2017-01-12 00:55:10 +00:00
|
|
|
return TestCoreWithSeal(t, &TestSeal{})
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// TestCoreWithSeal returns a pure in-memory, uninitialized core with the
|
|
|
|
// specified seal for testing.
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreWithSeal(t testing.T, testSeal Seal) *Core {
|
2017-02-16 18:16:06 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2017-08-03 17:24:27 +00:00
|
|
|
physicalBackend, err := physInmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-16 18:16:06 +00:00
|
|
|
|
|
|
|
conf := testCoreConfig(t, physicalBackend, logger)
|
|
|
|
|
|
|
|
if testSeal != nil {
|
|
|
|
conf.Seal = testSeal
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func testCoreConfig(t testing.T, physicalBackend physical.Backend, logger log.Logger) *CoreConfig {
|
2015-04-02 01:36:13 +00:00
|
|
|
noopAudits := map[string]audit.Factory{
|
2015-09-18 21:36:42 +00:00
|
|
|
"noop": func(config *audit.BackendConfig) (audit.Backend, error) {
|
2015-11-19 01:26:03 +00:00
|
|
|
view := &logical.InmemStorage{}
|
|
|
|
view.Put(&logical.StorageEntry{
|
|
|
|
Key: "salt",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2017-05-24 00:36:20 +00:00
|
|
|
config.SaltConfig = &salt.Config{
|
2015-11-19 01:26:03 +00:00
|
|
|
HMAC: sha256.New,
|
|
|
|
HMACType: "hmac-sha256",
|
|
|
|
}
|
2017-05-24 00:36:20 +00:00
|
|
|
config.SaltView = view
|
2015-09-18 21:36:42 +00:00
|
|
|
return &noopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
2015-04-02 01:36:13 +00:00
|
|
|
},
|
|
|
|
}
|
2015-04-01 03:24:51 +00:00
|
|
|
noopBackends := make(map[string]logical.Factory)
|
2015-09-04 20:58:12 +00:00
|
|
|
noopBackends["noop"] = func(config *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
b := new(framework.Backend)
|
|
|
|
b.Setup(config)
|
|
|
|
return b, nil
|
2015-04-01 03:24:51 +00:00
|
|
|
}
|
2015-09-04 20:58:12 +00:00
|
|
|
noopBackends["http"] = func(config *logical.BackendConfig) (logical.Backend, error) {
|
2015-05-27 21:19:12 +00:00
|
|
|
return new(rawHTTP), nil
|
|
|
|
}
|
2015-07-10 15:56:14 +00:00
|
|
|
logicalBackends := make(map[string]logical.Factory)
|
|
|
|
for backendName, backendFactory := range noopBackends {
|
|
|
|
logicalBackends[backendName] = backendFactory
|
|
|
|
}
|
2015-09-19 22:24:53 +00:00
|
|
|
logicalBackends["generic"] = LeasedPassthroughBackendFactory
|
2015-07-10 15:56:14 +00:00
|
|
|
for backendName, backendFactory := range testLogicalBackends {
|
|
|
|
logicalBackends[backendName] = backendFactory
|
|
|
|
}
|
2015-04-01 03:24:51 +00:00
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
conf := &CoreConfig{
|
2015-04-01 03:24:51 +00:00
|
|
|
Physical: physicalBackend,
|
2015-04-02 01:36:13 +00:00
|
|
|
AuditBackends: noopAudits,
|
2015-07-10 15:56:14 +00:00
|
|
|
LogicalBackends: logicalBackends,
|
2015-04-01 03:24:51 +00:00
|
|
|
CredentialBackends: noopBackends,
|
2015-04-29 01:12:57 +00:00
|
|
|
DisableMlock: true,
|
2016-04-26 03:10:32 +00:00
|
|
|
Logger: logger,
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
return conf
|
2015-03-13 18:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoreInit initializes the core with a single key, and returns
|
2015-03-24 18:37:07 +00:00
|
|
|
// the key that must be used to unseal the core and a root token.
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreInit(t testing.T, core *Core) ([][]byte, string) {
|
2017-05-24 14:38:48 +00:00
|
|
|
return TestCoreInitClusterWrapperSetup(t, core, nil, nil)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreInitClusterWrapperSetup(t testing.T, core *Core, clusterAddrs []*net.TCPAddr, handler http.Handler) ([][]byte, string) {
|
2016-08-19 15:03:53 +00:00
|
|
|
core.SetClusterListenerAddrs(clusterAddrs)
|
2017-05-24 14:38:48 +00:00
|
|
|
core.SetClusterHandler(handler)
|
2016-09-13 22:42:24 +00:00
|
|
|
result, err := core.Initialize(&InitParams{
|
|
|
|
BarrierConfig: &SealConfig{
|
2017-01-17 20:43:10 +00:00
|
|
|
SecretShares: 3,
|
|
|
|
SecretThreshold: 3,
|
2016-09-13 22:42:24 +00:00
|
|
|
},
|
2017-02-16 18:16:06 +00:00
|
|
|
RecoveryConfig: &SealConfig{
|
|
|
|
SecretShares: 3,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
},
|
2016-09-13 22:42:24 +00:00
|
|
|
})
|
2015-03-13 18:11:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
return result.SecretShares, result.RootToken
|
2015-03-15 00:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
func TestCoreUnseal(core *Core, key []byte) (bool, error) {
|
|
|
|
return core.Unseal(key)
|
|
|
|
}
|
|
|
|
|
2015-03-15 00:47:11 +00:00
|
|
|
// TestCoreUnsealed returns a pure in-memory core that is already
|
|
|
|
// initialized and unsealed.
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreUnsealed(t testing.T) (*Core, [][]byte, string) {
|
2015-03-15 00:47:11 +00:00
|
|
|
core := TestCore(t)
|
2017-01-17 20:43:10 +00:00
|
|
|
keys, token := TestCoreInit(t, core)
|
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2015-03-15 00:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sealed, err := core.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err checking seal status: %s", err)
|
|
|
|
}
|
|
|
|
if sealed {
|
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
return core, keys, token
|
2015-03-13 18:11:59 +00:00
|
|
|
}
|
2015-03-15 01:25:36 +00:00
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreUnsealedBackend(t testing.T, backend physical.Backend) (*Core, [][]byte, string) {
|
2017-02-16 18:16:06 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
conf := testCoreConfig(t, backend, logger)
|
|
|
|
conf.Seal = &TestSeal{}
|
|
|
|
|
|
|
|
core, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
keys, token := TestCoreInit(t, core)
|
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed, err := core.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err checking seal status: %s", err)
|
|
|
|
}
|
|
|
|
if sealed {
|
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return core, keys, token
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func testTokenStore(t testing.T, c *Core) *TokenStore {
|
2016-01-09 02:21:02 +00:00
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
2016-01-09 02:21:02 +00:00
|
|
|
Path: "token/",
|
|
|
|
Type: "token",
|
|
|
|
Description: "token based credentials",
|
|
|
|
}
|
|
|
|
|
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
me.UUID = meUUID
|
|
|
|
|
|
|
|
view := NewBarrierView(c.barrier, credentialBarrierPrefix+me.UUID+"/")
|
2017-02-17 04:09:39 +00:00
|
|
|
sysView := c.mountEntrySysView(me)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
tokenstore, _ := c.newCredentialBackend("token", sysView, view, nil)
|
|
|
|
if err := tokenstore.Initialize(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
ts := tokenstore.(*TokenStore)
|
|
|
|
|
|
|
|
router := NewRouter()
|
2017-07-01 00:54:05 +00:00
|
|
|
err = router.Mount(ts, "auth/token/", &MountEntry{Table: credentialTableType, UUID: "authtokenuuid", Path: "auth/token", Accessor: "authtokenaccessor"}, ts.view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
subview := c.systemBarrierView.SubView(expirationSubPath)
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
exp := NewExpirationManager(router, subview, ts, logger)
|
|
|
|
ts.SetExpirationManager(exp)
|
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoreWithTokenStore returns an in-memory core that has a token store
|
|
|
|
// mounted, so that logical token functions can be used
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreWithTokenStore(t testing.T) (*Core, *TokenStore, [][]byte, string) {
|
2017-02-16 18:16:06 +00:00
|
|
|
c, keys, root := TestCoreUnsealed(t)
|
|
|
|
ts := testTokenStore(t, c)
|
|
|
|
|
|
|
|
return c, ts, keys, root
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoreWithBackendTokenStore returns a core that has a token store
|
|
|
|
// mounted and used the provided physical backend, so that logical token
|
|
|
|
// functions can be used
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestCoreWithBackendTokenStore(t testing.T, backend physical.Backend) (*Core, *TokenStore, [][]byte, string) {
|
2017-02-16 18:16:06 +00:00
|
|
|
c, keys, root := TestCoreUnsealedBackend(t, backend)
|
|
|
|
ts := testTokenStore(t, c)
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
return c, ts, keys, root
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:25:36 +00:00
|
|
|
// TestKeyCopy is a silly little function to just copy the key so that
|
|
|
|
// it can be used with Unseal easily.
|
|
|
|
func TestKeyCopy(key []byte) []byte {
|
|
|
|
result := make([]byte, len(key))
|
|
|
|
copy(result, key)
|
|
|
|
return result
|
|
|
|
}
|
2015-04-02 01:36:13 +00:00
|
|
|
|
2017-03-23 22:54:15 +00:00
|
|
|
func TestDynamicSystemView(c *Core) *dynamicSystemView {
|
2017-04-07 22:50:03 +00:00
|
|
|
me := &MountEntry{
|
|
|
|
Config: MountConfig{
|
|
|
|
DefaultLeaseTTL: 24 * time.Hour,
|
|
|
|
MaxLeaseTTL: 2 * 24 * time.Hour,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dynamicSystemView{c, me}
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestAddTestPlugin(t testing.T, c *Core, name, testFunc string) {
|
2017-04-10 21:12:28 +00:00
|
|
|
file, err := os.Open(os.Args[0])
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
hash := sha256.New()
|
|
|
|
|
|
|
|
_, err = io.Copy(hash, file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := hash.Sum(nil)
|
2017-04-10 21:12:28 +00:00
|
|
|
c.pluginCatalog.directory, err = filepath.EvalSymlinks(os.Args[0])
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c.pluginCatalog.directory = filepath.Dir(c.pluginCatalog.directory)
|
|
|
|
|
2017-04-10 21:12:28 +00:00
|
|
|
command := fmt.Sprintf("%s --test.run=%s", filepath.Base(os.Args[0]), testFunc)
|
2017-04-07 22:50:03 +00:00
|
|
|
err = c.pluginCatalog.Set(name, command, sum)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-23 22:54:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 15:56:14 +00:00
|
|
|
var testLogicalBackends = map[string]logical.Factory{}
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
// Starts the test server which responds to SSH authentication.
|
|
|
|
// Used to test the SSH secret backend.
|
2015-08-18 23:48:50 +00:00
|
|
|
func StartSSHHostTestServer() (string, error) {
|
2015-07-10 22:18:02 +00:00
|
|
|
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(testSharedPublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error parsing public key")
|
|
|
|
}
|
|
|
|
serverConfig := &ssh.ServerConfig{
|
|
|
|
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
|
|
|
|
if bytes.Compare(pubKey.Marshal(), key.Marshal()) == 0 {
|
|
|
|
return &ssh.Permissions{}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Key does not match")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(testSharedPrivateKey))
|
|
|
|
if err != nil {
|
|
|
|
panic("Error parsing private key")
|
|
|
|
}
|
|
|
|
serverConfig.AddHostKey(signer)
|
|
|
|
|
|
|
|
soc, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error listening to connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := soc.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error accepting incoming connection: %s", err))
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
sshConn, chanReqs, _, err := ssh.NewServerConn(conn, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Handshaking error: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for chanReq := range chanReqs {
|
|
|
|
go func(chanReq ssh.NewChannel) {
|
|
|
|
if chanReq.ChannelType() != "session" {
|
|
|
|
chanReq.Reject(ssh.UnknownChannelType, "unknown channel type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, requests, err := chanReq.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error accepting channel: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(ch ssh.Channel, in <-chan *ssh.Request) {
|
|
|
|
for req := range in {
|
|
|
|
executeServerCommand(ch, req)
|
|
|
|
}
|
|
|
|
}(ch, requests)
|
|
|
|
}(chanReq)
|
|
|
|
}
|
|
|
|
sshConn.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return soc.Addr().String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This executes the commands requested to be run on the server.
|
|
|
|
// Used to test the SSH secret backend.
|
|
|
|
func executeServerCommand(ch ssh.Channel, req *ssh.Request) {
|
|
|
|
command := string(req.Payload[4:])
|
2016-05-15 19:22:21 +00:00
|
|
|
cmd := exec.Command("/bin/bash", []string{"-c", command}...)
|
2015-07-10 22:18:02 +00:00
|
|
|
req.Reply(true, nil)
|
|
|
|
|
|
|
|
cmd.Stdout = ch
|
|
|
|
cmd.Stderr = ch
|
|
|
|
cmd.Stdin = ch
|
|
|
|
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error starting the command: '%s'", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := cmd.Process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error while waiting for command to finish:'%s'", err))
|
|
|
|
}
|
|
|
|
ch.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:56:14 +00:00
|
|
|
// This adds a logical backend for the test core. This needs to be
|
|
|
|
// invoked before the test core is created.
|
|
|
|
func AddTestLogicalBackend(name string, factory logical.Factory) error {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("Missing backend name")
|
|
|
|
}
|
|
|
|
if factory == nil {
|
|
|
|
return fmt.Errorf("Missing backend factory function")
|
|
|
|
}
|
|
|
|
testLogicalBackends[name] = factory
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:36:42 +00:00
|
|
|
type noopAudit struct {
|
2017-05-24 00:36:20 +00:00
|
|
|
Config *audit.BackendConfig
|
|
|
|
salt *salt.Salt
|
|
|
|
saltMutex sync.RWMutex
|
2015-09-18 21:36:42 +00:00
|
|
|
}
|
2015-04-02 01:36:13 +00:00
|
|
|
|
2017-05-24 00:36:20 +00:00
|
|
|
func (n *noopAudit) GetHash(data string) (string, error) {
|
|
|
|
salt, err := n.Salt()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return salt.GetIdentifiedHMAC(data), nil
|
2015-11-19 01:26:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 01:30:18 +00:00
|
|
|
func (n *noopAudit) LogRequest(a *logical.Auth, r *logical.Request, e error) error {
|
2015-04-02 01:36:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopAudit) LogResponse(a *logical.Auth, r *logical.Request, re *logical.Response, err error) error {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-27 21:19:12 +00:00
|
|
|
|
2016-09-30 19:04:50 +00:00
|
|
|
func (n *noopAudit) Reload() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:36:20 +00:00
|
|
|
func (n *noopAudit) Invalidate() {
|
|
|
|
n.saltMutex.Lock()
|
|
|
|
defer n.saltMutex.Unlock()
|
|
|
|
n.salt = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopAudit) Salt() (*salt.Salt, error) {
|
|
|
|
n.saltMutex.RLock()
|
|
|
|
if n.salt != nil {
|
|
|
|
defer n.saltMutex.RUnlock()
|
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
n.saltMutex.RUnlock()
|
|
|
|
n.saltMutex.Lock()
|
|
|
|
defer n.saltMutex.Unlock()
|
|
|
|
if n.salt != nil {
|
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
salt, err := salt.NewSalt(n.Config.SaltView, n.Config.SaltConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n.salt = salt
|
|
|
|
return salt, nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:19:12 +00:00
|
|
|
type rawHTTP struct{}
|
|
|
|
|
|
|
|
func (n *rawHTTP) HandleRequest(req *logical.Request) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
logical.HTTPStatusCode: 200,
|
|
|
|
logical.HTTPContentType: "plain/text",
|
|
|
|
logical.HTTPRawBody: []byte("hello world"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-12 20:09:16 +00:00
|
|
|
func (n *rawHTTP) HandleExistenceCheck(req *logical.Request) (bool, bool, error) {
|
|
|
|
return false, false, nil
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 21:19:12 +00:00
|
|
|
func (n *rawHTTP) SpecialPaths() *logical.Paths {
|
|
|
|
return &logical.Paths{Unauthenticated: []string{"*"}}
|
|
|
|
}
|
2015-09-02 19:56:58 +00:00
|
|
|
|
|
|
|
func (n *rawHTTP) System() logical.SystemView {
|
|
|
|
return logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2015-09-02 19:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 14:11:37 +00:00
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
func (n *rawHTTP) Logger() log.Logger {
|
|
|
|
return logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
}
|
|
|
|
|
2015-09-10 14:11:37 +00:00
|
|
|
func (n *rawHTTP) Cleanup() {
|
|
|
|
// noop
|
|
|
|
}
|
2016-01-15 15:55:35 +00:00
|
|
|
|
2017-01-13 19:51:10 +00:00
|
|
|
func (n *rawHTTP) Initialize() error {
|
|
|
|
// noop
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:18:22 +00:00
|
|
|
func (n *rawHTTP) InvalidateKey(string) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
func (n *rawHTTP) Setup(config *logical.BackendConfig) error {
|
|
|
|
// noop
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *rawHTTP) Type() logical.BackendType {
|
|
|
|
return logical.TypeUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *rawHTTP) RegisterLicense(license interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func GenerateRandBytes(length int) ([]byte, error) {
|
|
|
|
if length < 0 {
|
|
|
|
return nil, fmt.Errorf("length must be >= 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, length)
|
|
|
|
if length == 0 {
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := rand.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != length {
|
|
|
|
return nil, fmt.Errorf("unable to read %d bytes; only read %d", length, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func TestWaitActive(t testing.T, core *Core) {
|
2016-08-15 13:42:42 +00:00
|
|
|
start := time.Now()
|
|
|
|
var standby bool
|
|
|
|
var err error
|
|
|
|
for time.Now().Sub(start) < time.Second {
|
|
|
|
standby, err = core.Standby()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !standby {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if standby {
|
|
|
|
t.Fatalf("should not be in standby mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
type TestCluster struct {
|
2017-07-31 19:31:44 +00:00
|
|
|
BarrierKeys [][]byte
|
|
|
|
CACert *x509.Certificate
|
|
|
|
CACertBytes []byte
|
|
|
|
CACertPEM []byte
|
|
|
|
CACertPEMFile string
|
|
|
|
CAKey *ecdsa.PrivateKey
|
|
|
|
CAKeyPEM []byte
|
|
|
|
Cores []*TestClusterCore
|
|
|
|
ID string
|
|
|
|
RootToken string
|
|
|
|
RootCAs *x509.CertPool
|
|
|
|
TempDir string
|
2017-07-03 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
func (t *TestCluster) Start() {
|
2017-07-03 18:54:01 +00:00
|
|
|
for _, core := range t.Cores {
|
|
|
|
if core.Server != nil {
|
|
|
|
for _, ln := range core.Listeners {
|
|
|
|
go core.Server.Serve(ln)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
func (t *TestCluster) Cleanup() {
|
2017-07-03 18:54:01 +00:00
|
|
|
for _, core := range t.Cores {
|
|
|
|
if core.Listeners != nil {
|
|
|
|
for _, ln := range core.Listeners {
|
|
|
|
ln.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
|
|
|
|
if t.TempDir != "" {
|
|
|
|
os.RemoveAll(t.TempDir)
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
// Give time to actually shut down/clean up before the next test
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
type TestListener struct {
|
|
|
|
net.Listener
|
|
|
|
Address *net.TCPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestClusterCore struct {
|
|
|
|
*Core
|
2017-07-31 15:28:06 +00:00
|
|
|
Client *api.Client
|
|
|
|
Handler http.Handler
|
|
|
|
Listeners []*TestListener
|
|
|
|
ReloadFuncs *map[string][]reload.ReloadFunc
|
|
|
|
ReloadFuncsLock *sync.RWMutex
|
|
|
|
Server *http.Server
|
|
|
|
ServerCert *x509.Certificate
|
|
|
|
ServerCertBytes []byte
|
|
|
|
ServerCertPEM []byte
|
|
|
|
ServerKey *ecdsa.PrivateKey
|
|
|
|
ServerKeyPEM []byte
|
|
|
|
TLSConfig *tls.Config
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
type TestClusterOptions struct {
|
|
|
|
KeepStandbysSealed bool
|
|
|
|
HandlerFunc func(*Core) http.Handler
|
|
|
|
BaseListenAddress string
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:07:08 +00:00
|
|
|
func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *TestCluster {
|
2017-07-31 15:28:06 +00:00
|
|
|
certIPs := []net.IP{
|
|
|
|
net.IPv6loopback,
|
|
|
|
net.ParseIP("127.0.0.1"),
|
|
|
|
}
|
|
|
|
var baseAddr *net.TCPAddr
|
2017-07-31 16:13:29 +00:00
|
|
|
if opts != nil && opts.BaseListenAddress != "" {
|
2017-07-31 15:28:06 +00:00
|
|
|
var err error
|
|
|
|
baseAddr, err = net.ResolveTCPAddr("tcp", opts.BaseListenAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not parse given base IP")
|
|
|
|
}
|
|
|
|
certIPs = append(certIPs, baseAddr.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCluster TestCluster
|
|
|
|
tempDir, err := ioutil.TempDir("", "vault-test-cluster-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCluster.TempDir = tempDir
|
|
|
|
|
|
|
|
caKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCluster.CAKey = caKey
|
|
|
|
caCertTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
|
|
|
DNSNames: []string{"localhost"},
|
|
|
|
IPAddresses: certIPs,
|
|
|
|
KeyUsage: x509.KeyUsage(x509.KeyUsageCertSign | x509.KeyUsageCRLSign),
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
|
|
|
}
|
|
|
|
caBytes, err := x509.CreateCertificate(rand.Reader, caCertTemplate, caCertTemplate, caKey.Public(), caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
caCert, err := x509.ParseCertificate(caBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
testCluster.CACert = caCert
|
|
|
|
testCluster.CACertBytes = caBytes
|
|
|
|
testCluster.RootCAs = x509.NewCertPool()
|
|
|
|
testCluster.RootCAs.AddCert(caCert)
|
|
|
|
caCertPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: caBytes,
|
|
|
|
}
|
|
|
|
testCluster.CACertPEM = pem.EncodeToMemory(caCertPEMBlock)
|
2017-07-31 19:31:44 +00:00
|
|
|
testCluster.CACertPEMFile = filepath.Join(testCluster.TempDir, "ca_cert.pem")
|
|
|
|
err = ioutil.WriteFile(testCluster.CACertPEMFile, testCluster.CACertPEM, 0755)
|
2017-07-31 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
marshaledCAKey, err := x509.MarshalECPrivateKey(caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
caKeyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: marshaledCAKey,
|
|
|
|
}
|
|
|
|
testCluster.CAKeyPEM = pem.EncodeToMemory(caKeyPEMBlock)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(testCluster.TempDir, "ca_key.pem"), testCluster.CAKeyPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
s1Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s1CertTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
2017-07-31 21:35:51 +00:00
|
|
|
DNSNames: []string{"localhost"},
|
|
|
|
IPAddresses: certIPs,
|
2017-07-31 15:28:06 +00:00
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
}
|
|
|
|
s1CertBytes, err := x509.CreateCertificate(rand.Reader, s1CertTemplate, caCert, s1Key.Public(), caKey)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s1Cert, err := x509.ParseCertificate(s1CertBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s1CertPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: s1CertBytes,
|
|
|
|
}
|
|
|
|
s1CertPEM := pem.EncodeToMemory(s1CertPEMBlock)
|
|
|
|
s1MarshaledKey, err := x509.MarshalECPrivateKey(s1Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s1KeyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: s1MarshaledKey,
|
|
|
|
}
|
|
|
|
s1KeyPEM := pem.EncodeToMemory(s1KeyPEMBlock)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
s2Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2CertTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
2017-07-31 21:35:51 +00:00
|
|
|
DNSNames: []string{"localhost"},
|
|
|
|
IPAddresses: certIPs,
|
2017-07-31 15:28:06 +00:00
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
}
|
|
|
|
s2CertBytes, err := x509.CreateCertificate(rand.Reader, s2CertTemplate, caCert, s2Key.Public(), caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2Cert, err := x509.ParseCertificate(s2CertBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2CertPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: s2CertBytes,
|
|
|
|
}
|
|
|
|
s2CertPEM := pem.EncodeToMemory(s2CertPEMBlock)
|
|
|
|
s2MarshaledKey, err := x509.MarshalECPrivateKey(s2Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2KeyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: s2MarshaledKey,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s2KeyPEM := pem.EncodeToMemory(s2KeyPEMBlock)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
s3Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
2016-08-15 13:42:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3CertTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
2017-07-31 21:35:51 +00:00
|
|
|
DNSNames: []string{"localhost"},
|
|
|
|
IPAddresses: certIPs,
|
2017-07-31 15:28:06 +00:00
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
}
|
|
|
|
s3CertBytes, err := x509.CreateCertificate(rand.Reader, s3CertTemplate, caCert, s3Key.Public(), caKey)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3Cert, err := x509.ParseCertificate(s3CertBytes)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3CertPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: s3CertBytes,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3CertPEM := pem.EncodeToMemory(s3CertPEMBlock)
|
|
|
|
s3MarshaledKey, err := x509.MarshalECPrivateKey(s3Key)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3KeyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: s3MarshaledKey,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3KeyPEM := pem.EncodeToMemory(s3KeyPEMBlock)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Listener setup
|
|
|
|
//
|
2017-07-31 15:28:06 +00:00
|
|
|
ports := []int{0, 0, 0}
|
|
|
|
if baseAddr != nil {
|
|
|
|
ports = []int{baseAddr.Port, baseAddr.Port + 1, baseAddr.Port + 2}
|
|
|
|
} else {
|
|
|
|
baseAddr = &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseAddr.Port = ports[0]
|
|
|
|
ln, err := net.ListenTCP("tcp", baseAddr)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s1CertFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node1_port_%d_cert.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
s1KeyFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node1_port_%d_key.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
err = ioutil.WriteFile(s1CertFile, s1CertPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
err = ioutil.WriteFile(s1KeyFile, s1KeyPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s1TLSCert, err := tls.X509KeyPair(s1CertPEM, s1KeyPEM)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s1CertGetter := reload.NewCertificateGetter(s1CertFile, s1KeyFile)
|
|
|
|
s1TLSConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{s1TLSCert},
|
|
|
|
RootCAs: testCluster.RootCAs,
|
|
|
|
ClientCAs: testCluster.RootCAs,
|
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
GetCertificate: s1CertGetter.GetCertificate,
|
|
|
|
}
|
|
|
|
s1TLSConfig.BuildNameToCertificate()
|
|
|
|
c1lns := []*TestListener{&TestListener{
|
|
|
|
Listener: tls.NewListener(ln, s1TLSConfig),
|
2016-08-15 13:42:42 +00:00
|
|
|
Address: ln.Addr().(*net.TCPAddr),
|
2017-07-31 15:28:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var handler1 http.Handler = http.NewServeMux()
|
2016-08-15 13:42:42 +00:00
|
|
|
server1 := &http.Server{
|
2017-07-03 18:54:01 +00:00
|
|
|
Handler: handler1,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-02-27 17:49:35 +00:00
|
|
|
if err := http2.ConfigureServer(server1, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
baseAddr.Port = ports[1]
|
|
|
|
ln, err = net.ListenTCP("tcp", baseAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2CertFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node2_port_%d_cert.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
s2KeyFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node2_port_%d_key.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
err = ioutil.WriteFile(s2CertFile, s2CertPEM, 0755)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
err = ioutil.WriteFile(s2KeyFile, s2KeyPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2TLSCert, err := tls.X509KeyPair(s2CertPEM, s2KeyPEM)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s2CertGetter := reload.NewCertificateGetter(s2CertFile, s2KeyFile)
|
|
|
|
s2TLSConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{s2TLSCert},
|
|
|
|
RootCAs: testCluster.RootCAs,
|
|
|
|
ClientCAs: testCluster.RootCAs,
|
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
GetCertificate: s2CertGetter.GetCertificate,
|
|
|
|
}
|
|
|
|
s2TLSConfig.BuildNameToCertificate()
|
2016-08-15 13:42:42 +00:00
|
|
|
c2lns := []*TestListener{&TestListener{
|
2017-07-31 15:28:06 +00:00
|
|
|
Listener: tls.NewListener(ln, s2TLSConfig),
|
2016-08-15 13:42:42 +00:00
|
|
|
Address: ln.Addr().(*net.TCPAddr),
|
|
|
|
},
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
var handler2 http.Handler = http.NewServeMux()
|
2016-08-15 13:42:42 +00:00
|
|
|
server2 := &http.Server{
|
2017-07-03 18:54:01 +00:00
|
|
|
Handler: handler2,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-02-27 17:49:35 +00:00
|
|
|
if err := http2.ConfigureServer(server2, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
baseAddr.Port = ports[2]
|
|
|
|
ln, err = net.ListenTCP("tcp", baseAddr)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
s3CertFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node3_port_%d_cert.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
s3KeyFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node3_port_%d_key.pem", ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
err = ioutil.WriteFile(s3CertFile, s3CertPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(s3KeyFile, s3KeyPEM, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s3TLSCert, err := tls.X509KeyPair(s3CertPEM, s3KeyPEM)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s3CertGetter := reload.NewCertificateGetter(s3CertFile, s3KeyFile)
|
|
|
|
s3TLSConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{s3TLSCert},
|
|
|
|
RootCAs: testCluster.RootCAs,
|
|
|
|
ClientCAs: testCluster.RootCAs,
|
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
GetCertificate: s3CertGetter.GetCertificate,
|
|
|
|
}
|
|
|
|
s3TLSConfig.BuildNameToCertificate()
|
2016-08-15 13:42:42 +00:00
|
|
|
c3lns := []*TestListener{&TestListener{
|
2017-07-31 15:28:06 +00:00
|
|
|
Listener: tls.NewListener(ln, s3TLSConfig),
|
2016-08-15 13:42:42 +00:00
|
|
|
Address: ln.Addr().(*net.TCPAddr),
|
|
|
|
},
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
var handler3 http.Handler = http.NewServeMux()
|
2016-08-15 13:42:42 +00:00
|
|
|
server3 := &http.Server{
|
2017-07-03 18:54:01 +00:00
|
|
|
Handler: handler3,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-02-27 17:49:35 +00:00
|
|
|
if err := http2.ConfigureServer(server3, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
// Create three cores with the same physical and different redirect/cluster
|
|
|
|
// addrs.
|
2016-11-08 15:31:35 +00:00
|
|
|
// N.B.: On OSX, instead of random ports, it assigns new ports to new
|
|
|
|
// listeners sequentially. Aside from being a bad idea in a security sense,
|
|
|
|
// it also broke tests that assumed it was OK to just use the port above
|
2017-07-31 15:28:06 +00:00
|
|
|
// the redirect addr. This has now been changed to 105 ports above, but if
|
2016-11-08 15:31:35 +00:00
|
|
|
// we ever do more than three nodes in a cluster it may need to be bumped.
|
2017-07-31 15:28:06 +00:00
|
|
|
// Note: it's 105 so that we don't conflict with a running Consul by
|
|
|
|
// default.
|
2016-08-15 13:42:42 +00:00
|
|
|
coreConfig := &CoreConfig{
|
|
|
|
LogicalBackends: make(map[string]logical.Factory),
|
|
|
|
CredentialBackends: make(map[string]logical.Factory),
|
|
|
|
AuditBackends: make(map[string]audit.Factory),
|
|
|
|
RedirectAddr: fmt.Sprintf("https://127.0.0.1:%d", c1lns[0].Address.Port),
|
2017-07-31 15:28:06 +00:00
|
|
|
ClusterAddr: fmt.Sprintf("https://127.0.0.1:%d", c1lns[0].Address.Port+105),
|
2016-08-15 13:42:42 +00:00
|
|
|
DisableMlock: true,
|
2017-07-31 15:28:06 +00:00
|
|
|
EnableUI: true,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if base != nil {
|
2017-07-31 15:28:06 +00:00
|
|
|
coreConfig.DisableCache = base.DisableCache
|
|
|
|
coreConfig.EnableUI = base.EnableUI
|
|
|
|
coreConfig.DefaultLeaseTTL = base.DefaultLeaseTTL
|
|
|
|
coreConfig.MaxLeaseTTL = base.MaxLeaseTTL
|
|
|
|
coreConfig.CacheSize = base.CacheSize
|
|
|
|
coreConfig.PluginDirectory = base.PluginDirectory
|
|
|
|
coreConfig.Seal = base.Seal
|
|
|
|
coreConfig.DevToken = base.DevToken
|
|
|
|
|
|
|
|
if !coreConfig.DisableMlock {
|
|
|
|
base.DisableMlock = false
|
|
|
|
}
|
|
|
|
|
2017-02-28 23:17:19 +00:00
|
|
|
if base.Physical != nil {
|
|
|
|
coreConfig.Physical = base.Physical
|
|
|
|
}
|
|
|
|
|
|
|
|
if base.HAPhysical != nil {
|
|
|
|
coreConfig.HAPhysical = base.HAPhysical
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Used to set something non-working to test fallback
|
|
|
|
switch base.ClusterAddr {
|
|
|
|
case "empty":
|
|
|
|
coreConfig.ClusterAddr = ""
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
coreConfig.ClusterAddr = base.ClusterAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if base.LogicalBackends != nil {
|
|
|
|
for k, v := range base.LogicalBackends {
|
|
|
|
coreConfig.LogicalBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if base.CredentialBackends != nil {
|
|
|
|
for k, v := range base.CredentialBackends {
|
|
|
|
coreConfig.CredentialBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if base.AuditBackends != nil {
|
|
|
|
for k, v := range base.AuditBackends {
|
|
|
|
coreConfig.AuditBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 11:31:06 +00:00
|
|
|
if base.Logger != nil {
|
|
|
|
coreConfig.Logger = base.Logger
|
|
|
|
}
|
2017-07-31 19:31:44 +00:00
|
|
|
|
|
|
|
coreConfig.DisableCache = base.DisableCache
|
|
|
|
|
|
|
|
coreConfig.DevToken = base.DevToken
|
2016-12-01 20:25:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 23:17:19 +00:00
|
|
|
if coreConfig.Physical == nil {
|
2017-08-03 17:24:27 +00:00
|
|
|
coreConfig.Physical, err = physInmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-28 23:17:19 +00:00
|
|
|
}
|
|
|
|
if coreConfig.HAPhysical == nil {
|
2017-08-03 17:24:27 +00:00
|
|
|
haPhys, err := physInmem.NewInmemHA(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
coreConfig.HAPhysical = haPhys.(physical.HABackend)
|
2017-02-28 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
c1, err := NewCore(coreConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
if opts != nil && opts.HandlerFunc != nil {
|
|
|
|
handler1 = opts.HandlerFunc(c1)
|
|
|
|
server1.Handler = handler1
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
coreConfig.RedirectAddr = fmt.Sprintf("https://127.0.0.1:%d", c2lns[0].Address.Port)
|
|
|
|
if coreConfig.ClusterAddr != "" {
|
2017-07-31 15:28:06 +00:00
|
|
|
coreConfig.ClusterAddr = fmt.Sprintf("https://127.0.0.1:%d", c2lns[0].Address.Port+105)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
c2, err := NewCore(coreConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
if opts != nil && opts.HandlerFunc != nil {
|
|
|
|
handler2 = opts.HandlerFunc(c2)
|
|
|
|
server2.Handler = handler2
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
coreConfig.RedirectAddr = fmt.Sprintf("https://127.0.0.1:%d", c3lns[0].Address.Port)
|
|
|
|
if coreConfig.ClusterAddr != "" {
|
2017-07-31 15:28:06 +00:00
|
|
|
coreConfig.ClusterAddr = fmt.Sprintf("https://127.0.0.1:%d", c3lns[0].Address.Port+105)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
c3, err := NewCore(coreConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
if opts != nil && opts.HandlerFunc != nil {
|
|
|
|
handler3 = opts.HandlerFunc(c3)
|
|
|
|
server3.Handler = handler3
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Clustering setup
|
|
|
|
//
|
2016-08-19 15:03:53 +00:00
|
|
|
clusterAddrGen := func(lns []*TestListener) []*net.TCPAddr {
|
|
|
|
ret := make([]*net.TCPAddr, len(lns))
|
2016-08-15 13:42:42 +00:00
|
|
|
for i, ln := range lns {
|
2016-08-19 15:03:53 +00:00
|
|
|
ret[i] = &net.TCPAddr{
|
|
|
|
IP: ln.Address.IP,
|
2017-07-31 15:28:06 +00:00
|
|
|
Port: ln.Address.Port + 105,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
c2.SetClusterListenerAddrs(clusterAddrGen(c2lns))
|
2017-07-03 18:54:01 +00:00
|
|
|
c2.SetClusterHandler(handler2)
|
2016-08-19 15:03:53 +00:00
|
|
|
c3.SetClusterListenerAddrs(clusterAddrGen(c3lns))
|
2017-07-03 18:54:01 +00:00
|
|
|
c3.SetClusterHandler(handler3)
|
2017-07-31 15:28:06 +00:00
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
keys, root := TestCoreInitClusterWrapperSetup(t, c1, clusterAddrGen(c1lns), handler1)
|
2017-07-31 15:28:06 +00:00
|
|
|
barrierKeys, _ := copystructure.Copy(keys)
|
|
|
|
testCluster.BarrierKeys = barrierKeys.([][]byte)
|
|
|
|
testCluster.RootToken = root
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(testCluster.TempDir, "root_token"), []byte(root), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for i, key := range testCluster.BarrierKeys {
|
|
|
|
buf.Write([]byte(base64.StdEncoding.EncodeToString(key)))
|
|
|
|
if i < len(testCluster.BarrierKeys)-1 {
|
|
|
|
buf.WriteRune('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(filepath.Join(testCluster.TempDir, "barrier_keys"), buf.Bytes(), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := c1.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify unsealed
|
|
|
|
sealed, err := c1.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err checking seal status: %s", err)
|
|
|
|
}
|
|
|
|
if sealed {
|
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWaitActive(t, c1)
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
if opts == nil || !opts.KeepStandbysSealed {
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := c2.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := c3.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let them come fully up to standby
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
// Ensure cluster connection info is populated
|
2017-07-31 22:25:27 +00:00
|
|
|
isLeader, _, _, err := c2.Leader()
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if isLeader {
|
|
|
|
t.Fatal("c2 should not be leader")
|
|
|
|
}
|
2017-07-31 22:25:27 +00:00
|
|
|
isLeader, _, _, err = c3.Leader()
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if isLeader {
|
|
|
|
t.Fatal("c3 should not be leader")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 23:17:19 +00:00
|
|
|
cluster, err := c1.Cluster()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
testCluster.ID = cluster.ID
|
2017-02-28 23:17:19 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
getAPIClient := func(port int, tlsConfig *tls.Config) *api.Client {
|
2017-02-24 15:45:29 +00:00
|
|
|
transport := cleanhttp.DefaultPooledTransport()
|
|
|
|
transport.TLSClientConfig = tlsConfig
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
// This can of course be overridden per-test by using its own client
|
|
|
|
return fmt.Errorf("redirects not allowed in these tests")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = fmt.Sprintf("https://127.0.0.1:%d", port)
|
|
|
|
config.HttpClient = client
|
|
|
|
apiClient, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
apiClient.SetToken(root)
|
|
|
|
return apiClient
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
var ret []*TestClusterCore
|
2017-07-31 15:28:06 +00:00
|
|
|
t1 := &TestClusterCore{
|
|
|
|
Core: c1,
|
|
|
|
ServerKey: s1Key,
|
|
|
|
ServerKeyPEM: s1KeyPEM,
|
|
|
|
ServerCert: s1Cert,
|
|
|
|
ServerCertBytes: s1CertBytes,
|
|
|
|
ServerCertPEM: s1CertPEM,
|
|
|
|
Listeners: c1lns,
|
|
|
|
Handler: handler1,
|
|
|
|
Server: server1,
|
|
|
|
TLSConfig: s1TLSConfig,
|
|
|
|
Client: getAPIClient(c1lns[0].Address.Port, s1TLSConfig),
|
|
|
|
}
|
|
|
|
t1.ReloadFuncs = &c1.reloadFuncs
|
|
|
|
t1.ReloadFuncsLock = &c1.reloadFuncsLock
|
|
|
|
t1.ReloadFuncsLock.Lock()
|
|
|
|
(*t1.ReloadFuncs)["listener|tcp"] = []reload.ReloadFunc{s1CertGetter.Reload}
|
|
|
|
t1.ReloadFuncsLock.Unlock()
|
|
|
|
ret = append(ret, t1)
|
|
|
|
|
|
|
|
t2 := &TestClusterCore{
|
|
|
|
Core: c2,
|
|
|
|
ServerKey: s2Key,
|
|
|
|
ServerKeyPEM: s2KeyPEM,
|
|
|
|
ServerCert: s2Cert,
|
|
|
|
ServerCertBytes: s2CertBytes,
|
|
|
|
ServerCertPEM: s2CertPEM,
|
|
|
|
Listeners: c2lns,
|
|
|
|
Handler: handler2,
|
|
|
|
Server: server2,
|
|
|
|
TLSConfig: s2TLSConfig,
|
|
|
|
Client: getAPIClient(c2lns[0].Address.Port, s2TLSConfig),
|
|
|
|
}
|
|
|
|
t2.ReloadFuncs = &c2.reloadFuncs
|
|
|
|
t2.ReloadFuncsLock = &c2.reloadFuncsLock
|
|
|
|
t2.ReloadFuncsLock.Lock()
|
|
|
|
(*t2.ReloadFuncs)["listener|tcp"] = []reload.ReloadFunc{s2CertGetter.Reload}
|
|
|
|
t2.ReloadFuncsLock.Unlock()
|
|
|
|
ret = append(ret, t2)
|
|
|
|
|
|
|
|
t3 := &TestClusterCore{
|
|
|
|
Core: c3,
|
|
|
|
ServerKey: s3Key,
|
|
|
|
ServerKeyPEM: s3KeyPEM,
|
|
|
|
ServerCert: s3Cert,
|
|
|
|
ServerCertBytes: s3CertBytes,
|
|
|
|
ServerCertPEM: s3CertPEM,
|
|
|
|
Listeners: c3lns,
|
|
|
|
Handler: handler3,
|
|
|
|
Server: server3,
|
|
|
|
TLSConfig: s3TLSConfig,
|
|
|
|
Client: getAPIClient(c3lns[0].Address.Port, s3TLSConfig),
|
|
|
|
}
|
|
|
|
t3.ReloadFuncs = &c3.reloadFuncs
|
|
|
|
t3.ReloadFuncsLock = &c3.reloadFuncsLock
|
|
|
|
t3.ReloadFuncsLock.Lock()
|
|
|
|
(*t3.ReloadFuncs)["listener|tcp"] = []reload.ReloadFunc{s3CertGetter.Reload}
|
|
|
|
t3.ReloadFuncsLock.Unlock()
|
|
|
|
ret = append(ret, t3)
|
|
|
|
|
|
|
|
testCluster.Cores = ret
|
|
|
|
return &testCluster
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|