2017-02-16 18:09:53 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2018-03-08 19:21:11 +00:00
|
|
|
"context"
|
2017-02-16 18:09:53 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/salt"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-02-16 18:09:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type noopFormatWriter struct {
|
2017-05-24 00:36:20 +00:00
|
|
|
salt *salt.Salt
|
|
|
|
SaltFunc func() (*salt.Salt, error)
|
2017-02-16 18:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopFormatWriter) WriteRequest(_ io.Writer, _ *AuditRequestEntry) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopFormatWriter) WriteResponse(_ io.Writer, _ *AuditResponseEntry) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
func (n *noopFormatWriter) Salt(ctx context.Context) (*salt.Salt, error) {
|
2017-05-24 00:36:20 +00:00
|
|
|
if n.salt != nil {
|
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
var err error
|
2018-03-08 19:21:11 +00:00
|
|
|
n.salt, err = salt.NewSalt(ctx, nil, nil)
|
2017-05-24 00:36:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-16 18:09:53 +00:00
|
|
|
}
|
2017-05-24 00:36:20 +00:00
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatRequestErrors(t *testing.T) {
|
|
|
|
config := FormatterConfig{}
|
2017-02-16 18:09:53 +00:00
|
|
|
formatter := AuditFormatter{
|
|
|
|
AuditFormatWriter: &noopFormatWriter{},
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
if err := formatter.FormatRequest(context.Background(), ioutil.Discard, config, &LogInput{}); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil request")
|
|
|
|
}
|
2018-03-02 17:18:39 +00:00
|
|
|
|
|
|
|
in := &LogInput{
|
|
|
|
Request: &logical.Request{},
|
|
|
|
}
|
2018-03-08 19:21:11 +00:00
|
|
|
if err := formatter.FormatRequest(context.Background(), nil, config, in); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil writer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatResponseErrors(t *testing.T) {
|
2017-05-24 00:36:20 +00:00
|
|
|
config := FormatterConfig{}
|
2017-02-16 18:09:53 +00:00
|
|
|
formatter := AuditFormatter{
|
|
|
|
AuditFormatWriter: &noopFormatWriter{},
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:21:11 +00:00
|
|
|
if err := formatter.FormatResponse(context.Background(), ioutil.Discard, config, &LogInput{}); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil request")
|
|
|
|
}
|
2018-03-02 17:18:39 +00:00
|
|
|
|
|
|
|
in := &LogInput{
|
|
|
|
Request: &logical.Request{},
|
|
|
|
}
|
2018-03-08 19:21:11 +00:00
|
|
|
if err := formatter.FormatResponse(context.Background(), nil, config, in); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil writer")
|
|
|
|
}
|
|
|
|
}
|