2020-12-12 06:26:26 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CertificateSigningHandler struct {
|
2020-12-12 08:59:06 +00:00
|
|
|
requestRegistry *SigningRequestRegistry
|
2020-12-12 06:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:59:06 +00:00
|
|
|
func NewCertificateSigningHandler(requestRegistry *SigningRequestRegistry) *CertificateSigningHandler {
|
|
|
|
return &CertificateSigningHandler{requestRegistry: requestRegistry}
|
2020-12-12 06:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CertificateSigningHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "Only POST requests support", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Header.Get("content-type") != "application/json" {
|
|
|
|
http.Error(w, "Only JSON content is accepted", http.StatusNotAcceptable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var requestBody requestData
|
|
|
|
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
2020-12-12 08:59:06 +00:00
|
|
|
log.Error(err)
|
2020-12-12 06:26:26 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:59:06 +00:00
|
|
|
type acceptedResponse struct {
|
|
|
|
RequestId string `json:"request_id"`
|
2020-12-12 06:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:59:06 +00:00
|
|
|
taskUuid, err := h.requestRegistry.AddSigningRequest(&requestBody)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2020-12-12 06:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:59:06 +00:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
response := &acceptedResponse{RequestId: taskUuid}
|
|
|
|
if err = json.NewEncoder(w).Encode(response); err != nil {
|
2020-12-12 06:26:26 +00:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestData struct {
|
|
|
|
Csr string `json:"csr"`
|
|
|
|
CommonName string `json:"commonName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseData struct {
|
|
|
|
Certificate string `json:"certificate"`
|
|
|
|
CAChain []string `json:"ca_chain"`
|
|
|
|
}
|