1package audit 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "io" 8 9 "github.com/hashicorp/vault/sdk/helper/salt" 10 "github.com/jefferai/jsonx" 11) 12 13// JSONxFormatWriter is an AuditFormatWriter implementation that structures data into 14// a XML format. 15type JSONxFormatWriter struct { 16 Prefix string 17 SaltFunc func(context.Context) (*salt.Salt, error) 18} 19 20func (f *JSONxFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error { 21 if req == nil { 22 return fmt.Errorf("request entry was nil, cannot encode") 23 } 24 25 if len(f.Prefix) > 0 { 26 _, err := w.Write([]byte(f.Prefix)) 27 if err != nil { 28 return err 29 } 30 } 31 32 jsonBytes, err := json.Marshal(req) 33 if err != nil { 34 return err 35 } 36 37 xmlBytes, err := jsonx.EncodeJSONBytes(jsonBytes) 38 if err != nil { 39 return err 40 } 41 42 _, err = w.Write(xmlBytes) 43 return err 44} 45 46func (f *JSONxFormatWriter) WriteResponse(w io.Writer, resp *AuditResponseEntry) error { 47 if resp == nil { 48 return fmt.Errorf("response entry was nil, cannot encode") 49 } 50 51 if len(f.Prefix) > 0 { 52 _, err := w.Write([]byte(f.Prefix)) 53 if err != nil { 54 return err 55 } 56 } 57 58 jsonBytes, err := json.Marshal(resp) 59 if err != nil { 60 return err 61 } 62 63 xmlBytes, err := jsonx.EncodeJSONBytes(jsonBytes) 64 if err != nil { 65 return err 66 } 67 68 _, err = w.Write(xmlBytes) 69 return err 70} 71 72func (f *JSONxFormatWriter) Salt(ctx context.Context) (*salt.Salt, error) { 73 return f.SaltFunc(ctx) 74} 75