Skip to content

Commit

Permalink
Merge pull request #2545 from openziti/check-peer-certs
Browse files Browse the repository at this point in the history
Check peer certs
  • Loading branch information
plorenz authored Nov 18, 2024
2 parents 215fd0c + e6834cd commit 01d8122
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions controller/raft/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (self *impl) GetOrConnectPeer(address string, timeout time.Duration) (*Peer

peer.Channel = binding.GetChannel()

if err = self.checkClusterIds(peer.Channel); err != nil {
if err = self.validateConnection(peer.Channel); err != nil {
return err
}

Expand Down Expand Up @@ -510,6 +510,14 @@ func (self *impl) GetOrConnectPeer(address string, timeout time.Duration) (*Peer
return peer, nil
}

func (self *impl) validateConnection(ch channel.Channel) error {
if err := self.checkClusterIds(ch); err != nil {
return err
}

return self.checkCerts(ch)
}

func (self *impl) checkClusterIds(ch channel.Channel) error {
clusterId := string(ch.Underlay().Headers()[ClusterIdHeader])
if clusterId != "" && self.env.GetClusterId() != "" && clusterId != self.env.GetClusterId() {
Expand All @@ -518,6 +526,21 @@ func (self *impl) checkClusterIds(ch channel.Channel) error {
return nil
}

func (self *impl) checkCerts(ch channel.Channel) error {
certs := ch.Underlay().Certificates()
if len(certs) == 0 {
return errors.New("unable to validate peer connection, no certs presented")
}

for _, cert := range ch.Underlay().Certificates() {
if _, err := self.env.GetNodeId().CaPool().VerifyToRoot(cert); err == nil {
return nil
}
}

return errors.New("unable to validate peer connection, no certs presented matched the CA for this node")
}

func (self *impl) GetPeerInfo(address string, timeout time.Duration) (raft.ServerID, raft.ServerAddress, error) {
log := pfxlog.Logger().WithField("address", address)
addr, err := transport.ParseAddress(address)
Expand Down Expand Up @@ -560,7 +583,7 @@ func (self *impl) GetPeerInfo(address string, timeout time.Duration) (raft.Serve
return err
}

if err = self.checkClusterIds(binding.GetChannel()); err != nil {
if err = self.validateConnection(binding.GetChannel()); err != nil {
return err
}

Expand Down Expand Up @@ -794,7 +817,7 @@ func (self *impl) AcceptUnderlay(underlay channel.Underlay) error {
}
}

if err = self.checkClusterIds(peer.Channel); err != nil {
if err = self.validateConnection(peer.Channel); err != nil {
return err
}

Expand Down

0 comments on commit 01d8122

Please sign in to comment.