-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
127 lines (105 loc) · 2.29 KB
/
server.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pkiapi
import (
"net"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/opencoff/go-pki"
)
type createSvrReq struct {
DNs []string `json:"domain_names"`
IPs []string `json:"ips"`
ValidityDays int `json:"validity_days"`
SignWith string `json:"sign_with"`
Password string `json:"password"`
}
func (svr *Server) createServer(c *gin.Context) {
cn := c.Param("cn")
if cn == "" {
c.AbortWithStatus(400)
return
}
_, err := svr.ca.Find(cn)
if err == nil {
c.AbortWithStatus(409)
return
}
csreq := createSvrReq{}
if err := c.BindJSON(&csreq); err != nil {
c.AbortWithError(400, err)
return
}
if strings.Index(cn, ".") > 0 {
csreq.DNs = append(csreq.DNs, cn)
}
iplist := IPList{}
(&iplist).Set(strings.Join(csreq.IPs, ","))
var days = time.Duration(365)
if csreq.ValidityDays > 0 {
days = time.Duration(csreq.ValidityDays)
}
var ca *pki.CA
if len(csreq.SignWith) > 0 {
ica, err := ca.FindCA(csreq.SignWith)
if err != nil {
jsonError(c, 400, "couldn't find provided signer %s", csreq.SignWith)
return
}
ca = ica
} else {
ca = svr.ca
}
ci := &pki.CertInfo{
Subject: ca.Subject,
Validity: time.Hour * 24 * days,
DNSNames: []string(csreq.DNs),
IPAddresses: []net.IP(iplist),
}
ci.Subject.CommonName = cn
// TODO: add private key password
crt, err := ca.NewServerCert(ci, csreq.Password)
if err != nil {
jsonError(c, 500, "can't create user cert: %s", err)
return
}
c.JSON(200, map[string]string{"cert": Cert(*crt.Certificate).String()})
}
func (svr *Server) deleteServer(c *gin.Context) {
cn := c.Param("cn")
if cn == "" {
c.AbortWithStatus(400)
return
}
ck, err := svr.ca.Find(cn)
if err == pki.ErrNotFound {
c.AbortWithStatus(404)
return
}
switch {
case ck.IsServer:
err = svr.ca.RevokeServer(cn)
case ck.IsCA:
c.AbortWithStatus(404)
return
default:
c.AbortWithStatus(404)
return
}
if err != nil {
jsonError(c, 500, "failed to delete server: %s", err)
return
}
c.Status(204)
}
func (svr *Server) listServers(c *gin.Context) {
certs, err := svr.ca.GetServers()
if err != nil {
jsonError(c, 500, "failed to list servers: %s", err)
return
}
svrs := []jsonableCert{}
for _, cert := range certs {
svrs = append(svrs, jsonableCert{cert})
}
c.JSON(200, svrs)
}