Skip to content

Commit b55da3b

Browse files
committed
return actual error messages in response
1 parent cda0a78 commit b55da3b

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

internal/handler/api.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,31 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
193193
user = r.Context().Value("user").(*entity.User)
194194
)
195195

196+
response := entity.CertificateResponse{}
197+
196198
csrMode := bh.DBSvc.GetSetting(global.SettingEnableCSRRequestMode)
197199
if csrMode != "true" {
198200
w.WriteHeader(http.StatusNotImplemented)
201+
response.Error = "CSR mode is not enabled on this instance"
202+
_ = json.NewEncoder(w).Encode(response)
199203
return
200204
}
201205

202206
csrBytes, err := io.ReadAll(r.Body)
203207
if err != nil {
204208
logger.Debugf("could not read request body: %s", err.Error())
205-
http.Error(w, "malformed http request", http.StatusBadRequest)
209+
w.WriteHeader(http.StatusBadRequest)
210+
response.Error = "could not read request body"
211+
_ = json.NewEncoder(w).Encode(response)
206212
return
207213
}
208214

209215
csr, err := x509.ParseCertificateRequest(csrBytes)
210216
if err != nil {
211217
logger.Debugf("could not parse certificate signing request: %s", err.Error())
212-
http.Error(w, "malformed certificate signing request", http.StatusBadRequest)
218+
w.WriteHeader(http.StatusBadRequest)
219+
response.Error = "could not parse request body"
220+
_ = json.NewEncoder(w).Encode(response)
213221
return
214222
}
215223

@@ -224,6 +232,8 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
224232
if err = bh.DBSvc.AddRequestInfo(&ri); err != nil {
225233
logger.Infof("error inserting request info: %s\n", err.Error())
226234
w.WriteHeader(http.StatusInternalServerError)
235+
response.Error = "internal server error"
236+
_ = json.NewEncoder(w).Encode(response)
227237
return
228238
}
229239

@@ -235,8 +245,6 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
235245
domainsToCheck = append(domainsToCheck, d)
236246
}
237247

238-
response := entity.CertificateResponse{}
239-
240248
var hasChallenge bool
241249
if len(domainsToCheck) > 0 {
242250
challengeID := fmt.Sprintf("%d-%s", user.ID, security.GenerateToken(20))
@@ -254,6 +262,8 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
254262
if err = bh.DBSvc.AddChallenge(ch); err != nil {
255263
logger.Infof("error inserting challenge: %s\n", err.Error())
256264
w.WriteHeader(http.StatusInternalServerError)
265+
response.Error = "internal server error"
266+
_ = json.NewEncoder(w).Encode(response)
257267
return
258268
}
259269
response.Challenges = append(response.Challenges, entity.ChallengeResponse{
@@ -276,6 +286,8 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
276286
if err = bh.DBSvc.AddChallenge(ch); err != nil {
277287
logger.Infof("error inserting challenge: %s\n", err.Error())
278288
w.WriteHeader(http.StatusInternalServerError)
289+
response.Error = "internal server error"
290+
_ = json.NewEncoder(w).Encode(response)
279291
return
280292
}
281293
response.Challenges = append(response.Challenges, entity.ChallengeResponse{
@@ -291,19 +303,17 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
291303
if hasChallenge {
292304
w.Header().Set("Content-Type", "application/json")
293305
w.WriteHeader(http.StatusAccepted)
294-
err = json.NewEncoder(w).Encode(response)
295-
if err != nil {
296-
logger.Infof("could not encode response: %s", err.Error())
297-
w.WriteHeader(http.StatusInternalServerError)
298-
return
299-
}
306+
_ = json.NewEncoder(w).Encode(response)
300307
return
301308
}
302309

310+
// no challenge, issue certificate right away
303311
certBytes, sn, err := bh.CertMaker.GenerateCertificateByCSR(csr)
304312
if err != nil {
305313
logger.Errorf("error generating certificate: %s\n", err.Error())
306314
w.WriteHeader(http.StatusInternalServerError)
315+
response.Error = "internal server error"
316+
_ = json.NewEncoder(w).Encode(response)
307317
return
308318
}
309319

@@ -317,6 +327,8 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
317327
if err != nil {
318328
logger.Errorf("could not insert cert info into DB: %s", err.Error())
319329
w.WriteHeader(http.StatusInternalServerError)
330+
response.Error = "internal server error"
331+
_ = json.NewEncoder(w).Encode(response)
320332
return
321333
}
322334

@@ -325,12 +337,7 @@ func (bh *BaseHandler) APIRequestCertificateWithCSRHandler(w http.ResponseWriter
325337
w.Header().Set("Content-Type", "application/json")
326338
w.WriteHeader(http.StatusCreated)
327339

328-
err = json.NewEncoder(w).Encode(response)
329-
if err != nil {
330-
logger.Infof("could not encode response: %s", err.Error())
331-
w.WriteHeader(http.StatusInternalServerError)
332-
return
333-
}
340+
_ = json.NewEncoder(w).Encode(response)
334341
}
335342

336343
// APIObtainCertificateHandler allows to actually download a certificate

0 commit comments

Comments
 (0)