2016-08-15 13:42:42 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
credCert "github.com/hashicorp/vault/builtin/credential/cert"
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/transit"
|
2016-10-26 23:52:31 +00:00
|
|
|
"github.com/hashicorp/vault/helper/keysutil"
|
2016-08-15 13:42:42 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_Fallback_Bad_Address(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"transit": transit.Factory,
|
|
|
|
},
|
|
|
|
ClusterAddr: "https://127.3.4.1:8382",
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
addrs := []string{
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[1].Listeners[0].Address.Port),
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[2].Listeners[0].Address.Port),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = addr
|
|
|
|
config.HttpClient = cleanhttp.DefaultClient()
|
|
|
|
config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cores[0].TLSConfig
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
secret, err := client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
t.Fatal("secret is nil")
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
if secret.Data["id"].(string) != cluster.RootToken {
|
2016-08-15 13:42:42 +00:00
|
|
|
t.Fatal("token mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_Fallback_Disabled(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"transit": transit.Factory,
|
|
|
|
},
|
|
|
|
ClusterAddr: "empty",
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
addrs := []string{
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[1].Listeners[0].Address.Port),
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[2].Listeners[0].Address.Port),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = addr
|
|
|
|
config.HttpClient = cleanhttp.DefaultClient()
|
|
|
|
config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cores[0].TLSConfig
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
secret, err := client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
t.Fatal("secret is nil")
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
if secret.Data["id"].(string) != cluster.RootToken {
|
2016-08-15 13:42:42 +00:00
|
|
|
t.Fatal("token mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function recreates the fuzzy testing from transit to pipe a large
|
|
|
|
// number of requests from the standbys to the active node.
|
|
|
|
func TestHTTP_Forwarding_Stress(t *testing.T) {
|
2017-05-24 13:34:59 +00:00
|
|
|
testHTTP_Forwarding_Stress_Common(t, false, 50)
|
|
|
|
testHTTP_Forwarding_Stress_Common(t, true, 50)
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 13:34:59 +00:00
|
|
|
func testHTTP_Forwarding_Stress_Common(t *testing.T, parallel bool, num uint64) {
|
2016-08-15 13:42:42 +00:00
|
|
|
testPlaintext := "the quick brown fox"
|
|
|
|
testPlaintextB64 := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"transit": transit.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
funcs := []string{"encrypt", "decrypt", "rotate", "change_min_version"}
|
|
|
|
keys := []string{"test1", "test2", "test3"}
|
|
|
|
|
|
|
|
hosts := []string{
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d/v1/transit/", cores[1].Listeners[0].Address.Port),
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d/v1/transit/", cores[2].Listeners[0].Address.Port),
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
transport := &http.Transport{
|
|
|
|
TLSClientConfig: cores[0].TLSConfig,
|
|
|
|
}
|
2017-02-27 17:49:35 +00:00
|
|
|
if err := http2.ConfigureTransport(transport); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return fmt.Errorf("redirects not allowed in this test")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//core.Logger().Printf("[TRACE] mounting transit")
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("https://127.0.0.1:%d/v1/sys/mounts/transit", cores[0].Listeners[0].Address.Port),
|
|
|
|
bytes.NewBuffer([]byte("{\"type\": \"transit\"}")))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
req.Header.Set(AuthHeaderName, cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
//core.Logger().Printf("[TRACE] done mounting transit")
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
var totalOps uint64
|
|
|
|
var successfulOps uint64
|
2016-08-15 13:42:42 +00:00
|
|
|
var key1ver int64 = 1
|
|
|
|
var key2ver int64 = 1
|
|
|
|
var key3ver int64 = 1
|
2016-08-19 15:03:53 +00:00
|
|
|
var numWorkers uint64 = 50
|
|
|
|
var numWorkersStarted uint64
|
|
|
|
var waitLock sync.Mutex
|
|
|
|
waitCond := sync.NewCond(&waitLock)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// This is the goroutine loop
|
2016-08-19 15:03:53 +00:00
|
|
|
doFuzzy := func(id int, parallel bool) {
|
|
|
|
var myTotalOps uint64
|
|
|
|
var mySuccessfulOps uint64
|
|
|
|
var keyVer int64 = 1
|
2016-08-15 13:42:42 +00:00
|
|
|
// Check for panics, otherwise notify we're done
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
core.Logger().Error("got a panic: %v", err)
|
2016-08-15 13:42:42 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
atomic.AddUint64(&totalOps, myTotalOps)
|
|
|
|
atomic.AddUint64(&successfulOps, mySuccessfulOps)
|
2016-08-15 13:42:42 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Holds the latest encrypted value for each key
|
|
|
|
latestEncryptedText := map[string]string{}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
var chosenFunc, chosenKey, chosenHost string
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
myRand := rand.New(rand.NewSource(int64(id) * 400))
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
doReq := func(method, url string, body io.Reader) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
req.Header.Set(AuthHeaderName, cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
doResp := func(resp *http.Response) (*api.Secret, error) {
|
|
|
|
if resp == nil {
|
|
|
|
return nil, fmt.Errorf("nil response")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Make sure we weren't redirected
|
|
|
|
if resp.StatusCode > 300 && resp.StatusCode < 400 {
|
|
|
|
return nil, fmt.Errorf("got status code %d, resp was %#v", resp.StatusCode, *resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := &api.Response{Response: resp}
|
2016-08-19 15:03:53 +00:00
|
|
|
err := result.Error()
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := api.ParseSecret(result.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chosenHost := range hosts {
|
|
|
|
for _, chosenKey := range keys {
|
|
|
|
// Try to write the key to make sure it exists
|
2016-08-19 15:03:53 +00:00
|
|
|
_, err := doReq("POST", chosenHost+"keys/"+fmt.Sprintf("%s-%t", chosenKey, parallel), bytes.NewBuffer([]byte("{}")))
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
if !parallel {
|
|
|
|
chosenHost = hosts[id%len(hosts)]
|
|
|
|
chosenKey = fmt.Sprintf("key-%t-%d", parallel, id)
|
|
|
|
|
|
|
|
_, err := doReq("POST", chosenHost+"keys/"+chosenKey, bytes.NewBuffer([]byte("{}")))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint64(&numWorkersStarted, 1)
|
|
|
|
|
|
|
|
waitCond.L.Lock()
|
|
|
|
for atomic.LoadUint64(&numWorkersStarted) != numWorkers {
|
|
|
|
waitCond.Wait()
|
|
|
|
}
|
|
|
|
waitCond.L.Unlock()
|
|
|
|
waitCond.Broadcast()
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
core.Logger().Trace("Starting goroutine", "id", id)
|
2016-08-19 15:03:53 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2016-08-15 13:42:42 +00:00
|
|
|
for {
|
|
|
|
// Stop after 10 seconds
|
|
|
|
if time.Now().Sub(startTime) > 10*time.Second {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
myTotalOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// Pick a function and a key
|
2016-08-19 15:03:53 +00:00
|
|
|
chosenFunc = funcs[myRand.Int()%len(funcs)]
|
|
|
|
if parallel {
|
|
|
|
chosenKey = fmt.Sprintf("%s-%t", keys[myRand.Int()%len(keys)], parallel)
|
|
|
|
chosenHost = hosts[myRand.Int()%len(hosts)]
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
switch chosenFunc {
|
|
|
|
// Encrypt our plaintext and store the result
|
|
|
|
case "encrypt":
|
|
|
|
//core.Logger().Printf("[TRACE] %s, %s, %d", chosenFunc, chosenKey, id)
|
|
|
|
resp, err := doReq("POST", chosenHost+"encrypt/"+chosenKey, bytes.NewBuffer([]byte(fmt.Sprintf("{\"plaintext\": \"%s\"}", testPlaintextB64))))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := doResp(resp)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
latest := secret.Data["ciphertext"].(string)
|
|
|
|
if latest == "" {
|
|
|
|
panic(fmt.Errorf("bad ciphertext"))
|
|
|
|
}
|
|
|
|
latestEncryptedText[chosenKey] = secret.Data["ciphertext"].(string)
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// Decrypt the ciphertext and compare the result
|
|
|
|
case "decrypt":
|
|
|
|
ct := latestEncryptedText[chosenKey]
|
|
|
|
if ct == "" {
|
2016-08-19 15:03:53 +00:00
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//core.Logger().Printf("[TRACE] %s, %s, %d", chosenFunc, chosenKey, id)
|
|
|
|
resp, err := doReq("POST", chosenHost+"decrypt/"+chosenKey, bytes.NewBuffer([]byte(fmt.Sprintf("{\"ciphertext\": \"%s\"}", ct))))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := doResp(resp)
|
|
|
|
if err != nil {
|
|
|
|
// This could well happen since the min version is jumping around
|
2016-10-26 23:52:31 +00:00
|
|
|
if strings.Contains(err.Error(), keysutil.ErrTooOld) {
|
2016-08-19 15:03:53 +00:00
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptb64 := secret.Data["plaintext"].(string)
|
|
|
|
pt, err := base64.StdEncoding.DecodeString(ptb64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("got an error decoding base64 plaintext: %v", err))
|
|
|
|
}
|
|
|
|
if string(pt) != testPlaintext {
|
|
|
|
panic(fmt.Errorf("got bad plaintext back: %s", pt))
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// Rotate to a new key version
|
|
|
|
case "rotate":
|
|
|
|
//core.Logger().Printf("[TRACE] %s, %s, %d", chosenFunc, chosenKey, id)
|
|
|
|
_, err := doReq("POST", chosenHost+"keys/"+chosenKey+"/rotate", bytes.NewBuffer([]byte("{}")))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
if parallel {
|
|
|
|
switch chosenKey {
|
|
|
|
case "test1":
|
|
|
|
atomic.AddInt64(&key1ver, 1)
|
|
|
|
case "test2":
|
|
|
|
atomic.AddInt64(&key2ver, 1)
|
|
|
|
case "test3":
|
|
|
|
atomic.AddInt64(&key3ver, 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyVer++
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
|
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// Change the min version, which also tests the archive functionality
|
|
|
|
case "change_min_version":
|
2016-08-19 15:03:53 +00:00
|
|
|
var latestVersion int64 = keyVer
|
|
|
|
if parallel {
|
|
|
|
switch chosenKey {
|
|
|
|
case "test1":
|
|
|
|
latestVersion = atomic.LoadInt64(&key1ver)
|
|
|
|
case "test2":
|
|
|
|
latestVersion = atomic.LoadInt64(&key2ver)
|
|
|
|
case "test3":
|
|
|
|
latestVersion = atomic.LoadInt64(&key3ver)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
setVersion := (myRand.Int63() % latestVersion) + 1
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
//core.Logger().Printf("[TRACE] %s, %s, %d, new min version %d", chosenFunc, chosenKey, id, setVersion)
|
|
|
|
|
|
|
|
_, err := doReq("POST", chosenHost+"keys/"+chosenKey+"/config", bytes.NewBuffer([]byte(fmt.Sprintf("{\"min_decryption_version\": %d}", setVersion))))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
mySuccessfulOps++
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
atomic.StoreUint64(&numWorkers, num)
|
|
|
|
|
|
|
|
// Spawn some of these workers for 10 seconds
|
|
|
|
for i := 0; i < int(atomic.LoadUint64(&numWorkers)); i++ {
|
2016-08-15 13:42:42 +00:00
|
|
|
wg.Add(1)
|
|
|
|
//core.Logger().Printf("[TRACE] spawning %d", i)
|
2016-08-19 15:03:53 +00:00
|
|
|
go doFuzzy(i+1, parallel)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for them all to finish
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
if totalOps == 0 || totalOps != successfulOps {
|
2017-05-24 13:34:59 +00:00
|
|
|
t.Fatalf("total/successful ops zero or mismatch: %d/%d; parallel: %t, num %d", totalOps, successfulOps, parallel, num)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2017-05-24 13:34:59 +00:00
|
|
|
t.Logf("total operations tried: %d, total successful: %d; parallel: %t, num %d", totalOps, successfulOps, parallel, num)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This tests TLS connection state forwarding by ensuring that we can use a
|
|
|
|
// client TLS to authenticate against the cert backend
|
|
|
|
func TestHTTP_Forwarding_ClientTLS(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
CredentialBackends: map[string]logical.Factory{
|
|
|
|
"cert": credCert.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
transport := cleanhttp.DefaultTransport()
|
|
|
|
transport.TLSClientConfig = cores[0].TLSConfig
|
2017-02-27 17:49:35 +00:00
|
|
|
if err := http2.ConfigureTransport(transport); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("https://127.0.0.1:%d/v1/sys/auth/cert", cores[0].Listeners[0].Address.Port),
|
|
|
|
bytes.NewBuffer([]byte("{\"type\": \"cert\"}")))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
req.Header.Set(AuthHeaderName, cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type certConfig struct {
|
|
|
|
Certificate string `json:"certificate"`
|
|
|
|
Policies string `json:"policies"`
|
|
|
|
}
|
|
|
|
encodedCertConfig, err := json.Marshal(&certConfig{
|
2017-07-31 15:28:06 +00:00
|
|
|
Certificate: string(cluster.CACertPEM),
|
2016-08-15 13:42:42 +00:00
|
|
|
Policies: "default",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest("POST", fmt.Sprintf("https://127.0.0.1:%d/v1/auth/cert/certs/test", cores[0].Listeners[0].Address.Port),
|
|
|
|
bytes.NewBuffer(encodedCertConfig))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
req.Header.Set(AuthHeaderName, cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs := []string{
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[1].Listeners[0].Address.Port),
|
|
|
|
fmt.Sprintf("https://127.0.0.1:%d", cores[2].Listeners[0].Address.Port),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we can't possibly use lingering connections even though it should be to a different address
|
|
|
|
|
|
|
|
transport = cleanhttp.DefaultTransport()
|
|
|
|
transport.TLSClientConfig = cores[0].TLSConfig
|
|
|
|
|
|
|
|
client = &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return fmt.Errorf("redirects not allowed in this test")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
//cores[0].Logger().Printf("cluster.RootToken token is %s", cluster.RootToken)
|
2016-08-15 13:42:42 +00:00
|
|
|
//time.Sleep(4 * time.Hour)
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
2017-02-27 17:49:35 +00:00
|
|
|
client := cores[0].Client
|
|
|
|
client.SetAddress(addr)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/cert/login", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
t.Fatal("secret is nil")
|
|
|
|
}
|
|
|
|
if secret.Auth == nil {
|
|
|
|
t.Fatal("auth is nil")
|
|
|
|
}
|
|
|
|
if secret.Auth.Policies == nil || len(secret.Auth.Policies) == 0 || secret.Auth.Policies[0] != "default" {
|
|
|
|
t.Fatalf("bad policies: %#v", secret.Auth.Policies)
|
|
|
|
}
|
2016-08-17 20:15:15 +00:00
|
|
|
if secret.Auth.ClientToken == "" {
|
|
|
|
t.Fatalf("bad client token: %#v", *secret.Auth)
|
|
|
|
}
|
|
|
|
client.SetToken(secret.Auth.ClientToken)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
t.Fatal("secret is nil")
|
|
|
|
}
|
|
|
|
if secret.Data == nil || len(secret.Data) == 0 {
|
|
|
|
t.Fatal("secret data was empty")
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 20:58:50 +00:00
|
|
|
|
|
|
|
func TestHTTP_Forwarding_HelpOperation(t *testing.T) {
|
2017-07-31 15:28:06 +00:00
|
|
|
cluster := vault.NewTestCluster(t, &vault.CoreConfig{}, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
|
|
|
|
2017-05-04 20:58:50 +00:00
|
|
|
vault.TestWaitActive(t, cores[0].Core)
|
|
|
|
|
|
|
|
testHelp := func(client *api.Client) {
|
|
|
|
help, err := client.Help("auth/token")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if help == nil {
|
|
|
|
t.Fatal("help was nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testHelp(cores[0].Client)
|
|
|
|
testHelp(cores[1].Client)
|
|
|
|
}
|