2015-04-21 15:02:03 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2015-09-18 16:18:37 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
2015-04-22 05:41:53 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-04-21 15:02:03 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/mitchellh/reflectwalk"
|
|
|
|
)
|
|
|
|
|
2015-11-19 01:26:03 +00:00
|
|
|
// HashString hashes the given opaque string and returns it
|
|
|
|
func HashString(salter *salt.Salt, data string) string {
|
|
|
|
return salter.GetIdentifiedHMAC(data)
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
// Hash will hash the given type. This has built-in support for auth,
|
|
|
|
// requests, and responses. If it is a type that isn't recognized, then
|
|
|
|
// it will be passed through.
|
|
|
|
//
|
|
|
|
// The structure is modified in-place.
|
2015-09-18 16:18:37 +00:00
|
|
|
func Hash(salter *salt.Salt, raw interface{}) error {
|
2015-09-18 21:36:42 +00:00
|
|
|
fn := salter.GetIdentifiedHMAC
|
2015-04-22 05:41:53 +00:00
|
|
|
|
|
|
|
switch s := raw.(type) {
|
|
|
|
case *logical.Auth:
|
2015-04-24 18:39:17 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
if s.ClientToken != "" {
|
2016-03-12 02:27:43 +00:00
|
|
|
s.ClientToken = fn(s.ClientToken)
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
2016-03-12 00:28:06 +00:00
|
|
|
if s.Accessor != "" {
|
2016-03-12 02:27:43 +00:00
|
|
|
s.Accessor = fn(s.Accessor)
|
2016-03-12 00:28:06 +00:00
|
|
|
}
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
case *logical.Request:
|
2015-04-24 18:39:17 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
if s.Auth != nil {
|
2015-09-18 16:18:37 +00:00
|
|
|
if err := Hash(salter, s.Auth); err != nil {
|
2015-04-22 05:41:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:24:30 +00:00
|
|
|
if s.ClientToken != "" {
|
2016-03-12 02:27:43 +00:00
|
|
|
s.ClientToken = fn(s.ClientToken)
|
2015-10-29 17:24:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:01:49 +00:00
|
|
|
if s.ClientTokenAccessor != "" {
|
|
|
|
s.ClientTokenAccessor = fn(s.ClientTokenAccessor)
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
data, err := HashStructure(s.Data, fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Data = data.(map[string]interface{})
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
case *logical.Response:
|
2015-04-24 18:39:17 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
if s.Auth != nil {
|
2015-09-18 16:18:37 +00:00
|
|
|
if err := Hash(salter, s.Auth); err != nil {
|
2015-04-22 05:41:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 00:03:56 +00:00
|
|
|
if s.WrapInfo != nil {
|
|
|
|
if err := Hash(salter, s.WrapInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:41:53 +00:00
|
|
|
data, err := HashStructure(s.Data, fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Data = data.(map[string]interface{})
|
2016-05-08 00:03:56 +00:00
|
|
|
|
2017-01-04 21:44:03 +00:00
|
|
|
case *logical.ResponseWrapInfo:
|
2016-05-08 00:03:56 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Token = fn(s.Token)
|
2016-06-13 23:58:17 +00:00
|
|
|
|
|
|
|
if s.WrappedAccessor != "" {
|
|
|
|
s.WrappedAccessor = fn(s.WrappedAccessor)
|
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashStructure takes an interface and hashes all the values within
|
2015-04-21 15:02:03 +00:00
|
|
|
// the structure. Only _values_ are hashed: keys of objects are not.
|
2015-04-21 15:13:06 +00:00
|
|
|
//
|
|
|
|
// For the HashCallback, see the built-in HashCallbacks below.
|
2015-04-21 15:02:03 +00:00
|
|
|
func HashStructure(s interface{}, cb HashCallback) (interface{}, error) {
|
|
|
|
s, err := copystructure.Copy(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := &hashWalker{Callback: cb}
|
|
|
|
if err := reflectwalk.Walk(s, walker); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashCallback is the callback called for HashStructure to hash
|
|
|
|
// a value.
|
2015-09-18 16:18:37 +00:00
|
|
|
type HashCallback func(string) string
|
2015-04-21 15:02:03 +00:00
|
|
|
|
|
|
|
// hashWalker implements interfaces for the reflectwalk package
|
|
|
|
// (github.com/mitchellh/reflectwalk) that can be used to automatically
|
|
|
|
// replace primitives with a hashed value.
|
|
|
|
type hashWalker struct {
|
|
|
|
// Callback is the function to call with the primitive that is
|
|
|
|
// to be hashed. If there is an error, walking will be halted
|
|
|
|
// immediately and the error returned.
|
|
|
|
Callback HashCallback
|
|
|
|
|
|
|
|
key []string
|
|
|
|
lastValue reflect.Value
|
|
|
|
loc reflectwalk.Location
|
|
|
|
cs []reflect.Value
|
|
|
|
csKey []reflect.Value
|
|
|
|
csData interface{}
|
|
|
|
sliceIndex int
|
|
|
|
unknownKeys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Enter(loc reflectwalk.Location) error {
|
|
|
|
w.loc = loc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Exit(loc reflectwalk.Location) error {
|
|
|
|
w.loc = reflectwalk.None
|
|
|
|
|
|
|
|
switch loc {
|
|
|
|
case reflectwalk.Map:
|
|
|
|
w.cs = w.cs[:len(w.cs)-1]
|
|
|
|
case reflectwalk.MapValue:
|
|
|
|
w.key = w.key[:len(w.key)-1]
|
|
|
|
w.csKey = w.csKey[:len(w.csKey)-1]
|
|
|
|
case reflectwalk.Slice:
|
|
|
|
w.cs = w.cs[:len(w.cs)-1]
|
|
|
|
case reflectwalk.SliceElem:
|
|
|
|
w.csKey = w.csKey[:len(w.csKey)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Map(m reflect.Value) error {
|
|
|
|
w.cs = append(w.cs, m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) MapElem(m, k, v reflect.Value) error {
|
|
|
|
w.csData = k
|
|
|
|
w.csKey = append(w.csKey, k)
|
|
|
|
w.key = append(w.key, k.String())
|
|
|
|
w.lastValue = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Slice(s reflect.Value) error {
|
|
|
|
w.cs = append(w.cs, s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) SliceElem(i int, elem reflect.Value) error {
|
|
|
|
w.csKey = append(w.csKey, reflect.ValueOf(i))
|
|
|
|
w.sliceIndex = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Primitive(v reflect.Value) error {
|
|
|
|
if w.Callback == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't touch map keys
|
|
|
|
if w.loc == reflectwalk.MapKey {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
setV := v
|
|
|
|
|
|
|
|
// We only care about strings
|
|
|
|
if v.Kind() == reflect.Interface {
|
|
|
|
setV = v
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
if v.Kind() != reflect.String {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-26 17:32:50 +00:00
|
|
|
replaceVal := w.Callback(v.String())
|
2015-04-21 15:02:03 +00:00
|
|
|
|
|
|
|
resultVal := reflect.ValueOf(replaceVal)
|
|
|
|
switch w.loc {
|
|
|
|
case reflectwalk.MapKey:
|
|
|
|
m := w.cs[len(w.cs)-1]
|
|
|
|
|
|
|
|
// Delete the old value
|
|
|
|
var zero reflect.Value
|
|
|
|
m.SetMapIndex(w.csData.(reflect.Value), zero)
|
|
|
|
|
|
|
|
// Set the new key with the existing value
|
|
|
|
m.SetMapIndex(resultVal, w.lastValue)
|
|
|
|
|
|
|
|
// Set the key to be the new key
|
|
|
|
w.csData = resultVal
|
|
|
|
case reflectwalk.MapValue:
|
|
|
|
// If we're in a map, then the only way to set a map value is
|
|
|
|
// to set it directly.
|
|
|
|
m := w.cs[len(w.cs)-1]
|
|
|
|
mk := w.csData.(reflect.Value)
|
|
|
|
m.SetMapIndex(mk, resultVal)
|
|
|
|
default:
|
|
|
|
// Otherwise, we should be addressable
|
|
|
|
setV.Set(resultVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) removeCurrent() {
|
|
|
|
// Append the key to the unknown keys
|
|
|
|
w.unknownKeys = append(w.unknownKeys, strings.Join(w.key, "."))
|
|
|
|
|
|
|
|
for i := 1; i <= len(w.cs); i++ {
|
|
|
|
c := w.cs[len(w.cs)-i]
|
|
|
|
switch c.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
// Zero value so that we delete the map key
|
|
|
|
var val reflect.Value
|
|
|
|
|
|
|
|
// Get the key and delete it
|
|
|
|
k := w.csData.(reflect.Value)
|
|
|
|
c.SetMapIndex(k, val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("No container found for removeCurrent")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) replaceCurrent(v reflect.Value) {
|
|
|
|
c := w.cs[len(w.cs)-2]
|
|
|
|
switch c.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
// Get the key and delete it
|
|
|
|
k := w.csKey[len(w.csKey)-1]
|
|
|
|
c.SetMapIndex(k, v)
|
|
|
|
}
|
|
|
|
}
|