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"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/config"
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/hsm"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
signerConfigFile string
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
log.SetFlags(log.Ldate | log.Lmicroseconds | log.LUTC)
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
log.Printf("cacert-gosigner %s (%s) - built %s\n", version, commit, date)
|
|
|
|
|
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-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if showVersion {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := os.Open(signerConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open singer configuration file %s: %v", signerConfigFile, err)
|
|
|
|
}
|
|
|
|
|
2022-04-20 07:03:26 +00:00
|
|
|
opts := make([]hsm.ConfigOption, 0)
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
caConfig, err := config.LoadConfiguration(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not load CA hierarchy: %v", err)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
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 {
|
|
|
|
log.Print("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-04-20 07:03:26 +00:00
|
|
|
ctx := hsm.SetupContext(opts...)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
err = hsm.EnsureCAKeysAndCertificates(ctx)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not ensure CA keys and certificates exist: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if setupMode {
|
|
|
|
return
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
log.Print("setup complete, starting signer operation")
|
2022-04-13 06:30:20 +00:00
|
|
|
}
|