cacert-gosigner/cmd/signer/main.go
Jan Dittberner 3107ad8abb Implement serial link and protocol handling infrastructure
This commit adds basic serial link and protocol support. None of the commands
from the docs/design.md document is implemented yet.

The following new packages have been added:

- seriallink containing the serial link handler including COBS decoding and
  encoding
- protocol containing the protocol handler including msgpack unmarshalling
  and marshaling
- health containing a rudimentary health check implementation
- messages containing command and response types and generated msgpack
  marshaling code

A client simulation command has been added in cmd/clientsim.

README.md got instructions how to run the client simulator. The
docs/config.sample.yaml contains a new section for the serial connection
parameters.
2022-08-03 14:38:36 +02:00

122 lines
3.1 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 main
import (
"flag"
"log"
"os"
"git.cacert.org/cacert-gosigner/pkg/config"
"git.cacert.org/cacert-gosigner/pkg/health"
"git.cacert.org/cacert-gosigner/pkg/hsm"
"git.cacert.org/cacert-gosigner/pkg/protocol"
"git.cacert.org/cacert-gosigner/pkg/seriallink"
)
var (
commit string
date string
version string
)
const (
defaultSignerConfigFile = "config.yaml"
)
func main() {
var (
showVersion, setupMode, verbose bool
signerConfigFile string
infoLog, errorLog *log.Logger
)
infoLog = log.New(os.Stdout, "INFO ", log.Ldate|log.Lmicroseconds|log.LUTC)
errorLog = log.New(os.Stderr, "ERROR ", log.Ldate|log.Lmicroseconds|log.LUTC)
infoLog.Printf("cacert-gosigner %s (%s) - built %s\n", version, commit, date)
flag.StringVar(&signerConfigFile, "config", defaultSignerConfigFile, "signer configuration file")
flag.BoolVar(&showVersion, "version", false, "show version")
flag.BoolVar(&setupMode, "setup", false, "setup mode")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.Parse()
if showVersion {
return
}
configFile, err := os.Open(signerConfigFile)
if err != nil {
errorLog.Fatalf("could not open signer configuration file %s: %v", signerConfigFile, err)
}
opts := make([]hsm.ConfigOption, 0)
caConfig, err := config.LoadConfiguration(configFile)
if err != nil {
errorLog.Fatalf("could not load CA hierarchy: %v", err)
}
opts = append(opts, hsm.CaConfigOption(caConfig))
if setupMode {
infoLog.Print("running in setup mode")
opts = append(opts, hsm.SetupModeOption())
}
if verbose {
opts = append(opts, hsm.VerboseLoggingOption())
}
acc, err := hsm.NewAccess(infoLog, opts...)
if err != nil {
errorLog.Fatalf("could not setup HSM access: %v", err)
}
err = acc.EnsureCAKeysAndCertificates()
if err != nil {
errorLog.Fatalf("could not ensure CA keys and certificates exist: %v", err)
}
if setupMode {
return
}
healthHandler := health.New(version)
proto, err := protocol.New(infoLog, errorLog, protocol.RegisterHealthHandler(healthHandler))
if err != nil {
errorLog.Fatalf("could not setup protocol handler: %v", err)
}
serialHandler, err := seriallink.New(caConfig.GetSerial(), proto)
if err != nil {
errorLog.Fatalf("could not setup serial link handler: %v", err)
}
defer func() { _ = serialHandler.Close() }()
if err = serialHandler.Run(); err != nil {
errorLog.Fatalf("error in serial handler: %v", err)
}
infoLog.Print("setup complete, starting signer operation")
}