forked from gravitational/teleport-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_test.go
766 lines (604 loc) · 23.3 KB
/
slack_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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
package main
import (
"context"
"os/user"
"regexp"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/gravitational/teleport-plugins/lib"
"github.com/gravitational/teleport-plugins/lib/logger"
. "github.com/gravitational/teleport-plugins/lib/testing"
"github.com/gravitational/teleport-plugins/lib/testing/integration"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
var msgFieldRegexp = regexp.MustCompile(`(?im)^\*([a-zA-Z ]+)\*: (.+)$`)
var requestReasonRegexp = regexp.MustCompile("(?im)^\\*Reason\\*:\\ ```\\n(.*?)```(.*?)$")
type SlackSuite struct {
Suite
appConfig Config
userNames struct {
ruler string
requestor string
reviewer1 string
reviewer2 string
plugin string
}
raceNumber int
fakeSlack *FakeSlack
clients map[string]*integration.Client
teleportFeatures *proto.Features
teleportConfig lib.TeleportConfig
}
func TestSlackbot(t *testing.T) { suite.Run(t, &SlackSuite{}) }
func (s *SlackSuite) SetupSuite() {
var err error
t := s.T()
logger.Init()
logger.Setup(logger.Config{Severity: "debug"})
s.raceNumber = runtime.GOMAXPROCS(0)
me, err := user.Current()
require.NoError(t, err)
// We set such a big timeout because integration.NewFromEnv could start
// downloading a Teleport *-bin.tar.gz file which can take a long time.
ctx := s.SetContextTimeout(2 * time.Minute)
teleport, err := integration.NewFromEnv(ctx)
require.NoError(t, err)
t.Cleanup(teleport.Close)
auth, err := teleport.NewAuthService()
require.NoError(t, err)
s.StartApp(auth)
s.clients = make(map[string]*integration.Client)
// Set up the user who has an access to all kinds of resources.
s.userNames.ruler = me.Username + "-ruler@example.com"
client, err := teleport.MakeAdmin(ctx, auth, s.userNames.ruler)
require.NoError(t, err)
s.clients[s.userNames.ruler] = client
// Get the server features.
pong, err := client.Ping(ctx)
require.NoError(t, err)
teleportFeatures := pong.GetServerFeatures()
var bootstrap integration.Bootstrap
// Set up user who can request the access to role "editor".
conditions := types.RoleConditions{Request: &types.AccessRequestConditions{Roles: []string{"editor"}}}
if teleportFeatures.AdvancedAccessWorkflows {
conditions.Request.Thresholds = []types.AccessReviewThreshold{types.AccessReviewThreshold{Approve: 2, Deny: 2}}
}
role, err := bootstrap.AddRole("foo", types.RoleSpecV5{Allow: conditions})
require.NoError(t, err)
user, err := bootstrap.AddUserWithRoles(me.Username+"@example.com", role.GetName())
require.NoError(t, err)
s.userNames.requestor = user.GetName()
// Set up TWO users who can review access requests to role "editor".
conditions = types.RoleConditions{}
if teleportFeatures.AdvancedAccessWorkflows {
conditions.ReviewRequests = &types.AccessReviewConditions{Roles: []string{"editor"}}
}
role, err = bootstrap.AddRole("foo-reviewer", types.RoleSpecV5{Allow: conditions})
require.NoError(t, err)
user, err = bootstrap.AddUserWithRoles(me.Username+"-reviewer1@example.com", role.GetName())
require.NoError(t, err)
s.userNames.reviewer1 = user.GetName()
user, err = bootstrap.AddUserWithRoles(me.Username+"-reviewer2@example.com", role.GetName())
require.NoError(t, err)
s.userNames.reviewer2 = user.GetName()
// Set up plugin user.
role, err = bootstrap.AddRole("access-slack", types.RoleSpecV5{
Allow: types.RoleConditions{
Rules: []types.Rule{
types.NewRule("access_request", []string{"list", "read"}),
types.NewRule("access_plugin_data", []string{"update"}),
},
},
})
require.NoError(t, err)
user, err = bootstrap.AddUserWithRoles("access-slack", role.GetName())
require.NoError(t, err)
s.userNames.plugin = user.GetName()
// Bake all the resources.
err = teleport.Bootstrap(ctx, auth, bootstrap.Resources())
require.NoError(t, err)
// Initialize the clients.
client, err = teleport.NewClient(ctx, auth, s.userNames.requestor)
require.NoError(t, err)
s.clients[s.userNames.requestor] = client
if teleportFeatures.AdvancedAccessWorkflows {
client, err = teleport.NewClient(ctx, auth, s.userNames.reviewer1)
require.NoError(t, err)
s.clients[s.userNames.reviewer1] = client
client, err = teleport.NewClient(ctx, auth, s.userNames.reviewer2)
require.NoError(t, err)
s.clients[s.userNames.reviewer2] = client
}
identityPath, err := teleport.Sign(ctx, auth, s.userNames.plugin)
require.NoError(t, err)
s.teleportConfig.Addr = auth.AuthAddr().String()
s.teleportConfig.Identity = identityPath
s.teleportFeatures = teleportFeatures
}
func (s *SlackSuite) SetupTest() {
t := s.T()
logger.Setup(logger.Config{Severity: "debug"})
s.fakeSlack = NewFakeSlack(User{Name: "slackbot"}, s.raceNumber)
t.Cleanup(s.fakeSlack.Close)
s.fakeSlack.StoreUser(User{Name: "Vladimir", Profile: UserProfile{Email: s.userNames.requestor}})
var conf Config
conf.Teleport = s.teleportConfig
conf.Slack.Token = "000000"
conf.Slack.APIURL = s.fakeSlack.URL() + "/"
s.appConfig = conf
s.SetContextTimeout(5 * time.Second)
}
func (s *SlackSuite) startApp() {
t := s.T()
t.Helper()
app, err := NewApp(s.appConfig)
require.NoError(t, err)
s.StartApp(app)
}
func (s *SlackSuite) ruler() *integration.Client {
return s.clients[s.userNames.ruler]
}
func (s *SlackSuite) requestor() *integration.Client {
return s.clients[s.userNames.requestor]
}
func (s *SlackSuite) reviewer1() *integration.Client {
return s.clients[s.userNames.reviewer1]
}
func (s *SlackSuite) reviewer2() *integration.Client {
return s.clients[s.userNames.reviewer2]
}
func (s *SlackSuite) newAccessRequest(reviewers []User) types.AccessRequest {
t := s.T()
t.Helper()
req, err := types.NewAccessRequest(uuid.New().String(), s.userNames.requestor, "editor")
require.NoError(t, err)
req.SetRequestReason("because of " + strings.Repeat("A", 5000))
var suggestedReviewers []string
for _, user := range reviewers {
suggestedReviewers = append(suggestedReviewers, user.Profile.Email)
}
req.SetSuggestedReviewers(suggestedReviewers)
return req
}
func (s *SlackSuite) createAccessRequest(reviewers []User) types.AccessRequest {
t := s.T()
t.Helper()
req := s.newAccessRequest(reviewers)
err := s.requestor().CreateAccessRequest(s.Context(), req)
require.NoError(t, err)
return req
}
func (s *SlackSuite) checkPluginData(reqID string, cond func(PluginData) bool) PluginData {
t := s.T()
t.Helper()
for {
rawData, err := s.ruler().PollAccessRequestPluginData(s.Context(), "slack", reqID)
require.NoError(t, err)
if data := DecodePluginData(rawData); cond(data) {
return data
}
}
}
func (s *SlackSuite) TestMessagePosting() {
t := s.T()
reviewer1 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
reviewer2 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer2}})
s.startApp()
request := s.createAccessRequest([]User{reviewer2, reviewer1})
pluginData := s.checkPluginData(request.GetName(), func(data PluginData) bool {
return len(data.SlackData) > 0
})
assert.Len(t, pluginData.SlackData, 2)
var messages []Msg
messageSet := make(SlackDataMessageSet)
for i := 0; i < 2; i++ {
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
messageSet.Add(SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.Timestamp})
messages = append(messages, msg)
}
assert.Len(t, messageSet, 2)
assert.Contains(t, messageSet, pluginData.SlackData[0])
assert.Contains(t, messageSet, pluginData.SlackData[1])
sort.Sort(SlackMessageSlice(messages))
assert.Equal(t, reviewer1.ID, messages[0].Channel)
assert.Equal(t, reviewer2.ID, messages[1].Channel)
msgUser, err := parseMessageField(messages[0], "User")
require.NoError(t, err)
assert.Equal(t, s.userNames.requestor, msgUser)
block, ok := messages[0].BlockItems[1].Block.(SectionBlock)
require.True(t, ok)
t.Logf("%q", block.Text.GetText())
matches := requestReasonRegexp.FindAllStringSubmatch(block.Text.GetText(), -1)
require.Equal(t, 1, len(matches))
require.Equal(t, 3, len(matches[0]))
assert.Equal(t, "because of "+strings.Repeat("A", 489), matches[0][1])
assert.Equal(t, " (truncated)", matches[0][2])
statusLine, err := getStatusLine(messages[0])
require.NoError(t, err)
assert.Equal(t, "*Status:* ⏳ PENDING", statusLine)
}
func (s *SlackSuite) TestRecipientsConfig() {
t := s.T()
reviewer1 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
reviewer2 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer2}})
s.appConfig.Recipients = RecipientsMap{
types.Wildcard: []string{reviewer2.Profile.Email, reviewer1.ID},
}
s.startApp()
request := s.createAccessRequest(nil)
pluginData := s.checkPluginData(request.GetName(), func(data PluginData) bool {
return len(data.SlackData) > 0
})
assert.Len(t, pluginData.SlackData, 2)
var (
msg Msg
messages []Msg
)
messageSet := make(SlackDataMessageSet)
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
messageSet.Add(SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.Timestamp})
messages = append(messages, msg)
msg, err = s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
messageSet.Add(SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.Timestamp})
messages = append(messages, msg)
assert.Len(t, messageSet, 2)
assert.Contains(t, messageSet, pluginData.SlackData[0])
assert.Contains(t, messageSet, pluginData.SlackData[1])
sort.Sort(SlackMessageSlice(messages))
assert.Equal(t, reviewer1.ID, messages[0].Channel)
assert.Equal(t, reviewer2.ID, messages[1].Channel)
}
func (s *SlackSuite) TestApproval() {
t := s.T()
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
req := s.createAccessRequest([]User{reviewer})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
s.ruler().ApproveAccessRequest(s.Context(), req.GetName(), "okay")
msgUpdate, err := s.fakeSlack.CheckMessageUpdateByAPI(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msgUpdate.Channel)
assert.Equal(t, msg.Timestamp, msgUpdate.Timestamp)
statusLine, err := getStatusLine(msgUpdate)
require.NoError(t, err)
assert.Equal(t, "*Status:* ✅ APPROVED\n*Resolution reason*: ```\nokay```", statusLine)
}
func (s *SlackSuite) TestDenial() {
t := s.T()
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
req := s.createAccessRequest([]User{reviewer})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
s.ruler().DenyAccessRequest(s.Context(), req.GetName(), "not okay "+strings.Repeat("A", 10000))
msgUpdate, err := s.fakeSlack.CheckMessageUpdateByAPI(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msgUpdate.Channel)
assert.Equal(t, msg.Timestamp, msgUpdate.Timestamp)
statusLine, err := getStatusLine(msgUpdate)
require.NoError(t, err)
assert.Equal(t, "*Status:* ❌ DENIED\n*Resolution reason*: ```\nnot okay "+strings.Repeat("A", 491)+"``` (truncated)", statusLine)
}
func (s *SlackSuite) TestReviewReplies() {
t := s.T()
if !s.teleportFeatures.AdvancedAccessWorkflows {
t.Skip("Doesn't work in OSS version")
}
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
req := s.createAccessRequest([]User{reviewer})
s.checkPluginData(req.GetName(), func(data PluginData) bool {
return len(data.SlackData) > 0
})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
err = s.reviewer1().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer1,
ProposedState: types.RequestState_APPROVED,
Created: time.Now(),
Reason: "okay",
})
require.NoError(t, err)
reply, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer1+" reviewed the request", "reply must contain a review author")
assert.Contains(t, reply.Text, "Resolution: ✅ APPROVED", "reply must contain a proposed state")
assert.Contains(t, reply.Text, "Reason: ```\nokay```", "reply must contain a reason")
err = s.reviewer2().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer2,
ProposedState: types.RequestState_DENIED,
Created: time.Now(),
Reason: "not okay",
})
require.NoError(t, err)
reply, err = s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer2+" reviewed the request", "reply must contain a review author")
assert.Contains(t, reply.Text, "Resolution: ❌ DENIED", "reply must contain a proposed state")
assert.Contains(t, reply.Text, "Reason: ```\nnot okay```", "reply must contain a reason")
}
func (s *SlackSuite) TestApprovalByReview() {
t := s.T()
if !s.teleportFeatures.AdvancedAccessWorkflows {
t.Skip("Doesn't work in OSS version")
}
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
req := s.createAccessRequest([]User{reviewer})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
err = s.reviewer1().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer1,
ProposedState: types.RequestState_APPROVED,
Created: time.Now(),
Reason: "okay",
})
require.NoError(t, err)
reply, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer1+" reviewed the request", "reply must contain a review author")
err = s.reviewer2().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer2,
ProposedState: types.RequestState_APPROVED,
Created: time.Now(),
Reason: "finally okay",
})
require.NoError(t, err)
reply, err = s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer2+" reviewed the request", "reply must contain a review author")
msgUpdate, err := s.fakeSlack.CheckMessageUpdateByAPI(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msgUpdate.Channel)
assert.Equal(t, msg.Timestamp, msgUpdate.Timestamp)
statusLine, err := getStatusLine(msgUpdate)
require.NoError(t, err)
assert.Equal(t, "*Status:* ✅ APPROVED\n*Resolution reason*: ```\nfinally okay```", statusLine)
}
func (s *SlackSuite) TestDenialByReview() {
t := s.T()
if !s.teleportFeatures.AdvancedAccessWorkflows {
t.Skip("Doesn't work in OSS version")
}
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
req := s.createAccessRequest([]User{reviewer})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
err = s.reviewer1().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer1,
ProposedState: types.RequestState_DENIED,
Created: time.Now(),
Reason: "not okay",
})
require.NoError(t, err)
reply, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer1+" reviewed the request", "reply must contain a review author")
err = s.reviewer2().SubmitAccessRequestReview(s.Context(), req.GetName(), types.AccessReview{
Author: s.userNames.reviewer2,
ProposedState: types.RequestState_DENIED,
Created: time.Now(),
Reason: "finally not okay",
})
require.NoError(t, err)
reply, err = s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, msg.Channel, reply.Channel)
assert.Equal(t, msg.Timestamp, reply.ThreadTs)
assert.Contains(t, reply.Text, s.userNames.reviewer2+" reviewed the request", "reply must contain a review author")
msgUpdate, err := s.fakeSlack.CheckMessageUpdateByAPI(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msgUpdate.Channel)
assert.Equal(t, msg.Timestamp, msgUpdate.Timestamp)
statusLine, err := getStatusLine(msgUpdate)
require.NoError(t, err)
assert.Equal(t, "*Status:* ❌ DENIED\n*Resolution reason*: ```\nfinally not okay```", statusLine)
}
func (s *SlackSuite) TestExpiration() {
t := s.T()
reviewer := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
s.startApp()
request := s.createAccessRequest([]User{reviewer})
msg, err := s.fakeSlack.CheckNewMessage(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msg.Channel)
s.checkPluginData(request.GetName(), func(data PluginData) bool {
return len(data.SlackData) > 0
})
err = s.ruler().DeleteAccessRequest(s.Context(), request.GetName()) // simulate expiration
require.NoError(t, err)
msgUpdate, err := s.fakeSlack.CheckMessageUpdateByAPI(s.Context())
require.NoError(t, err)
assert.Equal(t, reviewer.ID, msgUpdate.Channel)
assert.Equal(t, msg.Timestamp, msgUpdate.Timestamp)
statusLine, err := getStatusLine(msgUpdate)
require.NoError(t, err)
assert.Equal(t, "*Status:* ⌛ EXPIRED", statusLine)
}
func (s *SlackSuite) TestRace() {
t := s.T()
if !s.teleportFeatures.AdvancedAccessWorkflows {
t.Skip("Doesn't work in OSS version")
}
logger.Setup(logger.Config{Severity: "info"}) // Turn off noisy debug logging
reviewer1 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer1}})
reviewer2 := s.fakeSlack.StoreUser(User{Profile: UserProfile{Email: s.userNames.reviewer2}})
s.SetContextTimeout(20 * time.Second)
s.startApp()
var (
raceErr error
raceErrOnce sync.Once
threadMsgIDs sync.Map
threadMsgsCount int32
msgUpdateCounters sync.Map
reviewReplyCounters sync.Map
)
setRaceErr := func(err error) error {
raceErrOnce.Do(func() {
raceErr = err
})
return err
}
process := lib.NewProcess(s.Context())
for i := 0; i < s.raceNumber; i++ {
process.SpawnCritical(func(ctx context.Context) error {
req, err := types.NewAccessRequest(uuid.New().String(), s.userNames.requestor, "editor")
if err != nil {
return setRaceErr(trace.Wrap(err))
}
req.SetSuggestedReviewers([]string{reviewer1.Profile.Email, reviewer2.Profile.Email})
if err := s.requestor().CreateAccessRequest(ctx, req); err != nil {
return setRaceErr(trace.Wrap(err))
}
return nil
})
}
// Having TWO suggested reviewers will post TWO messages for each request.
// We also have approval threshold of TWO set in the role properties
// so lets simply submit the approval from each of the suggested reviewers.
//
// Multiplier SIX means that we handle TWO messages for each request and also
// TWO comments for each message: 2 * (1 message + 2 comments).
for i := 0; i < 6*s.raceNumber; i++ {
process.SpawnCritical(func(ctx context.Context) error {
msg, err := s.fakeSlack.CheckNewMessage(ctx)
if err != nil {
return setRaceErr(trace.Wrap(err))
}
if msg.ThreadTs == "" {
// Handle "root" notifications.
threadMsgKey := SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.Timestamp}
if _, loaded := threadMsgIDs.LoadOrStore(threadMsgKey, struct{}{}); loaded {
return setRaceErr(trace.Errorf("thread %v already stored", threadMsgKey))
}
atomic.AddInt32(&threadMsgsCount, 1)
user, ok := s.fakeSlack.GetUser(msg.Channel)
if !ok {
return setRaceErr(trace.Errorf("user %s is not found", msg.Channel))
}
reqID, err := parseMessageField(msg, "ID")
if err != nil {
return setRaceErr(trace.Wrap(err))
}
if err = s.clients[user.Profile.Email].SubmitAccessRequestReview(ctx, reqID, types.AccessReview{
Author: user.Profile.Email,
ProposedState: types.RequestState_APPROVED,
Created: time.Now(),
Reason: "okay",
}); err != nil {
return setRaceErr(trace.Wrap(err))
}
} else {
// Handle review comments.
threadMsgKey := SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.ThreadTs}
var newCounter int32
val, _ := reviewReplyCounters.LoadOrStore(threadMsgKey, &newCounter)
counterPtr := val.(*int32)
atomic.AddInt32(counterPtr, 1)
}
return nil
})
}
// Multiplier TWO means that we handle updates for each of the two messages posted to reviewers.
for i := 0; i < 2*s.raceNumber; i++ {
process.SpawnCritical(func(ctx context.Context) error {
msg, err := s.fakeSlack.CheckMessageUpdateByAPI(ctx)
if err != nil {
return setRaceErr(trace.Wrap(err))
}
threadMsgKey := SlackDataMessage{ChannelID: msg.Channel, Timestamp: msg.Timestamp}
var newCounter int32
val, _ := msgUpdateCounters.LoadOrStore(threadMsgKey, &newCounter)
counterPtr := val.(*int32)
atomic.AddInt32(counterPtr, 1)
return nil
})
}
process.Terminate()
<-process.Done()
require.NoError(t, raceErr)
assert.Equal(t, int32(2*s.raceNumber), threadMsgsCount)
threadMsgIDs.Range(func(key, value interface{}) bool {
next := true
val, loaded := reviewReplyCounters.LoadAndDelete(key)
next = next && assert.True(t, loaded)
counterPtr := val.(*int32)
next = next && assert.Equal(t, int32(2), *counterPtr)
val, loaded = msgUpdateCounters.LoadAndDelete(key)
next = next && assert.True(t, loaded)
counterPtr = val.(*int32)
next = next && assert.Equal(t, int32(1), *counterPtr)
return next
})
}
func parseMessageField(msg Msg, field string) (string, error) {
block := msg.BlockItems[1].Block
sectionBlock, ok := block.(SectionBlock)
if !ok {
return "", trace.Errorf("invalid block type %T", block)
}
if sectionBlock.Text.TextObject == nil {
return "", trace.Errorf("section block does not contain text")
}
text := sectionBlock.Text.GetText()
matches := msgFieldRegexp.FindAllStringSubmatch(text, -1)
if matches == nil {
return "", trace.Errorf("cannot parse fields from text %s", text)
}
var fields []string
for _, match := range matches {
if match[1] == field {
return match[2], nil
}
fields = append(fields, match[1])
}
return "", trace.Errorf("cannot find field %s in %v", field, fields)
}
func getStatusLine(msg Msg) (string, error) {
block := msg.BlockItems[2].Block
contextBlock, ok := block.(ContextBlock)
if !ok {
return "", trace.Errorf("invalid block type %T", block)
}
elementItems := contextBlock.ElementItems
if n := len(elementItems); n != 1 {
return "", trace.Errorf("expected only one context element, got %v", n)
}
element := elementItems[0].ContextElement
textBlock, ok := element.(TextObject)
if !ok {
return "", trace.Errorf("invalid element type %T", element)
}
return textBlock.GetText(), nil
}