mirror of
https://github.com/hashicorp/vault.git
synced 2025-12-02 08:01:44 +01:00
* 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>
72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package audit
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/jefferai/jsonx"
|
|
)
|
|
|
|
var _ Writer = (*JSONxWriter)(nil)
|
|
|
|
// JSONxWriter is a Writer implementation that structures data into an XML format.
|
|
type JSONxWriter struct {
|
|
Prefix string
|
|
}
|
|
|
|
func (f *JSONxWriter) 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
|
|
}
|
|
}
|
|
|
|
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 *JSONxWriter) 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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|