-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
79 lines (63 loc) · 1.35 KB
/
routes.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package pkiapi
import (
"bytes"
"github.com/gin-gonic/gin"
"github.com/opencoff/go-pki"
)
func NewServer(ca *pki.CA) *Server {
return &Server{ca: ca}
}
type Server struct {
ca *pki.CA
}
func (svr *Server) SetupRoutes(r gin.IRouter) {
r.GET("/servers", svr.listServers)
r.GET("/servers/:cn/export", svr.ExportCert)
r.POST("/servers/:cn", svr.createServer)
r.DELETE("/servers/:cn", svr.deleteServer)
r.GET("/users", svr.listUsers)
r.GET("/users/:cn/export", svr.ExportCert)
r.POST("/users/:cn", svr.createUser)
r.DELETE("/users/:cn", svr.deleteUser)
r.GET("/crl/:days", svr.generateCRL)
}
func (svr *Server) ExportCert(c *gin.Context) {
cn := c.Param("cn")
if cn == "" {
c.AbortWithStatus(400)
return
}
chain := c.Query("chain") != ""
withCA := c.Query("ca") != ""
cert, err := svr.ca.Find(cn)
if err != nil {
c.AbortWithStatus(404)
return
}
var pem []byte
var key []byte
if cert.IsCA && chain {
cas, err := svr.ca.ChainFor(cert)
if err != nil {
jsonError(c, 500, "can't find cert chain: %s", err)
return
}
var cw bytes.Buffer
for i := range cas {
ck := cas[i]
cw.Write(ck.PEM())
}
pem = cw.Bytes()
_, key = cert.PEM()
} else {
pem, key = cert.PEM()
}
data := map[string]string{
"key": string(key),
"pem": string(pem),
}
if withCA {
data["ca"] = string(svr.ca.PEM())
}
c.JSON(200, data)
}