@@ -20,6 +20,7 @@ import (
20
20
21
21
"cdr.dev/slog"
22
22
"github.com/coder/coder/coderd"
23
+ "github.com/coder/coder/coderd/audit"
23
24
"github.com/coder/coder/coderd/database"
24
25
"github.com/coder/coder/coderd/httpapi"
25
26
"github.com/coder/coder/coderd/rbac"
@@ -59,7 +60,18 @@ var Keys = map[string]ed25519.PublicKey{"2022-08-12": ed25519.PublicKey(key20220
59
60
// @Success 201 {object} codersdk.License
60
61
// @Router /licenses [post]
61
62
func (api * API ) postLicense (rw http.ResponseWriter , r * http.Request ) {
62
- ctx := r .Context ()
63
+ var (
64
+ ctx = r .Context ()
65
+ auditor = api .AGPL .Auditor .Load ()
66
+ aReq , commitAudit = audit .InitRequest [database.License ](rw , & audit.RequestParams {
67
+ Audit : * auditor ,
68
+ Log : api .Logger ,
69
+ Request : r ,
70
+ Action : database .AuditActionCreate ,
71
+ })
72
+ )
73
+ defer commitAudit ()
74
+
63
75
if ! api .AGPL .Authorize (r , rbac .ActionCreate , rbac .ResourceLicense ) {
64
76
httpapi .Forbidden (rw )
65
77
return
@@ -119,6 +131,8 @@ func (api *API) postLicense(rw http.ResponseWriter, r *http.Request) {
119
131
})
120
132
return
121
133
}
134
+ aReq .New = dl
135
+
122
136
err = api .updateEntitlements (ctx )
123
137
if err != nil {
124
138
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
@@ -186,11 +200,10 @@ func (api *API) licenses(rw http.ResponseWriter, r *http.Request) {
186
200
// @Success 200
187
201
// @Router /licenses/{id} [delete]
188
202
func (api * API ) deleteLicense (rw http.ResponseWriter , r * http.Request ) {
189
- ctx := r .Context ()
190
- if ! api .AGPL .Authorize (r , rbac .ActionDelete , rbac .ResourceLicense ) {
191
- httpapi .Forbidden (rw )
192
- return
193
- }
203
+ var (
204
+ ctx = r .Context ()
205
+ auditor = api .AGPL .Auditor .Load ()
206
+ )
194
207
195
208
idStr := chi .URLParam (r , "id" )
196
209
id , err := strconv .ParseInt (idStr , 10 , 32 )
@@ -201,6 +214,26 @@ func (api *API) deleteLicense(rw http.ResponseWriter, r *http.Request) {
201
214
return
202
215
}
203
216
217
+ dl , err := api .Database .GetLicenseByID (ctx , int32 (id ))
218
+ if err != nil {
219
+ // don't fail the HTTP request simply because we cannot audit
220
+ api .Logger .Warn (context .Background (), "could not retrieve license; cannot audit" , slog .Error (err ))
221
+ }
222
+
223
+ aReq , commitAudit := audit .InitRequest [database.License ](rw , & audit.RequestParams {
224
+ Audit : * auditor ,
225
+ Log : api .Logger ,
226
+ Request : r ,
227
+ Action : database .AuditActionDelete ,
228
+ })
229
+ defer commitAudit ()
230
+ aReq .Old = dl
231
+
232
+ if ! api .AGPL .Authorize (r , rbac .ActionDelete , rbac .ResourceLicense ) {
233
+ httpapi .Forbidden (rw )
234
+ return
235
+ }
236
+
204
237
_ , err = api .Database .DeleteLicense (ctx , int32 (id ))
205
238
if xerrors .Is (err , sql .ErrNoRows ) {
206
239
httpapi .Write (ctx , rw , http .StatusNotFound , codersdk.Response {
0 commit comments