app.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/config"
  13. "github.com/smallstep/certificates/ca"
  14. "github.com/urfave/cli"
  15. "go.step.sm/cli-utils/errs"
  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>] [**--issuer-password-file**=<file>] [**--resolver**=<addr>]`,
  23. Flags: []cli.Flag{
  24. cli.StringFlag{
  25. Name: "password-file",
  26. Usage: `path to the <file> containing the password to decrypt the
  27. intermediate private key.`,
  28. },
  29. cli.StringFlag{
  30. Name: "issuer-password-file",
  31. Usage: `path to the <file> containing the password to decrypt the
  32. certificate issuer private key used in the RA mode.`,
  33. },
  34. cli.StringFlag{
  35. Name: "resolver",
  36. Usage: "address of a DNS resolver to be used instead of the default.",
  37. },
  38. },
  39. }
  40. // AppAction is the action used when the top command runs.
  41. func appAction(ctx *cli.Context) error {
  42. passFile := ctx.String("password-file")
  43. issuerPassFile := ctx.String("issuer-password-file")
  44. resolver := ctx.String("resolver")
  45. // If zero cmd line args show help, if >1 cmd line args show error.
  46. if ctx.NArg() == 0 {
  47. return cli.ShowAppHelp(ctx)
  48. }
  49. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  50. return err
  51. }
  52. configFile := ctx.Args().Get(0)
  53. config, err := config.LoadConfiguration(configFile)
  54. if err != nil {
  55. fatal(err)
  56. }
  57. var password []byte
  58. if passFile != "" {
  59. if password, err = ioutil.ReadFile(passFile); err != nil {
  60. fatal(errors.Wrapf(err, "error reading %s", passFile))
  61. }
  62. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  63. }
  64. var issuerPassword []byte
  65. if issuerPassFile != "" {
  66. if issuerPassword, err = ioutil.ReadFile(issuerPassFile); err != nil {
  67. fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
  68. }
  69. issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
  70. }
  71. // replace resolver if requested
  72. if resolver != "" {
  73. net.DefaultResolver.PreferGo = true
  74. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  75. return net.Dial(network, resolver)
  76. }
  77. }
  78. srv, err := ca.New(config,
  79. ca.WithConfigFile(configFile),
  80. ca.WithPassword(password),
  81. ca.WithIssuerPassword(issuerPassword))
  82. if err != nil {
  83. fatal(err)
  84. }
  85. go ca.StopReloaderHandler(srv)
  86. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  87. fatal(err)
  88. }
  89. return nil
  90. }
  91. // fatal writes the passed error on the standard error and exits with the exit
  92. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  93. // stack trace of the error.
  94. func fatal(err error) {
  95. if os.Getenv("STEPDEBUG") == "1" {
  96. fmt.Fprintf(os.Stderr, "%+v\n", err)
  97. } else {
  98. fmt.Fprintln(os.Stderr, err)
  99. }
  100. os.Exit(2)
  101. }