Jan Dittberner
2de592d30c
This commit changes the wire protocol to split between command announcement and command payload to allow proper typing of sent and received msgpack messages. CRL fetching has been implemented as second command after the existing health check command.
220 lines
5.5 KiB
Go
220 lines
5.5 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 (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/x509/revoking"
|
|
|
|
"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 {
|
|
HandleCommandAnnounce([]byte) (*messages.CommandAnnounce, error)
|
|
HandleCommand(*messages.CommandAnnounce, []byte) ([]byte, []byte, error)
|
|
}
|
|
|
|
type MsgPackHandler struct {
|
|
logger *logrus.Logger
|
|
healthHandler *health.Handler
|
|
fetchCRLHandler *revoking.FetchCRLHandler
|
|
}
|
|
|
|
func (m *MsgPackHandler) HandleCommandAnnounce(frame []byte) (*messages.CommandAnnounce, error) {
|
|
var ann messages.CommandAnnounce
|
|
|
|
if err := msgpack.Unmarshal(frame, &ann); err != nil {
|
|
return nil, fmt.Errorf("could not unmarshal command announcement: %w", err)
|
|
}
|
|
|
|
m.logger.Infof("received command announcement %+v", ann)
|
|
|
|
return &ann, nil
|
|
}
|
|
|
|
func (m *MsgPackHandler) HandleCommand(announce *messages.CommandAnnounce, frame []byte) ([]byte, []byte, error) {
|
|
var (
|
|
response *Response
|
|
clientError, err error
|
|
)
|
|
|
|
switch announce.Code {
|
|
case messages.CmdHealth:
|
|
// health has no payload, ignore the frame
|
|
response, err = m.handleCommand(&Command{Announce: announce, Command: nil})
|
|
if err != nil {
|
|
m.logger.WithError(err).Error("health handling failed")
|
|
|
|
clientError = errors.New("could not handle request")
|
|
}
|
|
case messages.CmdFetchCRL:
|
|
var command messages.FetchCRLCommand
|
|
|
|
err = msgpack.Unmarshal(frame, &command)
|
|
if err != nil {
|
|
m.logger.WithError(err).Error("unmarshal failed")
|
|
|
|
clientError = errors.New("could not unmarshal fetch crl command")
|
|
|
|
break
|
|
}
|
|
|
|
response, err = m.handleCommand(&Command{Announce: announce, Command: command})
|
|
if err != nil {
|
|
m.logger.WithError(err).Error("fetch CRL handling failed")
|
|
|
|
clientError = errors.New("could not handle request")
|
|
}
|
|
}
|
|
|
|
if clientError != nil {
|
|
response = buildErrorResponse(clientError.Error())
|
|
}
|
|
|
|
announceData, err := msgpack.Marshal(response.Announce)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not marshal response announcement: %w", err)
|
|
}
|
|
|
|
responseData, err := msgpack.Marshal(response.Response)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not marshal response: %w", err)
|
|
}
|
|
|
|
return announceData, responseData, nil
|
|
}
|
|
|
|
type Command struct {
|
|
Announce *messages.CommandAnnounce
|
|
Command interface{}
|
|
}
|
|
|
|
type Response struct {
|
|
Announce *messages.ResponseAnnounce
|
|
Response interface{}
|
|
}
|
|
|
|
func (r *Response) String() string {
|
|
return fmt.Sprintf("Response[Code=%s] created=%s data=%s", r.Announce.Code, r.Announce.Created, r.Response)
|
|
}
|
|
|
|
func (m *MsgPackHandler) handleCommand(command *Command) (*Response, error) {
|
|
var (
|
|
err error
|
|
responseData interface{}
|
|
responseCode messages.ResponseCode
|
|
)
|
|
|
|
switch command.Announce.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, responseData = messages.RespHealth, response
|
|
case messages.CmdFetchCRL:
|
|
var res *revoking.Result
|
|
|
|
fetchCRLPayload, ok := command.Command.(messages.FetchCRLCommand)
|
|
if !ok {
|
|
return nil, fmt.Errorf("could not use payload as FetchCRLPayload")
|
|
}
|
|
|
|
res, err = m.fetchCRLHandler.FetchCRL(fetchCRLPayload.IssuerID)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
response := messages.FetchCRLResponse{
|
|
IsDelta: false,
|
|
CRLData: res.Data,
|
|
}
|
|
|
|
responseCode, responseData = messages.RespFetchCRL, response
|
|
default:
|
|
return nil, fmt.Errorf("unhandled command %v", command)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error from command handler: %w", err)
|
|
}
|
|
|
|
return &Response{
|
|
Announce: messages.BuildResponseAnnounce(responseCode),
|
|
Response: responseData,
|
|
}, nil
|
|
}
|
|
|
|
func buildErrorResponse(errMsg string) *Response {
|
|
return &Response{
|
|
Announce: messages.BuildResponseAnnounce(messages.RespError),
|
|
Response: messages.ErrorResponse{Message: errMsg},
|
|
}
|
|
}
|
|
|
|
func New(logger *logrus.Logger, handlers ...RegisterHandler) (Handler, error) {
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
h := &MsgPackHandler{
|
|
logger: logger,
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
func RegisterFetchCRLHandler(fetchCRLHandler *revoking.FetchCRLHandler) func(handler *MsgPackHandler) {
|
|
return func(h *MsgPackHandler) {
|
|
h.fetchCRLHandler = fetchCRLHandler
|
|
}
|
|
}
|