-
Notifications
You must be signed in to change notification settings - Fork 129
/
connect.go
122 lines (101 loc) · 3.74 KB
/
connect.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
/*
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 handler_ctrl
import (
"crypto/sha1"
"crypto/x509"
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/ziti/controller/network"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/identity"
"github.com/pkg/errors"
"time"
)
type ConnectHandler struct {
identity identity.Identity
network *network.Network
}
func NewConnectHandler(identity identity.Identity, network *network.Network) *ConnectHandler {
return &ConnectHandler{
identity: identity,
network: network,
}
}
func (self *ConnectHandler) HandleConnection(hello *channel.Hello, certificates []*x509.Certificate) error {
if _, found := hello.Headers[channel.TypeHeader]; found {
return nil
}
id := hello.IdToken
log := pfxlog.Logger().WithField("routerId", id)
// verify cert chain
if len(certificates) == 0 {
return errors.Errorf("no certificates provided, unable to verify dialer, routerId: %v", id)
}
config := self.identity.ServerTLSConfig()
opts := x509.VerifyOptions{
Roots: config.RootCAs,
Intermediates: x509.NewCertPool(),
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
var validFingerPrints []string
var errorList errorz.MultipleErrors
for _, cert := range certificates {
if cert.IsCA {
opts.Intermediates.AddCert(cert)
}
}
for i, cert := range certificates {
if !cert.IsCA {
if _, err := cert.Verify(opts); err == nil {
fingerprint := fmt.Sprintf("%x", sha1.Sum(cert.Raw))
validFingerPrints = append(validFingerPrints, fingerprint)
log.Debugf("%d): peer certificate fingerprint [%s]", i, fingerprint)
log.Debugf("%d): peer common name [%s]", i, cert.Subject.CommonName)
} else {
errorList = append(errorList, err)
}
}
}
if len(validFingerPrints) == 0 && len(errorList) > 0 {
return errorList.ToError()
}
log.Debugf("peer has [%d] valid certificates out of [%v] submitted", len(validFingerPrints), len(certificates))
if router := self.network.GetConnectedRouter(id); router != nil {
if time.Since(router.ConnectTime) < self.network.GetOptions().RouterConnectChurnLimit {
log.WithField("routerName", router.Name).Error("router already connected and churn threshold not met")
return errors.Errorf("router already connected id: %s, name: %s", id, router.Name)
}
log.WithField("routerName", router.Name).Warn("router already connected, but churn threshold met. replacing connection")
}
if r, err := self.network.GetRouter(id); err == nil {
if r.Fingerprint == nil {
log.Error("router enrollment incomplete")
return errors.Errorf("router enrollment incomplete, routerId: %v", id)
}
if !stringz.Contains(validFingerPrints, *r.Fingerprint) {
log.WithField("fp", *r.Fingerprint).WithField("givenFps", validFingerPrints).Error("router fingerprint mismatch")
return errors.Errorf("incorrect fingerprint/unenrolled router, routerId: %v, given fingerprints: %v", id, validFingerPrints)
}
if r.Disabled {
log.Error("router disabled")
return errors.Errorf("router disabld, routerId: %v", id)
}
} else {
log.Error("unknown/unenrolled router")
return errors.Errorf("unknown/unenrolled router, routerId: %v", id)
}
return nil
}