diff --git a/audit/format_json_test.go b/audit/format_json_test.go index fdef20806d..88e1df0901 100644 --- a/audit/format_json_test.go +++ b/audit/format_json_test.go @@ -5,12 +5,14 @@ import ( "testing" "github.com/hashicorp/vault/logical" + "errors" ) func TestFormatJSON_formatRequest(t *testing.T) { cases := map[string]struct { Auth *logical.Auth Req *logical.Request + Err error Result string }{ "auth, request": { @@ -18,7 +20,11 @@ func TestFormatJSON_formatRequest(t *testing.T) { &logical.Request{ Operation: logical.WriteOperation, Path: "/foo", + Connection: &logical.Connection{ + RemoteAddr: "127.0.0.1", + }, }, + errors.New("this is an error"), testFormatJSONReqBasicStr, }, } @@ -26,7 +32,7 @@ func TestFormatJSON_formatRequest(t *testing.T) { for name, tc := range cases { var buf bytes.Buffer var format FormatJSON - if err := format.FormatRequest(&buf, tc.Auth, tc.Req); err != nil { + if err := format.FormatRequest(&buf, tc.Auth, tc.Req, tc.Err); err != nil { t.Fatalf("bad: %s\nerr: %s", name, err) } @@ -38,5 +44,5 @@ func TestFormatJSON_formatRequest(t *testing.T) { } } -const testFormatJSONReqBasicStr = `{"type":"request","auth":{"display_name":"","policies":["root"],"metadata":null},"request":{"operation":"write","path":"/foo","data":null}} +const testFormatJSONReqBasicStr = `{"type":"request","auth":{"display_name":"","policies":["root"],"metadata":null},"request":{"operation":"write","path":"/foo","data":null,"remote_address":"127.0.0.1"},"error":"this is an error"} ` diff --git a/vault/audit_test.go b/vault/audit_test.go index 56c71197dd..f4e484806b 100644 --- a/vault/audit_test.go +++ b/vault/audit_test.go @@ -10,12 +10,14 @@ import ( "github.com/hashicorp/vault/audit" "github.com/hashicorp/vault/logical" + "errors" ) type NoopAudit struct { ReqErr error ReqAuth []*logical.Auth Req []*logical.Request + ReqErrs []error RespErr error RespAuth []*logical.Auth @@ -24,9 +26,10 @@ type NoopAudit struct { RespErrs []error } -func (n *NoopAudit) LogRequest(a *logical.Auth, r *logical.Request) error { +func (n *NoopAudit) LogRequest(a *logical.Auth, r *logical.Request, err error) error { n.ReqAuth = append(n.ReqAuth, a) n.Req = append(n.Req, r) + n.ReqErrs = append(n.ReqErrs, err) return n.ReqErr } @@ -203,8 +206,9 @@ func TestAuditBroker_LogRequest(t *testing.T) { Operation: logical.ReadOperation, Path: "sys/mounts", } + reqErrs := errors.New("errs") - err := b.LogRequest(auth, req) + err := b.LogRequest(auth, req, reqErrs) if err != nil { t.Fatalf("err: %v", err) } @@ -216,17 +220,20 @@ func TestAuditBroker_LogRequest(t *testing.T) { if !reflect.DeepEqual(a.Req[0], req) { t.Fatalf("Bad: %#v", a.Req[0]) } + if !reflect.DeepEqual(a.ReqErrs[0], reqErrs) { + t.Fatalf("Bad: %#v", a.ReqErrs[0]) + } } // Should still work with one failing backend a1.ReqErr = fmt.Errorf("failed") - if err := b.LogRequest(auth, req); err != nil { + if err := b.LogRequest(auth, req, nil); err != nil { t.Fatalf("err: %v", err) } // Should FAIL work with both failing backends a2.ReqErr = fmt.Errorf("failed") - if err := b.LogRequest(auth, req); err.Error() != "no audit backend succeeded in logging the request" { + if err := b.LogRequest(auth, req, nil); err.Error() != "no audit backend succeeded in logging the request" { t.Fatalf("err: %v", err) } }