main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path"
  10. "unicode"
  11. "github.com/pkg/errors"
  12. "github.com/smallstep/certificates/authority"
  13. "github.com/smallstep/certificates/ca"
  14. )
  15. func usage() {
  16. fmt.Fprintf(os.Stderr, "Usage: %s [options] <config.json>\n\n", path.Base(os.Args[0]))
  17. flag.PrintDefaults()
  18. }
  19. func main() {
  20. var configFile, passFile string
  21. flag.StringVar(&passFile, "password-file", "", "Path to file containing a password")
  22. flag.Usage = usage
  23. flag.Parse()
  24. if flag.NArg() != 1 {
  25. flag.Usage()
  26. os.Exit(1)
  27. }
  28. configFile = flag.Arg(0)
  29. config, err := authority.LoadConfiguration(configFile)
  30. if err != nil {
  31. fatal(err)
  32. }
  33. var password []byte
  34. if passFile != "" {
  35. if password, err = ioutil.ReadFile(passFile); err != nil {
  36. fatal(errors.Wrapf(err, "error reading %s", passFile))
  37. }
  38. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  39. }
  40. srv, err := ca.New(config, ca.WithConfigFile(configFile), ca.WithPassword(password))
  41. if err != nil {
  42. fatal(err)
  43. }
  44. go ca.StopReloaderHandler(srv)
  45. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  46. fatal(err)
  47. }
  48. }
  49. // fatal writes the passed error on the standard error and exits with the exit
  50. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  51. // stack trace of the error.
  52. func fatal(err error) {
  53. if os.Getenv("STEPDEBUG") == "1" {
  54. fmt.Fprintf(os.Stderr, "%+v\n", err)
  55. } else {
  56. fmt.Fprintln(os.Stderr, err)
  57. }
  58. os.Exit(2)
  59. }