@@ -65,48 +65,48 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
65
65
return
66
66
}
67
67
68
- workspace , err := db . GetWorkspaceByAgentID ( systemCtx , agent . ID )
68
+ subject , err := getAgentSubject ( ctx , db , agent )
69
69
if err != nil {
70
- // TODO: details
71
- httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
72
- Message : "Workspace agent not authorized." ,
73
- })
74
- return
75
- }
76
-
77
- user , err := db .GetUserByID (systemCtx , workspace .OwnerID )
78
- if err != nil {
79
- // TODO: details
80
- httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
81
- Message : "Workspace agent not authorized." ,
82
- })
83
- return
84
- }
85
-
86
- roles , err := db .GetAuthorizationUserRoles (systemCtx , user .ID )
87
- if err != nil {
88
- // TODO: details
89
- httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
90
- Message : "Workspace agent not authorized." ,
70
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
71
+ Message : "Internal error fetching workspace agent." ,
72
+ Detail : err .Error (),
91
73
})
92
74
return
93
75
}
94
76
95
- // A user that creates a workspace can use this agent auth token and
96
- // impersonate the workspace. So to prevent privilege escalation, the
97
- // subject inherits the roles of the user that owns the workspace.
98
- // We then add a workspace-agent scope to limit the permissions
99
- // to only what the workspace agent needs.
100
- subject := rbac.Subject {
101
- ID : user .ID .String (),
102
- Roles : rbac .RoleNames (roles .Roles ),
103
- Groups : roles .Groups ,
104
- Scope : rbac .WorkspaceAgentScope (workspace .ID ),
105
- }
106
-
107
77
ctx = context .WithValue (ctx , workspaceAgentContextKey {}, agent )
108
78
ctx = authzquery .WithAuthorizeContext (ctx , subject )
109
79
next .ServeHTTP (rw , r .WithContext (ctx ))
110
80
})
111
81
}
112
82
}
83
+
84
+ func getAgentSubject (ctx context.Context , db database.Store , agent database.WorkspaceAgent ) (rbac.Subject , error ) {
85
+ // TODO: make a different query that gets the workspace owner and roles along with the agent.
86
+ workspace , err := db .GetWorkspaceByAgentID (ctx , agent .ID )
87
+ if err != nil {
88
+ return rbac.Subject {}, err
89
+ }
90
+
91
+ user , err := db .GetUserByID (ctx , workspace .OwnerID )
92
+ if err != nil {
93
+ return rbac.Subject {}, err
94
+ }
95
+
96
+ roles , err := db .GetAuthorizationUserRoles (ctx , user .ID )
97
+ if err != nil {
98
+ return rbac.Subject {}, err
99
+ }
100
+
101
+ // A user that creates a workspace can use this agent auth token and
102
+ // impersonate the workspace. So to prevent privilege escalation, the
103
+ // subject inherits the roles of the user that owns the workspace.
104
+ // We then add a workspace-agent scope to limit the permissions
105
+ // to only what the workspace agent needs.
106
+ return rbac.Subject {
107
+ ID : user .ID .String (),
108
+ Roles : rbac .RoleNames (roles .Roles ),
109
+ Groups : roles .Groups ,
110
+ Scope : rbac .WorkspaceAgentScope (workspace .ID ),
111
+ }, nil
112
+ }
0 commit comments