Skip to content

Commit f3f484c

Browse files
committed
Log errors using slog.Logger
This commit allows logging errors in a slog.Logger injected in the context. This type of logger is not currently used directly in step-ca, but this will change in the future.
1 parent fdb0cf0 commit f3f484c

34 files changed

+574
-523
lines changed

acme/api/account.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ func NewAccount(w http.ResponseWriter, r *http.Request) {
8282

8383
payload, err := payloadFromContext(ctx)
8484
if err != nil {
85-
render.Error(w, err)
85+
render.Error(w, r, err)
8686
return
8787
}
8888
var nar NewAccountRequest
8989
if err := json.Unmarshal(payload.value, &nar); err != nil {
90-
render.Error(w, acme.WrapError(acme.ErrorMalformedType, err,
90+
render.Error(w, r, acme.WrapError(acme.ErrorMalformedType, err,
9191
"failed to unmarshal new-account request payload"))
9292
return
9393
}
9494
if err := nar.Validate(); err != nil {
95-
render.Error(w, err)
95+
render.Error(w, r, err)
9696
return
9797
}
9898

9999
prov, err := acmeProvisionerFromContext(ctx)
100100
if err != nil {
101-
render.Error(w, err)
101+
render.Error(w, r, err)
102102
return
103103
}
104104

@@ -108,26 +108,26 @@ func NewAccount(w http.ResponseWriter, r *http.Request) {
108108
var acmeErr *acme.Error
109109
if !errors.As(err, &acmeErr) || acmeErr.Status != http.StatusBadRequest {
110110
// Something went wrong ...
111-
render.Error(w, err)
111+
render.Error(w, r, err)
112112
return
113113
}
114114

115115
// Account does not exist //
116116
if nar.OnlyReturnExisting {
117-
render.Error(w, acme.NewError(acme.ErrorAccountDoesNotExistType,
117+
render.Error(w, r, acme.NewError(acme.ErrorAccountDoesNotExistType,
118118
"account does not exist"))
119119
return
120120
}
121121

122122
jwk, err := jwkFromContext(ctx)
123123
if err != nil {
124-
render.Error(w, err)
124+
render.Error(w, r, err)
125125
return
126126
}
127127

128128
eak, err := validateExternalAccountBinding(ctx, &nar)
129129
if err != nil {
130-
render.Error(w, err)
130+
render.Error(w, r, err)
131131
return
132132
}
133133

@@ -140,17 +140,17 @@ func NewAccount(w http.ResponseWriter, r *http.Request) {
140140
ProvisionerName: prov.Name,
141141
}
142142
if err := db.CreateAccount(ctx, acc); err != nil {
143-
render.Error(w, acme.WrapErrorISE(err, "error creating account"))
143+
render.Error(w, r, acme.WrapErrorISE(err, "error creating account"))
144144
return
145145
}
146146

147147
if eak != nil { // means that we have a (valid) External Account Binding key that should be bound, updated and sent in the response
148148
if err := eak.BindTo(acc); err != nil {
149-
render.Error(w, err)
149+
render.Error(w, r, err)
150150
return
151151
}
152152
if err := db.UpdateExternalAccountKey(ctx, prov.ID, eak); err != nil {
153-
render.Error(w, acme.WrapErrorISE(err, "error updating external account binding key"))
153+
render.Error(w, r, acme.WrapErrorISE(err, "error updating external account binding key"))
154154
return
155155
}
156156
acc.ExternalAccountBinding = nar.ExternalAccountBinding
@@ -163,7 +163,7 @@ func NewAccount(w http.ResponseWriter, r *http.Request) {
163163
linker.LinkAccount(ctx, acc)
164164

165165
w.Header().Set("Location", getAccountLocationPath(ctx, linker, acc.ID))
166-
render.JSONStatus(w, acc, httpStatus)
166+
render.JSONStatus(w, r, acc, httpStatus)
167167
}
168168

169169
// GetOrUpdateAccount is the api for updating an ACME account.
@@ -174,12 +174,12 @@ func GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
174174

175175
acc, err := accountFromContext(ctx)
176176
if err != nil {
177-
render.Error(w, err)
177+
render.Error(w, r, err)
178178
return
179179
}
180180
payload, err := payloadFromContext(ctx)
181181
if err != nil {
182-
render.Error(w, err)
182+
render.Error(w, r, err)
183183
return
184184
}
185185

@@ -188,12 +188,12 @@ func GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
188188
if !payload.isPostAsGet {
189189
var uar UpdateAccountRequest
190190
if err := json.Unmarshal(payload.value, &uar); err != nil {
191-
render.Error(w, acme.WrapError(acme.ErrorMalformedType, err,
191+
render.Error(w, r, acme.WrapError(acme.ErrorMalformedType, err,
192192
"failed to unmarshal new-account request payload"))
193193
return
194194
}
195195
if err := uar.Validate(); err != nil {
196-
render.Error(w, err)
196+
render.Error(w, r, err)
197197
return
198198
}
199199
if len(uar.Status) > 0 || len(uar.Contact) > 0 {
@@ -204,7 +204,7 @@ func GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
204204
}
205205

206206
if err := db.UpdateAccount(ctx, acc); err != nil {
207-
render.Error(w, acme.WrapErrorISE(err, "error updating account"))
207+
render.Error(w, r, acme.WrapErrorISE(err, "error updating account"))
208208
return
209209
}
210210
}
@@ -213,7 +213,7 @@ func GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
213213
linker.LinkAccount(ctx, acc)
214214

215215
w.Header().Set("Location", linker.GetLink(ctx, acme.AccountLinkType, acc.ID))
216-
render.JSON(w, acc)
216+
render.JSON(w, r, acc)
217217
}
218218

219219
func logOrdersByAccount(w http.ResponseWriter, oids []string) {
@@ -233,23 +233,23 @@ func GetOrdersByAccountID(w http.ResponseWriter, r *http.Request) {
233233

234234
acc, err := accountFromContext(ctx)
235235
if err != nil {
236-
render.Error(w, err)
236+
render.Error(w, r, err)
237237
return
238238
}
239239
accID := chi.URLParam(r, "accID")
240240
if acc.ID != accID {
241-
render.Error(w, acme.NewError(acme.ErrorUnauthorizedType, "account ID '%s' does not match url param '%s'", acc.ID, accID))
241+
render.Error(w, r, acme.NewError(acme.ErrorUnauthorizedType, "account ID '%s' does not match url param '%s'", acc.ID, accID))
242242
return
243243
}
244244

245245
orders, err := db.GetOrdersByAccountID(ctx, acc.ID)
246246
if err != nil {
247-
render.Error(w, err)
247+
render.Error(w, r, err)
248248
return
249249
}
250250

251251
linker.LinkOrdersByAccountID(ctx, orders)
252252

253-
render.JSON(w, orders)
253+
render.JSON(w, r, orders)
254254
logOrdersByAccount(w, orders)
255255
}

acme/api/handler.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ func GetDirectory(w http.ResponseWriter, r *http.Request) {
223223
ctx := r.Context()
224224
acmeProv, err := acmeProvisionerFromContext(ctx)
225225
if err != nil {
226-
render.Error(w, err)
226+
render.Error(w, r, err)
227227
return
228228
}
229229

230230
linker := acme.MustLinkerFromContext(ctx)
231231

232-
render.JSON(w, &Directory{
232+
render.JSON(w, r, &Directory{
233233
NewNonce: linker.GetLink(ctx, acme.NewNonceLinkType),
234234
NewAccount: linker.GetLink(ctx, acme.NewAccountLinkType),
235235
NewOrder: linker.GetLink(ctx, acme.NewOrderLinkType),
@@ -273,8 +273,8 @@ func shouldAddMetaObject(p *provisioner.ACME) bool {
273273

274274
// NotImplemented returns a 501 and is generally a placeholder for functionality which
275275
// MAY be added at some point in the future but is not in any way a guarantee of such.
276-
func NotImplemented(w http.ResponseWriter, _ *http.Request) {
277-
render.Error(w, acme.NewError(acme.ErrorNotImplementedType, "this API is not implemented"))
276+
func NotImplemented(w http.ResponseWriter, r *http.Request) {
277+
render.Error(w, r, acme.NewError(acme.ErrorNotImplementedType, "this API is not implemented"))
278278
}
279279

280280
// GetAuthorization ACME api for retrieving an Authz.
@@ -285,28 +285,28 @@ func GetAuthorization(w http.ResponseWriter, r *http.Request) {
285285

286286
acc, err := accountFromContext(ctx)
287287
if err != nil {
288-
render.Error(w, err)
288+
render.Error(w, r, err)
289289
return
290290
}
291291
az, err := db.GetAuthorization(ctx, chi.URLParam(r, "authzID"))
292292
if err != nil {
293-
render.Error(w, acme.WrapErrorISE(err, "error retrieving authorization"))
293+
render.Error(w, r, acme.WrapErrorISE(err, "error retrieving authorization"))
294294
return
295295
}
296296
if acc.ID != az.AccountID {
297-
render.Error(w, acme.NewError(acme.ErrorUnauthorizedType,
297+
render.Error(w, r, acme.NewError(acme.ErrorUnauthorizedType,
298298
"account '%s' does not own authorization '%s'", acc.ID, az.ID))
299299
return
300300
}
301301
if err = az.UpdateStatus(ctx, db); err != nil {
302-
render.Error(w, acme.WrapErrorISE(err, "error updating authorization status"))
302+
render.Error(w, r, acme.WrapErrorISE(err, "error updating authorization status"))
303303
return
304304
}
305305

306306
linker.LinkAuthorization(ctx, az)
307307

308308
w.Header().Set("Location", linker.GetLink(ctx, acme.AuthzLinkType, az.ID))
309-
render.JSON(w, az)
309+
render.JSON(w, r, az)
310310
}
311311

312312
// GetChallenge ACME api for retrieving a Challenge.
@@ -317,13 +317,13 @@ func GetChallenge(w http.ResponseWriter, r *http.Request) {
317317

318318
acc, err := accountFromContext(ctx)
319319
if err != nil {
320-
render.Error(w, err)
320+
render.Error(w, r, err)
321321
return
322322
}
323323

324324
payload, err := payloadFromContext(ctx)
325325
if err != nil {
326-
render.Error(w, err)
326+
render.Error(w, r, err)
327327
return
328328
}
329329

@@ -336,30 +336,30 @@ func GetChallenge(w http.ResponseWriter, r *http.Request) {
336336
azID := chi.URLParam(r, "authzID")
337337
ch, err := db.GetChallenge(ctx, chi.URLParam(r, "chID"), azID)
338338
if err != nil {
339-
render.Error(w, acme.WrapErrorISE(err, "error retrieving challenge"))
339+
render.Error(w, r, acme.WrapErrorISE(err, "error retrieving challenge"))
340340
return
341341
}
342342
ch.AuthorizationID = azID
343343
if acc.ID != ch.AccountID {
344-
render.Error(w, acme.NewError(acme.ErrorUnauthorizedType,
344+
render.Error(w, r, acme.NewError(acme.ErrorUnauthorizedType,
345345
"account '%s' does not own challenge '%s'", acc.ID, ch.ID))
346346
return
347347
}
348348
jwk, err := jwkFromContext(ctx)
349349
if err != nil {
350-
render.Error(w, err)
350+
render.Error(w, r, err)
351351
return
352352
}
353353
if err = ch.Validate(ctx, db, jwk, payload.value); err != nil {
354-
render.Error(w, acme.WrapErrorISE(err, "error validating challenge"))
354+
render.Error(w, r, acme.WrapErrorISE(err, "error validating challenge"))
355355
return
356356
}
357357

358358
linker.LinkChallenge(ctx, ch, azID)
359359

360360
w.Header().Add("Link", link(linker.GetLink(ctx, acme.AuthzLinkType, azID), "up"))
361361
w.Header().Set("Location", linker.GetLink(ctx, acme.ChallengeLinkType, azID, ch.ID))
362-
render.JSON(w, ch)
362+
render.JSON(w, r, ch)
363363
}
364364

365365
// GetCertificate ACME api for retrieving a Certificate.
@@ -369,18 +369,18 @@ func GetCertificate(w http.ResponseWriter, r *http.Request) {
369369

370370
acc, err := accountFromContext(ctx)
371371
if err != nil {
372-
render.Error(w, err)
372+
render.Error(w, r, err)
373373
return
374374
}
375375

376376
certID := chi.URLParam(r, "certID")
377377
cert, err := db.GetCertificate(ctx, certID)
378378
if err != nil {
379-
render.Error(w, acme.WrapErrorISE(err, "error retrieving certificate"))
379+
render.Error(w, r, acme.WrapErrorISE(err, "error retrieving certificate"))
380380
return
381381
}
382382
if cert.AccountID != acc.ID {
383-
render.Error(w, acme.NewError(acme.ErrorUnauthorizedType,
383+
render.Error(w, r, acme.NewError(acme.ErrorUnauthorizedType,
384384
"account '%s' does not own certificate '%s'", acc.ID, certID))
385385
return
386386
}

0 commit comments

Comments
 (0)