2016-09-21 14:29:42 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2017-05-24 00:36:20 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
2016-09-21 14:29:42 +00:00
|
|
|
"github.com/jefferai/jsonx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JSONxFormatWriter is an AuditFormatWriter implementation that structures data into
|
|
|
|
// a XML format.
|
2017-02-11 00:56:28 +00:00
|
|
|
type JSONxFormatWriter struct {
|
2017-05-24 00:36:20 +00:00
|
|
|
Prefix string
|
|
|
|
SaltFunc func() (*salt.Salt, error)
|
2017-02-11 00:56:28 +00:00
|
|
|
}
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
func (f *JSONxFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error {
|
|
|
|
if req == nil {
|
|
|
|
return fmt.Errorf("request entry was nil, cannot encode")
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:56:28 +00:00
|
|
|
if len(f.Prefix) > 0 {
|
|
|
|
_, err := w.Write([]byte(f.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:56:28 +00:00
|
|
|
if len(f.Prefix) > 0 {
|
|
|
|
_, err := w.Write([]byte(f.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
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
|
|
|
|
}
|
2017-05-24 00:36:20 +00:00
|
|
|
|
|
|
|
func (f *JSONxFormatWriter) Salt() (*salt.Salt, error) {
|
|
|
|
return f.SaltFunc()
|
|
|
|
}
|