2022-04-24 07:25:04 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-04-13 06:30:20 +00:00
|
|
|
package main
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/config"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/health"
|
2022-04-16 20:24:32 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/hsm"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/seriallink"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
commit string
|
|
|
|
date string
|
|
|
|
version string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-19 14:48:32 +00:00
|
|
|
defaultSignerConfigFile = "config.yaml"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 06:30:20 +00:00
|
|
|
func main() {
|
2022-04-16 20:24:32 +00:00
|
|
|
var (
|
2022-04-20 07:03:26 +00:00
|
|
|
showVersion, setupMode, verbose bool
|
2022-11-20 09:07:02 +00:00
|
|
|
signerConfigFile, logLevel string
|
|
|
|
logger *logrus.Logger
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
logger = logrus.New()
|
|
|
|
logger.SetOutput(os.Stdout)
|
|
|
|
logger.SetLevel(logrus.InfoLevel)
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Infof("cacert-gosigner %s (%s) - built %s\n", version, commit, date)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-24 12:49:17 +00:00
|
|
|
flag.StringVar(&signerConfigFile, "config", defaultSignerConfigFile, "signer configuration file")
|
2022-04-16 20:24:32 +00:00
|
|
|
flag.BoolVar(&showVersion, "version", false, "show version")
|
2022-04-19 14:48:32 +00:00
|
|
|
flag.BoolVar(&setupMode, "setup", false, "setup mode")
|
2022-04-20 07:03:26 +00:00
|
|
|
flag.BoolVar(&verbose, "verbose", false, "verbose output")
|
2022-11-20 09:07:02 +00:00
|
|
|
flag.StringVar(&logLevel, "loglevel", "INFO", "log level")
|
2022-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if showVersion {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
parsedLevel, err := logrus.ParseLevel(logLevel)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not parse log level: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetLevel(parsedLevel)
|
|
|
|
|
|
|
|
caConfig := LoadConfig(signerConfigFile, logger)
|
2022-08-03 13:45:27 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
access := initializeHSM(caConfig, setupMode, verbose, logger)
|
2022-08-03 13:45:27 +00:00
|
|
|
|
|
|
|
if setupMode {
|
|
|
|
return
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
healthHandler := health.New(version, access)
|
2022-04-20 07:03:26 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
proto, err := protocol.New(logger, protocol.RegisterHealthHandler(healthHandler))
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not setup protocol handler: %v", err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
serialHandler, err := seriallink.New(caConfig.GetSerial(), proto)
|
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not setup serial link handler: %v", err)
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = serialHandler.Close() }()
|
|
|
|
|
|
|
|
if err = serialHandler.Run(); err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("error in serial handler: %v", err)
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Infof("setup complete, starting signer operation")
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
func initializeHSM(caConfig *config.SignerConfig, setupMode, verbose bool, logger *logrus.Logger) *hsm.Access {
|
2022-08-03 13:45:27 +00:00
|
|
|
opts := make([]hsm.ConfigOption, 0)
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
opts = append(opts, hsm.CaConfigOption(caConfig))
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if setupMode {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Infof("running in setup mode")
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
opts = append(opts, hsm.SetupModeOption())
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
opts = append(opts, hsm.VerboseLoggingOption())
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
access, err := hsm.NewAccess(logger, opts...)
|
2022-08-03 07:59:26 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not setup HSM access: %v", err)
|
2022-08-03 07:59:26 +00:00
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
err = access.EnsureCAKeysAndCertificates()
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not ensure CA keys and certificates exist: %v", err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
return access
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
func LoadConfig(signerConfigFile string, logger *logrus.Logger) *config.SignerConfig {
|
2022-08-03 13:45:27 +00:00
|
|
|
configFile, err := os.Open(signerConfigFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not open signer configuration file %s: %v", signerConfigFile, err)
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
caConfig, err := config.LoadConfiguration(configFile)
|
2022-08-03 12:38:36 +00:00
|
|
|
if err != nil {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.Fatalf("could not load CA hierarchy: %v", err)
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
return caConfig
|
2022-04-13 06:30:20 +00:00
|
|
|
}
|