130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
|
/*
|
||
|
Copyright 2022 CAcert Inc.
|
||
|
SPDX-License-Identifier: Apache-2.0
|
||
|
|
||
|
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
|
||
|
|
||
|
http://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 protocol handles the protocol message marshaling and unmarshalling.
|
||
|
package protocol
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"time"
|
||
|
|
||
|
"github.com/shamaton/msgpackgen/msgpack"
|
||
|
|
||
|
"git.cacert.org/cacert-gosigner/pkg/health"
|
||
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
||
|
)
|
||
|
|
||
|
// Handler is responsible for parsing incoming frames and calling commands
|
||
|
type Handler interface {
|
||
|
HandleFrame([]byte) ([]byte, error)
|
||
|
}
|
||
|
|
||
|
type MsgPackHandler struct {
|
||
|
infoLog, errorLog *log.Logger
|
||
|
healthHandler *health.Handler
|
||
|
}
|
||
|
|
||
|
func (m *MsgPackHandler) HandleFrame(frame []byte) ([]byte, error) {
|
||
|
var command messages.Command
|
||
|
|
||
|
err := msgpack.Unmarshal(frame, &command)
|
||
|
if err != nil {
|
||
|
m.errorLog.Printf("unmarshal failed: %v", err)
|
||
|
|
||
|
errorResponse, innerErr := buildErrorResponse("do not understand")
|
||
|
if innerErr != nil {
|
||
|
return nil, innerErr
|
||
|
}
|
||
|
|
||
|
return errorResponse, nil
|
||
|
}
|
||
|
|
||
|
m.infoLog.Printf("Received %s command sent at %s", command.Code, command.TimeStamp)
|
||
|
|
||
|
response, err := m.handleCommand(&command)
|
||
|
if err != nil {
|
||
|
m.errorLog.Printf("command failed: %v", err)
|
||
|
|
||
|
errorResponse, innerErr := buildErrorResponse("command failed")
|
||
|
if innerErr != nil {
|
||
|
return nil, innerErr
|
||
|
}
|
||
|
|
||
|
return errorResponse, nil
|
||
|
}
|
||
|
|
||
|
responseData, err := msgpack.Marshal(response)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not marshal response: %w", err)
|
||
|
}
|
||
|
|
||
|
return responseData, nil
|
||
|
}
|
||
|
|
||
|
func (m *MsgPackHandler) handleCommand(command *messages.Command) (*messages.Response, error) {
|
||
|
var (
|
||
|
payload interface{}
|
||
|
responseCode messages.ResponseCode
|
||
|
)
|
||
|
|
||
|
switch command.Code {
|
||
|
case messages.CmdHealth:
|
||
|
responseCode, payload = messages.RspHealth, messages.HealthResponse{Version: m.healthHandler.Version}
|
||
|
default:
|
||
|
return nil, fmt.Errorf("unhandled command %s", command)
|
||
|
}
|
||
|
|
||
|
return &messages.Response{TimeStamp: time.Now().UTC(), Code: responseCode, Payload: payload}, nil
|
||
|
}
|
||
|
|
||
|
func buildErrorResponse(errMsg string) ([]byte, error) {
|
||
|
marshal, err := msgpack.Marshal(&messages.Response{
|
||
|
Code: messages.RspError,
|
||
|
TimeStamp: time.Now().UTC(),
|
||
|
Payload: messages.ErrorResponse{Message: errMsg},
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not marshal error response: %w", err)
|
||
|
}
|
||
|
|
||
|
return marshal, nil
|
||
|
}
|
||
|
|
||
|
func New(infoLog *log.Logger, errorLog *log.Logger, handlers ...RegisterHandler) (Handler, error) {
|
||
|
messages.RegisterGeneratedResolver()
|
||
|
|
||
|
h := &MsgPackHandler{
|
||
|
infoLog: infoLog,
|
||
|
errorLog: errorLog,
|
||
|
}
|
||
|
|
||
|
for _, reg := range handlers {
|
||
|
reg(h)
|
||
|
}
|
||
|
|
||
|
return h, nil
|
||
|
}
|
||
|
|
||
|
type RegisterHandler func(handler *MsgPackHandler)
|
||
|
|
||
|
func RegisterHealthHandler(healthHandler *health.Handler) func(*MsgPackHandler) {
|
||
|
return func(h *MsgPackHandler) {
|
||
|
h.healthHandler = healthHandler
|
||
|
}
|
||
|
}
|