@@ -24,6 +24,7 @@ import (
24
24
"github.com/coder/coder/v2/coderd/database"
25
25
"github.com/coder/coder/v2/coderd/database/dbtime"
26
26
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
27
+ "github.com/coder/coder/v2/coderd/httpmw/loggermw"
27
28
"github.com/coder/coder/v2/coderd/rbac"
28
29
"github.com/coder/coder/v2/coderd/util/slice"
29
30
"github.com/coder/coder/v2/provisionersdk"
@@ -162,6 +163,7 @@ func ActorFromContext(ctx context.Context) (rbac.Subject, bool) {
162
163
163
164
var (
164
165
subjectProvisionerd = rbac.Subject {
166
+ Type : rbac .SubjectTypeProvisionerd ,
165
167
FriendlyName : "Provisioner Daemon" ,
166
168
ID : uuid .Nil .String (),
167
169
Roles : rbac .Roles ([]rbac.Role {
@@ -196,6 +198,7 @@ var (
196
198
}.WithCachedASTValue ()
197
199
198
200
subjectAutostart = rbac.Subject {
201
+ Type : rbac .SubjectTypeAutostart ,
199
202
FriendlyName : "Autostart" ,
200
203
ID : uuid .Nil .String (),
201
204
Roles : rbac .Roles ([]rbac.Role {
@@ -219,6 +222,7 @@ var (
219
222
220
223
// See unhanger package.
221
224
subjectHangDetector = rbac.Subject {
225
+ Type : rbac .SubjectTypeHangDetector ,
222
226
FriendlyName : "Hang Detector" ,
223
227
ID : uuid .Nil .String (),
224
228
Roles : rbac .Roles ([]rbac.Role {
@@ -239,6 +243,7 @@ var (
239
243
240
244
// See cryptokeys package.
241
245
subjectCryptoKeyRotator = rbac.Subject {
246
+ Type : rbac .SubjectTypeCryptoKeyRotator ,
242
247
FriendlyName : "Crypto Key Rotator" ,
243
248
ID : uuid .Nil .String (),
244
249
Roles : rbac .Roles ([]rbac.Role {
@@ -257,6 +262,7 @@ var (
257
262
258
263
// See cryptokeys package.
259
264
subjectCryptoKeyReader = rbac.Subject {
265
+ Type : rbac .SubjectTypeCryptoKeyReader ,
260
266
FriendlyName : "Crypto Key Reader" ,
261
267
ID : uuid .Nil .String (),
262
268
Roles : rbac .Roles ([]rbac.Role {
@@ -274,6 +280,7 @@ var (
274
280
}.WithCachedASTValue ()
275
281
276
282
subjectNotifier = rbac.Subject {
283
+ Type : rbac .SubjectTypeNotifier ,
277
284
FriendlyName : "Notifier" ,
278
285
ID : uuid .Nil .String (),
279
286
Roles : rbac .Roles ([]rbac.Role {
@@ -294,6 +301,7 @@ var (
294
301
}.WithCachedASTValue ()
295
302
296
303
subjectResourceMonitor = rbac.Subject {
304
+ Type : rbac .SubjectTypeResourceMonitor ,
297
305
FriendlyName : "Resource Monitor" ,
298
306
ID : uuid .Nil .String (),
299
307
Roles : rbac .Roles ([]rbac.Role {
@@ -312,6 +320,7 @@ var (
312
320
}.WithCachedASTValue ()
313
321
314
322
subjectSystemRestricted = rbac.Subject {
323
+ Type : rbac .SubjectTypeSystemRestricted ,
315
324
FriendlyName : "System" ,
316
325
ID : uuid .Nil .String (),
317
326
Roles : rbac .Roles ([]rbac.Role {
@@ -346,6 +355,7 @@ var (
346
355
}.WithCachedASTValue ()
347
356
348
357
subjectSystemReadProvisionerDaemons = rbac.Subject {
358
+ Type : rbac .SubjectTypeSystemReadProvisionerDaemons ,
349
359
FriendlyName : "Provisioner Daemons Reader" ,
350
360
ID : uuid .Nil .String (),
351
361
Roles : rbac .Roles ([]rbac.Role {
@@ -366,53 +376,53 @@ var (
366
376
// AsProvisionerd returns a context with an actor that has permissions required
367
377
// for provisionerd to function.
368
378
func AsProvisionerd (ctx context.Context ) context.Context {
369
- return context . WithValue (ctx , authContextKey {} , subjectProvisionerd )
379
+ return As (ctx , subjectProvisionerd )
370
380
}
371
381
372
382
// AsAutostart returns a context with an actor that has permissions required
373
383
// for autostart to function.
374
384
func AsAutostart (ctx context.Context ) context.Context {
375
- return context . WithValue (ctx , authContextKey {} , subjectAutostart )
385
+ return As (ctx , subjectAutostart )
376
386
}
377
387
378
388
// AsHangDetector returns a context with an actor that has permissions required
379
389
// for unhanger.Detector to function.
380
390
func AsHangDetector (ctx context.Context ) context.Context {
381
- return context . WithValue (ctx , authContextKey {} , subjectHangDetector )
391
+ return As (ctx , subjectHangDetector )
382
392
}
383
393
384
394
// AsKeyRotator returns a context with an actor that has permissions required for rotating crypto keys.
385
395
func AsKeyRotator (ctx context.Context ) context.Context {
386
- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyRotator )
396
+ return As (ctx , subjectCryptoKeyRotator )
387
397
}
388
398
389
399
// AsKeyReader returns a context with an actor that has permissions required for reading crypto keys.
390
400
func AsKeyReader (ctx context.Context ) context.Context {
391
- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyReader )
401
+ return As (ctx , subjectCryptoKeyReader )
392
402
}
393
403
394
404
// AsNotifier returns a context with an actor that has permissions required for
395
405
// creating/reading/updating/deleting notifications.
396
406
func AsNotifier (ctx context.Context ) context.Context {
397
- return context . WithValue (ctx , authContextKey {} , subjectNotifier )
407
+ return As (ctx , subjectNotifier )
398
408
}
399
409
400
410
// AsResourceMonitor returns a context with an actor that has permissions required for
401
411
// updating resource monitors.
402
412
func AsResourceMonitor (ctx context.Context ) context.Context {
403
- return context . WithValue (ctx , authContextKey {} , subjectResourceMonitor )
413
+ return As (ctx , subjectResourceMonitor )
404
414
}
405
415
406
416
// AsSystemRestricted returns a context with an actor that has permissions
407
417
// required for various system operations (login, logout, metrics cache).
408
418
func AsSystemRestricted (ctx context.Context ) context.Context {
409
- return context . WithValue (ctx , authContextKey {} , subjectSystemRestricted )
419
+ return As (ctx , subjectSystemRestricted )
410
420
}
411
421
412
422
// AsSystemReadProvisionerDaemons returns a context with an actor that has permissions
413
423
// to read provisioner daemons.
414
424
func AsSystemReadProvisionerDaemons (ctx context.Context ) context.Context {
415
- return context . WithValue (ctx , authContextKey {} , subjectSystemReadProvisionerDaemons )
425
+ return As (ctx , subjectSystemReadProvisionerDaemons )
416
426
}
417
427
418
428
var AsRemoveActor = rbac.Subject {
@@ -430,6 +440,9 @@ func As(ctx context.Context, actor rbac.Subject) context.Context {
430
440
// should be removed from the context.
431
441
return context .WithValue (ctx , authContextKey {}, nil )
432
442
}
443
+ if rlogger := loggermw .RequestLoggerFromContext (ctx ); rlogger != nil {
444
+ rlogger .WithAuthContext (actor )
445
+ }
433
446
return context .WithValue (ctx , authContextKey {}, actor )
434
447
}
435
448
0 commit comments