-
Notifications
You must be signed in to change notification settings - Fork 12
/
github_user_events_scanner_test.go
435 lines (367 loc) · 15.1 KB
/
github_user_events_scanner_test.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements;
// and to You under the Apache License, Version 2.0. See LICENSE in project root for full license + copyright.
package keynuker
import (
"encoding/json"
"log"
"os"
"testing"
"time"
"context"
"fmt"
"io/ioutil"
"net/url"
"github.com/aws/aws-sdk-go/aws"
"github.com/google/go-github/github"
"github.com/stretchr/testify/assert"
"github.com/tleyden/keynuker/keynuker-go-common"
"gopkg.in/h2non/gock.v1"
)
var fakePushEventRawPayload = `{
"push_id":2031009949,
"size":1,
"distinct_size":1,
"ref":"refs/heads/patch-1",
"head":"148f8d11f8f50ed4c43026a009176c00dd521ada",
"before":"64c38a12bfba4aefcb14b60fc9cd196f4f31634e",
"commits":[
{
"sha":"148f8d11f8f50ed4c43026a009176c00dd521ada",
"author":{
"email":"[email protected]",
"name":"Test User"
},
"message":"Fixes #743 Panic when calling github.Event.ParsePayload() with nil RawPayload (artificial mock scenario)",
"distinct":true,
"url":"https://api.github.com/repos/testuser/go-github/commits/148f8d11f8f50ed4c43026a009176c00dd521ada"
}
]
}`
func TestScanGithubUserEventsForAwsKeys(t *testing.T) {
// Unless integration tests are enabled, use mock fetcher
useMockFetcher := !IntegrationTestsEnabled()
accessToken := "github_access_token"
leakedKey := "FakeAccessKey"
accessKeyMetadata := []FetchedAwsAccessKey{
{
AccessKeyId: aws.String(leakedKey),
UserName: aws.String("[email protected]"),
},
}
githubUser := &github.User{
Login: aws.String("tleyden"),
}
githubUserNoEvents := &github.User{
Login: aws.String("nobody"),
}
githubUserOnlyEventsBeforeCheckpoint := &github.User{
Login: aws.String("userOnlyEventsBeforeCheckpoint"),
}
githubUsers := []*github.User{
githubUser,
githubUserNoEvents,
githubUserOnlyEventsBeforeCheckpoint,
}
// Make a fake checkpoint event that is 24 hours ago
githubCheckpointEventCreatedAt := time.Now().Add(time.Hour * -24)
githubCheckpointEvent := &github.Event{
CreatedAt: aws.Time(githubCheckpointEventCreatedAt),
ID: aws.String("mockGithubEventBeforeCheckpoint3"),
}
githubEventCheckpoints := GithubEventCheckpoints{}
githubEventCheckpoints[*githubUser.Login] = githubCheckpointEvent
githubEventCheckpoints[*githubUserNoEvents.Login] = githubCheckpointEvent
githubEventCheckpoints[*githubUserOnlyEventsBeforeCheckpoint.Login] = githubCheckpointEvent
var fetcher GithubUserEventFetcher
var mockFetcher *GithubUserEventFetcherMock
var liveGithubFetcher *GoGithubUserEventFetcher
var (
mockGithubEvent1 *github.Event
mockGithubEvent2 *github.Event
mockGithubEvent3 *github.Event
mockGithubEventBeforeCheckpoint *github.Event
mockGithubEventBeforeCheckpoint2 *github.Event
mockGithubEventBeforeCheckpoint3 *github.Event
)
switch useMockFetcher {
case true:
// Create mock user event fetcher
mockFetcher = NewGithubUserEventFetcherMock()
for _, mockGithubUser := range githubUsers {
switch mockGithubUser {
case githubUser:
// Tee up a response to call to fetcher.FetchUserEvents() that returns a single event
expectedFetchUserEventsInput := FetchUserEventsInput{
Username: *githubUser.Login,
SinceEventTimestamp: githubCheckpointEvent.CreatedAt,
CheckpointID: *githubCheckpointEvent.ID,
}
rawPayload := json.RawMessage(fakePushEventRawPayload)
mockGithubEvent1 = &github.Event{
Type: aws.String("PushEvent"),
ID: aws.String("mockGithubEvent1"),
CreatedAt: aws.Time(time.Now().Add(time.Minute * -5)),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockGithubEvent2 = &github.Event{
Type: aws.String("PushEvent"),
ID: aws.String("mockGithubEvent2"),
CreatedAt: aws.Time(time.Now().Add(time.Minute * -10)),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockGithubEvent3 = &github.Event{ // this event will be ignored since it's 24 hours before than checkpoint
Type: aws.String("PushEvent"),
ID: aws.String("mockGithubEvent3"),
CreatedAt: aws.Time(githubCheckpointEventCreatedAt.Add(time.Hour * -24)),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockFetcher.On("FetchUserEvents", context.Background(), expectedFetchUserEventsInput).Return(
[]*github.Event{
mockGithubEvent1,
mockGithubEvent2,
mockGithubEvent3,
},
nil, // no error
)
// Tee up a response to fetcher.ScanDownstreamContent which returns some content
// ScanDownstreamContent(ctx context.Context, userEvent *github.Event) (content []byte, err error)
mockFetcher.On("ScanDownstreamContent", context.Background(), mockGithubEvent1, accessKeyMetadata).Return(
accessKeyMetadata,
nil, // no error
)
mockFetcher.On("ScanDownstreamContent", context.Background(), mockGithubEvent2, accessKeyMetadata).Return(
[]FetchedAwsAccessKey{},
nil, // no error
)
case githubUserNoEvents:
expectedFetchUserEventsInput := FetchUserEventsInput{
Username: *githubUserNoEvents.Login,
SinceEventTimestamp: githubCheckpointEvent.CreatedAt,
CheckpointID: *githubCheckpointEvent.ID,
}
mockFetcher.On("FetchUserEvents", context.Background(), expectedFetchUserEventsInput).Return(
[]*github.Event{},
nil, // no error
)
case githubUserOnlyEventsBeforeCheckpoint:
expectedFetchUserEventsInput := FetchUserEventsInput{
Username: *githubUserOnlyEventsBeforeCheckpoint.Login,
SinceEventTimestamp: githubCheckpointEvent.CreatedAt,
CheckpointID: *githubCheckpointEvent.ID,
}
rawPayload := json.RawMessage(fakePushEventRawPayload)
mockGithubEventBeforeCheckpoint = &github.Event{
Type: aws.String("PushEvent"),
ID: aws.String("mockGithubEventBeforeCheckpoint"),
CreatedAt: aws.Time(githubCheckpointEventCreatedAt.Add(time.Hour * -24)),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockGithubEventBeforeCheckpoint2 = &github.Event{
Type: aws.String("PushEvent"),
ID: aws.String("mockGithubEventBeforeCheckpoint2"),
CreatedAt: aws.Time(githubCheckpointEventCreatedAt.Add(time.Hour * -12)),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockGithubEventBeforeCheckpoint3 = &github.Event{ // simulate the case where one of the events is the checkpoint event
Type: aws.String("PushEvent"),
ID: githubCheckpointEvent.ID, // this github event has same ID and CreatedAt as the checkpoint
CreatedAt: aws.Time(githubCheckpointEventCreatedAt),
RawPayload: &rawPayload, // not used except to satisfy go-github parser
}
mockFetcher.On("FetchUserEvents", context.Background(), expectedFetchUserEventsInput).Return(
[]*github.Event{
mockGithubEventBeforeCheckpoint,
mockGithubEventBeforeCheckpoint2,
mockGithubEventBeforeCheckpoint3,
},
nil, // no error
)
}
}
fetcher = mockFetcher
default:
// Don't use a mock fetcher, connect to live github API instead
var ok bool
accessToken, ok = os.LookupEnv(keynuker_go_common.EnvVarKeyNukerTestGithubAccessToken)
if !ok {
t.Skip("You must define environment variable keynuker_test_gh_access_token to run this test")
}
liveGithubFetcher = NewGoGithubUserEventFetcher(accessToken, GetIntegrationGithubApiBaseUrl())
fetcher = liveGithubFetcher
}
params := ParamsScanGithubUserEventsForAwsKeys{
AccessKeyMetadata: accessKeyMetadata,
GithubUsers: githubUsers,
GithubConnectionParams: GithubConnectionParams{
GithubAccessToken: accessToken,
},
KeyNukerOrg: "test",
GithubEventCheckpoints: githubEventCheckpoints,
}
// Create events scanner and run
scanner := NewGithubUserEventsScanner(fetcher)
docWrapper, err := scanner.ScanAwsKeys(params)
if useMockFetcher {
// Make assertions on the result
assert.True(t, err == nil)
assert.True(t, len(docWrapper.LeakedKeyEvents) == 1)
assert.Equal(t, *docWrapper.LeakedKeyEvents[0].GithubEvent.ID, *mockGithubEvent1.ID)
assert.Equal(t, "[email protected]", docWrapper.LeakedKeyEvents[0].LeakerEmail)
assert.True(t, len(docWrapper.GithubEventCheckpoints) == 3)
assert.Equal(t, *mockGithubEvent1.ID, *docWrapper.GithubEventCheckpoints[*githubUser.Login].ID)
// The user with actual events should have a non-nil checkpoint
assert.NotNil(t, docWrapper.GithubEventCheckpoints[*githubUser.Login])
// The user with no events should get it's original checkpoint back
assert.NotNil(t, docWrapper.GithubEventCheckpoints[*githubUserNoEvents.Login])
assert.Equal(t, *docWrapper.GithubEventCheckpoints[*githubUserNoEvents.Login].ID, *githubCheckpointEvent.ID)
// The user with only events older than the checkpoint should also have a non-nil checkpoint
// corresponding to the most recent event that was scanned and skipped
checkpoint := docWrapper.GithubEventCheckpoints[*githubUserOnlyEventsBeforeCheckpoint.Login]
assert.NotNil(t, checkpoint)
// Three events were returned for the githubUserOnlyEventsBeforeCheckpoint -- the one that was
// recorded as the checkpoint should be the most recent from the three events (and happens to be
// identical to the checkpointed event). The three events are:
// - An event that has the same created_at (and ID) as the checkpoint <-- this should be the new checkpoint
// - A recent event that was 12 hours before checkpoint (36 hours ago)
// - Older event was 24 hours before checkpoint (48 hours ago)
assert.True(t, checkpoint.CreatedAt.Equal(githubCheckpointEventCreatedAt))
assert.True(t, *checkpoint.ID == *githubCheckpointEvent.ID)
// There should only be two calls to ScanDownstreamContent -- this should catch cases where
// the event that is older than the checkpoint erroneously triggers a call to ScanDownstreamContent
mockFetcher.AssertNumberOfCalls(t, "ScanDownstreamContent", 2)
}
// Emit the result
docWrapperBytes, err := json.MarshalIndent(docWrapper, "", " ")
assert.True(t, err == nil)
log.Printf("docWrapperBytes: %v", string(docWrapperBytes))
}
// Regression test against mock for reprducing https://github.com/tleyden/keynuker/issues/6
// TODO: This test isn't finished. The mock needs to be extended to handle the scanning of additional commits
// TODO: See the end-to-end-integration test for examples.
func TestScanGithubLargePushEvents(t *testing.T) {
// ------------------------------------ Create Event Fetcher -------------------------------------------------------
var ok bool
accessToken, ok := os.LookupEnv(keynuker_go_common.EnvVarKeyNukerTestGithubAccessToken)
if !ok {
t.Skip("You must define environment variable keynuker_test_gh_access_token to run this test")
}
// Create user event fetcher
fetcher := NewGoGithubUserEventFetcher(accessToken, "")
githubUser := &github.User{
Login: aws.String("tleyden"),
}
// Make a fake checkpoint event that has the current timestamp
githubCheckpointEvent := &github.Event{
CreatedAt: aws.Time(time.Now().Add(time.Hour * -24)),
ID: aws.String("FakeEventID"),
}
githubEventCheckpoints := GithubEventCheckpoints{}
githubEventCheckpoints[*githubUser.Login] = githubCheckpointEvent
leakedKey := "FakeAccessKey"
// ------------------------------------ Setup Gock HTTP mock -------------------------------------------------------
defer gock.Off() // Flush pending mocks after test execution
filename := "testdata/large_push_event.json"
largePushEventData, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("Unable to read file: %v. Err: %v", filename, err)
}
largePushEvent := &github.Event{}
errUnmarshal := json.Unmarshal(largePushEventData, largePushEvent)
if errUnmarshal != nil {
t.Fatalf("Unable to unmarshal data: %v", errUnmarshal)
}
payload, err := largePushEvent.ParsePayload()
if err != nil {
t.Fatalf("Unable to parse payload. Err: %v", err)
}
pushEvent := payload.(*github.PushEvent)
events := []*github.Event{
largePushEvent,
}
gock.New("https://api.github.com").
Get(fmt.Sprintf("/users/%s/events", *githubUser.Login)).
MatchParam("per_page", "100").
Reply(200).
JSON(events)
for i, commit := range pushEvent.Commits {
commitUrl, _ := url.Parse(commit.GetURL())
schemeAndHost := fmt.Sprintf("%s://%s", commitUrl.Scheme, commitUrl.Host)
gock.New(schemeAndHost).
Get(commitUrl.Path).
Reply(200).
JSON(map[string]string{
"content": fmt.Sprintf("commit %d", i),
},
)
}
// ------------------------------------ Invoke Scanner -------------------------------------------------------------
params := ParamsScanGithubUserEventsForAwsKeys{
AccessKeyMetadata: []FetchedAwsAccessKey{
{
AccessKeyId: aws.String(leakedKey),
UserName: aws.String("[email protected]"),
},
},
GithubUsers: []*github.User{
githubUser,
},
GithubConnectionParams: GithubConnectionParams{
GithubAccessToken: "github_access_token",
},
KeyNukerOrg: "test",
GithubEventCheckpoints: githubEventCheckpoints,
}
// Create events scanner and run
scanner := NewGithubUserEventsScanner(fetcher)
docWrapper, err := scanner.ScanAwsKeys(params)
if err != nil {
t.Fatalf("Error calling ScanAwsKeys(): %v", err)
}
log.Printf("doc result: %v err: %v", docWrapper, err)
}
func TestCreateFetchUserEventsInputNilCheckpoint(t *testing.T) {
testUsername := "testuser"
p := ParamsScanGithubUserEventsForAwsKeys{
GithubEventCheckpoints: GithubEventCheckpoints{
testUsername: nil,
},
}
user := &github.User{}
user.Login = aws.String(testUsername)
fetchUserEventsInput := p.CreateFetchUserEventsInput(user)
assert.EqualValues(t, fetchUserEventsInput.Username, testUsername)
assert.True(t, fetchUserEventsInput.SinceEventTimestamp.Before(time.Now()))
}
func TestCreateFetchUserEventsInputNormalCheckpoint(t *testing.T) {
testUsername := "testuser"
now := time.Now()
p := ParamsScanGithubUserEventsForAwsKeys{
GithubEventCheckpoints: GithubEventCheckpoints{
testUsername: &github.Event{
CreatedAt: aws.Time(now),
ID: aws.String("FakeEventID"),
},
},
}
user := &github.User{}
user.Login = aws.String(testUsername)
fetchUserEventsInput := p.CreateFetchUserEventsInput(user)
assert.EqualValues(t, fetchUserEventsInput.Username, testUsername)
assert.True(t, fetchUserEventsInput.SinceEventTimestamp.Equal(now))
}
func TestSetDefaultCheckpointsForMissing(t *testing.T) {
testUsername := "testuser"
params := ParamsScanGithubUserEventsForAwsKeys{
GithubUsers: []*github.User{
{
Login: aws.String(testUsername),
},
},
GithubEventCheckpoints: GithubEventCheckpoints{
testUsername: nil,
},
}
paramsWithDefaultCheckpoints := params.SetDefaultCheckpointsForMissing(keynuker_go_common.DefaultCheckpointEventTimeWindow)
checkpointForTestUser := paramsWithDefaultCheckpoints.GithubEventCheckpoints[testUsername]
assert.True(t, checkpointForTestUser.CreatedAt.Before(time.Now()))
}