@@ -25,6 +25,7 @@ import (
25
25
"github.com/coder/coder/v2/coderd/database"
26
26
"github.com/coder/coder/v2/coderd/database/dbtime"
27
27
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
28
+ "github.com/coder/coder/v2/coderd/httpmw/loggermw"
28
29
"github.com/coder/coder/v2/coderd/rbac"
29
30
"github.com/coder/coder/v2/coderd/util/slice"
30
31
"github.com/coder/coder/v2/provisionersdk"
@@ -163,6 +164,7 @@ func ActorFromContext(ctx context.Context) (rbac.Subject, bool) {
163
164
164
165
var (
165
166
subjectProvisionerd = rbac.Subject {
167
+ Type : rbac .SubjectTypeProvisionerd ,
166
168
FriendlyName : "Provisioner Daemon" ,
167
169
ID : uuid .Nil .String (),
168
170
Roles : rbac .Roles ([]rbac.Role {
@@ -197,6 +199,7 @@ var (
197
199
}.WithCachedASTValue ()
198
200
199
201
subjectAutostart = rbac.Subject {
202
+ Type : rbac .SubjectTypeAutostart ,
200
203
FriendlyName : "Autostart" ,
201
204
ID : uuid .Nil .String (),
202
205
Roles : rbac .Roles ([]rbac.Role {
@@ -220,6 +223,7 @@ var (
220
223
221
224
// See unhanger package.
222
225
subjectHangDetector = rbac.Subject {
226
+ Type : rbac .SubjectTypeHangDetector ,
223
227
FriendlyName : "Hang Detector" ,
224
228
ID : uuid .Nil .String (),
225
229
Roles : rbac .Roles ([]rbac.Role {
@@ -240,6 +244,7 @@ var (
240
244
241
245
// See cryptokeys package.
242
246
subjectCryptoKeyRotator = rbac.Subject {
247
+ Type : rbac .SubjectTypeCryptoKeyRotator ,
243
248
FriendlyName : "Crypto Key Rotator" ,
244
249
ID : uuid .Nil .String (),
245
250
Roles : rbac .Roles ([]rbac.Role {
@@ -258,6 +263,7 @@ var (
258
263
259
264
// See cryptokeys package.
260
265
subjectCryptoKeyReader = rbac.Subject {
266
+ Type : rbac .SubjectTypeCryptoKeyReader ,
261
267
FriendlyName : "Crypto Key Reader" ,
262
268
ID : uuid .Nil .String (),
263
269
Roles : rbac .Roles ([]rbac.Role {
@@ -275,6 +281,7 @@ var (
275
281
}.WithCachedASTValue ()
276
282
277
283
subjectNotifier = rbac.Subject {
284
+ Type : rbac .SubjectTypeNotifier ,
278
285
FriendlyName : "Notifier" ,
279
286
ID : uuid .Nil .String (),
280
287
Roles : rbac .Roles ([]rbac.Role {
@@ -295,6 +302,7 @@ var (
295
302
}.WithCachedASTValue ()
296
303
297
304
subjectResourceMonitor = rbac.Subject {
305
+ Type : rbac .SubjectTypeResourceMonitor ,
298
306
FriendlyName : "Resource Monitor" ,
299
307
ID : uuid .Nil .String (),
300
308
Roles : rbac .Roles ([]rbac.Role {
@@ -313,6 +321,7 @@ var (
313
321
}.WithCachedASTValue ()
314
322
315
323
subjectSystemRestricted = rbac.Subject {
324
+ Type : rbac .SubjectTypeSystemRestricted ,
316
325
FriendlyName : "System" ,
317
326
ID : uuid .Nil .String (),
318
327
Roles : rbac .Roles ([]rbac.Role {
@@ -347,6 +356,7 @@ var (
347
356
}.WithCachedASTValue ()
348
357
349
358
subjectSystemReadProvisionerDaemons = rbac.Subject {
359
+ Type : rbac .SubjectTypeSystemReadProvisionerDaemons ,
350
360
FriendlyName : "Provisioner Daemons Reader" ,
351
361
ID : uuid .Nil .String (),
352
362
Roles : rbac .Roles ([]rbac.Role {
@@ -364,6 +374,7 @@ var (
364
374
}.WithCachedASTValue ()
365
375
366
376
subjectPrebuildsOrchestrator = rbac.Subject {
377
+ Type : rbac .SubjectTypePrebuildsOrchestrator ,
367
378
FriendlyName : "Prebuilds Orchestrator" ,
368
379
ID : prebuilds .SystemUserID .String (),
369
380
Roles : rbac .Roles ([]rbac.Role {
@@ -388,59 +399,59 @@ var (
388
399
// AsProvisionerd returns a context with an actor that has permissions required
389
400
// for provisionerd to function.
390
401
func AsProvisionerd (ctx context.Context ) context.Context {
391
- return context . WithValue (ctx , authContextKey {} , subjectProvisionerd )
402
+ return As (ctx , subjectProvisionerd )
392
403
}
393
404
394
405
// AsAutostart returns a context with an actor that has permissions required
395
406
// for autostart to function.
396
407
func AsAutostart (ctx context.Context ) context.Context {
397
- return context . WithValue (ctx , authContextKey {} , subjectAutostart )
408
+ return As (ctx , subjectAutostart )
398
409
}
399
410
400
411
// AsHangDetector returns a context with an actor that has permissions required
401
412
// for unhanger.Detector to function.
402
413
func AsHangDetector (ctx context.Context ) context.Context {
403
- return context . WithValue (ctx , authContextKey {} , subjectHangDetector )
414
+ return As (ctx , subjectHangDetector )
404
415
}
405
416
406
417
// AsKeyRotator returns a context with an actor that has permissions required for rotating crypto keys.
407
418
func AsKeyRotator (ctx context.Context ) context.Context {
408
- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyRotator )
419
+ return As (ctx , subjectCryptoKeyRotator )
409
420
}
410
421
411
422
// AsKeyReader returns a context with an actor that has permissions required for reading crypto keys.
412
423
func AsKeyReader (ctx context.Context ) context.Context {
413
- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyReader )
424
+ return As (ctx , subjectCryptoKeyReader )
414
425
}
415
426
416
427
// AsNotifier returns a context with an actor that has permissions required for
417
428
// creating/reading/updating/deleting notifications.
418
429
func AsNotifier (ctx context.Context ) context.Context {
419
- return context . WithValue (ctx , authContextKey {} , subjectNotifier )
430
+ return As (ctx , subjectNotifier )
420
431
}
421
432
422
433
// AsResourceMonitor returns a context with an actor that has permissions required for
423
434
// updating resource monitors.
424
435
func AsResourceMonitor (ctx context.Context ) context.Context {
425
- return context . WithValue (ctx , authContextKey {} , subjectResourceMonitor )
436
+ return As (ctx , subjectResourceMonitor )
426
437
}
427
438
428
439
// AsSystemRestricted returns a context with an actor that has permissions
429
440
// required for various system operations (login, logout, metrics cache).
430
441
func AsSystemRestricted (ctx context.Context ) context.Context {
431
- return context . WithValue (ctx , authContextKey {} , subjectSystemRestricted )
442
+ return As (ctx , subjectSystemRestricted )
432
443
}
433
444
434
445
// AsSystemReadProvisionerDaemons returns a context with an actor that has permissions
435
446
// to read provisioner daemons.
436
447
func AsSystemReadProvisionerDaemons (ctx context.Context ) context.Context {
437
- return context . WithValue (ctx , authContextKey {} , subjectSystemReadProvisionerDaemons )
448
+ return As (ctx , subjectSystemReadProvisionerDaemons )
438
449
}
439
450
440
451
// AsPrebuildsOrchestrator returns a context with an actor that has permissions
441
452
// to read orchestrator workspace prebuilds.
442
453
func AsPrebuildsOrchestrator (ctx context.Context ) context.Context {
443
- return context . WithValue (ctx , authContextKey {} , subjectPrebuildsOrchestrator )
454
+ return As (ctx , subjectPrebuildsOrchestrator )
444
455
}
445
456
446
457
var AsRemoveActor = rbac.Subject {
@@ -458,6 +469,9 @@ func As(ctx context.Context, actor rbac.Subject) context.Context {
458
469
// should be removed from the context.
459
470
return context .WithValue (ctx , authContextKey {}, nil )
460
471
}
472
+ if rlogger := loggermw .RequestLoggerFromContext (ctx ); rlogger != nil {
473
+ rlogger .WithAuthContext (actor )
474
+ }
461
475
return context .WithValue (ctx , authContextKey {}, actor )
462
476
}
463
477
0 commit comments