@@ -16,8 +16,11 @@ limitations under the License.
1616package cmd
1717
1818import (
19+ "path/filepath"
1920 "testing"
21+ "time"
2022
23+ "github.com/ncode/vault-audit-filter/pkg/auditserver"
2124 "github.com/spf13/viper"
2225 "github.com/stretchr/testify/assert"
2326 "github.com/stretchr/testify/require"
@@ -56,6 +59,76 @@ func TestAuditServerCmd_ValidConfig(t *testing.T) {
5659 assert .NotNil (t , auditServerCmd .RunE )
5760}
5861
62+ func TestAuditServerRuntimeSettingsFromViper (t * testing.T ) {
63+ viper .Reset ()
64+ logPath := filepath .Join (t .TempDir (), "audit.log" )
65+ viper .Set ("vault.audit_protocol" , "tcp" )
66+ viper .Set ("async.queue_size" , 9 )
67+ viper .Set ("async.workers" , 3 )
68+ viper .Set ("async.enqueue_mode" , "wait" )
69+ viper .Set ("async.enqueue_timeout" , "13ms" )
70+ viper .Set ("async.timeout" , "19ms" )
71+ viper .Set ("async.durable.enabled" , true )
72+ viper .Set ("async.durable.dir" , t .TempDir ())
73+ viper .Set ("async.retry.max_attempts" , 6 )
74+ viper .Set ("async.retry.backoff" , "29ms" )
75+ viper .Set ("rule_groups" , []map [string ]interface {}{{
76+ "name" : "configured" ,
77+ "rules" : []string {"Request.Operation == 'read'" },
78+ "log_file" : map [string ]interface {}{"file_path" : logPath , "max_size" : 1 },
79+ }})
80+
81+ settings , err := auditServerRuntimeSettings ()
82+ require .NoError (t , err )
83+ assert .Equal (t , "tcp" , settings .AuditProtocol )
84+ assert .Equal (t , 9 , settings .Async .QueueSize )
85+ assert .Equal (t , 3 , settings .Async .Workers )
86+ assert .Equal (t , "wait" , settings .Async .EnqueueMode )
87+ assert .Equal (t , 13 * time .Millisecond , settings .Async .EnqueueTimeout )
88+ assert .Equal (t , 19 * time .Millisecond , settings .Async .Timeout )
89+ assert .True (t , settings .Async .Durable .Enabled )
90+ assert .Equal (t , 6 , settings .Async .Retry .MaxAttempts )
91+ assert .Equal (t , 29 * time .Millisecond , settings .Async .Retry .Backoff )
92+ require .Len (t , settings .RuleGroups , 1 )
93+ assert .Equal (t , "configured" , settings .RuleGroups [0 ].Name )
94+ assert .Equal (t , logPath , settings .RuleGroups [0 ].LogFile .FilePath )
95+ }
96+
97+ func TestAuditServerRuntimeSettings_InvalidAsyncValuesFallBackToDefaults (t * testing.T ) {
98+ viper .Reset ()
99+ viper .Set ("vault.audit_protocol" , "udp" )
100+ viper .Set ("async.queue_size" , 0 )
101+ viper .Set ("async.workers" , - 1 )
102+ viper .Set ("async.enqueue_mode" , "invalid" )
103+ viper .Set ("async.enqueue_timeout" , "bad" )
104+ viper .Set ("async.timeout" , "also-bad" )
105+ viper .Set ("async.durable.dir" , "" )
106+ viper .Set ("async.retry.max_attempts" , 0 )
107+ viper .Set ("async.retry.backoff" , "worse" )
108+ viper .Set ("rule_groups" , []map [string ]interface {}{})
109+
110+ settings , err := auditServerRuntimeSettings ()
111+ require .NoError (t , err )
112+ defaults := auditserver .DefaultRuntimeSettings ()
113+ assert .Equal (t , defaults .Async .QueueSize , settings .Async .QueueSize )
114+ assert .Equal (t , defaults .Async .Workers , settings .Async .Workers )
115+ assert .Equal (t , defaults .Async .EnqueueMode , settings .Async .EnqueueMode )
116+ assert .Equal (t , defaults .Async .EnqueueTimeout , settings .Async .EnqueueTimeout )
117+ assert .Equal (t , defaults .Async .Timeout , settings .Async .Timeout )
118+ assert .Equal (t , defaults .Async .Durable .Dir , settings .Async .Durable .Dir )
119+ assert .Equal (t , defaults .Async .Retry .MaxAttempts , settings .Async .Retry .MaxAttempts )
120+ assert .Equal (t , defaults .Async .Retry .Backoff , settings .Async .Retry .Backoff )
121+ }
122+
123+ func TestAuditServerRuntimeSettings_InvalidProtocol (t * testing.T ) {
124+ viper .Reset ()
125+ viper .Set ("vault.audit_protocol" , "invalid" )
126+
127+ _ , err := auditServerRuntimeSettings ()
128+ require .Error (t , err )
129+ assert .Contains (t , err .Error (), "unsupported vault.audit_protocol" )
130+ }
131+
59132func TestAuditServerCmd_RunE_InvokesGnetRun (t * testing.T ) {
60133 viper .Reset ()
61134 viper .Set ("vault.audit_address" , "bad host" )
0 commit comments