2015-04-21 15:02:03 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2015-09-18 21:36:42 +00:00
|
|
|
"crypto/sha256"
|
2015-04-22 05:41:53 +00:00
|
|
|
"fmt"
|
2015-04-21 15:02:03 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-04-27 22:54:14 +00:00
|
|
|
"time"
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2015-09-18 21:36:42 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
2015-04-22 05:41:53 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-04-27 22:54:14 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2015-04-21 15:02:03 +00:00
|
|
|
)
|
|
|
|
|
2015-04-27 22:54:14 +00:00
|
|
|
func TestCopy_auth(t *testing.T) {
|
|
|
|
// Make a non-pointer one so that it can't be modified directly
|
|
|
|
expected := logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 05:27:01 +00:00
|
|
|
TTL: 1 * time.Hour,
|
|
|
|
IssueTime: time.Now().UTC(),
|
2015-04-27 22:54:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ClientToken: "foo",
|
|
|
|
}
|
|
|
|
auth := expected
|
|
|
|
|
|
|
|
// Copy it
|
|
|
|
dup, err := copystructure.Copy(&auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check equality
|
|
|
|
auth2 := dup.(*logical.Auth)
|
|
|
|
if !reflect.DeepEqual(*auth2, expected) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", *auth2, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopy_request(t *testing.T) {
|
|
|
|
// Make a non-pointer one so that it can't be modified directly
|
|
|
|
expected := logical.Request{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
arg := expected
|
|
|
|
|
|
|
|
// Copy it
|
|
|
|
dup, err := copystructure.Copy(&arg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check equality
|
|
|
|
arg2 := dup.(*logical.Request)
|
|
|
|
if !reflect.DeepEqual(*arg2, expected) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", *arg2, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopy_response(t *testing.T) {
|
|
|
|
// Make a non-pointer one so that it can't be modified directly
|
|
|
|
expected := logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
arg := expected
|
|
|
|
|
|
|
|
// Copy it
|
|
|
|
dup, err := copystructure.Copy(&arg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check equality
|
|
|
|
arg2 := dup.(*logical.Response)
|
|
|
|
if !reflect.DeepEqual(*arg2, expected) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", *arg2, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
func TestHash(t *testing.T) {
|
2015-06-19 10:31:19 +00:00
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
cases := []struct {
|
|
|
|
Input interface{}
|
|
|
|
Output interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&logical.Auth{ClientToken: "foo"},
|
2015-09-18 21:36:42 +00:00
|
|
|
&logical.Auth{ClientToken: "hmac-sha256:08ba357e274f528065766c770a639abf6809b39ccfd37c2a3157c7f51954da0a"},
|
2015-04-22 05:41:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&logical.Request{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&logical.Request{
|
|
|
|
Data: map[string]interface{}{
|
2015-09-18 21:36:42 +00:00
|
|
|
"foo": "hmac-sha256:f9320baf0249169e73850cd6156ded0106e2bb6ad8cab01b7bbbebe6d1065317",
|
2015-04-22 05:41:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2015-09-18 21:36:42 +00:00
|
|
|
"foo": "hmac-sha256:f9320baf0249169e73850cd6156ded0106e2bb6ad8cab01b7bbbebe6d1065317",
|
2015-04-22 05:41:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-22 05:42:37 +00:00
|
|
|
{
|
|
|
|
"foo",
|
|
|
|
"foo",
|
|
|
|
},
|
2015-06-19 10:31:19 +00:00
|
|
|
{
|
|
|
|
&logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 05:27:01 +00:00
|
|
|
TTL: 1 * time.Hour,
|
|
|
|
IssueTime: now,
|
2015-06-19 10:31:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ClientToken: "foo",
|
|
|
|
},
|
|
|
|
&logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 05:27:01 +00:00
|
|
|
TTL: 1 * time.Hour,
|
|
|
|
IssueTime: now,
|
2015-06-19 10:31:19 +00:00
|
|
|
},
|
|
|
|
|
2015-09-18 21:36:42 +00:00
|
|
|
ClientToken: "hmac-sha256:08ba357e274f528065766c770a639abf6809b39ccfd37c2a3157c7f51954da0a",
|
2015-06-19 10:31:19 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 21:36:42 +00:00
|
|
|
localSalt, err := salt.NewSalt(nil, &salt.Config{
|
|
|
|
HMAC: sha256.New,
|
|
|
|
HMACType: "hmac-sha256",
|
|
|
|
StaticSalt: "foo",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error instantiating salt: %s", err)
|
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
input := fmt.Sprintf("%#v", tc.Input)
|
2015-09-18 21:36:42 +00:00
|
|
|
if err := Hash(localSalt, tc.Input); err != nil {
|
2015-04-22 05:41:53 +00:00
|
|
|
t.Fatalf("err: %s\n\n%s", err, input)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tc.Input, tc.Output) {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%#v\n\n%#v", input, tc.Input, tc.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:02:03 +00:00
|
|
|
func TestHashWalker(t *testing.T) {
|
|
|
|
replaceText := "foo"
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Input interface{}
|
|
|
|
Output interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
map[string]interface{}{
|
|
|
|
"hello": "foo",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"hello": replaceText,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
map[string]interface{}{
|
|
|
|
"hello": []interface{}{"world"},
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"hello": []interface{}{replaceText},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2015-09-18 21:36:42 +00:00
|
|
|
output, err := HashStructure(tc.Input, func(string) string {
|
|
|
|
return replaceText
|
2015-04-21 15:02:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s\n\n%#v", err, tc.Input)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(output, tc.Output) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", tc.Input, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|