-
Notifications
You must be signed in to change notification settings - Fork 141
/
enroll_router.go
293 lines (235 loc) · 8.93 KB
/
enroll_router.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package routes
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"github.com/fullsailor/pkcs7"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/edge-api/rest_client_api_server/operations/enroll"
client_well_known "github.com/openziti/edge-api/rest_client_api_server/operations/well_known"
management_well_known "github.com/openziti/edge-api/rest_management_api_server/operations/well_known"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/foundation/v2/errorz"
cert2 "github.com/openziti/ziti/common/cert"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/env"
"github.com/openziti/ziti/controller/internal/permissions"
"github.com/openziti/ziti/controller/model"
"github.com/openziti/ziti/controller/response"
"net/http"
"strings"
)
func init() {
r := NewEnrollRouter()
env.AddRouter(r)
}
type EnrollRouter struct {
}
func NewEnrollRouter() *EnrollRouter {
return &EnrollRouter{}
}
func (ro *EnrollRouter) Register(ae *env.AppEnv) {
//Enroll
ae.ClientApi.EnrollEnrollHandler = enroll.EnrollHandlerFunc(func(params enroll.EnrollParams) middleware.Responder {
return ae.IsAllowed(ro.enrollHandler, params.HTTPRequest, "", "", permissions.Always())
})
ae.ClientApi.EnrollEnrollCaHandler = enroll.EnrollCaHandlerFunc(func(params enroll.EnrollCaParams) middleware.Responder {
return ae.IsAllowed(ro.enrollHandler, params.HTTPRequest, "", "", permissions.Always())
})
ae.ClientApi.EnrollEnrollOttCaHandler = enroll.EnrollOttCaHandlerFunc(func(params enroll.EnrollOttCaParams) middleware.Responder {
return ae.IsAllowed(ro.enrollHandler, params.HTTPRequest, "", "", permissions.Always())
})
ae.ClientApi.EnrollEnrollOttHandler = enroll.EnrollOttHandlerFunc(func(params enroll.EnrollOttParams) middleware.Responder {
return ae.IsAllowed(ro.enrollHandler, params.HTTPRequest, "", "", permissions.Always())
})
ae.ClientApi.EnrollEnrollErOttHandler = enroll.EnrollErOttHandlerFunc(func(params enroll.EnrollErOttParams) middleware.Responder {
return ae.IsAllowed(ro.enrollHandler, params.HTTPRequest, "", "", permissions.Always())
})
// Extend Enrollment
ae.ClientApi.EnrollExtendRouterEnrollmentHandler = enroll.ExtendRouterEnrollmentHandlerFunc(func(params enroll.ExtendRouterEnrollmentParams) middleware.Responder {
return ae.IsAllowed(func(ae *env.AppEnv, rc *response.RequestContext) {
ro.extendRouterEnrollment(ae, rc, params)
}, params.HTTPRequest, "", "", permissions.Always())
})
// Utility, well-known
ae.ClientApi.WellKnownListWellKnownCasHandler = client_well_known.ListWellKnownCasHandlerFunc(func(params client_well_known.ListWellKnownCasParams) middleware.Responder {
return ae.IsAllowed(ro.getCaCerts, params.HTTPRequest, "", "", permissions.Always())
})
ae.ManagementApi.WellKnownListWellKnownCasHandler = management_well_known.ListWellKnownCasHandlerFunc(func(params management_well_known.ListWellKnownCasParams) middleware.Responder {
return ae.IsAllowed(ro.getCaCerts, params.HTTPRequest, "", "", permissions.Always())
})
}
func (ro *EnrollRouter) getCaCerts(ae *env.AppEnv, rc *response.RequestContext) {
rc.ResponseWriter.Header().Set("content-type", "application/pkcs7-mime")
rc.ResponseWriter.Header().Set("Content-Transfer-Encoding", "base64")
rc.ResponseWriter.WriteHeader(http.StatusOK)
// Decode each PEM block in the input and append the ASN.1
// DER bytes for each certificate therein to the data slice.
input := ae.Config.CaPems()
var data []byte
for len(input) > 0 {
var block *pem.Block
block, input = pem.Decode(input)
data = append(data, block.Bytes...)
}
// Build a PKCS#7 degenerate "certs only" structure from that ASN.1 certificates data.
var err error
data, err = pkcs7.DegenerateCertificate(data)
if err != nil {
pfxlog.Logger().Errorf("unexpected issue creating pkcs7 degenerate: %s", err)
rc.RespondWithError(errorz.NewUnhandled(err))
return
}
//encode as b64 and write to a string so the string can be written out in 64 byte lines
//there has to be a standard library for this - it feels strange to have to reinvent this
//write the bytes out in 64 byte lines...
bytes := []byte(base64.StdEncoding.EncodeToString(data))
step := 64
tot := len(bytes)
for i := 0; i < tot; i += step {
if i+step < tot {
_, _ = rc.ResponseWriter.Write(bytes[i : i+step])
_, _ = rc.ResponseWriter.Write([]byte("\n"))
} else {
_, _ = rc.ResponseWriter.Write(bytes[i:tot])
}
}
}
func (ro *EnrollRouter) enrollHandler(ae *env.AppEnv, rc *response.RequestContext) {
enrollContext := &model.EnrollmentContextHttp{}
err := enrollContext.FillFromHttpRequest(rc.Request, rc.NewChangeContext())
if err != nil {
rc.RespondWithError(err)
return
}
result, err := ae.Managers.Enrollment.Enroll(enrollContext)
if err != nil {
rc.RespondWithError(err)
return
}
if result == nil {
rc.RespondWithApiError(errorz.NewUnauthorized())
return
}
//prefer json producer for non ott methods (backwards compat for ott)
explicitJsonAccept := false
if accept := rc.Request.Header.Values("accept"); len(accept) == 0 {
explicitJsonAccept = false //no headers specified
} else {
for _, val := range accept {
if strings.Split(val, ";")[0] == "application/json" {
explicitJsonAccept = true
}
}
}
// for non ott enrollment, always return JSON
//prefer JSON if explicitly acceptable
if enrollContext.GetMethod() != db.MethodEnrollOtt || explicitJsonAccept {
rc.SetProducer(runtime.JSONProducer())
}
if producer, ok := rc.GetProducer().(*env.PemProducer); ok {
rc.RespondWithProducer(producer, result.TextContent, http.StatusOK)
return
}
rc.RespondWithOk(result.Content, &rest_model.Meta{})
}
func (ro *EnrollRouter) extendRouterEnrollment(ae *env.AppEnv, rc *response.RequestContext, params enroll.ExtendRouterEnrollmentParams) {
peerCerts := rc.Request.TLS.PeerCertificates
if len(peerCerts) == 0 {
rc.RespondWithApiError(errorz.NewUnauthorized())
return
}
var cert *x509.Certificate
for _, peerCert := range peerCerts {
if !peerCert.IsCA {
cert = peerCert
}
}
if cert == nil {
rc.RespondWithApiError(errorz.NewUnauthorized())
return
}
fingerprint := ae.GetFingerprintGenerator().FromCert(cert)
if fingerprint == "" {
rc.RespondWithApiError(errorz.NewUnauthorized())
return
}
if params.RouterExtendEnrollmentRequest.CertCsr == nil {
rc.RespondWithError(errorz.NewFieldApiError(&errorz.FieldError{
Reason: "client CSR is required",
FieldName: "certCsr",
FieldValue: params.RouterExtendEnrollmentRequest.CertCsr,
}))
return
}
if params.RouterExtendEnrollmentRequest.ServerCertCsr == nil {
rc.RespondWithError(errorz.NewFieldApiError(&errorz.FieldError{
Reason: "server CSR is required",
FieldName: "serverCertCsr",
FieldValue: params.RouterExtendEnrollmentRequest.ServerCertCsr,
}))
return
}
certCsr := []byte(*params.RouterExtendEnrollmentRequest.CertCsr)
serverCertCsr := []byte(*params.RouterExtendEnrollmentRequest.ServerCertCsr)
if edgeRouter, _ := ae.Managers.EdgeRouter.ReadOneByFingerprint(fingerprint); edgeRouter != nil {
certs, err := ae.Managers.EdgeRouter.ExtendEnrollment(edgeRouter, certCsr, serverCertCsr, rc.NewChangeContext())
if err != nil {
rc.RespondWithError(err)
return
}
clientChainPem, err := ae.Managers.Enrollment.GetClientCertChain(certs.RawClientCert)
if err != nil {
rc.RespondWithError(err)
return
}
serverPem, err := cert2.RawToPem(certs.RawServerCert)
if err != nil {
rc.RespondWithError(err)
return
}
rc.RespondWithOk(&rest_model.EnrollmentCerts{
Cert: clientChainPem,
ServerCert: string(serverPem),
}, &rest_model.Meta{})
return
}
if router, _ := ae.Managers.TransitRouter.ReadOneByFingerprint(fingerprint); router != nil {
certs, err := ae.Managers.TransitRouter.ExtendEnrollment(router, certCsr, serverCertCsr, rc.NewChangeContext())
if err != nil {
rc.RespondWithError(err)
return
}
clientChainPem, err := ae.Managers.Enrollment.GetClientCertChain(certs.RawClientCert)
if err != nil {
rc.RespondWithError(err)
return
}
serverPem, err := cert2.RawToPem(certs.RawServerCert)
if err != nil {
rc.RespondWithError(err)
return
}
rc.RespondWithOk(&rest_model.EnrollmentCerts{
Cert: clientChainPem,
ServerCert: string(serverPem),
}, &rest_model.Meta{})
return
}
//default unauthorized
rc.RespondWithApiError(errorz.NewUnauthorized())
}