mirror of
https://github.com/openziti/ziti.git
synced 2026-09-10 16:55:41 +00:00
1c122af490
- moves the channel dependency to channel/v5 v5.0.10 and sdk-golang to v1.9.0 in the root and zititest modules - mechanically rewrites every channel/v4 import path to channel/v5 This is the import-path-only step; the API-level changes the switch requires land in the following commit. This commit does not build on its own.
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
/*
|
|
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_mgmt
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/michaelquigley/pfxlog"
|
|
"github.com/openziti/channel/v5"
|
|
"github.com/openziti/channel/v5/protobufs"
|
|
"github.com/openziti/ziti/v2/common/pb/mgmt_pb"
|
|
"github.com/openziti/ziti/v2/controller/network"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type validateTerminatorsHandler struct {
|
|
network *network.Network
|
|
}
|
|
|
|
func newValidateTerminatorsHandler(network *network.Network) *validateTerminatorsHandler {
|
|
return &validateTerminatorsHandler{network: network}
|
|
}
|
|
|
|
func (*validateTerminatorsHandler) ContentType() int32 {
|
|
return int32(mgmt_pb.ContentType_ValidateTerminatorsRequestType)
|
|
}
|
|
|
|
func (handler *validateTerminatorsHandler) HandleReceive(msg *channel.Message, ch channel.Channel) {
|
|
log := pfxlog.ContextLogger(ch.Label())
|
|
request := &mgmt_pb.ValidateTerminatorsRequest{}
|
|
|
|
var err error
|
|
var terminatorCount uint64
|
|
|
|
terminatorDetailHandler := func(detail *mgmt_pb.TerminatorDetail) {
|
|
if !ch.IsClosed() {
|
|
if sendErr := protobufs.MarshalTyped(detail).WithTimeout(15 * time.Second).SendAndWaitForWire(ch); sendErr != nil {
|
|
log.WithError(sendErr).Error("send of terminator detail failed, closing channel")
|
|
if closeErr := ch.Close(); closeErr != nil {
|
|
log.WithError(closeErr).Error("failed to close channel")
|
|
}
|
|
}
|
|
} else {
|
|
log.Info("channel closed, unable to send terminator detail")
|
|
}
|
|
}
|
|
|
|
hostStateErrorHandler := func(detail *mgmt_pb.InvalidTerminatorHostState) {
|
|
if !ch.IsClosed() {
|
|
if sendErr := protobufs.MarshalTyped(detail).WithTimeout(15 * time.Second).SendAndWaitForWire(ch); sendErr != nil {
|
|
log.WithError(sendErr).Error("send of invalid terminator host status failed, closing channel")
|
|
if closeErr := ch.Close(); closeErr != nil {
|
|
log.WithError(closeErr).Error("failed to close channel")
|
|
}
|
|
}
|
|
} else {
|
|
log.Info("channel closed, unable to send invalid terminator host status")
|
|
}
|
|
}
|
|
|
|
response := &mgmt_pb.ValidateTerminatorsResponse{}
|
|
|
|
if err = proto.Unmarshal(msg.Body, request); err != nil {
|
|
response.Success = false
|
|
response.Message = fmt.Sprintf("%v: failed to unmarshall request: %v", handler.network.GetAppId(), err)
|
|
} else {
|
|
terminatorCount, err = handler.network.Managers.Terminator.ValidateTerminators(request, terminatorDetailHandler, hostStateErrorHandler)
|
|
|
|
if err == nil {
|
|
response.Success = true
|
|
response.TerminatorCount = terminatorCount
|
|
} else {
|
|
response.Success = false
|
|
response.Message = fmt.Sprintf("%v: failed to run terminator validation: %v", handler.network.GetAppId(), err)
|
|
}
|
|
}
|
|
|
|
body, err := proto.Marshal(response)
|
|
if err != nil {
|
|
pfxlog.Logger().WithError(err).Error("unexpected error serializing ValidateTerminatorsResponse")
|
|
return
|
|
}
|
|
|
|
responseMsg := channel.NewMessage(int32(mgmt_pb.ContentType_ValidateTerminatorResponseType), body)
|
|
responseMsg.ReplyTo(msg)
|
|
if err = ch.Send(responseMsg); err != nil {
|
|
pfxlog.Logger().WithError(err).Error("unexpected error sending ValidateTerminatorsResponse")
|
|
}
|
|
}
|