2018-03-27 20:23:33 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
|
2018-04-03 18:07:43 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-04-03 16:03:35 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logging"
|
2018-03-27 20:23:33 +00:00
|
|
|
"github.com/hashicorp/vault/physical/inmem"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfig_Enabled(t *testing.T) {
|
2018-04-03 18:07:43 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2018-03-27 20:23:33 +00:00
|
|
|
phys, err := inmem.NewTransactionalInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
logl := &logical.InmemStorage{}
|
|
|
|
|
|
|
|
config := NewUIConfig(true, phys, logl)
|
|
|
|
if !config.Enabled() {
|
|
|
|
t.Fatal("ui should be enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
config = NewUIConfig(false, phys, logl)
|
|
|
|
if config.Enabled() {
|
|
|
|
t.Fatal("ui should not be enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Headers(t *testing.T) {
|
2018-04-03 18:07:43 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2018-03-27 20:23:33 +00:00
|
|
|
phys, err := inmem.NewTransactionalInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
logl := &logical.InmemStorage{}
|
|
|
|
|
|
|
|
config := NewUIConfig(true, phys, logl)
|
|
|
|
headers, err := config.Headers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-03-28 20:57:58 +00:00
|
|
|
if len(headers) != len(config.defaultHeaders) {
|
|
|
|
t.Fatalf("expected %d headers, got %d", len(config.defaultHeaders), len(headers))
|
2018-03-27 20:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
head, err := config.GetHeader(context.Background(), "Test-Header")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if head != "" {
|
|
|
|
t.Fatal("header returned found, should not be found")
|
|
|
|
}
|
|
|
|
err = config.SetHeader(context.Background(), "Test-Header", "123")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
head, err = config.GetHeader(context.Background(), "Test-Header")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if head == "" {
|
|
|
|
t.Fatal("header not found when it should be")
|
|
|
|
}
|
|
|
|
if head != "123" {
|
|
|
|
t.Fatalf("expected: %s, got: %s", "123", head)
|
|
|
|
}
|
|
|
|
|
|
|
|
head, err = config.GetHeader(context.Background(), "tEST-hEADER")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if head == "" {
|
|
|
|
t.Fatal("header not found when it should be")
|
|
|
|
}
|
|
|
|
if head != "123" {
|
|
|
|
t.Fatalf("expected: %s, got: %s", "123", head)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := config.HeaderKeys(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("expected 1 key, got %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.SetHeader(context.Background(), "Test-Header-2", "321")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
keys, err = config.HeaderKeys(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 2 {
|
|
|
|
t.Fatalf("expected 1 key, got %d", len(keys))
|
|
|
|
}
|
|
|
|
err = config.DeleteHeader(context.Background(), "Test-Header-2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.DeleteHeader(context.Background(), "Test-Header")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
head, err = config.GetHeader(context.Background(), "Test-Header")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if head != "" {
|
|
|
|
t.Fatal("header returned found, should not be found")
|
|
|
|
}
|
|
|
|
keys, err = config.HeaderKeys(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatalf("expected 0 key, got %d", len(keys))
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 20:57:58 +00:00
|
|
|
|
|
|
|
func TestConfig_DefaultHeaders(t *testing.T) {
|
2018-04-03 18:07:43 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2018-03-28 20:57:58 +00:00
|
|
|
phys, err := inmem.NewTransactionalInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
logl := &logical.InmemStorage{}
|
|
|
|
|
|
|
|
config := NewUIConfig(true, phys, logl)
|
|
|
|
headers, err := config.Headers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(headers) != len(config.defaultHeaders) {
|
|
|
|
t.Fatalf("expected %d headers, got %d", len(config.defaultHeaders), len(headers))
|
|
|
|
}
|
|
|
|
|
|
|
|
headers, err = config.Headers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defaultCSP := config.defaultHeaders.Get("Content-security-Policy")
|
|
|
|
head := headers.Get("Content-Security-Policy")
|
|
|
|
if head != defaultCSP {
|
|
|
|
t.Fatalf("header does not match: expected %s, got %s", defaultCSP, head)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.SetHeader(context.Background(), "Content-security-Policy", "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
headers, err = config.Headers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
head = headers.Get("Content-Security-Policy")
|
|
|
|
if head != "test" {
|
|
|
|
t.Fatalf("header does not match: expected %s, got %s", "test", head)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = config.DeleteHeader(context.Background(), "Content-Security-Policy")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
headers, err = config.Headers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
head = headers.Get("Content-Security-Policy")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if head != defaultCSP {
|
|
|
|
t.Fatalf("header does not match: expected %s, got %s", defaultCSP, head)
|
|
|
|
}
|
|
|
|
}
|