2015-04-13 21:12:03 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2018-03-08 19:21:11 +00:00
|
|
|
"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"
|
2017-05-24 00:36:20 +00:00
|
|
|
|
|
|
|
"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.
|
2017-02-11 00:56:28 +00:00
|
|
|
type JSONFormatWriter struct {
|
2017-05-24 00:36:20 +00:00
|
|
|
Prefix string
|
2018-03-08 19:21:11 +00:00
|
|
|
SaltFunc func(context.Context) (*salt.Salt, error)
|
2017-02-11 00:56:28 +00:00
|
|
|
}
|
2015-06-19 01:30:18 +00:00
|
|
|
|
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-06-19 01:30:18 +00:00
|
|
|
}
|
2015-04-13 21:12:03 +00:00
|
|
|
|
2017-02-11 00:56:28 +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
|
|
|
}
|
|
|
|
|
2017-02-11 00:56:28 +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)
|
2015-06-29 22:27:28 +00:00
|
|
|
}
|
2017-05-24 00:36:20 +00:00
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
func (f *JSONFormatWriter) Salt(ctx context.Context) (*salt.Salt, error) {
|
|
|
|
return f.SaltFunc(ctx)
|
2017-05-24 00:36:20 +00:00
|
|
|
}
|