-
Notifications
You must be signed in to change notification settings - Fork 12
/
audit2log_tests.go
323 lines (302 loc) · 11.7 KB
/
audit2log_tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wrapped1logtests
import (
"bytes"
"encoding/json"
"io"
"testing"
"github.com/palantir/pkg/objmatcher"
"github.com/palantir/pkg/safejson"
"github.com/palantir/witchcraft-go-logging/wlog/auditlog/audit2log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Audit2TestCase struct {
Name string
UID string
SID string
TokenID string
OrgID string
TraceID string
OtherUIDs []string
Origin string
AuditName string
AuditResult audit2log.AuditResultType
RequestParams map[string]interface{}
ResultParams map[string]interface{}
JSONMatcher objmatcher.MapMatcher
}
func (tc Audit2TestCase) Params() []audit2log.Param {
return []audit2log.Param{
audit2log.UID(tc.UID),
audit2log.SID(tc.SID),
audit2log.TokenID(tc.TokenID),
audit2log.OrgID(tc.OrgID),
audit2log.TraceID(tc.TraceID),
audit2log.OtherUIDs(tc.OtherUIDs...),
audit2log.Origin(tc.Origin),
audit2log.RequestParams(tc.RequestParams),
audit2log.ResultParams(tc.ResultParams),
}
}
func Audit2TestCases(entityName, entityVersion string) []Audit2TestCase {
return []Audit2TestCase{
{
Name: "basic audit log entry",
UID: "user-1",
SID: "session-1",
TokenID: "X-Y-Z",
OrgID: "org-1",
TraceID: "trace-id-1",
OtherUIDs: []string{"user-2", "user-3"},
Origin: "0.0.0.0",
AuditName: "AUDITED_ACTION_NAME",
AuditResult: audit2log.AuditResultSuccess,
RequestParams: map[string]interface{}{"requestKey": "requestValue"},
ResultParams: map[string]interface{}{"resultKey": "resultValue"},
JSONMatcher: objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("wrapped.1"),
"entityName": objmatcher.NewEqualsMatcher(entityName),
"entityVersion": objmatcher.NewEqualsMatcher(entityVersion),
"payload": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("auditLogV2"),
"auditLogV2": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"time": objmatcher.NewRegExpMatcher(".+"),
"uid": objmatcher.NewEqualsMatcher("user-1"),
"sid": objmatcher.NewEqualsMatcher("session-1"),
"tokenId": objmatcher.NewEqualsMatcher("X-Y-Z"),
"orgId": objmatcher.NewEqualsMatcher("org-1"),
"traceId": objmatcher.NewEqualsMatcher("trace-id-1"),
"otherUids": objmatcher.NewEqualsMatcher([]interface{}{"user-2", "user-3"}),
"origin": objmatcher.NewEqualsMatcher("0.0.0.0"),
"name": objmatcher.NewEqualsMatcher("AUDITED_ACTION_NAME"),
"result": objmatcher.NewEqualsMatcher("SUCCESS"),
"requestParams": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"requestKey": objmatcher.NewEqualsMatcher("requestValue"),
}),
"resultParams": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"resultKey": objmatcher.NewEqualsMatcher("resultValue"),
}),
"type": objmatcher.NewEqualsMatcher("audit.2"),
}),
}),
}),
},
}
}
func Audit2LogJSONTestSuite(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) audit2log.Logger) {
audit2LogJSONOutputTests(t, entityName, entityVersion, loggerProvider)
rParamIsntOverwrittenByRParamsTest(t, entityName, entityVersion, loggerProvider)
extraRParamsDoNotAppear(t, entityName, entityVersion, loggerProvider)
}
func audit2LogJSONOutputTests(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) audit2log.Logger) {
for i, tc := range Audit2TestCases(entityName, entityVersion) {
t.Run(tc.Name, func(t *testing.T) {
buf := &bytes.Buffer{}
logger := loggerProvider(buf)
logger.Audit(
tc.AuditName,
tc.AuditResult,
audit2log.UID(tc.UID),
audit2log.SID(tc.SID),
audit2log.TokenID(tc.TokenID),
audit2log.OrgID(tc.OrgID),
audit2log.TraceID(tc.TraceID),
audit2log.OtherUIDs(tc.OtherUIDs...),
audit2log.Origin(tc.Origin),
audit2log.RequestParams(tc.RequestParams),
audit2log.ResultParams(tc.ResultParams))
gotAuditLog := map[string]interface{}{}
logEntry := buf.Bytes()
err := safejson.Unmarshal(logEntry, &gotAuditLog)
require.NoError(t, err, "Case %d: %s\nAudit log line is not a valid map: %v", i, tc.Name, string(logEntry))
assert.NoError(t, tc.JSONMatcher.Matches(gotAuditLog), "Case %d: %s", i, tc.Name)
})
}
}
// Verifies that if different parameters are specified using ResultParam/RequestParam and ResultParams/RequestParams,
// all of the values are present in the final output (that is, these parameters should be additive).
func rParamIsntOverwrittenByRParamsTest(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) audit2log.Logger) {
mapFieldMatcher := objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"key1": objmatcher.NewEqualsMatcher("val1"),
"key2": objmatcher.NewEqualsMatcher("val2"),
})
for i, tc := range []struct {
name string
params []audit2log.Param
want objmatcher.MapMatcher
}{
{
name: "ResultParam params are additive",
params: []audit2log.Param{
audit2log.ResultParam("key1", "val1"),
audit2log.ResultParams(map[string]interface{}{"key2": "val2"}),
},
want: objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("wrapped.1"),
"entityName": objmatcher.NewEqualsMatcher(entityName),
"entityVersion": objmatcher.NewEqualsMatcher(entityVersion),
"payload": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("auditLogV2"),
"auditLogV2": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"time": objmatcher.NewRegExpMatcher(".+"),
"name": objmatcher.NewEqualsMatcher("audited action name"),
"type": objmatcher.NewEqualsMatcher("audit.2"),
"result": objmatcher.NewEqualsMatcher("SUCCESS"),
"resultParams": mapFieldMatcher,
}),
}),
}),
},
{
name: "RequestParam params are additive",
params: []audit2log.Param{
audit2log.RequestParam("key1", "val1"),
audit2log.RequestParams(map[string]interface{}{
"key2": "val2",
}),
},
want: objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("wrapped.1"),
"entityName": objmatcher.NewEqualsMatcher(entityName),
"entityVersion": objmatcher.NewEqualsMatcher(entityVersion),
"payload": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("auditLogV2"),
"auditLogV2": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"time": objmatcher.NewRegExpMatcher(".+"),
"name": objmatcher.NewEqualsMatcher("audited action name"),
"type": objmatcher.NewEqualsMatcher("audit.2"),
"result": objmatcher.NewEqualsMatcher("SUCCESS"),
"requestParams": mapFieldMatcher,
}),
}),
}),
},
} {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
logger := loggerProvider(&buf)
logger.Audit("audited action name", audit2log.AuditResultSuccess, tc.params...)
auditLog := map[string]interface{}{}
logEntry := buf.Bytes()
err := safejson.Unmarshal(logEntry, &auditLog)
require.NoError(
t,
err,
"Case %d: %s\nAudit log line is not a valid map: %v",
i,
tc.name,
string(logEntry))
assert.NoError(t, tc.want.Matches(auditLog), "Case %d: %s", i, tc.name)
})
}
}
// Verifies that parameters remain separate between different logger calls (ensures there is not a bug where parameters
// are modified by making a logger call).
func extraRParamsDoNotAppear(t *testing.T, entityName, entityVersion string, loggerProvider func(w io.Writer) audit2log.Logger) {
const (
resultParamsKey = "resultParams"
requestParamsKey = "requestParams"
)
for i, tc := range []struct {
name string
paramKey string
paramFunc func(key string, val interface{}) audit2log.Param
paramsFunc func(map[string]interface{}) audit2log.Param
}{
{
name: "Params stay separate across calls for ResultParam",
paramKey: resultParamsKey,
paramFunc: audit2log.ResultParam,
paramsFunc: audit2log.ResultParams,
},
{
name: "Params stay separate across calls for RequestParam",
paramKey: requestParamsKey,
paramFunc: audit2log.RequestParam,
paramsFunc: audit2log.RequestParams,
},
} {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
logger := loggerProvider(&buf)
reusedParams := tc.paramsFunc(map[string]interface{}{"key1": "val1"})
logger.Audit(
"audited action name",
audit2log.AuditResultSuccess,
reusedParams,
tc.paramFunc("key2", "val2"))
want := objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("wrapped.1"),
"entityName": objmatcher.NewEqualsMatcher(entityName),
"entityVersion": objmatcher.NewEqualsMatcher(entityVersion),
"payload": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("auditLogV2"),
"auditLogV2": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"time": objmatcher.NewRegExpMatcher(".+"),
"name": objmatcher.NewEqualsMatcher("audited action name"),
"type": objmatcher.NewEqualsMatcher("audit.2"),
"result": objmatcher.NewEqualsMatcher("SUCCESS"),
tc.paramKey: objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"key1": objmatcher.NewEqualsMatcher("val1"),
"key2": objmatcher.NewEqualsMatcher("val2"),
}),
}),
}),
})
auditLog := map[string]interface{}{}
logEntry := buf.Bytes()
err := json.Unmarshal(logEntry, &auditLog)
require.NoError(
t,
err,
"Case %d: %s\nAudit log is not a valid map: %v",
i,
tc.name,
string(logEntry))
assert.NoError(t, want.Matches(auditLog), "Case %d: %s", i, tc.name)
buf.Reset()
logger.Audit("audited action name", audit2log.AuditResultSuccess, reusedParams)
want = objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("wrapped.1"),
"entityName": objmatcher.NewEqualsMatcher(entityName),
"entityVersion": objmatcher.NewEqualsMatcher(entityVersion),
"payload": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"type": objmatcher.NewEqualsMatcher("auditLogV2"),
"auditLogV2": objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"time": objmatcher.NewRegExpMatcher(".+"),
"name": objmatcher.NewEqualsMatcher("audited action name"),
"type": objmatcher.NewEqualsMatcher("audit.2"),
"result": objmatcher.NewEqualsMatcher("SUCCESS"),
tc.paramKey: objmatcher.MapMatcher(map[string]objmatcher.Matcher{
"key1": objmatcher.NewEqualsMatcher("val1"),
}),
}),
}),
})
auditLog = map[string]interface{}{}
logEntry = buf.Bytes()
err = json.Unmarshal(logEntry, &auditLog)
require.NoError(
t,
err,
"Case %d: %s\nAudit log is not a valid map: %v",
i,
tc.name,
string(logEntry))
assert.NoError(t, want.Matches(auditLog), "Case %d: %s", i, tc.name)
})
}
}