|
|
|
@ -21,7 +21,6 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
@ -33,186 +32,185 @@ import (
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const readCommandTimeOut = 5 * time.Second
|
|
|
|
|
|
|
|
|
|
var errReadCommandTimeout = errors.New("read command timeout expired")
|
|
|
|
|
|
|
|
|
|
// MsgPackHandler is a ServerHandler implementation for the msgpack serialization format.
|
|
|
|
|
// MsgPackHandler is a Handler implementation for the msgpack serialization format.
|
|
|
|
|
type MsgPackHandler struct {
|
|
|
|
|
logger *logrus.Logger
|
|
|
|
|
healthHandler *health.Handler
|
|
|
|
|
fetchCRLHandler *revoking.FetchCRLHandler
|
|
|
|
|
currentCommand *protocol.Command
|
|
|
|
|
currentResponse *protocol.Response
|
|
|
|
|
lock sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) CommandAnnounce(frames chan []byte) (*protocol.Command, error) {
|
|
|
|
|
func (m *MsgPackHandler) HandleCommandAnnounce(frame []byte) error {
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
frame := <-frames
|
|
|
|
|
|
|
|
|
|
var ann messages.CommandAnnounce
|
|
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &ann); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("could not unmarshal command announcement: %w", err)
|
|
|
|
|
return fmt.Errorf("could not unmarshal command announcement: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.logger.WithField("announcement", &ann).Info("received command announcement")
|
|
|
|
|
|
|
|
|
|
return &protocol.Command{Announce: &ann}, nil
|
|
|
|
|
m.currentCommand = &protocol.Command{Announce: &ann}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) CommandData(frames chan []byte, command *protocol.Command) error {
|
|
|
|
|
func (m *MsgPackHandler) HandleCommand(frame []byte) error {
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case frame := <-frames:
|
|
|
|
|
err := m.parseCommand(frame, command)
|
|
|
|
|
err := m.parseCommand(frame)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
m.currentResponse = m.buildErrorResponse(err.Error())
|
|
|
|
|
|
|
|
|
|
m.logCommandResponse()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
case <-time.After(readCommandTimeOut):
|
|
|
|
|
return errReadCommandTimeout
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) HandleCommand(command *protocol.Command) (*protocol.Response, error) {
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
response *protocol.Response
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response, err = m.handleCommand(command)
|
|
|
|
|
err = m.handleCommand()
|
|
|
|
|
if err != nil {
|
|
|
|
|
m.logger.WithError(err).Error("command handling failed")
|
|
|
|
|
|
|
|
|
|
response = m.buildErrorResponse(command.Announce.ID, "command handling failed")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.logCommandResponse(command, response)
|
|
|
|
|
m.logCommandResponse()
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
m.currentCommand = nil
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) logCommandResponse(command *protocol.Command, response *protocol.Response) {
|
|
|
|
|
m.logger.WithField("command", command.Announce).Info("handled command")
|
|
|
|
|
m.logger.WithField("command", command).WithField("response", response).Debug("command and response")
|
|
|
|
|
func (m *MsgPackHandler) logCommandResponse() {
|
|
|
|
|
m.logger.WithField("command", m.currentCommand.Announce).Info("handled command")
|
|
|
|
|
m.logger.WithField(
|
|
|
|
|
"command",
|
|
|
|
|
m.currentCommand,
|
|
|
|
|
).WithField(
|
|
|
|
|
"response",
|
|
|
|
|
m.currentResponse,
|
|
|
|
|
).Debug("command and response")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) Respond(response *protocol.Response, out chan []byte) error {
|
|
|
|
|
func (m *MsgPackHandler) ResponseAnnounce() ([]byte, error) {
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
announce, err := msgpack.Marshal(response)
|
|
|
|
|
announceData, err := msgpack.Marshal(m.currentResponse.Announce)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("could not marshal response announcement: %w", err)
|
|
|
|
|
return nil, fmt.Errorf("could not marshal response announcement: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.logger.WithField("length", len(announce)).Debug("write response announcement")
|
|
|
|
|
m.logger.WithField("announcement", m.currentResponse.Announce).Debug("write response announcement")
|
|
|
|
|
|
|
|
|
|
out <- announce
|
|
|
|
|
return announceData, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := msgpack.Marshal(response.Response)
|
|
|
|
|
func (m *MsgPackHandler) ResponseData() ([]byte, error) {
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
responseData, err := msgpack.Marshal(m.currentResponse.Response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("could not marshal response: %w", err)
|
|
|
|
|
return nil, fmt.Errorf("could not marshal response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.logger.WithField("length", len(data)).Debug("write response")
|
|
|
|
|
|
|
|
|
|
out <- announce
|
|
|
|
|
m.logger.WithField("response", m.currentResponse.Response).Debug("write response")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
return responseData, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) parseHealthCommand(frame []byte) (*messages.HealthCommand, error) {
|
|
|
|
|
func (m *MsgPackHandler) parseHealthCommand(frame []byte) error {
|
|
|
|
|
var command messages.HealthCommand
|
|
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &command); err != nil {
|
|
|
|
|
m.logger.WithError(err).Error("unmarshal failed")
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("could not unmarshal health command")
|
|
|
|
|
return errors.New("could not unmarshal health command")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &command, nil
|
|
|
|
|
m.currentCommand.Command = &command
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) parseFetchCRLCommand(frame []byte) (*messages.FetchCRLCommand, error) {
|
|
|
|
|
func (m *MsgPackHandler) parseFetchCRLCommand(frame []byte) error {
|
|
|
|
|
var command messages.FetchCRLCommand
|
|
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &command); err != nil {
|
|
|
|
|
m.logger.WithError(err).Error("unmarshal failed")
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("could not unmarshal fetch crl command")
|
|
|
|
|
return errors.New("could not unmarshal fetch crl command")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &command, nil
|
|
|
|
|
m.currentCommand.Command = &command
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) handleCommand(command *protocol.Command) (*protocol.Response, error) {
|
|
|
|
|
func (m *MsgPackHandler) currentID() string {
|
|
|
|
|
return m.currentCommand.Announce.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) handleCommand() error {
|
|
|
|
|
var (
|
|
|
|
|
responseCode messages.ResponseCode
|
|
|
|
|
err error
|
|
|
|
|
responseData interface{}
|
|
|
|
|
responseCode messages.ResponseCode
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
switch cmd := command.Command.(type) {
|
|
|
|
|
switch m.currentCommand.Command.(type) {
|
|
|
|
|
case *messages.HealthCommand:
|
|
|
|
|
response, err := m.handleHealthCommand()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responseCode, responseData = messages.RespHealth, response
|
|
|
|
|
case *messages.FetchCRLCommand:
|
|
|
|
|
response, err := m.handleFetchCRLCommand(cmd)
|
|
|
|
|
response, err := m.handleFetchCRLCommand()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responseCode, responseData = messages.RespFetchCRL, response
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("unhandled command %s", command.Announce)
|
|
|
|
|
return fmt.Errorf("unhandled command %s", m.currentCommand.Announce)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &protocol.Response{
|
|
|
|
|
Announce: messages.BuildResponseAnnounce(responseCode, command.Announce.ID),
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error from command handler: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.currentResponse = &protocol.Response{
|
|
|
|
|
Announce: messages.BuildResponseAnnounce(responseCode, m.currentID()),
|
|
|
|
|
Response: responseData,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) buildErrorResponse(commandID string, errMsg string) *protocol.Response {
|
|
|
|
|
func (m *MsgPackHandler) buildErrorResponse(errMsg string) *protocol.Response {
|
|
|
|
|
return &protocol.Response{
|
|
|
|
|
Announce: messages.BuildResponseAnnounce(messages.RespError, commandID),
|
|
|
|
|
Announce: messages.BuildResponseAnnounce(messages.RespError, m.currentID()),
|
|
|
|
|
Response: &messages.ErrorResponse{Message: errMsg},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) parseCommand(frame []byte, command *protocol.Command) error {
|
|
|
|
|
switch command.Announce.Code {
|
|
|
|
|
func (m *MsgPackHandler) parseCommand(frame []byte) error {
|
|
|
|
|
switch m.currentCommand.Announce.Code {
|
|
|
|
|
case messages.CmdHealth:
|
|
|
|
|
healthCommand, err := m.parseHealthCommand(frame)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command.Command = healthCommand
|
|
|
|
|
return m.parseHealthCommand(frame)
|
|
|
|
|
case messages.CmdFetchCRL:
|
|
|
|
|
fetchCRLCommand, err := m.parseFetchCRLCommand(frame)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command.Command = fetchCRLCommand
|
|
|
|
|
return m.parseFetchCRLCommand(frame)
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("unhandled command code %s", command.Announce.Code)
|
|
|
|
|
return fmt.Errorf("unhandled command code %s", m.currentCommand.Announce.Code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) handleHealthCommand() (*messages.HealthResponse, error) {
|
|
|
|
@ -237,20 +235,27 @@ func (m *MsgPackHandler) handleHealthCommand() (*messages.HealthResponse, error)
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgPackHandler) handleFetchCRLCommand(command *messages.FetchCRLCommand) (*messages.FetchCRLResponse, error) {
|
|
|
|
|
res, err := m.fetchCRLHandler.FetchCRL(command.IssuerID)
|
|
|
|
|
func (m *MsgPackHandler) handleFetchCRLCommand() (*messages.FetchCRLResponse, error) {
|
|
|
|
|
fetchCRLPayload, ok := m.currentCommand.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 {
|
|
|
|
|
return nil, fmt.Errorf("could not fetch CRL: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &messages.FetchCRLResponse{
|
|
|
|
|
response := &messages.FetchCRLResponse{
|
|
|
|
|
IsDelta: false,
|
|
|
|
|
CRLNumber: res.Number,
|
|
|
|
|
CRLData: res.CRLData,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(logger *logrus.Logger, handlers ...RegisterHandler) (protocol.ServerHandler, error) {
|
|
|
|
|
func New(logger *logrus.Logger, handlers ...RegisterHandler) (protocol.Handler, error) {
|
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
|
|
|
|
|
|
h := &MsgPackHandler{
|
|
|
|
|