VAULT-17080: refactor prior to introducing JSON formatting nodes for audit (#21708)

* Split out interfaces for salter/formatter/writer
This commit is contained in:
Peter Wilson
2023-07-11 15:30:24 +01:00
committed by GitHub
parent a9778be3f2
commit 637679a327
11 changed files with 376 additions and 240 deletions

View File

@@ -13,22 +13,31 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/helper/salt"
"github.com/hashicorp/vault/sdk/logical"
)
func TestFormatJSON_formatRequest(t *testing.T) {
salter, err := salt.NewSalt(context.Background(), nil, nil)
if err != nil {
t.Fatal(err)
}
saltFunc := func(context.Context) (*salt.Salt, error) {
return salter, nil
}
// staticSalt is a struct which can be used to obtain a static salt.
// a salt must be assigned when the struct is initialized.
type staticSalt struct {
salt *salt.Salt
}
expectedResultStr := fmt.Sprintf(testFormatJSONReqBasicStrFmt, salter.GetIdentifiedHMAC("foo"))
// Salt returns the static salt and no error.
func (s *staticSalt) Salt(ctx context.Context) (*salt.Salt, error) {
return s.salt, nil
}
func TestFormatJSON_formatRequest(t *testing.T) {
s, err := salt.NewSalt(context.Background(), nil, nil)
require.NoError(t, err)
tempStaticSalt := &staticSalt{salt: s}
expectedResultStr := fmt.Sprintf(testFormatJSONReqBasicStrFmt, s.GetIdentifiedHMAC("foo"))
issueTime, _ := time.Parse(time.RFC3339, "2020-05-28T13:40:18-05:00")
cases := map[string]struct {
@@ -104,10 +113,12 @@ func TestFormatJSON_formatRequest(t *testing.T) {
for name, tc := range cases {
var buf bytes.Buffer
formatter := AuditFormatter{
AuditFormatWriter: &JSONFormatWriter{
Prefix: tc.Prefix,
SaltFunc: saltFunc,
f, err := NewAuditFormatter(tempStaticSalt)
require.NoError(t, err)
formatter := AuditFormatterWriter{
Formatter: f,
Writer: &JSONWriter{
Prefix: tc.Prefix,
},
}
config := FormatterConfig{
@@ -118,29 +129,29 @@ func TestFormatJSON_formatRequest(t *testing.T) {
Request: tc.Req,
OuterErr: tc.Err,
}
if err := formatter.FormatRequest(namespace.RootContext(nil), &buf, config, in); err != nil {
t.Fatalf("bad: %s\nerr: %s", name, err)
}
err = formatter.FormatAndWriteRequest(namespace.RootContext(nil), &buf, config, in)
require.NoErrorf(t, err, "bad: %s\nerr: %s", name, err)
if !strings.HasPrefix(buf.String(), tc.Prefix) {
t.Fatalf("no prefix: %s \n log: %s\nprefix: %s", name, expectedResultStr, tc.Prefix)
}
expectedjson := new(AuditRequestEntry)
expectedJSON := new(AuditRequestEntry)
if err := jsonutil.DecodeJSON([]byte(expectedResultStr), &expectedjson); err != nil {
if err := jsonutil.DecodeJSON([]byte(expectedResultStr), &expectedJSON); err != nil {
t.Fatalf("bad json: %s", err)
}
expectedjson.Request.Namespace = &AuditNamespace{ID: "root"}
expectedJSON.Request.Namespace = &AuditNamespace{ID: "root"}
actualjson := new(AuditRequestEntry)
if err := jsonutil.DecodeJSON([]byte(buf.String())[len(tc.Prefix):], &actualjson); err != nil {
t.Fatalf("bad json: %s", err)
}
expectedjson.Time = actualjson.Time
expectedJSON.Time = actualjson.Time
expectedBytes, err := json.Marshal(expectedjson)
expectedBytes, err := json.Marshal(expectedJSON)
if err != nil {
t.Fatalf("unable to marshal json: %s", err)
}