2015-05-11 17:56:41 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2020-02-12 22:20:22 +00:00
|
|
|
"crypto/tls"
|
2016-07-06 16:25:40 +00:00
|
|
|
"encoding/json"
|
2015-08-10 17:27:25 +00:00
|
|
|
"errors"
|
2020-02-12 22:20:22 +00:00
|
|
|
"io/ioutil"
|
2015-05-11 17:56:41 +00:00
|
|
|
"net/http"
|
2015-08-10 17:27:25 +00:00
|
|
|
"net/http/httptest"
|
2018-09-18 03:03:00 +00:00
|
|
|
"net/textproto"
|
2020-02-12 22:20:22 +00:00
|
|
|
"net/url"
|
2015-05-11 17:56:41 +00:00
|
|
|
"reflect"
|
2017-08-07 14:03:30 +00:00
|
|
|
"strings"
|
2015-05-11 17:56:41 +00:00
|
|
|
"testing"
|
2021-10-20 12:44:56 +00:00
|
|
|
"time"
|
2015-05-11 17:56:41 +00:00
|
|
|
|
2019-04-24 19:27:43 +00:00
|
|
|
"github.com/go-test/deep"
|
2021-04-20 22:25:04 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2021-10-13 19:24:31 +00:00
|
|
|
kv "github.com/hashicorp/vault-plugin-secrets-kv"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/audit"
|
|
|
|
auditFile "github.com/hashicorp/vault/builtin/audit/file"
|
2018-11-07 01:21:24 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-05-11 17:56:41 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func TestHandler_parseMFAHandler(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var expectedMFACreds logical.MFACreds
|
|
|
|
req := &logical.Request{
|
|
|
|
Headers: make(map[string][]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
headerName := textproto.CanonicalMIMEHeaderKey(MFAHeaderName)
|
|
|
|
|
|
|
|
// Set TOTP passcode in the MFA header
|
|
|
|
req.Headers[headerName] = []string{
|
|
|
|
"my_totp:123456",
|
|
|
|
"my_totp:111111",
|
|
|
|
"my_second_mfa:hi=hello",
|
|
|
|
"my_third_mfa",
|
|
|
|
}
|
|
|
|
err = parseMFAHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it is being parsed properly
|
|
|
|
expectedMFACreds = logical.MFACreds{
|
|
|
|
"my_totp": []string{
|
|
|
|
"123456",
|
|
|
|
"111111",
|
|
|
|
},
|
|
|
|
"my_second_mfa": []string{
|
|
|
|
"hi=hello",
|
|
|
|
},
|
|
|
|
"my_third_mfa": []string{},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expectedMFACreds, req.MFACreds) {
|
|
|
|
t.Fatalf("bad: parsed MFACreds; expected: %#v\n actual: %#v\n", expectedMFACreds, req.MFACreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the creds of a method type in different headers and check if they
|
|
|
|
// all get merged together
|
|
|
|
req.Headers[headerName] = []string{
|
|
|
|
"my_mfa:passcode=123456",
|
|
|
|
"my_mfa:month=july",
|
|
|
|
"my_mfa:day=tuesday",
|
|
|
|
}
|
|
|
|
err = parseMFAHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMFACreds = logical.MFACreds{
|
|
|
|
"my_mfa": []string{
|
|
|
|
"passcode=123456",
|
|
|
|
"month=july",
|
|
|
|
"day=tuesday",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expectedMFACreds, req.MFACreds) {
|
|
|
|
t.Fatalf("bad: parsed MFACreds; expected: %#v\n actual: %#v\n", expectedMFACreds, req.MFACreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header without method name should error out
|
|
|
|
req.Headers[headerName] = []string{
|
|
|
|
":passcode=123456",
|
|
|
|
}
|
|
|
|
err = parseMFAHeader(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error; actual: %#v\n", req.MFACreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header without method name and method value should error out
|
|
|
|
req.Headers[headerName] = []string{
|
|
|
|
":",
|
|
|
|
}
|
|
|
|
err = parseMFAHeader(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error; actual: %#v\n", req.MFACreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header without method name and method value should error out
|
|
|
|
req.Headers[headerName] = []string{
|
|
|
|
"my_totp:",
|
|
|
|
}
|
|
|
|
err = parseMFAHeader(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error; actual: %#v\n", req.MFACreds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
func TestHandler_cors(t *testing.T) {
|
|
|
|
core, _, _ := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
// Enable CORS and allow from any origin for testing.
|
|
|
|
corsConfig := core.CORSConfig()
|
2018-01-19 06:44:44 +00:00
|
|
|
err := corsConfig.Enable(context.Background(), []string{addr}, nil)
|
2017-06-17 04:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error enabling CORS: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodOptions, addr+"/v1/sys/seal-status", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Origin", "BAD ORIGIN")
|
|
|
|
|
|
|
|
// Requests from unacceptable origins will be rejected with a 403.
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusForbidden {
|
|
|
|
t.Fatalf("Bad status:\nexpected: 403 Forbidden\nactual: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test preflight requests
|
|
|
|
//
|
|
|
|
|
|
|
|
// Set a valid origin
|
|
|
|
req.Header.Set("Origin", addr)
|
|
|
|
|
|
|
|
// Server should NOT accept arbitrary methods.
|
|
|
|
req.Header.Set("Access-Control-Request-Method", "FOO")
|
|
|
|
|
|
|
|
client = cleanhttp.DefaultClient()
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if an arbitrary method is accepted.
|
|
|
|
if resp.StatusCode != http.StatusMethodNotAllowed {
|
|
|
|
t.Fatalf("Bad status:\nexpected: 405 Method Not Allowed\nactual: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server SHOULD accept acceptable methods.
|
|
|
|
req.Header.Set("Access-Control-Request-Method", http.MethodPost)
|
|
|
|
|
|
|
|
client = cleanhttp.DefaultClient()
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test that the CORS headers are applied correctly.
|
|
|
|
//
|
|
|
|
expHeaders := map[string]string{
|
|
|
|
"Access-Control-Allow-Origin": addr,
|
2017-08-07 19:02:08 +00:00
|
|
|
"Access-Control-Allow-Headers": strings.Join(vault.StdAllowedHeaders, ","),
|
2017-06-17 04:04:55 +00:00
|
|
|
"Access-Control-Max-Age": "300",
|
2018-10-02 18:30:10 +00:00
|
|
|
"Vary": "Origin",
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for expHeader, expected := range expHeaders {
|
|
|
|
actual := resp.Header.Get(expHeader)
|
|
|
|
if actual == "" {
|
|
|
|
t.Fatalf("bad:\nHeader: %#v was not on response.", expHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\nExpected: %#v\nActual: %#v\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
func TestHandler_HostnameHeader(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
config *vault.CoreConfig
|
|
|
|
headerPresent bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "with no header configured",
|
|
|
|
config: nil,
|
|
|
|
headerPresent: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with header configured",
|
|
|
|
config: &vault.CoreConfig{
|
|
|
|
EnableResponseHeaderHostname: true,
|
|
|
|
},
|
|
|
|
headerPresent: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
var core *vault.Core
|
|
|
|
|
|
|
|
if tc.config == nil {
|
|
|
|
core, _, _ = vault.TestCoreUnsealed(t)
|
|
|
|
} else {
|
|
|
|
core, _, _ = vault.TestCoreUnsealedWithConfig(t, tc.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", addr+"/v1/sys/seal-status", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("nil response")
|
|
|
|
}
|
|
|
|
|
|
|
|
hnHeader := resp.Header.Get("X-Vault-Hostname")
|
|
|
|
if tc.headerPresent && hnHeader == "" {
|
|
|
|
t.Logf("header configured = %t", core.HostnameHeaderEnabled())
|
|
|
|
t.Fatal("missing 'X-Vault-Hostname' header entry in response")
|
|
|
|
}
|
|
|
|
if !tc.headerPresent && hnHeader != "" {
|
|
|
|
t.Fatal("didn't expect 'X-Vault-Hostname' header but it was present anyway")
|
|
|
|
}
|
|
|
|
|
|
|
|
rniHeader := resp.Header.Get("X-Vault-Raft-Node-ID")
|
|
|
|
if rniHeader != "" {
|
|
|
|
t.Fatalf("no raft node ID header was expected, since we're not running a raft cluster. instead, got %s", rniHeader)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:53:07 +00:00
|
|
|
func TestHandler_CacheControlNoStore(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", addr+"/v1/sys/mounts", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, token)
|
2016-12-15 22:53:07 +00:00
|
|
|
req.Header.Set(WrapTTLHeaderName, "60s")
|
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("nil response")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resp.Header.Get("Cache-Control")
|
|
|
|
|
|
|
|
if actual == "" {
|
|
|
|
t.Fatalf("missing 'Cache-Control' header entry in response writer")
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "no-store" {
|
|
|
|
t.Fatalf("bad: Cache-Control. Expected: 'no-store', Actual: %q", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:17:25 +00:00
|
|
|
// TestHandler_MissingToken tests the response / error code if a request comes
|
|
|
|
// in with a missing client token. See
|
|
|
|
// https://github.com/hashicorp/vault/issues/8377
|
|
|
|
func TestHandler_MissingToken(t *testing.T) {
|
|
|
|
// core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
core, _, _ := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", addr+"/v1/sys/internal/ui/mounts/cubbyhole", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set(WrapTTLHeaderName, "60s")
|
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 400 {
|
|
|
|
t.Fatalf("expected code 400, got: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
func TestHandler_Accepted(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", addr+"/v1/auth/token/tidy", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, token)
|
2018-07-11 19:45:09 +00:00
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testResponseStatus(t, resp, 202)
|
|
|
|
}
|
|
|
|
|
2015-05-11 17:56:41 +00:00
|
|
|
// We use this test to verify header auth
|
|
|
|
func TestSysMounts_headerAuth(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", addr+"/v1/sys/mounts", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, token)
|
2015-05-11 17:56:41 +00:00
|
|
|
|
2015-10-22 18:37:12 +00:00
|
|
|
client := cleanhttp.DefaultClient()
|
2015-05-11 17:56:41 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
2016-08-08 20:00:31 +00:00
|
|
|
"lease_id": "",
|
|
|
|
"renewable": false,
|
|
|
|
"lease_duration": json.Number("0"),
|
|
|
|
"wrap_info": nil,
|
|
|
|
"warnings": nil,
|
|
|
|
"auth": nil,
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"secret/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "key/value secret storage",
|
|
|
|
"type": "kv",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2016-08-08 20:00:31 +00:00
|
|
|
"config": map[string]interface{}{
|
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
2017-03-08 14:20:09 +00:00
|
|
|
"force_no_cache": false,
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
2017-11-13 16:22:22 +00:00
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
2018-04-09 20:14:44 +00:00
|
|
|
"options": map[string]interface{}{"version": "1"},
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
|
|
|
"sys/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "system endpoints used for control, policy and debugging",
|
|
|
|
"type": "system",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2016-08-08 20:00:31 +00:00
|
|
|
"config": map[string]interface{}{
|
2019-02-20 20:12:21 +00:00
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
2019-02-15 19:15:02 +00:00
|
|
|
"passthrough_request_headers": []interface{}{"Accept"},
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
2017-11-13 16:22:22 +00:00
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
2018-03-21 19:04:27 +00:00
|
|
|
"options": interface{}(nil),
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
|
|
|
"cubbyhole/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "per-token private secret storage",
|
|
|
|
"type": "cubbyhole",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2016-08-08 20:00:31 +00:00
|
|
|
"config": map[string]interface{}{
|
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
2017-03-08 14:20:09 +00:00
|
|
|
"force_no_cache": false,
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
2017-11-13 16:22:22 +00:00
|
|
|
"local": true,
|
|
|
|
"seal_wrap": false,
|
2018-03-21 19:04:27 +00:00
|
|
|
"options": interface{}(nil),
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
2017-10-11 17:21:20 +00:00
|
|
|
"identity/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "identity store",
|
|
|
|
"type": "identity",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2017-10-11 17:21:20 +00:00
|
|
|
"config": map[string]interface{}{
|
2021-10-14 01:59:36 +00:00
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
|
|
|
"passthrough_request_headers": []interface{}{"Authorization"},
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
2017-11-13 16:22:22 +00:00
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
2018-03-21 19:04:27 +00:00
|
|
|
"options": interface{}(nil),
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
2016-08-08 20:00:31 +00:00
|
|
|
},
|
2018-09-05 15:45:17 +00:00
|
|
|
"secret/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "key/value secret storage",
|
|
|
|
"type": "kv",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2018-09-05 15:45:17 +00:00
|
|
|
"config": map[string]interface{}{
|
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
|
|
|
},
|
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
|
|
|
"options": map[string]interface{}{"version": "1"},
|
|
|
|
},
|
|
|
|
"sys/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "system endpoints used for control, policy and debugging",
|
|
|
|
"type": "system",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2018-09-05 15:45:17 +00:00
|
|
|
"config": map[string]interface{}{
|
2019-02-20 20:12:21 +00:00
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
2019-02-15 19:15:02 +00:00
|
|
|
"passthrough_request_headers": []interface{}{"Accept"},
|
2018-09-05 15:45:17 +00:00
|
|
|
},
|
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
|
|
|
"options": interface{}(nil),
|
|
|
|
},
|
|
|
|
"cubbyhole/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "per-token private secret storage",
|
|
|
|
"type": "cubbyhole",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2018-09-05 15:45:17 +00:00
|
|
|
"config": map[string]interface{}{
|
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
|
|
|
},
|
|
|
|
"local": true,
|
|
|
|
"seal_wrap": false,
|
|
|
|
"options": interface{}(nil),
|
|
|
|
},
|
|
|
|
"identity/": map[string]interface{}{
|
2019-11-07 16:54:34 +00:00
|
|
|
"description": "identity store",
|
|
|
|
"type": "identity",
|
2019-10-17 17:33:00 +00:00
|
|
|
"external_entropy_access": false,
|
2018-09-05 15:45:17 +00:00
|
|
|
"config": map[string]interface{}{
|
2021-10-14 01:59:36 +00:00
|
|
|
"default_lease_ttl": json.Number("0"),
|
|
|
|
"max_lease_ttl": json.Number("0"),
|
|
|
|
"force_no_cache": false,
|
|
|
|
"passthrough_request_headers": []interface{}{"Authorization"},
|
2018-09-05 15:45:17 +00:00
|
|
|
},
|
|
|
|
"local": false,
|
|
|
|
"seal_wrap": false,
|
|
|
|
"options": interface{}(nil),
|
|
|
|
},
|
2015-05-11 17:56:41 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-08-08 20:00:31 +00:00
|
|
|
|
|
|
|
expected["request_id"] = actual["request_id"]
|
2017-06-26 17:14:36 +00:00
|
|
|
for k, v := range actual["data"].(map[string]interface{}) {
|
|
|
|
if v.(map[string]interface{})["accessor"] == "" {
|
|
|
|
t.Fatalf("no accessor from %s", k)
|
|
|
|
}
|
2019-04-24 19:27:43 +00:00
|
|
|
if v.(map[string]interface{})["uuid"] == "" {
|
|
|
|
t.Fatalf("no uuid from %s", k)
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:45:17 +00:00
|
|
|
expected[k].(map[string]interface{})["accessor"] = v.(map[string]interface{})["accessor"]
|
2019-04-24 19:27:43 +00:00
|
|
|
expected[k].(map[string]interface{})["uuid"] = v.(map[string]interface{})["uuid"]
|
2017-06-26 17:14:36 +00:00
|
|
|
expected["data"].(map[string]interface{})[k].(map[string]interface{})["accessor"] = v.(map[string]interface{})["accessor"]
|
2019-04-24 19:27:43 +00:00
|
|
|
expected["data"].(map[string]interface{})[k].(map[string]interface{})["uuid"] = v.(map[string]interface{})["uuid"]
|
2017-06-26 17:14:36 +00:00
|
|
|
}
|
2016-08-08 20:00:31 +00:00
|
|
|
|
2019-02-15 19:15:02 +00:00
|
|
|
if diff := deep.Equal(actual, expected); len(diff) > 0 {
|
|
|
|
t.Fatalf("bad, diff: %#v", diff)
|
2015-05-11 17:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 07:59:19 +00:00
|
|
|
|
2016-05-08 01:08:13 +00:00
|
|
|
// We use this test to verify header auth wrapping
|
|
|
|
func TestSysMounts_headerAuth_Wrapped(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", addr+"/v1/sys/mounts", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, token)
|
2016-05-08 01:08:13 +00:00
|
|
|
req.Header.Set(WrapTTLHeaderName, "60s")
|
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:00:31 +00:00
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"request_id": "",
|
|
|
|
"lease_id": "",
|
|
|
|
"renewable": false,
|
|
|
|
"lease_duration": json.Number("0"),
|
|
|
|
"data": nil,
|
|
|
|
"wrap_info": map[string]interface{}{
|
|
|
|
"ttl": json.Number("60"),
|
|
|
|
},
|
|
|
|
"warnings": nil,
|
|
|
|
"auth": nil,
|
|
|
|
}
|
|
|
|
|
2016-05-08 01:08:13 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-08-08 20:00:31 +00:00
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
|
|
|
actualToken, ok := actual["wrap_info"].(map[string]interface{})["token"]
|
|
|
|
if !ok || actualToken == "" {
|
|
|
|
t.Fatal("token missing in wrap info")
|
|
|
|
}
|
|
|
|
expected["wrap_info"].(map[string]interface{})["token"] = actualToken
|
|
|
|
|
|
|
|
actualCreationTime, ok := actual["wrap_info"].(map[string]interface{})["creation_time"]
|
|
|
|
if !ok || actualCreationTime == "" {
|
|
|
|
t.Fatal("creation_time missing in wrap info")
|
|
|
|
}
|
|
|
|
expected["wrap_info"].(map[string]interface{})["creation_time"] = actualCreationTime
|
|
|
|
|
2017-08-02 22:28:58 +00:00
|
|
|
actualCreationPath, ok := actual["wrap_info"].(map[string]interface{})["creation_path"]
|
|
|
|
if !ok || actualCreationPath == "" {
|
|
|
|
t.Fatal("creation_path missing in wrap info")
|
|
|
|
}
|
|
|
|
expected["wrap_info"].(map[string]interface{})["creation_path"] = actualCreationPath
|
|
|
|
|
2017-11-13 20:31:32 +00:00
|
|
|
actualAccessor, ok := actual["wrap_info"].(map[string]interface{})["accessor"]
|
|
|
|
if !ok || actualAccessor == "" {
|
|
|
|
t.Fatal("accessor missing in wrap info")
|
|
|
|
}
|
|
|
|
expected["wrap_info"].(map[string]interface{})["accessor"] = actualAccessor
|
|
|
|
|
2016-08-08 20:00:31 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad:\nExpected: %#v\nActual: %#v\n%T %T", expected, actual, actual["warnings"], actual["data"])
|
2016-05-08 01:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 07:59:19 +00:00
|
|
|
func TestHandler_sealed(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
core.Seal(token)
|
|
|
|
|
|
|
|
resp, err := http.Get(addr + "/v1/secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 503)
|
|
|
|
}
|
2015-08-10 17:27:25 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func TestHandler_ui_default(t *testing.T) {
|
|
|
|
core := vault.TestCoreUI(t, false)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(addr + "/ui/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_ui_enabled(t *testing.T) {
|
|
|
|
core := vault.TestCoreUI(t, true)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(addr + "/ui/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:27:25 +00:00
|
|
|
func TestHandler_error(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2018-04-09 18:35:21 +00:00
|
|
|
respondError(w, 500, errors.New("test Error"))
|
2015-08-10 17:27:25 +00:00
|
|
|
|
|
|
|
if w.Code != 500 {
|
|
|
|
t.Fatalf("expected 500, got %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The code inside of the error should override
|
|
|
|
// the argument to respondError
|
|
|
|
w2 := httptest.NewRecorder()
|
|
|
|
e := logical.CodedError(403, "error text")
|
|
|
|
|
|
|
|
respondError(w2, 500, e)
|
|
|
|
|
|
|
|
if w2.Code != 403 {
|
|
|
|
t.Fatalf("expected 403, got %d", w2.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// vault.ErrSealed is a special case
|
|
|
|
w3 := httptest.NewRecorder()
|
|
|
|
|
2017-02-16 20:15:02 +00:00
|
|
|
respondError(w3, 400, consts.ErrSealed)
|
2015-08-10 17:27:25 +00:00
|
|
|
|
|
|
|
if w3.Code != 503 {
|
|
|
|
t.Fatalf("expected 503, got %d", w3.Code)
|
|
|
|
}
|
2017-12-16 01:19:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:33:21 +00:00
|
|
|
func TestHandler_requestAuth(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
|
|
|
|
rootCtx := namespace.RootContext(nil)
|
|
|
|
te, err := core.LookupToken(rootCtx, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rWithAuthorization, err := http.NewRequest("GET", "v1/test/path", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
rWithAuthorization.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
|
|
|
|
rWithVault, err := http.NewRequest("GET", "v1/test/path", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
rWithVault.Header.Set(consts.AuthHeaderName, token)
|
|
|
|
|
|
|
|
for _, r := range []*http.Request{rWithVault, rWithAuthorization} {
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "test/path")
|
|
|
|
r = r.WithContext(rootCtx)
|
2021-06-11 17:18:16 +00:00
|
|
|
requestAuth(r, req)
|
|
|
|
err = core.PopulateTokenEntry(rootCtx, req)
|
2018-10-01 17:33:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.ClientToken != token {
|
|
|
|
t.Fatalf("client token should be filled with %s, got %s", token, req.ClientToken)
|
|
|
|
}
|
|
|
|
if req.TokenEntry() == nil {
|
|
|
|
t.Fatal("token entry should not be nil")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(req.TokenEntry(), te) {
|
|
|
|
t.Fatalf("token entry should be the same as the core")
|
|
|
|
}
|
|
|
|
if req.ClientTokenAccessor == "" {
|
|
|
|
t.Fatal("token accessor should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rNothing, err := http.NewRequest("GET", "v1/test/path", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2019-02-05 21:02:15 +00:00
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "test/path")
|
2018-10-01 17:33:21 +00:00
|
|
|
|
2021-06-11 17:18:16 +00:00
|
|
|
requestAuth(rNothing, req)
|
|
|
|
err = core.PopulateTokenEntry(rootCtx, req)
|
2018-10-01 17:33:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected no error, got %s", err)
|
|
|
|
}
|
|
|
|
if req.ClientToken != "" {
|
|
|
|
t.Fatalf("client token should not be filled, got %s", req.ClientToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_getTokenFromReq(t *testing.T) {
|
|
|
|
r := http.Request{Header: http.Header{}}
|
|
|
|
|
2019-02-05 21:02:15 +00:00
|
|
|
tok, _ := getTokenFromReq(&r)
|
|
|
|
if tok != "" {
|
2018-10-01 17:33:21 +00:00
|
|
|
t.Fatalf("expected '' as result, got '%s'", tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set("Authorization", "Bearer TOKEN NOT_GOOD_TOKEN")
|
2019-02-05 21:02:15 +00:00
|
|
|
token, fromHeader := getTokenFromReq(&r)
|
|
|
|
if !fromHeader {
|
|
|
|
t.Fatal("expected from header")
|
|
|
|
} else if token != "TOKEN NOT_GOOD_TOKEN" {
|
|
|
|
t.Fatal("did not get expected token value")
|
|
|
|
} else if r.Header.Get("Authorization") == "" {
|
|
|
|
t.Fatal("expected value to be passed through")
|
2018-10-01 17:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set(consts.AuthHeaderName, "NEWTOKEN")
|
2019-02-05 21:02:15 +00:00
|
|
|
tok, _ = getTokenFromReq(&r)
|
|
|
|
if tok == "TOKEN" {
|
2018-10-01 17:33:21 +00:00
|
|
|
t.Fatalf("%s header should be prioritized", consts.AuthHeaderName)
|
|
|
|
} else if tok != "NEWTOKEN" {
|
|
|
|
t.Fatalf("expected 'NEWTOKEN' as result, got '%s'", tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header = http.Header{}
|
|
|
|
r.Header.Set("Authorization", "Basic TOKEN")
|
2019-02-05 21:02:15 +00:00
|
|
|
tok, fromHeader = getTokenFromReq(&r)
|
|
|
|
if tok != "" {
|
2018-10-01 17:33:21 +00:00
|
|
|
t.Fatalf("expected '' as result, got '%s'", tok)
|
2019-02-05 21:02:15 +00:00
|
|
|
} else if fromHeader {
|
|
|
|
t.Fatal("expected not from header")
|
2018-10-01 17:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 01:19:37 +00:00
|
|
|
func TestHandler_nonPrintableChars(t *testing.T) {
|
2018-07-12 20:29:36 +00:00
|
|
|
testNonPrintable(t, false)
|
|
|
|
testNonPrintable(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNonPrintable(t *testing.T, disable bool) {
|
2018-11-19 21:13:16 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealedWithConfig(t, &vault.CoreConfig{
|
|
|
|
DisableKeyEncodingChecks: disable,
|
|
|
|
})
|
2018-07-12 20:29:36 +00:00
|
|
|
ln, addr := TestListener(t)
|
|
|
|
props := &vault.HandlerProperties{
|
|
|
|
Core: core,
|
|
|
|
DisablePrintableCheck: disable,
|
|
|
|
}
|
|
|
|
TestServerWithListenerAndProperties(t, ln, addr, core, props)
|
2017-12-16 01:19:37 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
req, err := http.NewRequest("PUT", addr+"/v1/cubbyhole/foo\u2028bar", strings.NewReader(`{"zip": "zap"}`))
|
2017-12-16 01:19:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, token)
|
2017-12-16 01:19:37 +00:00
|
|
|
|
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-08-10 17:27:25 +00:00
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
if disable {
|
|
|
|
testResponseStatus(t, resp, 204)
|
|
|
|
} else {
|
|
|
|
testResponseStatus(t, resp, 400)
|
|
|
|
}
|
2015-08-10 17:27:25 +00:00
|
|
|
}
|
2020-02-12 22:20:22 +00:00
|
|
|
|
|
|
|
func TestHandler_Parse_Form(t *testing.T) {
|
|
|
|
cluster := vault.NewTestCluster(t, &vault.CoreConfig{}, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
c := cleanhttp.DefaultClient()
|
|
|
|
c.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: cluster.RootCAs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
values := url.Values{
|
|
|
|
"zip": []string{"zap"},
|
|
|
|
"abc": []string{"xyz"},
|
|
|
|
"multi": []string{"first", "second"},
|
|
|
|
"empty": []string{},
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", cores[0].Client.Address()+"/v1/secret/foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(values.Encode()))
|
|
|
|
req.Header.Set("x-vault-token", cluster.RootToken)
|
|
|
|
req.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 204 {
|
|
|
|
t.Fatalf("bad response: %#v\nrequest was: %#v\nurl was: %#v", *resp, *req, req.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := cores[0].Client
|
|
|
|
client.SetToken(cluster.RootToken)
|
|
|
|
|
|
|
|
apiResp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if apiResp == nil {
|
|
|
|
t.Fatal("api resp is nil")
|
|
|
|
}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
"abc": "xyz",
|
|
|
|
"multi": "first,second",
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(expected, apiResp.Data); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 19:24:31 +00:00
|
|
|
|
|
|
|
func TestHandler_Patch_BadContentTypeHeader(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"kv": kv.VersionedKVFactory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
core := cores[0].Core
|
|
|
|
c := cluster.Cores[0].Client
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
// Mount a KVv2 backend
|
|
|
|
err := c.Sys().Mount("kv", &api.MountInput{
|
|
|
|
Type: "kv-v2",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kvData := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"bar": "a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.Logical().Write("kv/data/foo", kvData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write failed - err :%#v, resp: %#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = c.Logical().Read("kv/data/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("read failed - err :%#v, resp: %#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := c.NewRequest("PATCH", "/v1/kv/data/foo")
|
|
|
|
req.Headers = http.Header{
|
|
|
|
"Content-Type": []string{"application/json"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.SetJSONBody(kvData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiResp, err := c.RawRequestWithContext(context.Background(), req)
|
|
|
|
if err == nil || apiResp.StatusCode != http.StatusUnsupportedMediaType {
|
|
|
|
t.Fatalf("expected PATCH request to fail with %d status code - err :%#v, resp: %#v\n", http.StatusUnsupportedMediaType, err, apiResp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
func kvRequestWithRetry(t *testing.T, req func() (*api.Secret, error)) (*api.Secret, error){
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var resp *api.Secret
|
|
|
|
|
|
|
|
// Loop until return message does not indicate upgrade, or timeout.
|
|
|
|
timeout := time.After(20 * time.Second)
|
|
|
|
ticker := time.Tick(time.Second)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Error("timeout expired waiting for upgrade")
|
|
|
|
case <-ticker:
|
|
|
|
resp, err = req()
|
|
|
|
|
|
|
|
if err == nil {
|
2021-10-20 17:41:51 +00:00
|
|
|
return resp, nil
|
2021-10-20 12:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
responseError := err.(*api.ResponseError)
|
|
|
|
if !strings.Contains(responseError.Error(), "Upgrading from non-versioned to versioned data") {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:24:31 +00:00
|
|
|
func TestHandler_Patch_NotFound(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"kv": kv.VersionedKVFactory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
core := cores[0].Core
|
|
|
|
c := cluster.Cores[0].Client
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
// Mount a KVv2 backend
|
|
|
|
err := c.Sys().Mount("kv", &api.MountInput{
|
|
|
|
Type: "kv-v2",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kvData := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"bar": "a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
resp, err := kvRequestWithRetry(t, func() (*api.Secret, error) {
|
|
|
|
return c.Logical().JSONMergePatch(context.Background(), "kv/data/foo", kvData)
|
|
|
|
})
|
|
|
|
|
2021-10-13 19:24:31 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected PATCH request to fail, resp: %#v\n", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
responseError := err.(*api.ResponseError)
|
|
|
|
if responseError.StatusCode != http.StatusNotFound {
|
|
|
|
t.Fatalf("expected PATCH request to fail with %d status code - err: %#v, resp: %#v\n", http.StatusNotFound, responseError, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_Patch_Audit(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"kv": kv.VersionedKVFactory,
|
|
|
|
},
|
|
|
|
AuditBackends: map[string]audit.Factory{
|
|
|
|
"file": auditFile.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
})
|
|
|
|
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
core := cores[0].Core
|
|
|
|
c := cluster.Cores[0].Client
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
|
|
|
if err := c.Sys().Mount("kv/", &api.MountInput{
|
|
|
|
Type: "kv-v2",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("kv-v2 mount attempt failed - err: %#v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
auditLogFile, err := ioutil.TempFile("", "httppatch")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Sys().EnableAuditWithOptions("file", &api.EnableAuditOptions{
|
|
|
|
Type: "file",
|
|
|
|
Options: map[string]string{
|
|
|
|
"file_path": auditLogFile.Name(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
writeData := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"bar": "a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
resp, err := kvRequestWithRetry(t, func() (*api.Secret, error) {
|
|
|
|
return c.Logical().Write("kv/data/foo", writeData)
|
|
|
|
})
|
|
|
|
|
2021-10-13 19:24:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write request failed, err: %#v, resp: %#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
patchData := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"baz": "b",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
resp, err = kvRequestWithRetry(t, func() (*api.Secret, error) {
|
|
|
|
return c.Logical().JSONMergePatch(context.Background(), "kv/data/foo", patchData)
|
|
|
|
})
|
|
|
|
|
2021-10-13 19:24:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("patch request failed, err: %#v, resp: %#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
patchRequestLogCount := 0
|
|
|
|
patchResponseLogCount := 0
|
|
|
|
decoder := json.NewDecoder(auditLogFile)
|
|
|
|
|
|
|
|
var auditRecord map[string]interface{}
|
|
|
|
for decoder.Decode(&auditRecord) == nil {
|
|
|
|
auditRequest := map[string]interface{}{}
|
|
|
|
|
|
|
|
if req, ok := auditRecord["request"]; ok {
|
|
|
|
auditRequest = req.(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if auditRequest["operation"] == "patch" && auditRecord["type"] == "request" {
|
|
|
|
patchRequestLogCount += 1
|
|
|
|
} else if auditRequest["operation"] == "patch" && auditRecord["type"] == "response" {
|
|
|
|
patchResponseLogCount += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if patchRequestLogCount != 1 {
|
|
|
|
t.Fatalf("expected 1 patch request audit log record, saw %d\n", patchRequestLogCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
if patchResponseLogCount != 1 {
|
|
|
|
t.Fatalf("expected 1 patch response audit log record, saw %d\n", patchResponseLogCount)
|
|
|
|
}
|
|
|
|
}
|