@@ -25,6 +25,7 @@ import (
2525 "github.com/coder/coder/v2/coderd/database"
2626 "github.com/coder/coder/v2/coderd/database/dbtime"
2727 "github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
28+ "github.com/coder/coder/v2/coderd/httpmw/loggermw"
2829 "github.com/coder/coder/v2/coderd/rbac"
2930 "github.com/coder/coder/v2/coderd/util/slice"
3031 "github.com/coder/coder/v2/provisionersdk"
@@ -163,6 +164,7 @@ func ActorFromContext(ctx context.Context) (rbac.Subject, bool) {
163164
164165var (
165166 subjectProvisionerd = rbac.Subject {
167+ Type : rbac .SubjectTypeProvisionerd ,
166168 FriendlyName : "Provisioner Daemon" ,
167169 ID : uuid .Nil .String (),
168170 Roles : rbac .Roles ([]rbac.Role {
@@ -197,6 +199,7 @@ var (
197199 }.WithCachedASTValue ()
198200
199201 subjectAutostart = rbac.Subject {
202+ Type : rbac .SubjectTypeAutostart ,
200203 FriendlyName : "Autostart" ,
201204 ID : uuid .Nil .String (),
202205 Roles : rbac .Roles ([]rbac.Role {
@@ -220,6 +223,7 @@ var (
220223
221224 // See unhanger package.
222225 subjectHangDetector = rbac.Subject {
226+ Type : rbac .SubjectTypeHangDetector ,
223227 FriendlyName : "Hang Detector" ,
224228 ID : uuid .Nil .String (),
225229 Roles : rbac .Roles ([]rbac.Role {
@@ -240,6 +244,7 @@ var (
240244
241245 // See cryptokeys package.
242246 subjectCryptoKeyRotator = rbac.Subject {
247+ Type : rbac .SubjectTypeCryptoKeyRotator ,
243248 FriendlyName : "Crypto Key Rotator" ,
244249 ID : uuid .Nil .String (),
245250 Roles : rbac .Roles ([]rbac.Role {
@@ -258,6 +263,7 @@ var (
258263
259264 // See cryptokeys package.
260265 subjectCryptoKeyReader = rbac.Subject {
266+ Type : rbac .SubjectTypeCryptoKeyReader ,
261267 FriendlyName : "Crypto Key Reader" ,
262268 ID : uuid .Nil .String (),
263269 Roles : rbac .Roles ([]rbac.Role {
@@ -275,6 +281,7 @@ var (
275281 }.WithCachedASTValue ()
276282
277283 subjectNotifier = rbac.Subject {
284+ Type : rbac .SubjectTypeNotifier ,
278285 FriendlyName : "Notifier" ,
279286 ID : uuid .Nil .String (),
280287 Roles : rbac .Roles ([]rbac.Role {
@@ -295,6 +302,7 @@ var (
295302 }.WithCachedASTValue ()
296303
297304 subjectResourceMonitor = rbac.Subject {
305+ Type : rbac .SubjectTypeResourceMonitor ,
298306 FriendlyName : "Resource Monitor" ,
299307 ID : uuid .Nil .String (),
300308 Roles : rbac .Roles ([]rbac.Role {
@@ -313,6 +321,7 @@ var (
313321 }.WithCachedASTValue ()
314322
315323 subjectSystemRestricted = rbac.Subject {
324+ Type : rbac .SubjectTypeSystemRestricted ,
316325 FriendlyName : "System" ,
317326 ID : uuid .Nil .String (),
318327 Roles : rbac .Roles ([]rbac.Role {
@@ -347,6 +356,7 @@ var (
347356 }.WithCachedASTValue ()
348357
349358 subjectSystemReadProvisionerDaemons = rbac.Subject {
359+ Type : rbac .SubjectTypeSystemReadProvisionerDaemons ,
350360 FriendlyName : "Provisioner Daemons Reader" ,
351361 ID : uuid .Nil .String (),
352362 Roles : rbac .Roles ([]rbac.Role {
@@ -364,6 +374,7 @@ var (
364374 }.WithCachedASTValue ()
365375
366376 subjectPrebuildsOrchestrator = rbac.Subject {
377+ Type : rbac .SubjectTypePrebuildsOrchestrator ,
367378 FriendlyName : "Prebuilds Orchestrator" ,
368379 ID : prebuilds .SystemUserID .String (),
369380 Roles : rbac .Roles ([]rbac.Role {
@@ -388,59 +399,59 @@ var (
388399// AsProvisionerd returns a context with an actor that has permissions required
389400// for provisionerd to function.
390401func AsProvisionerd (ctx context.Context ) context.Context {
391- return context . WithValue (ctx , authContextKey {} , subjectProvisionerd )
402+ return As (ctx , subjectProvisionerd )
392403}
393404
394405// AsAutostart returns a context with an actor that has permissions required
395406// for autostart to function.
396407func AsAutostart (ctx context.Context ) context.Context {
397- return context . WithValue (ctx , authContextKey {} , subjectAutostart )
408+ return As (ctx , subjectAutostart )
398409}
399410
400411// AsHangDetector returns a context with an actor that has permissions required
401412// for unhanger.Detector to function.
402413func AsHangDetector (ctx context.Context ) context.Context {
403- return context . WithValue (ctx , authContextKey {} , subjectHangDetector )
414+ return As (ctx , subjectHangDetector )
404415}
405416
406417// AsKeyRotator returns a context with an actor that has permissions required for rotating crypto keys.
407418func AsKeyRotator (ctx context.Context ) context.Context {
408- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyRotator )
419+ return As (ctx , subjectCryptoKeyRotator )
409420}
410421
411422// AsKeyReader returns a context with an actor that has permissions required for reading crypto keys.
412423func AsKeyReader (ctx context.Context ) context.Context {
413- return context . WithValue (ctx , authContextKey {} , subjectCryptoKeyReader )
424+ return As (ctx , subjectCryptoKeyReader )
414425}
415426
416427// AsNotifier returns a context with an actor that has permissions required for
417428// creating/reading/updating/deleting notifications.
418429func AsNotifier (ctx context.Context ) context.Context {
419- return context . WithValue (ctx , authContextKey {} , subjectNotifier )
430+ return As (ctx , subjectNotifier )
420431}
421432
422433// AsResourceMonitor returns a context with an actor that has permissions required for
423434// updating resource monitors.
424435func AsResourceMonitor (ctx context.Context ) context.Context {
425- return context . WithValue (ctx , authContextKey {} , subjectResourceMonitor )
436+ return As (ctx , subjectResourceMonitor )
426437}
427438
428439// AsSystemRestricted returns a context with an actor that has permissions
429440// required for various system operations (login, logout, metrics cache).
430441func AsSystemRestricted (ctx context.Context ) context.Context {
431- return context . WithValue (ctx , authContextKey {} , subjectSystemRestricted )
442+ return As (ctx , subjectSystemRestricted )
432443}
433444
434445// AsSystemReadProvisionerDaemons returns a context with an actor that has permissions
435446// to read provisioner daemons.
436447func AsSystemReadProvisionerDaemons (ctx context.Context ) context.Context {
437- return context . WithValue (ctx , authContextKey {} , subjectSystemReadProvisionerDaemons )
448+ return As (ctx , subjectSystemReadProvisionerDaemons )
438449}
439450
440451// AsPrebuildsOrchestrator returns a context with an actor that has permissions
441452// to read orchestrator workspace prebuilds.
442453func AsPrebuildsOrchestrator (ctx context.Context ) context.Context {
443- return context . WithValue (ctx , authContextKey {} , subjectPrebuildsOrchestrator )
454+ return As (ctx , subjectPrebuildsOrchestrator )
444455}
445456
446457var AsRemoveActor = rbac.Subject {
@@ -458,6 +469,9 @@ func As(ctx context.Context, actor rbac.Subject) context.Context {
458469 // should be removed from the context.
459470 return context .WithValue (ctx , authContextKey {}, nil )
460471 }
472+ if rlogger := loggermw .RequestLoggerFromContext (ctx ); rlogger != nil {
473+ rlogger .WithAuthContext (actor )
474+ }
461475 return context .WithValue (ctx , authContextKey {}, actor )
462476}
463477
0 commit comments