137 lines
3.5 KiB
Go
137 lines
3.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 handler
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"github.com/shamaton/msgpackgen/msgpack"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
|
||
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
||
|
|
||
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
||
|
"git.cacert.org/cacert-gosignerclient/internal/config"
|
||
|
)
|
||
|
|
||
|
type SignerClientHandler struct {
|
||
|
logger *logrus.Logger
|
||
|
commands chan *protocol.Command
|
||
|
config *config.ClientConfig
|
||
|
}
|
||
|
|
||
|
func (s *SignerClientHandler) Send(command *protocol.Command, out chan []byte) error {
|
||
|
var (
|
||
|
frame []byte
|
||
|
err error
|
||
|
)
|
||
|
|
||
|
frame, err = msgpack.Marshal(command.Announce)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not marshal command annoucement: %w", err)
|
||
|
}
|
||
|
|
||
|
s.logger.WithField("announcement", command.Announce).Info("write command announcement")
|
||
|
|
||
|
s.logger.Trace("writing command announcement")
|
||
|
|
||
|
out <- frame
|
||
|
|
||
|
frame, err = msgpack.Marshal(command.Command)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not marshal command data: %w", err)
|
||
|
}
|
||
|
|
||
|
s.logger.WithField("command", command.Command).Info("write command data")
|
||
|
|
||
|
out <- frame
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (s *SignerClientHandler) ResponseAnnounce(in chan []byte) (*protocol.Response, error) {
|
||
|
response := &protocol.Response{}
|
||
|
|
||
|
var announce messages.ResponseAnnounce
|
||
|
|
||
|
select {
|
||
|
case frame := <-in:
|
||
|
if err := msgpack.Unmarshal(frame, &announce); err != nil {
|
||
|
return nil, fmt.Errorf("could not unmarshal response announcement: %w", err)
|
||
|
}
|
||
|
|
||
|
response.Announce = &announce
|
||
|
|
||
|
s.logger.WithField("announcement", response.Announce).Debug("received response announcement")
|
||
|
|
||
|
return response, nil
|
||
|
case <-time.After(s.config.ResponseAnnounceTimeout):
|
||
|
return nil, protocol.ErrResponseAnnounceTimeoutExpired
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *SignerClientHandler) ResponseData(in chan []byte, response *protocol.Response) error {
|
||
|
select {
|
||
|
case frame := <-in:
|
||
|
switch response.Announce.Code {
|
||
|
case messages.RespHealth:
|
||
|
var resp messages.HealthResponse
|
||
|
if err := msgpack.Unmarshal(frame, &resp); err != nil {
|
||
|
return fmt.Errorf("could not unmarshal health response data: %w", err)
|
||
|
}
|
||
|
|
||
|
response.Response = &resp
|
||
|
case messages.RespFetchCRL:
|
||
|
var resp messages.FetchCRLResponse
|
||
|
if err := msgpack.Unmarshal(frame, &resp); err != nil {
|
||
|
return fmt.Errorf("could not unmarshal fetch CRL response data: %w", err)
|
||
|
}
|
||
|
|
||
|
response.Response = &resp
|
||
|
default:
|
||
|
return fmt.Errorf("unhandled response code %s", response.Announce.Code)
|
||
|
}
|
||
|
case <-time.After(s.config.ResponseDataTimeout):
|
||
|
return protocol.ErrResponseDataTimeoutExpired
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (s *SignerClientHandler) HandleResponse(response *protocol.Response) error {
|
||
|
s.logger.WithField("response", response.Announce).Info("handled response")
|
||
|
s.logger.WithField("response", response).Debug("full response")
|
||
|
|
||
|
// TODO: add real implementations
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func New(
|
||
|
config *config.ClientConfig,
|
||
|
logger *logrus.Logger,
|
||
|
commands chan *protocol.Command,
|
||
|
) (protocol.ClientHandler, error) {
|
||
|
return &SignerClientHandler{
|
||
|
logger: logger,
|
||
|
config: config,
|
||
|
commands: commands,
|
||
|
}, nil
|
||
|
}
|