2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-21 15:02:03 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2019-07-02 22:18:40 +00:00
|
|
|
"encoding/json"
|
2017-05-08 18:06:08 +00:00
|
|
|
"errors"
|
2015-04-21 15:02:03 +00:00
|
|
|
"reflect"
|
2017-05-08 18:06:08 +00:00
|
|
|
"time"
|
2015-04-21 15:02:03 +00:00
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/salt"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/wrapping"
|
|
|
|
"github.com/hashicorp/vault/sdk/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)
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// HashAuth returns a hashed copy of the logical.Auth input.
|
|
|
|
func HashAuth(salter *salt.Salt, in *logical.Auth, HMACAccessor bool) (*logical.Auth, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:36:42 +00:00
|
|
|
fn := salter.GetIdentifiedHMAC
|
2019-07-02 22:18:40 +00:00
|
|
|
auth := *in
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
if auth.ClientToken != "" {
|
|
|
|
auth.ClientToken = fn(auth.ClientToken)
|
|
|
|
}
|
|
|
|
if HMACAccessor && auth.Accessor != "" {
|
|
|
|
auth.Accessor = fn(auth.Accessor)
|
|
|
|
}
|
|
|
|
return &auth, nil
|
|
|
|
}
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// HashRequest returns a hashed copy of the logical.Request input.
|
|
|
|
func HashRequest(salter *salt.Salt, in *logical.Request, HMACAccessor bool, nonHMACDataKeys []string) (*logical.Request, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
fn := salter.GetIdentifiedHMAC
|
|
|
|
req := *in
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
if req.Auth != nil {
|
|
|
|
cp, err := copystructure.Copy(req.Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-29 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
req.Auth, err = HashAuth(salter, cp.(*logical.Auth), HMACAccessor)
|
2015-04-22 05:41:53 +00:00
|
|
|
if err != nil {
|
2019-07-02 22:18:40 +00:00
|
|
|
return nil, err
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
if req.ClientToken != "" {
|
|
|
|
req.ClientToken = fn(req.ClientToken)
|
|
|
|
}
|
|
|
|
if HMACAccessor && req.ClientTokenAccessor != "" {
|
|
|
|
req.ClientTokenAccessor = fn(req.ClientTokenAccessor)
|
|
|
|
}
|
2015-10-29 17:24:30 +00:00
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
if req.Data != nil {
|
|
|
|
copy, err := copystructure.Copy(req.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hashMap(fn, copy.(map[string]interface{}), nonHMACDataKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Data = copy.(map[string]interface{})
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &req, nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
func hashMap(fn func(string) string, data map[string]interface{}, nonHMACDataKeys []string) error {
|
|
|
|
for k, v := range data {
|
2019-07-02 22:18:40 +00:00
|
|
|
if o, ok := v.(logical.OptMarshaler); ok {
|
|
|
|
marshaled, err := o.MarshalJSONWithOptions(&logical.MarshalOptions{
|
|
|
|
ValueHasher: fn,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-03 16:53:02 +00:00
|
|
|
return err
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
2020-02-03 16:53:02 +00:00
|
|
|
data[k] = json.RawMessage(marshaled)
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
return HashStructure(data, fn, nonHMACDataKeys)
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HashResponse returns a hashed copy of the logical.Request input.
|
Add option 'elide_list_responses' to audit backends (#18128)
This PR relates to a feature request logged through HashiCorp commercial
support.
Vault lacks pagination in its APIs. As a result, certain list operations
can return **very** large responses. The user's chosen audit sinks may
experience difficulty consuming audit records that swell to tens of
megabytes of JSON.
In our case, one of the systems consuming audit log data could not cope,
and failed.
The responses of list operations are typically not very interesting, as
they are mostly lists of keys, or, even when they include a "key_info"
field, are not returning confidential information. They become even less
interesting once HMAC-ed by the audit system.
Some example Vault "list" operations that are prone to becoming very
large in an active Vault installation are:
auth/token/accessors/
identity/entity/id/
identity/entity-alias/id/
pki/certs/
In response, I've coded a new option that can be applied to audit
backends, `elide_list_responses`. When enabled, response data is elided
from audit logs, only when the operation type is "list".
For added safety, the elision only applies to the "keys" and "key_info"
fields within the response data - these are conventionally the only
fields present in a list response - see logical.ListResponse, and
logical.ListResponseWithInfo. However, other fields are technically
possible if a plugin author writes unusual code, and these will be
preserved in the audit log even with this option enabled.
The elision replaces the values of the "keys" and "key_info" fields with
an integer count of the number of entries. This allows even the elided
audit logs to still be useful for answering questions like "Was any data
returned?" or "How many records were listed?".
2023-01-11 21:15:52 +00:00
|
|
|
func HashResponse(
|
|
|
|
salter *salt.Salt,
|
|
|
|
in *logical.Response,
|
|
|
|
HMACAccessor bool,
|
|
|
|
nonHMACDataKeys []string,
|
|
|
|
elideListResponseData bool,
|
|
|
|
) (*logical.Response, error) {
|
2019-07-02 22:18:40 +00:00
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := salter.GetIdentifiedHMAC
|
|
|
|
resp := *in
|
|
|
|
|
|
|
|
if resp.Auth != nil {
|
|
|
|
cp, err := copystructure.Copy(resp.Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-05-08 00:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
resp.Auth, err = HashAuth(salter, cp.(*logical.Auth), HMACAccessor)
|
2015-04-22 05:41:53 +00:00
|
|
|
if err != nil {
|
2019-07-02 22:18:40 +00:00
|
|
|
return nil, err
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
if resp.Data != nil {
|
|
|
|
copy, err := copystructure.Copy(resp.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-08 00:03:56 +00:00
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
mapCopy := copy.(map[string]interface{})
|
|
|
|
if b, ok := mapCopy[logical.HTTPRawBody].([]byte); ok {
|
|
|
|
mapCopy[logical.HTTPRawBody] = string(b)
|
|
|
|
}
|
|
|
|
|
Add option 'elide_list_responses' to audit backends (#18128)
This PR relates to a feature request logged through HashiCorp commercial
support.
Vault lacks pagination in its APIs. As a result, certain list operations
can return **very** large responses. The user's chosen audit sinks may
experience difficulty consuming audit records that swell to tens of
megabytes of JSON.
In our case, one of the systems consuming audit log data could not cope,
and failed.
The responses of list operations are typically not very interesting, as
they are mostly lists of keys, or, even when they include a "key_info"
field, are not returning confidential information. They become even less
interesting once HMAC-ed by the audit system.
Some example Vault "list" operations that are prone to becoming very
large in an active Vault installation are:
auth/token/accessors/
identity/entity/id/
identity/entity-alias/id/
pki/certs/
In response, I've coded a new option that can be applied to audit
backends, `elide_list_responses`. When enabled, response data is elided
from audit logs, only when the operation type is "list".
For added safety, the elision only applies to the "keys" and "key_info"
fields within the response data - these are conventionally the only
fields present in a list response - see logical.ListResponse, and
logical.ListResponseWithInfo. However, other fields are technically
possible if a plugin author writes unusual code, and these will be
preserved in the audit log even with this option enabled.
The elision replaces the values of the "keys" and "key_info" fields with
an integer count of the number of entries. This allows even the elided
audit logs to still be useful for answering questions like "Was any data
returned?" or "How many records were listed?".
2023-01-11 21:15:52 +00:00
|
|
|
// Processing list response data elision takes place at this point in the code for performance reasons:
|
|
|
|
// - take advantage of the deep copy of resp.Data that was going to be done anyway for hashing
|
|
|
|
// - but elide data before potentially spending time hashing it
|
|
|
|
if elideListResponseData {
|
|
|
|
doElideListResponseData(mapCopy)
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:53:02 +00:00
|
|
|
err = hashMap(fn, mapCopy, nonHMACDataKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.Data = mapCopy
|
|
|
|
}
|
Add option 'elide_list_responses' to audit backends (#18128)
This PR relates to a feature request logged through HashiCorp commercial
support.
Vault lacks pagination in its APIs. As a result, certain list operations
can return **very** large responses. The user's chosen audit sinks may
experience difficulty consuming audit records that swell to tens of
megabytes of JSON.
In our case, one of the systems consuming audit log data could not cope,
and failed.
The responses of list operations are typically not very interesting, as
they are mostly lists of keys, or, even when they include a "key_info"
field, are not returning confidential information. They become even less
interesting once HMAC-ed by the audit system.
Some example Vault "list" operations that are prone to becoming very
large in an active Vault installation are:
auth/token/accessors/
identity/entity/id/
identity/entity-alias/id/
pki/certs/
In response, I've coded a new option that can be applied to audit
backends, `elide_list_responses`. When enabled, response data is elided
from audit logs, only when the operation type is "list".
For added safety, the elision only applies to the "keys" and "key_info"
fields within the response data - these are conventionally the only
fields present in a list response - see logical.ListResponse, and
logical.ListResponseWithInfo. However, other fields are technically
possible if a plugin author writes unusual code, and these will be
preserved in the audit log even with this option enabled.
The elision replaces the values of the "keys" and "key_info" fields with
an integer count of the number of entries. This allows even the elided
audit logs to still be useful for answering questions like "Was any data
returned?" or "How many records were listed?".
2023-01-11 21:15:52 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
if resp.WrapInfo != nil {
|
|
|
|
var err error
|
|
|
|
resp.WrapInfo, err = HashWrapInfo(salter, resp.WrapInfo, HMACAccessor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-05-08 00:03:56 +00:00
|
|
|
}
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
2016-05-08 00:03:56 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
return &resp, nil
|
|
|
|
}
|
2016-06-13 23:58:17 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// HashWrapInfo returns a hashed copy of the wrapping.ResponseWrapInfo input.
|
|
|
|
func HashWrapInfo(salter *salt.Salt, in *wrapping.ResponseWrapInfo, HMACAccessor bool) (*wrapping.ResponseWrapInfo, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := salter.GetIdentifiedHMAC
|
|
|
|
wrapinfo := *in
|
|
|
|
|
|
|
|
wrapinfo.Token = fn(wrapinfo.Token)
|
|
|
|
|
|
|
|
if HMACAccessor {
|
|
|
|
wrapinfo.Accessor = fn(wrapinfo.Accessor)
|
|
|
|
|
|
|
|
if wrapinfo.WrappedAccessor != "" {
|
|
|
|
wrapinfo.WrappedAccessor = fn(wrapinfo.WrappedAccessor)
|
2016-06-13 23:58:17 +00:00
|
|
|
}
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
return &wrapinfo, nil
|
2015-04-22 05:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-07-02 22:18:40 +00:00
|
|
|
func HashStructure(s interface{}, cb HashCallback, ignoredKeys []string) error {
|
2018-03-02 17:18:39 +00:00
|
|
|
walker := &hashWalker{Callback: cb, IgnoredKeys: ignoredKeys}
|
2019-07-02 22:18:40 +00:00
|
|
|
return reflectwalk.Walk(s, walker)
|
2015-04-21 15:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-03-02 17:18:39 +00:00
|
|
|
// IgnoreKeys are the keys that wont have the HashCallback applied
|
|
|
|
IgnoredKeys []string
|
2019-07-02 22:18:40 +00:00
|
|
|
// MapElem appends the key itself (not the reflect.Value) to key.
|
|
|
|
// The last element in key is the most recently entered map key.
|
|
|
|
// Since Exit pops the last element of key, only nesting to another
|
|
|
|
// structure increases the size of this slice.
|
|
|
|
key []string
|
|
|
|
lastValue reflect.Value
|
|
|
|
// Enter appends to loc and exit pops loc. The last element of loc is thus
|
|
|
|
// the current location.
|
|
|
|
loc []reflectwalk.Location
|
|
|
|
// Map and Slice append to cs, Exit pops the last element off cs.
|
|
|
|
// The last element in cs is the most recently entered map or slice.
|
|
|
|
cs []reflect.Value
|
|
|
|
// MapElem and SliceElem append to csKey. The last element in csKey is the
|
|
|
|
// most recently entered map key or slice index. Since Exit pops the last
|
|
|
|
// element of csKey, only nesting to another structure increases the size of
|
|
|
|
// this slice.
|
|
|
|
csKey []reflect.Value
|
2015-04-21 15:02:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 18:06:08 +00:00
|
|
|
// hashTimeType stores a pre-computed reflect.Type for a time.Time so
|
|
|
|
// we can quickly compare in hashWalker.Struct. We create an empty/invalid
|
|
|
|
// time.Time{} so we don't need to incur any additional startup cost vs.
|
|
|
|
// Now() or Unix().
|
|
|
|
var hashTimeType = reflect.TypeOf(time.Time{})
|
|
|
|
|
2015-04-21 15:02:03 +00:00
|
|
|
func (w *hashWalker) Enter(loc reflectwalk.Location) error {
|
2019-07-02 22:18:40 +00:00
|
|
|
w.loc = append(w.loc, loc)
|
2015-04-21 15:02:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) Exit(loc reflectwalk.Location) error {
|
2019-07-02 22:18:40 +00:00
|
|
|
w.loc = w.loc[:len(w.loc)-1]
|
2015-04-21 15:02:03 +00:00
|
|
|
|
|
|
|
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.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))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-08 18:06:08 +00:00
|
|
|
func (w *hashWalker) Struct(v reflect.Value) error {
|
|
|
|
// We are looking for time values. If it isn't one, ignore it.
|
|
|
|
if v.Type() != hashTimeType {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
if len(w.loc) < 3 {
|
|
|
|
// The last element of w.loc is reflectwalk.Struct, by definition.
|
|
|
|
// If len(w.loc) < 3 that means hashWalker.Walk was given a struct
|
|
|
|
// value and this is the very first step in the walk, and we don't
|
|
|
|
// currently support structs as inputs,
|
|
|
|
return errors.New("structs as direct inputs not supported")
|
2017-05-08 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// Second to last element of w.loc is location that contains this struct.
|
|
|
|
switch w.loc[len(w.loc)-2] {
|
|
|
|
case reflectwalk.MapValue:
|
|
|
|
// Create a string value of the time. IMPORTANT: this must never change
|
|
|
|
// across Vault versions or the hash value of equivalent time.Time will
|
|
|
|
// change.
|
|
|
|
strVal := v.Interface().(time.Time).Format(time.RFC3339Nano)
|
2017-05-08 18:06:08 +00:00
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// Set the map value to the string instead of the time.Time object
|
|
|
|
m := w.cs[len(w.cs)-1]
|
|
|
|
mk := w.csKey[len(w.cs)-1]
|
|
|
|
m.SetMapIndex(mk, reflect.ValueOf(strVal))
|
|
|
|
case reflectwalk.SliceElem:
|
|
|
|
// Create a string value of the time. IMPORTANT: this must never change
|
|
|
|
// across Vault versions or the hash value of equivalent time.Time will
|
|
|
|
// change.
|
|
|
|
strVal := v.Interface().(time.Time).Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
// Set the map value to the string instead of the time.Time object
|
|
|
|
s := w.cs[len(w.cs)-1]
|
|
|
|
si := int(w.csKey[len(w.cs)-1].Int())
|
|
|
|
s.Slice(si, si+1).Index(0).Set(reflect.ValueOf(strVal))
|
|
|
|
}
|
2017-05-08 18:06:08 +00:00
|
|
|
|
|
|
|
// Skip this entry so that we don't walk the struct.
|
|
|
|
return reflectwalk.SkipEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hashWalker) StructField(reflect.StructField, reflect.Value) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
// Primitive calls Callback to transform strings in-place, except for map keys.
|
|
|
|
// Strings hiding within interfaces are also transformed.
|
2015-04-21 15:02:03 +00:00
|
|
|
func (w *hashWalker) Primitive(v reflect.Value) error {
|
|
|
|
if w.Callback == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't touch map keys
|
2019-07-02 22:18:40 +00:00
|
|
|
if w.loc[len(w.loc)-1] == reflectwalk.MapKey {
|
2015-04-21 15:02:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
setV := v
|
|
|
|
|
|
|
|
// We only care about strings
|
|
|
|
if v.Kind() == reflect.Interface {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
if v.Kind() != reflect.String {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:18:39 +00:00
|
|
|
// See if the current key is part of the ignored keys
|
|
|
|
currentKey := w.key[len(w.key)-1]
|
|
|
|
if strutil.StrListContains(w.IgnoredKeys, currentKey) {
|
|
|
|
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)
|
2019-07-02 22:18:40 +00:00
|
|
|
switch w.loc[len(w.loc)-1] {
|
2015-04-21 15:02:03 +00:00
|
|
|
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]
|
2019-07-02 22:18:40 +00:00
|
|
|
mk := w.csKey[len(w.cs)-1]
|
2015-04-21 15:02:03 +00:00
|
|
|
m.SetMapIndex(mk, resultVal)
|
2019-07-02 22:18:40 +00:00
|
|
|
case reflectwalk.SliceElem:
|
|
|
|
s := w.cs[len(w.cs)-1]
|
|
|
|
si := int(w.csKey[len(w.cs)-1].Int())
|
|
|
|
s.Slice(si, si+1).Index(0).Set(resultVal)
|
2015-04-21 15:02:03 +00:00
|
|
|
default:
|
|
|
|
// Otherwise, we should be addressable
|
|
|
|
setV.Set(resultVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|