open-vault/audit/format_jsonx.go
Tommy Murphy ca06bc0b53 audit: support a configurable prefix string to write before each message (#2359)
A static token at the beginning of a log line can help systems parse
logs better. For example, rsyslog and syslog-ng will recognize the
'@cee: ' prefix and will parse the rest of the line as a valid json message.
This is useful in environments where there is a mix of structured and
unstructured logs.
2017-02-10 16:56:28 -08:00

68 lines
1.2 KiB
Go

package audit
import (
"encoding/json"
"fmt"
"io"
"github.com/jefferai/jsonx"
)
// JSONxFormatWriter is an AuditFormatWriter implementation that structures data into
// a XML format.
type JSONxFormatWriter struct {
Prefix string
}
func (f *JSONxFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) 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
}
}
jsonBytes, err := json.Marshal(req)
if err != nil {
return err
}
xmlBytes, err := jsonx.EncodeJSONBytes(jsonBytes)
if err != nil {
return err
}
_, err = w.Write(xmlBytes)
return err
}
func (f *JSONxFormatWriter) WriteResponse(w io.Writer, resp *AuditResponseEntry) 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
}
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
return err
}
xmlBytes, err := jsonx.EncodeJSONBytes(jsonBytes)
if err != nil {
return err
}
_, err = w.Write(xmlBytes)
return err
}