app.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package commands
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "os"
  10. "unicode"
  11. "github.com/pkg/errors"
  12. "github.com/smallstep/certificates/authority"
  13. "github.com/smallstep/certificates/ca"
  14. "github.com/smallstep/cli/errs"
  15. "github.com/urfave/cli"
  16. )
  17. // AppCommand is the action used as the top action.
  18. var AppCommand = cli.Command{
  19. Name: "start",
  20. Action: appAction,
  21. UsageText: `**step-ca** <config>
  22. [**--password-file**=<file>]
  23. [**--resolver**=<addr>]`,
  24. Flags: []cli.Flag{
  25. cli.StringFlag{
  26. Name: "password-file",
  27. Usage: `path to the <file> containing the password to decrypt the
  28. intermediate private key.`,
  29. },
  30. cli.StringFlag{
  31. Name: "resolver",
  32. Usage: "address of a DNS resolver to be used instead of the default.",
  33. },
  34. cli.IntFlag{
  35. Name: "ordinal",
  36. Usage: `Unique <index> identifying this instance of step-ca in a highly-
  37. available (replicated) deployment.`,
  38. },
  39. },
  40. }
  41. // AppAction is the action used when the top command runs.
  42. func appAction(ctx *cli.Context) error {
  43. passFile := ctx.String("password-file")
  44. resolver := ctx.String("resolver")
  45. // grab the ordinal or default to 0
  46. ordinal := ctx.Int("ordinal")
  47. // If zero cmd line args show help, if >1 cmd line args show error.
  48. if ctx.NArg() == 0 {
  49. return cli.ShowAppHelp(ctx)
  50. }
  51. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  52. return err
  53. }
  54. configFile := ctx.Args().Get(0)
  55. config, err := authority.LoadConfiguration(configFile)
  56. if err != nil {
  57. fatal(err)
  58. }
  59. var password []byte
  60. if passFile != "" {
  61. if password, err = ioutil.ReadFile(passFile); err != nil {
  62. fatal(errors.Wrapf(err, "error reading %s", passFile))
  63. }
  64. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  65. }
  66. // replace resolver if requested
  67. if resolver != "" {
  68. net.DefaultResolver.PreferGo = true
  69. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  70. return net.Dial(network, resolver)
  71. }
  72. }
  73. srv, err := ca.New(config, ca.WithConfigFile(configFile), ca.WithPassword(password), ca.WithOrdinal(ordinal))
  74. if err != nil {
  75. fatal(err)
  76. }
  77. go ca.StopReloaderHandler(srv)
  78. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  79. fatal(err)
  80. }
  81. return nil
  82. }
  83. // fatal writes the passed error on the standard error and exits with the exit
  84. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  85. // stack trace of the error.
  86. func fatal(err error) {
  87. if os.Getenv("STEPDEBUG") == "1" {
  88. fmt.Fprintf(os.Stderr, "%+v\n", err)
  89. } else {
  90. fmt.Fprintln(os.Stderr, err)
  91. }
  92. os.Exit(2)
  93. }