2017-02-02 19:49:20 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-02-02 19:49:20 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/salt"
|
2017-02-02 19:49:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func mockAuditedHeadersConfig(t *testing.T) *AuditedHeadersConfig {
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "foo/")
|
|
|
|
return &AuditedHeadersConfig{
|
|
|
|
Headers: make(map[string]*auditedHeaderSettings),
|
|
|
|
view: view,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuditedHeadersConfig_CRUD(t *testing.T) {
|
|
|
|
conf := mockAuditedHeadersConfig(t)
|
|
|
|
|
|
|
|
testAuditedHeadersConfig_Add(t, conf)
|
|
|
|
testAuditedHeadersConfig_Remove(t, conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAuditedHeadersConfig_Add(t *testing.T, conf *AuditedHeadersConfig) {
|
2018-01-19 06:44:44 +00:00
|
|
|
err := conf.add(context.Background(), "X-Test-Header", false)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding header to config: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:35:35 +00:00
|
|
|
settings, ok := conf.Headers["x-test-header"]
|
2017-02-02 19:49:20 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("Expected header to be found in config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.HMAC {
|
|
|
|
t.Fatal("Expected HMAC to be set to false, got true")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := conf.view.Get(context.Background(), auditedHeadersEntry)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve headers entry from config: %s", err)
|
|
|
|
}
|
2021-11-01 18:43:00 +00:00
|
|
|
if out == nil {
|
|
|
|
t.Fatal("nil value")
|
|
|
|
}
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
headers := make(map[string]*auditedHeaderSettings)
|
|
|
|
err = out.DecodeJSON(&headers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding header view: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]*auditedHeaderSettings{
|
2021-04-08 16:43:39 +00:00
|
|
|
"x-test-header": {
|
2017-02-02 19:49:20 +00:00
|
|
|
HMAC: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(headers, expected) {
|
|
|
|
t.Fatalf("Expected config didn't match actual. Expected: %#v, Got: %#v", expected, headers)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = conf.add(context.Background(), "X-Vault-Header", true)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding header to config: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:35:35 +00:00
|
|
|
settings, ok = conf.Headers["x-vault-header"]
|
2017-02-02 19:49:20 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("Expected header to be found in config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !settings.HMAC {
|
|
|
|
t.Fatal("Expected HMAC to be set to true, got false")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = conf.view.Get(context.Background(), auditedHeadersEntry)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve headers entry from config: %s", err)
|
|
|
|
}
|
2021-11-01 18:43:00 +00:00
|
|
|
if out == nil {
|
|
|
|
t.Fatal("nil value")
|
|
|
|
}
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
headers = make(map[string]*auditedHeaderSettings)
|
|
|
|
err = out.DecodeJSON(&headers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding header view: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:35:35 +00:00
|
|
|
expected["x-vault-header"] = &auditedHeaderSettings{
|
2017-02-02 19:49:20 +00:00
|
|
|
HMAC: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(headers, expected) {
|
|
|
|
t.Fatalf("Expected config didn't match actual. Expected: %#v, Got: %#v", expected, headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAuditedHeadersConfig_Remove(t *testing.T, conf *AuditedHeadersConfig) {
|
2018-01-19 06:44:44 +00:00
|
|
|
err := conf.remove(context.Background(), "X-Test-Header")
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding header to config: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:35:35 +00:00
|
|
|
_, ok := conf.Headers["x-Test-HeAder"]
|
2017-02-02 19:49:20 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatal("Expected header to not be found in config")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := conf.view.Get(context.Background(), auditedHeadersEntry)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve headers entry from config: %s", err)
|
|
|
|
}
|
2021-11-01 18:43:00 +00:00
|
|
|
if out == nil {
|
|
|
|
t.Fatal("nil value")
|
|
|
|
}
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
headers := make(map[string]*auditedHeaderSettings)
|
|
|
|
err = out.DecodeJSON(&headers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding header view: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]*auditedHeaderSettings{
|
2021-04-08 16:43:39 +00:00
|
|
|
"x-vault-header": {
|
2017-02-02 19:49:20 +00:00
|
|
|
HMAC: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(headers, expected) {
|
|
|
|
t.Fatalf("Expected config didn't match actual. Expected: %#v, Got: %#v", expected, headers)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = conf.remove(context.Background(), "x-VaulT-Header")
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when adding header to config: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:35:35 +00:00
|
|
|
_, ok = conf.Headers["x-vault-header"]
|
2017-02-02 19:49:20 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatal("Expected header to not be found in config")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = conf.view.Get(context.Background(), auditedHeadersEntry)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve headers entry from config: %s", err)
|
|
|
|
}
|
2021-11-01 18:43:00 +00:00
|
|
|
if out == nil {
|
|
|
|
t.Fatal("nil value")
|
|
|
|
}
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
headers = make(map[string]*auditedHeaderSettings)
|
|
|
|
err = out.DecodeJSON(&headers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding header view: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = make(map[string]*auditedHeaderSettings)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(headers, expected) {
|
|
|
|
t.Fatalf("Expected config didn't match actual. Expected: %#v, Got: %#v", expected, headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuditedHeadersConfig_ApplyConfig(t *testing.T) {
|
|
|
|
conf := mockAuditedHeadersConfig(t)
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
conf.add(context.Background(), "X-TesT-Header", false)
|
|
|
|
conf.add(context.Background(), "X-Vault-HeAdEr", true)
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
reqHeaders := map[string][]string{
|
2021-04-08 16:43:39 +00:00
|
|
|
"X-Test-Header": {"foo"},
|
|
|
|
"X-Vault-Header": {"bar", "bar"},
|
|
|
|
"Content-Type": {"json"},
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
hashFunc := func(ctx context.Context, s string) (string, error) { return "hashed", nil }
|
2017-02-02 19:49:20 +00:00
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
result, err := conf.ApplyConfig(context.Background(), reqHeaders, hashFunc)
|
2017-05-24 00:36:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
expected := map[string][]string{
|
2021-04-08 16:43:39 +00:00
|
|
|
"x-test-header": {"foo"},
|
|
|
|
"x-vault-header": {"hashed", "hashed"},
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(result, expected) {
|
|
|
|
t.Fatalf("Expected headers did not match actual: Expected %#v\n Got %#v\n", expected, result)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
// Make sure we didn't edit the reqHeaders map
|
2017-02-02 19:49:20 +00:00
|
|
|
reqHeadersCopy := map[string][]string{
|
2021-04-08 16:43:39 +00:00
|
|
|
"X-Test-Header": {"foo"},
|
|
|
|
"X-Vault-Header": {"bar", "bar"},
|
|
|
|
"Content-Type": {"json"},
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(reqHeaders, reqHeadersCopy) {
|
|
|
|
t.Fatalf("Req headers were changed, expected %#v\n got %#v", reqHeadersCopy, reqHeaders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAuditedHeaderConfig_ApplyConfig(b *testing.B) {
|
|
|
|
conf := &AuditedHeadersConfig{
|
|
|
|
Headers: make(map[string]*auditedHeaderSettings),
|
|
|
|
view: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Headers = map[string]*auditedHeaderSettings{
|
2021-04-08 16:43:39 +00:00
|
|
|
"X-Test-Header": {false},
|
|
|
|
"X-Vault-Header": {true},
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqHeaders := map[string][]string{
|
2021-04-08 16:43:39 +00:00
|
|
|
"X-Test-Header": {"foo"},
|
|
|
|
"X-Vault-Header": {"bar", "bar"},
|
|
|
|
"Content-Type": {"json"},
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
salter, err := salt.NewSalt(context.Background(), nil, nil)
|
2017-02-02 19:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
hashFunc := func(ctx context.Context, s string) (string, error) { return salter.GetIdentifiedHMAC(s), nil }
|
2017-02-02 19:49:20 +00:00
|
|
|
|
|
|
|
// Reset the timer since we did a lot above
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2018-03-08 19:21:11 +00:00
|
|
|
conf.ApplyConfig(context.Background(), reqHeaders, hashFunc)
|
2017-02-02 19:49:20 +00:00
|
|
|
}
|
|
|
|
}
|