open-vault/audit/format_json.go

54 lines
1.0 KiB
Go
Raw Normal View History

2015-04-13 21:12:03 +00:00
package audit
import (
"context"
2015-04-13 21:12:03 +00:00
"encoding/json"
2016-09-21 14:29:42 +00:00
"fmt"
2015-04-13 21:12:03 +00:00
"io"
"github.com/hashicorp/vault/helper/salt"
2015-04-13 21:12:03 +00:00
)
2016-09-21 14:29:42 +00:00
// JSONFormatWriter is an AuditFormatWriter implementation that structures data into
2015-04-13 21:12:03 +00:00
// a JSON format.
type JSONFormatWriter struct {
Prefix string
SaltFunc func(context.Context) (*salt.Salt, error)
}
2016-09-21 14:29:42 +00:00
func (f *JSONFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error {
if req == nil {
return fmt.Errorf("request entry was nil, cannot encode")
}
2015-04-13 21:12:03 +00:00
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
2015-04-13 21:12:03 +00:00
enc := json.NewEncoder(w)
2016-09-21 14:29:42 +00:00
return enc.Encode(req)
2015-04-13 21:12:03 +00:00
}
2016-09-21 14:29:42 +00:00
func (f *JSONFormatWriter) WriteResponse(w io.Writer, resp *AuditResponseEntry) error {
2015-04-13 21:12:03 +00:00
if resp == nil {
2016-09-21 14:29:42 +00:00
return fmt.Errorf("response entry was nil, cannot encode")
2015-04-13 21:12:03 +00:00
}
if len(f.Prefix) > 0 {
_, err := w.Write([]byte(f.Prefix))
if err != nil {
return err
}
}
2015-04-13 21:12:03 +00:00
enc := json.NewEncoder(w)
2016-09-21 14:29:42 +00:00
return enc.Encode(resp)
}
func (f *JSONFormatWriter) Salt(ctx context.Context) (*salt.Salt, error) {
return f.SaltFunc(ctx)
}