vault/audit/writer_json.go
Peter Wilson 31074bc448
VAULT-18284: Audit refactor packages (#21972)
* initial git mv to rename 'audit' packages

* remove 'Audit' prefix from structs inside audit package

* refactor of event/audit pacakges

* EventFormatter => EntryFormatter

* 'AuditFormat' => EntryFormat

* Use NewFormatterConfig func

---------

Co-authored-by: Marc Boudreau <marc.boudreau@hashicorp.com>
2023-07-20 13:32:06 -04:00

50 lines
914 B
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package audit
import (
"encoding/json"
"fmt"
"io"
)
var _ Writer = (*JSONWriter)(nil)
// JSONWriter is a Writer implementation that structures data into a JSON format.
type JSONWriter struct {
Prefix string
}
func (f *JSONWriter) WriteRequest(w io.Writer, req *RequestEntry) error {
if req == nil {
return fmt.Errorf("request entry was nil, cannot encode")
}
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
enc := json.NewEncoder(w)
return enc.Encode(req)
}
func (f *JSONWriter) WriteResponse(w io.Writer, resp *ResponseEntry) error {
if resp == nil {
return fmt.Errorf("response entry was nil, cannot encode")
}
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
enc := json.NewEncoder(w)
return enc.Encode(resp)
}