/* 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 ( err error payload interface{} responseCode messages.ResponseCode ) switch command.Code { case messages.CmdHealth: var res *health.Result res, err = m.healthHandler.CheckHealth() if err != nil { break } response := messages.HealthResponse{ Version: res.Version, Healthy: res.Healthy, } for _, info := range res.Info { response.Info = append(response.Info, messages.HealthInfo{ Source: info.Source, Healthy: info.Healthy, MoreInfo: info.MoreInfo, }) } responseCode, payload = messages.RspHealth, response default: return nil, fmt.Errorf("unhandled command %s", command) } if err != nil { return nil, fmt.Errorf("error from command handler: %w", err) } 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 } }