forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessrequestsuite.go
416 lines (360 loc) · 15.5 KB
/
accessrequestsuite.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
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package integration
import (
"context"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/logger"
"github.com/gravitational/teleport/lib/services"
)
// This file is the future of the plugin integration testing.
// Previous integration tests relied on an existing teleport enterprise binary.
// This caused several issues, the main one being versionning plugins.
// Once all plugins are migrated to this new test suite, we'll be able to
// remove most of the integration package.
const (
// RulerUserName is the name of the admin user.
// Its client has full admin access to Teleport and can be used to setup
// fixtures or approve requests in OSS tests
RulerUserName = "admin"
// RequesterOSSUserName is the user allowed to request RequestedRoleName.
// Their role does not have any approval threshold and is compatible with
// Teleport OSS.
RequesterOSSUserName = "requester-oss@example.com"
// Requester1UserName is the name of main role requester. They are allowed
// to request RequestedRoleName, but require two approvals. This user is
// only created in Enterprise tests.
Requester1UserName = "requester1@example.com"
// Requester2UserName is the name of secondary role requester.
// Like Requester1UserName they need 2 approvals. This user is used in some
// auto-approval race tests to see how the plugin behaves when several users
// request the same role. This user is only created in Enterprise tests.
Requester2UserName = "requester2@example.com"
// Reviewer1UserName is one of the two access reviewers. This user is used
// to test advanced approval workflows (multiple approvals required).
// This user is only created in enterprise tests.
Reviewer1UserName = "reviewer1@example.com"
// Reviewer2UserName is exactly like Reviewer1UserName.
// This user is only created in enterprise tests.
Reviewer2UserName = "reviewer2@example.com"
// PluginUserName is the Teleport user for the plugin.
PluginUserName = "plugin"
RequestedRoleName = teleport.PresetEditorRoleName
OSSRequesterRoleName = "oss-requester"
AdvancedRequesterRoleName = "advanced-requester"
ReviewerRoleName = "reviewer"
)
// AccessRequestSuite is the base test suite for access requests plugins.
// It sets up a single Teleport server for all the tests and creates the following fixtures:
// - Ruler user (admin client)
// - Requester1 and Requester2 users with the Requester role and an approval threshold of
// - reviewer users 1 and 2 with the reviewer role (only when running against teleport.e)
// - access plugin user and roles (a role for access requests, and another for access lists)
//
// It also signs an identity for the plugin and generates a working teleport
// client configuration.
type AccessRequestSuite struct {
suite.Suite
AuthHelper AuthHelper
clients map[string]*Client
teleportConfig lib.TeleportConfig
teleportFeatures *proto.Features
// requestPadding allows tests to pad the access requests reason to test
// how the plugin behaves when a message exceeds the max size.
requestPadding int
}
// SetupSuite runs once for the whole test suite.
// It starts a Teleport instance, creates all the fixtures (users and roles).
func (s *AccessRequestSuite) SetupSuite() {
var err error
t := s.T()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
s.clients = make(map[string]*Client)
// Start the Teleport Auth server and get the admin client.
adminClient := s.AuthHelper.StartServer(t)
s.clients[RulerUserName] = NewAccessRequestClient(adminClient)
// Check the client and recover the cluster features
pong, err := adminClient.Ping(ctx)
require.NoError(t, err)
s.teleportFeatures = pong.ServerFeatures
// Create the editor role. This is is the role that will be requested.
requestedRole := services.NewPresetEditorRole()
requestedRole, err = adminClient.CreateRole(ctx, requestedRole)
require.NoError(t, err)
// Create the OSS Requester role and user.
// This is a simple role allowing to request access, no threshold, advanced workflow.
// This is compatible with Teleport OSS.
ossRequesterRole, err := types.NewRole(OSSRequesterRoleName, types.RoleSpecV6{Allow: types.RoleConditions{
Request: &types.AccessRequestConditions{
Roles: []string{requestedRole.GetName()},
}}})
require.NoError(t, err)
ossRequesterRole, err = adminClient.CreateRole(ctx, ossRequesterRole)
require.NoError(t, err)
ossRequester, err := types.NewUser(RequesterOSSUserName)
require.NoError(t, err)
ossRequester.SetRoles([]string{ossRequesterRole.GetName()})
ossRequester, err = adminClient.CreateUser(ctx, ossRequester)
require.NoError(t, err)
s.clients[RequesterOSSUserName] = NewAccessRequestClient(s.newClientForUser(ctx, ossRequester))
// If AdvancedAccessWorkflows is enabled (Teleport Enterprise) we will test
// review thresholds and multiple reviewers.
// Else we'll just use the Ruler user to approve/deny requests.
if s.teleportFeatures.AdvancedAccessWorkflows {
// Create the role and users for advanced access request workflows
advancedRequesterRole, err := types.NewRole(AdvancedRequesterRoleName, types.RoleSpecV6{Allow: types.RoleConditions{
Request: &types.AccessRequestConditions{
Roles: []string{requestedRole.GetName()},
Thresholds: []types.AccessReviewThreshold{{Approve: 2, Deny: 2}},
}}})
require.NoError(t, err)
advancedRequesterRole, err = adminClient.CreateRole(ctx, advancedRequesterRole)
require.NoError(t, err)
requester1, err := types.NewUser(Requester1UserName)
require.NoError(t, err)
requester1.SetRoles([]string{advancedRequesterRole.GetName()})
requester1, err = adminClient.CreateUser(ctx, requester1)
require.NoError(t, err)
requester2, err := types.NewUser(Requester2UserName)
require.NoError(t, err)
requester2.SetRoles([]string{advancedRequesterRole.GetName()})
requester2, err = adminClient.CreateUser(ctx, requester2)
require.NoError(t, err)
// Create the reviewer role and the two reviewer users
reviewerRole, err := types.NewRole(ReviewerRoleName, types.RoleSpecV6{
Allow: types.RoleConditions{
ReviewRequests: &types.AccessReviewConditions{
Roles: []string{requestedRole.GetName()},
},
}})
require.NoError(t, err)
reviewerRole, err = adminClient.CreateRole(ctx, reviewerRole)
require.NoError(t, err)
reviewer1, err := types.NewUser(Reviewer1UserName)
require.NoError(t, err)
reviewer1.SetRoles([]string{reviewerRole.GetName()})
reviewer1, err = adminClient.CreateUser(ctx, reviewer1)
require.NoError(t, err)
reviewer2, err := types.NewUser(Reviewer2UserName)
require.NoError(t, err)
reviewer2.SetRoles([]string{reviewerRole.GetName()})
reviewer2, err = adminClient.CreateUser(ctx, reviewer2)
require.NoError(t, err)
// Build and store the teleport clients
s.clients[Requester1UserName] = NewAccessRequestClient(s.newClientForUser(ctx, requester1))
s.clients[Requester2UserName] = NewAccessRequestClient(s.newClientForUser(ctx, requester2))
s.clients[Reviewer1UserName] = NewAccessRequestClient(s.newClientForUser(ctx, reviewer1))
s.clients[Reviewer2UserName] = NewAccessRequestClient(s.newClientForUser(ctx, reviewer2))
}
// Create the role for the plugin to watch access requests and write plugin data
pluginRole, err := types.NewRole("access-plugin", types.RoleSpecV6{
Allow: types.RoleConditions{
Rules: []types.Rule{
types.NewRule("access_request", []string{"list", "read"}),
types.NewRule("access_plugin_data", []string{"update"}),
},
},
})
require.NoError(t, err)
pluginRole, err = adminClient.CreateRole(ctx, pluginRole)
require.NoError(t, err)
// Create the role for the reminder plugin to read access lists
reminderRole, err := types.NewRole("reminder-plugin", types.RoleSpecV6{
Allow: types.RoleConditions{
Rules: []types.Rule{
types.NewRule("access_list", []string{"list", "read"}),
},
},
})
require.NoError(t, err)
reminderRole, err = adminClient.CreateRole(ctx, reminderRole)
require.NoError(t, err)
pluginRoles := []string{pluginRole.GetName(), reminderRole.GetName()}
// Auto approval requires setting some fields that are enterprise-only.
// We must skip role creation in OSS, else it will fail.
if s.TeleportFeatures().AdvancedAccessWorkflows {
// Create the role for the plugin to automatically approve access requests
autoApprovalRole, err := types.NewRole("auto-approval-plugin", types.RoleSpecV6{
Allow: types.RoleConditions{
ReviewRequests: &types.AccessReviewConditions{
Roles: []string{RequestedRoleName},
},
},
})
require.NoError(t, err)
autoApprovalRole, err = adminClient.CreateRole(ctx, autoApprovalRole)
require.NoError(t, err)
pluginRoles = append(pluginRoles, autoApprovalRole.GetName())
}
pluginUser, err := types.NewUser(PluginUserName)
require.NoError(t, err)
pluginUser.SetRoles(pluginRoles)
pluginUser, err = adminClient.CreateUser(ctx, pluginUser)
require.NoError(t, err)
// Sign an identity for the access plugin and generate its configuration
s.teleportConfig.Addr = s.AuthHelper.ServerAddr()
s.teleportConfig.Identity = s.AuthHelper.SignIdentityForUser(t, ctx, pluginUser)
}
// newClientForUser creates a teleport client for a give user.
// The user must be created beforehand.
func (s *AccessRequestSuite) newClientForUser(ctx context.Context, user types.User) *client.Client {
t := s.T()
creds := s.AuthHelper.CredentialsForUser(t, ctx, user)
clientConfig := client.Config{
Addrs: []string{s.AuthHelper.ServerAddr()},
Credentials: []client.Credentials{creds},
}
userClient, err := client.New(ctx, clientConfig)
require.NoError(t, err)
_, err = userClient.Ping(ctx)
require.NoError(t, err)
return userClient
}
// Ruler returns the AccessRequestClient for the Ruler user
func (s *AccessRequestSuite) Ruler() *Client {
return s.clients[RulerUserName]
}
// RequesterOSS returns the AccessRequestClient for the RequesterOSS user
func (s *AccessRequestSuite) RequesterOSS() *Client {
return s.clients[RequesterOSSUserName]
}
// Requester1 returns the AccessRequestClient for the Requester1 user
func (s *AccessRequestSuite) Requester1() *Client {
return s.clients[Requester1UserName]
}
// Requester2 returns the AccessRequestClient for the Requester2 user
func (s *AccessRequestSuite) Requester2() *Client {
return s.clients[Requester2UserName]
}
// Reviewer1 returns the AccessRequestClient for the Reviewer1 user
func (s *AccessRequestSuite) Reviewer1() *Client {
return s.clients[Reviewer1UserName]
}
// Reviewer2 returns the AccessRequestClient for the Reviewer2 user
func (s *AccessRequestSuite) Reviewer2() *Client {
return s.clients[Reviewer2UserName]
}
// ClientByName returns the AccessRequestClient for any user.
// While this can be done via the nice helper functions like Ruler(),
// there are cases where we want to get clients based on a username
// (see race tests where the username is in a variable)
func (s *AccessRequestSuite) ClientByName(name string) *Client {
return s.clients[name]
}
// NewAccessRequest creates an access request.
// The access request reason can be padded with "A" by setting
// SetReasonPadding.
func (s *AccessRequestSuite) NewAccessRequest(userName string, suggestedReviewers []string, padding int) types.AccessRequest {
t := s.T()
t.Helper()
reason := "because of"
if padding > 0 {
reason = reason + " " + strings.Repeat("A", padding)
}
req, err := types.NewAccessRequest(uuid.New().String(), userName, RequestedRoleName)
require.NoError(t, err)
req.SetRequestReason(reason)
req.SetSuggestedReviewers(suggestedReviewers)
return req
}
// CreateAccessRequest creates a new access request and submits it.
func (s *AccessRequestSuite) CreateAccessRequest(ctx context.Context, userName string, suggestedReviewers []string) types.AccessRequest {
t := s.T()
t.Helper()
req := s.NewAccessRequest(userName, suggestedReviewers, s.requestPadding)
out, err := s.ClientByName(userName).CreateAccessRequestV2(ctx, req)
require.NoError(t, err)
return out
}
// RunAndWaitReady is a helper to start an app implementing AppI and wait for
// it to become ready.
// This is used to start plugins.
func (s *AccessRequestSuite) RunAndWaitReady(t *testing.T, app AppI) {
appCtx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go func() {
ctx := appCtx
if err := app.Run(ctx); err != nil {
logger.Get(ctx).WithError(err).Error("Application failed")
assert.Fail(t, "Application failed")
}
}()
t.Cleanup(func() {
err := app.Shutdown(appCtx)
assert.NoError(t, err)
assert.NoError(t, app.Err())
})
waitCtx, cancel := context.WithTimeout(appCtx, 20*time.Second)
defer cancel()
ok, err := app.WaitReady(waitCtx)
require.NoError(t, err)
require.True(t, ok)
}
// TeleportFeatures returns the teleport features of the auth server the tests
// are running against.
func (s *AccessRequestSuite) TeleportFeatures() *proto.Features {
return s.teleportFeatures
}
// TeleportConfig returns a valid teleport config for the auth server the tests
// are running against. This config can then be passed to plugins.
func (s *AccessRequestSuite) TeleportConfig() lib.TeleportConfig {
return s.teleportConfig
}
// SetReasonPadding sets the padding when creating access request. This is used
// to test how plugins are behaving when too large messages are sent.
func (s *AccessRequestSuite) SetReasonPadding(padding int) {
s.requestPadding = padding
}
// AnnotateRequesterRoleAccessRequests sets the access request annotations on both
// requester roles (OSS and Advanced workflows). Those annotations can then be
// used to route notifications to specific channels, or trigger automatic approval.
func (s *AccessRequestSuite) AnnotateRequesterRoleAccessRequests(ctx context.Context, annotationKey string, annotationValue []string) {
t := s.T()
t.Helper()
adminClient := s.Ruler()
// If we're running in OSS, we have a single requester role, but if we're
// running against an enterprise server we also have the advanced requester.
roles := []string{OSSRequesterRoleName}
if s.TeleportFeatures().AdvancedAccessWorkflows {
roles = append(roles, AdvancedRequesterRoleName)
}
for _, roleName := range roles {
role, err := adminClient.GetRole(ctx, roleName)
require.NoError(t, err)
conditions := role.GetAccessRequestConditions(types.Allow)
if conditions.Annotations == nil {
conditions.Annotations = make(map[string][]string)
}
conditions.Annotations[annotationKey] = annotationValue
role.SetAccessRequestConditions(types.Allow, conditions)
_, err = adminClient.UpdateRole(ctx, role)
require.NoError(t, err)
}
}