This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
64 lines (50 loc) · 1.52 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package apiserver
import (
"encoding/base64"
"encoding/json"
"net/http"
"strings"
"github.com/go-chi/chi"
log "github.com/sirupsen/logrus"
"github.com/nais/outtune/pkg/cert"
)
func certHandler(ca cert.CA) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
var cReq CertRequest
err := json.NewDecoder(request.Body).Decode(&cReq)
if err != nil {
log.Errorf("unmarshaling request: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
publicKey, err := base64.StdEncoding.DecodeString(cReq.PublicKeyPem)
if err != nil {
log.Errorf("base64 decoding: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
trimmedSerial := strings.Replace(cReq.Serial, "\n", "", -1)
generatedCert, err := ca.MakeCert(request.Context(), trimmedSerial, publicKey)
if err != nil {
log.Errorf("generating cert: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
log.Infof("(%s) Returning cert for serial: %s", request.URL.Path, cReq.Serial)
err = json.NewEncoder(writer).Encode(CertResponse{CertPem: generatedCert})
if err != nil {
log.Errorf("writing response: %v", err)
}
}
}
func observability(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusOK)
}
func New(localCA cert.CA) chi.Router {
r := chi.NewRouter()
r.Post("/local/cert", certHandler(localCA))
r.Post("/cert", certHandler(localCA))
r.Get("/isalive", observability)
r.Get("/isready", observability)
return r
}