certificates/cmd/step-ca/main.go

99 lines
2.2 KiB
Go
Raw Normal View History

2018-10-05 21:48:36 +00:00
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
2018-11-08 19:45:19 +00:00
"runtime"
"time"
2018-10-05 21:48:36 +00:00
"unicode"
"github.com/pkg/errors"
"github.com/smallstep/certificates/authority"
"github.com/smallstep/certificates/ca"
2018-10-05 21:48:36 +00:00
)
2018-11-08 19:45:19 +00:00
// Version is set by an LDFLAG at build time representing the git tag or commit
// for the current release
var Version = "N/A"
// BuildTime is set by an LDFLAG at build time representing the timestamp at
// the time of build
var BuildTime = "N/A"
2018-10-05 21:48:36 +00:00
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <config.json>\n\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
2018-11-08 19:45:19 +00:00
func printVersion() {
version, buildTime := Version, BuildTime
if version == "N/A" {
version = "0000000-dev"
}
if buildTime == "N/A" {
buildTime = time.Now().UTC().Format("2006-01-02 15:04 MST")
}
fmt.Printf("Smallstep CA/%s (%s/%s)\n", version, runtime.GOOS, runtime.GOARCH)
fmt.Printf("Release Date: %s\n", buildTime)
}
2018-10-05 21:48:36 +00:00
func main() {
2018-11-08 19:45:19 +00:00
var version bool
2018-10-05 21:48:36 +00:00
var configFile, passFile string
2018-11-08 19:45:19 +00:00
flag.StringVar(&passFile, "password-file", "", "path to file containing a password")
flag.BoolVar(&version, "version", false, "print version and exit")
2018-10-05 21:48:36 +00:00
flag.Usage = usage
flag.Parse()
2018-11-08 19:45:19 +00:00
if version {
printVersion()
os.Exit(0)
}
2018-10-05 21:48:36 +00:00
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
configFile = flag.Arg(0)
config, err := authority.LoadConfiguration(configFile)
if err != nil {
fatal(err)
}
var password []byte
if passFile != "" {
if password, err = ioutil.ReadFile(passFile); err != nil {
fatal(errors.Wrapf(err, "error reading %s", passFile))
}
password = bytes.TrimRightFunc(password, unicode.IsSpace)
}
srv, err := ca.New(config, ca.WithConfigFile(configFile), ca.WithPassword(password))
if err != nil {
fatal(err)
}
go ca.StopReloaderHandler(srv)
if err = srv.Run(); err != nil && err != http.ErrServerClosed {
fatal(err)
}
}
// fatal writes the passed error on the standard error and exits with the exit
// code 1. If the environment variable STEPDEBUG is set to 1 it shows the
// stack trace of the error.
func fatal(err error) {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(2)
}