2015-04-13 17:12:03 -04:00
package audit
import (
"bytes"
2016-01-07 15:10:05 -05:00
"encoding/json"
2015-08-05 10:44:48 -04:00
"strings"
2015-04-13 17:12:03 -04:00
"testing"
2016-05-07 21:08:13 -04:00
"time"
2015-04-13 17:12:03 -04:00
2015-06-18 23:14:20 -04:00
"errors"
2016-01-07 15:10:05 -05:00
2017-06-05 18:04:31 -04:00
"fmt"
2018-03-02 12:18:39 -05:00
2016-07-06 12:25:40 -04:00
"github.com/hashicorp/vault/helper/jsonutil"
2016-09-21 10:29:42 -04:00
"github.com/hashicorp/vault/helper/salt"
2016-01-07 15:10:05 -05:00
"github.com/hashicorp/vault/logical"
2015-04-13 17:12:03 -04:00
)
func TestFormatJSON_formatRequest ( t * testing . T ) {
2017-05-23 20:36:20 -04:00
salter , err := salt . NewSalt ( nil , nil )
if err != nil {
t . Fatal ( err )
}
saltFunc := func ( ) ( * salt . Salt , error ) {
return salter , nil
}
2017-06-05 18:04:31 -04:00
expectedResultStr := fmt . Sprintf ( testFormatJSONReqBasicStrFmt , salter . GetIdentifiedHMAC ( "foo" ) )
2015-04-13 17:12:03 -04:00
cases := map [ string ] struct {
2017-06-05 18:04:31 -04:00
Auth * logical . Auth
Req * logical . Request
Err error
Prefix string
ExpectedStr string
2015-04-13 17:12:03 -04:00
} {
"auth, request" : {
2017-06-05 18:04:31 -04:00
& logical . Auth { ClientToken : "foo" , Accessor : "bar" , DisplayName : "testtoken" , Policies : [ ] string { "root" } } ,
2015-04-13 17:12:03 -04:00
& logical . Request {
2016-01-07 10:30:47 -05:00
Operation : logical . UpdateOperation ,
2015-04-13 17:12:03 -04:00
Path : "/foo" ,
2015-06-18 23:14:20 -04:00
Connection : & logical . Connection {
RemoteAddr : "127.0.0.1" ,
} ,
2017-01-04 16:44:03 -05:00
WrapInfo : & logical . RequestWrapInfo {
TTL : 60 * time . Second ,
} ,
2017-02-02 14:49:20 -05:00
Headers : map [ string ] [ ] string {
"foo" : [ ] string { "bar" } ,
} ,
2015-04-13 17:12:03 -04:00
} ,
2015-06-18 23:14:20 -04:00
errors . New ( "this is an error" ) ,
2017-02-10 19:56:28 -05:00
"" ,
2017-06-05 18:04:31 -04:00
expectedResultStr ,
2017-02-10 19:56:28 -05:00
} ,
"auth, request with prefix" : {
2017-06-05 18:04:31 -04:00
& logical . Auth { ClientToken : "foo" , Accessor : "bar" , DisplayName : "testtoken" , Policies : [ ] string { "root" } } ,
2017-02-10 19:56:28 -05:00
& logical . Request {
Operation : logical . UpdateOperation ,
Path : "/foo" ,
Connection : & logical . Connection {
RemoteAddr : "127.0.0.1" ,
} ,
WrapInfo : & logical . RequestWrapInfo {
TTL : 60 * time . Second ,
} ,
Headers : map [ string ] [ ] string {
"foo" : [ ] string { "bar" } ,
} ,
} ,
errors . New ( "this is an error" ) ,
"@cee: " ,
2017-06-05 18:04:31 -04:00
expectedResultStr ,
2015-04-13 17:12:03 -04:00
} ,
}
for name , tc := range cases {
var buf bytes . Buffer
2016-09-21 10:29:42 -04:00
formatter := AuditFormatter {
2017-02-10 19:56:28 -05:00
AuditFormatWriter : & JSONFormatWriter {
2017-05-23 20:36:20 -04:00
Prefix : tc . Prefix ,
SaltFunc : saltFunc ,
2017-02-10 19:56:28 -05:00
} ,
2016-09-21 10:29:42 -04:00
}
2017-06-05 18:04:31 -04:00
config := FormatterConfig {
HMACAccessor : false ,
}
2018-03-02 12:18:39 -05:00
in := & LogInput {
Auth : tc . Auth ,
Request : tc . Req ,
OuterErr : tc . Err ,
}
if err := formatter . FormatRequest ( & buf , config , in ) ; err != nil {
2015-04-13 17:12:03 -04:00
t . Fatalf ( "bad: %s\nerr: %s" , name , err )
}
2017-02-10 19:56:28 -05:00
if ! strings . HasPrefix ( buf . String ( ) , tc . Prefix ) {
2017-06-05 18:04:31 -04:00
t . Fatalf ( "no prefix: %s \n log: %s\nprefix: %s" , name , expectedResultStr , tc . Prefix )
2017-02-10 19:56:28 -05:00
}
2016-09-21 10:29:42 -04:00
var expectedjson = new ( AuditRequestEntry )
2017-06-05 18:04:31 -04:00
if err := jsonutil . DecodeJSON ( [ ] byte ( expectedResultStr ) , & expectedjson ) ; err != nil {
2015-08-05 10:44:48 -04:00
t . Fatalf ( "bad json: %s" , err )
}
2016-09-21 10:29:42 -04:00
var actualjson = new ( AuditRequestEntry )
2017-02-10 19:56:28 -05:00
if err := jsonutil . DecodeJSON ( [ ] byte ( buf . String ( ) ) [ len ( tc . Prefix ) : ] , & actualjson ) ; err != nil {
2015-08-05 10:44:48 -04:00
t . Fatalf ( "bad json: %s" , err )
}
expectedjson . Time = actualjson . Time
expectedBytes , err := json . Marshal ( expectedjson )
if err != nil {
t . Fatalf ( "unable to marshal json: %s" , err )
}
2017-02-10 19:56:28 -05:00
if ! strings . HasSuffix ( strings . TrimSpace ( buf . String ( ) ) , string ( expectedBytes ) ) {
2015-04-13 17:12:03 -04:00
t . Fatalf (
2015-08-05 10:44:48 -04:00
"bad: %s\nResult:\n\n'%s'\n\nExpected:\n\n'%s'" ,
name , buf . String ( ) , string ( expectedBytes ) )
2015-04-13 17:12:03 -04:00
}
}
}
2017-06-05 18:04:31 -04:00
const testFormatJSONReqBasicStrFmt = ` { "time" : "2015-08-05T13:45:46Z" , "type" : "request" , "auth" : { "client_token" : "%s" , "accessor" : "bar" , "display_name" : "testtoken" , "policies" : [ "root" ] , "metadata" : null } , "request" : { "operation" : "update" , "path" : "/foo" , "data" : null , "wrap_ttl" : 60 , "remote_address" : "127.0.0.1" , "headers" : { "foo" : [ "bar" ] } } , "error" : "this is an error" }
2015-04-13 17:12:03 -04:00
`