@@ -82,77 +82,68 @@ type provisionerDaemonAuth struct {
82
82
83
83
// authorize returns mutated tags if the given HTTP request is authorized to access the provisioner daemon
84
84
// protobuf API, and returns nil, err otherwise.
85
- func (p * provisionerDaemonAuth ) authorize (r * http.Request , orgID uuid. UUID , tags map [string ]string ) (uuid.UUID , map [string ]string , error ) {
85
+ func (p * provisionerDaemonAuth ) authorize (r * http.Request , org database. Organization , tags map [string ]string ) (uuid. UUID , uuid.UUID , map [string ]string , error ) {
86
86
ctx := r .Context ()
87
87
apiKey , apiKeyOK := httpmw .APIKeyOptional (r )
88
88
pk , pkOK := httpmw .ProvisionerKeyAuthOptional (r )
89
89
provAuth := httpmw .ProvisionerDaemonAuthenticated (r )
90
90
if ! provAuth && ! apiKeyOK {
91
- return uuid .Nil , nil , xerrors .New ("no API key or provisioner key provided" )
91
+ return uuid .Nil , uuid . Nil , nil , xerrors .New ("no API key or provisioner key provided" )
92
92
}
93
93
if apiKeyOK && pkOK {
94
- return uuid .Nil , nil , xerrors .New ("Both API key and provisioner key authentication provided. Only one is allowed." )
94
+ return uuid .Nil , uuid . Nil , nil , xerrors .New ("Both API key and provisioner key authentication provided. Only one is allowed." )
95
95
}
96
96
97
97
// Provisioner Key Auth
98
98
if pkOK {
99
- if pk .OrganizationID != orgID {
100
- return uuid .Nil , nil , xerrors .New ("provisioner key unauthorized" )
101
- }
102
99
if tags != nil && ! maps .Equal (tags , map [string ]string {}) {
103
- return uuid .Nil , nil , xerrors .New ("tags are not allowed when using a provisioner key" )
100
+ return uuid .Nil , uuid . Nil , nil , xerrors .New ("tags are not allowed when using a provisioner key" )
104
101
}
105
102
106
103
// If using provisioner key / PSK auth, the daemon is, by definition, scoped to the organization.
107
104
// Use the provisioner key tags here.
108
105
tags = provisionersdk .MutateTags (uuid .Nil , pk .Tags )
109
- return pk .ID , tags , nil
106
+ return pk .ID , pk . OrganizationID , tags , nil
110
107
}
111
108
112
- // User Auth
113
- if apiKeyOK {
114
- userKey , err := uuid .Parse (codersdk .ProvisionerKeyIDUserAuth )
115
- if err != nil {
116
- return uuid .Nil , nil , xerrors .Errorf ("parse user provisioner key id: %w" , err )
109
+ // PSK Auth
110
+ if provAuth {
111
+ if ! org .IsDefault {
112
+ return uuid .Nil , uuid .Nil , nil , xerrors .Errorf ("PSK auth is only allowed for the default organization '%s'" , org .Name )
117
113
}
118
114
119
- tags = provisionersdk .MutateTags (apiKey .UserID , tags )
120
- if tags [provisionersdk .TagScope ] == provisionersdk .ScopeUser {
121
- // Any authenticated user can create provisioner daemons scoped
122
- // for jobs that they own,
123
- return userKey , tags , nil
124
- }
125
- ua := httpmw .UserAuthorization (r )
126
- err = p .authorizer .Authorize (ctx , ua , policy .ActionCreate , rbac .ResourceProvisionerDaemon .InOrg (orgID ))
115
+ pskKey , err := uuid .Parse (codersdk .ProvisionerKeyIDPSK )
127
116
if err != nil {
128
- if ! provAuth {
129
- return uuid .Nil , nil , xerrors .New ("user unauthorized" )
130
- }
131
-
132
- pskKey , err := uuid .Parse (codersdk .ProvisionerKeyIDPSK )
133
- if err != nil {
134
- return uuid .Nil , nil , xerrors .Errorf ("parse psk provisioner key id: %w" , err )
135
- }
117
+ return uuid .Nil , uuid .Nil , nil , xerrors .Errorf ("parse psk provisioner key id: %w" , err )
118
+ }
136
119
137
- // Allow fallback to PSK auth if the user is not allowed to create provisioner daemons.
138
- // This is to preserve backwards compatibility with existing user provisioner daemons.
139
- // If using PSK auth, the daemon is, by definition, scoped to the organization.
140
- tags = provisionersdk .MutateTags (uuid .Nil , tags )
120
+ tags = provisionersdk .MutateTags (uuid .Nil , tags )
121
+ return pskKey , org .ID , tags , nil
122
+ }
141
123
142
- return pskKey , tags , nil
143
- }
124
+ // User Auth
125
+ if ! apiKeyOK {
126
+ return uuid .Nil , uuid .Nil , nil , xerrors .New ("no API key provided" )
127
+ }
144
128
145
- return userKey , tags , nil
129
+ userKey , err := uuid .Parse (codersdk .ProvisionerKeyIDUserAuth )
130
+ if err != nil {
131
+ return uuid .Nil , uuid .Nil , nil , xerrors .Errorf ("parse user provisioner key id: %w" , err )
146
132
}
147
133
148
- // PSK Auth
149
- pskKey , err := uuid .Parse (codersdk .ProvisionerKeyIDPSK )
134
+ tags = provisionersdk .MutateTags (apiKey .UserID , tags )
135
+ if tags [provisionersdk .TagScope ] == provisionersdk .ScopeUser {
136
+ // Any authenticated user can create provisioner daemons scoped
137
+ // for jobs that they own,
138
+ return userKey , org .ID , tags , nil
139
+ }
140
+ ua := httpmw .UserAuthorization (r )
141
+ err = p .authorizer .Authorize (ctx , ua , policy .ActionCreate , rbac .ResourceProvisionerDaemon .InOrg (org .ID ))
150
142
if err != nil {
151
- return uuid .Nil , nil , xerrors .Errorf ( "parse psk provisioner key id: %w" , err )
143
+ return uuid .Nil , uuid . Nil , nil , xerrors .New ( "user unauthorized" )
152
144
}
153
145
154
- tags = provisionersdk .MutateTags (uuid .Nil , tags )
155
- return pskKey , tags , nil
146
+ return userKey , org .ID , tags , nil
156
147
}
157
148
158
149
// Serves the provisioner daemon protobuf API over a WebSocket.
@@ -166,7 +157,6 @@ func (p *provisionerDaemonAuth) authorize(r *http.Request, orgID uuid.UUID, tags
166
157
// @Router /organizations/{organization}/provisionerdaemons/serve [get]
167
158
func (api * API ) provisionerDaemonServe (rw http.ResponseWriter , r * http.Request ) {
168
159
ctx := r .Context ()
169
- organization := httpmw .OrganizationParam (r )
170
160
171
161
tags := map [string ]string {}
172
162
if r .URL .Query ().Has ("tag" ) {
@@ -215,7 +205,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)
215
205
api .Logger .Warn (ctx , "unnamed provisioner daemon" )
216
206
}
217
207
218
- keyID , tags , err := api .provisionerDaemonAuth .authorize (r , organization . ID , tags )
208
+ keyID , orgID , tags , err := api .provisionerDaemonAuth .authorize (r , httpmw . OrganizationParam ( r ) , tags )
219
209
if err != nil {
220
210
api .Logger .Warn (ctx , "unauthorized provisioner daemon serve request" , slog .F ("tags" , tags ), slog .Error (err ))
221
211
httpapi .Write (ctx , rw , http .StatusForbidden ,
@@ -287,7 +277,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)
287
277
LastSeenAt : sql.NullTime {Time : now , Valid : true },
288
278
Version : versionHdrVal ,
289
279
APIVersion : apiVersion ,
290
- OrganizationID : organization . ID ,
280
+ OrganizationID : orgID ,
291
281
KeyID : keyID ,
292
282
})
293
283
if err != nil {
@@ -351,7 +341,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)
351
341
srvCtx ,
352
342
api .AccessURL ,
353
343
daemon .ID ,
354
- organization . ID ,
344
+ orgID ,
355
345
logger ,
356
346
provisioners ,
357
347
tags ,
0 commit comments