2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-09-29 04:01:28 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-01-31 18:57:50 +00:00
|
|
|
"errors"
|
2016-09-29 04:01:28 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2016-09-29 04:01:28 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test wrapping functionality
|
|
|
|
func TestHTTP_Wrapping(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()
|
2016-09-29 04:01:28 +00:00
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
cores := cluster.Cores
|
2016-09-29 04:01:28 +00:00
|
|
|
|
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
|
2017-02-27 17:49:35 +00:00
|
|
|
client := cores[0].Client
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2016-09-29 04:01:28 +00:00
|
|
|
|
|
|
|
// Write a value that we will use with wrapping for lookup
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err := client.Logical().Write("secret/foo", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"zip": "zap",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a wrapping lookup function for reads on that path
|
|
|
|
client.SetWrappingLookupFunc(func(operation, path string) string {
|
|
|
|
if operation == "GET" && path == "secret/foo" {
|
|
|
|
return "5m"
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.DefaultWrappingLookupFunc(operation, path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// First test: basic things that should fail, lookup edition
|
|
|
|
// Root token isn't a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/lookup", nil)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
// Not supplied
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/lookup", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"foo": "bar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
// Nonexistent token isn't a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/lookup", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": "bar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second: basic things that should fail, unwrap edition
|
|
|
|
// Root token isn't a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Unwrap(cluster.RootToken)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
// Root token isn't a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", nil)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
// Not supplied
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"foo": "bar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
// Nonexistent token isn't a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": "bar",
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test lookup
|
|
|
|
//
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err := client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo := secret.WrapInfo
|
|
|
|
|
|
|
|
// Test this twice to ensure no ill effect to the wrapping token as a result of the lookup
|
|
|
|
for i := 0; i < 2; i++ {
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/lookup", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": wrapInfo.Token,
|
|
|
|
})
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
t.Fatal("secret or secret data is nil")
|
|
|
|
}
|
|
|
|
creationTTL, _ := secret.Data["creation_ttl"].(json.Number).Int64()
|
|
|
|
if int(creationTTL) != wrapInfo.TTL {
|
2018-03-20 18:54:10 +00:00
|
|
|
t.Fatalf("mismatched ttls: %d vs %d", creationTTL, wrapInfo.TTL)
|
2016-09-29 04:01:28 +00:00
|
|
|
}
|
|
|
|
if secret.Data["creation_time"].(string) != wrapInfo.CreationTime.Format(time.RFC3339Nano) {
|
2018-03-20 18:54:10 +00:00
|
|
|
t.Fatalf("mismatched creation times: %q vs %q", secret.Data["creation_time"].(string), wrapInfo.CreationTime.Format(time.RFC3339Nano))
|
2016-09-29 04:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test unwrap
|
|
|
|
//
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo = secret.WrapInfo
|
|
|
|
|
|
|
|
// Test unwrap via the client token
|
|
|
|
client.SetToken(wrapInfo.Token)
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/unwrap", nil)
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-23 12:51:23 +00:00
|
|
|
if secret.Warnings != nil {
|
|
|
|
t.Fatalf("Warnings found: %v", secret.Warnings)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
t.Fatal("secret or secret data is nil")
|
|
|
|
}
|
|
|
|
ret1 := secret
|
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", nil)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo = secret.WrapInfo
|
|
|
|
|
|
|
|
// Test as a separate token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/unwrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": wrapInfo.Token,
|
|
|
|
})
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
ret2 := secret
|
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": wrapInfo.Token,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo = secret.WrapInfo
|
|
|
|
|
|
|
|
// Read response directly
|
|
|
|
client.SetToken(wrapInfo.Token)
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("cubbyhole/response")
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
ret3 := secret
|
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("cubbyhole/response", nil)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo = secret.WrapInfo
|
|
|
|
|
|
|
|
// Read via Unwrap method
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Unwrap(wrapInfo.Token)
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-23 12:51:23 +00:00
|
|
|
if secret.Warnings != nil {
|
|
|
|
t.Fatalf("Warnings found: %v", secret.Warnings)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
ret4 := secret
|
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Unwrap(wrapInfo.Token)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(ret1.Data, map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
}) {
|
|
|
|
t.Fatalf("ret1 data did not match expected: %#v", ret1.Data)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ret2.Data, map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
}) {
|
|
|
|
t.Fatalf("ret2 data did not match expected: %#v", ret2.Data)
|
|
|
|
}
|
|
|
|
var ret3Secret api.Secret
|
|
|
|
err = jsonutil.DecodeJSON([]byte(ret3.Data["response"].(string)), &ret3Secret)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ret3Secret.Data, map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
}) {
|
|
|
|
t.Fatalf("ret3 data did not match expected: %#v", ret3Secret.Data)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ret4.Data, map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
}) {
|
|
|
|
t.Fatalf("ret4 data did not match expected: %#v", ret4.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Custom wrapping
|
|
|
|
//
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
client.SetToken(cluster.RootToken)
|
2016-09-29 04:01:28 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
"three": json.Number("2"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't set a request TTL on that path, should fail
|
|
|
|
client.SetWrappingLookupFunc(func(operation, path string) string {
|
|
|
|
return ""
|
|
|
|
})
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/wrap", data)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-set the lookup function
|
|
|
|
client.SetWrappingLookupFunc(func(operation, path string) string {
|
|
|
|
if operation == "GET" && path == "secret/foo" {
|
|
|
|
return "5m"
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.DefaultWrappingLookupFunc(operation, path)
|
|
|
|
})
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/wrap", data)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-23 12:51:23 +00:00
|
|
|
if secret.Warnings != nil {
|
|
|
|
t.Fatalf("Warnings found: %v", secret.Warnings)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Unwrap(secret.WrapInfo.Token)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-23 12:51:23 +00:00
|
|
|
if secret.Warnings != nil {
|
|
|
|
t.Fatalf("Warnings found: %v", secret.Warnings)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
if !reflect.DeepEqual(data, secret.Data) {
|
2016-12-21 18:08:27 +00:00
|
|
|
t.Fatalf("custom wrap did not match expected: %#v", secret.Data)
|
2016-09-29 04:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test rewrap
|
|
|
|
//
|
|
|
|
|
|
|
|
// Create a wrapping token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("secret/foo")
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret == nil || secret.WrapInfo == nil {
|
|
|
|
t.Fatal("secret or wrap info is nil")
|
|
|
|
}
|
|
|
|
wrapInfo = secret.WrapInfo
|
|
|
|
|
2017-08-02 22:28:58 +00:00
|
|
|
// Check for correct CreationPath before rewrap
|
|
|
|
if wrapInfo.CreationPath != "secret/foo" {
|
2018-02-05 01:37:57 +00:00
|
|
|
t.Fatalf("error on wrapInfo.CreationPath: expected: secret/foo, got: %s", wrapInfo.CreationPath)
|
2017-08-02 22:28:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:01:28 +00:00
|
|
|
// Test rewrapping
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Write("sys/wrapping/rewrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": wrapInfo.Token,
|
|
|
|
})
|
2017-07-05 13:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-23 12:51:23 +00:00
|
|
|
if secret.Warnings != nil {
|
|
|
|
t.Fatalf("Warnings found: %v", secret.Warnings)
|
|
|
|
}
|
2017-08-02 22:28:58 +00:00
|
|
|
|
|
|
|
// Check for correct Creation path after rewrap
|
|
|
|
if wrapInfo.CreationPath != "secret/foo" {
|
2018-02-05 01:37:57 +00:00
|
|
|
t.Fatalf("error on wrapInfo.CreationPath: expected: secret/foo, got: %s", wrapInfo.CreationPath)
|
2017-08-02 22:28:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:01:28 +00:00
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("sys/wrapping/unwrap", map[string]interface{}{
|
2016-09-29 04:01:28 +00:00
|
|
|
"token": wrapInfo.Token,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt unwrapping the rewrapped token
|
|
|
|
wrapToken := secret.WrapInfo.Token
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Unwrap(wrapToken)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Should be expired and fail
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Unwrap(wrapToken)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(secret.Data, map[string]interface{}{
|
|
|
|
"zip": "zap",
|
|
|
|
}) {
|
|
|
|
t.Fatalf("secret data did not match expected: %#v", secret.Data)
|
|
|
|
}
|
2023-01-31 18:57:50 +00:00
|
|
|
|
|
|
|
// Ensure that wrapping lookup without a client token responds correctly
|
|
|
|
client.ClearToken()
|
|
|
|
secret, err = client.Logical().Read("sys/wrapping/lookup")
|
|
|
|
if secret != nil {
|
|
|
|
t.Fatalf("expected no response: %#v", secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
var respError *api.ResponseError
|
|
|
|
if errors.As(err, &respError); respError.StatusCode != 403 {
|
|
|
|
t.Fatalf("expected 403 response, actual: %d", respError.StatusCode)
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
}
|