@@ -9,8 +9,10 @@ import (
9
9
10
10
"github.com/google/uuid"
11
11
12
+ "github.com/coder/coder/coderd/authzquery"
12
13
"github.com/coder/coder/coderd/database"
13
14
"github.com/coder/coder/coderd/httpapi"
15
+ "github.com/coder/coder/coderd/rbac"
14
16
"github.com/coder/coder/codersdk"
15
17
)
16
18
@@ -30,6 +32,7 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
30
32
return func (next http.Handler ) http.Handler {
31
33
return http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
32
34
ctx := r .Context ()
35
+ systemCtx := authzquery .WithAuthorizeSystemContext (ctx , rbac .RolesAdminSystem ())
33
36
tokenValue := apiTokenFromRequest (r )
34
37
if tokenValue == "" {
35
38
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
@@ -45,7 +48,7 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
45
48
})
46
49
return
47
50
}
48
- agent , err := db .GetWorkspaceAgentByAuthToken (ctx , token )
51
+ agent , err := db .GetWorkspaceAgentByAuthToken (systemCtx , token )
49
52
if err != nil {
50
53
if errors .Is (err , sql .ErrNoRows ) {
51
54
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
@@ -62,7 +65,42 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
62
65
return
63
66
}
64
67
68
+ workspace , err := db .GetWorkspaceByAgentID (systemCtx , agent .ID )
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." ,
91
+ })
92
+ return
93
+ }
94
+
95
+ subject := rbac.Subject {
96
+ ID : user .ID .String (),
97
+ Roles : rbac .RoleNames (roles .Roles ),
98
+ Groups : roles .Groups ,
99
+ Scope : rbac .ScopeAll , // TODO: ScopeWorkspaceAgent
100
+ }
101
+
65
102
ctx = context .WithValue (ctx , workspaceAgentContextKey {}, agent )
103
+ ctx = authzquery .WithAuthorizeContext (ctx , subject )
66
104
next .ServeHTTP (rw , r .WithContext (ctx ))
67
105
})
68
106
}
0 commit comments