app.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package commands
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "unicode"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/certificates/authority/config"
  14. "github.com/smallstep/certificates/ca"
  15. "github.com/smallstep/certificates/pki"
  16. "github.com/urfave/cli"
  17. "go.step.sm/cli-utils/errs"
  18. )
  19. // AppCommand is the action used as the top action.
  20. var AppCommand = cli.Command{
  21. Name: "start",
  22. Action: appAction,
  23. UsageText: `**step-ca** <config>
  24. [**--password-file**=<file>] [**--issuer-password-file**=<file>] [**--resolver**=<addr>]`,
  25. Flags: []cli.Flag{
  26. cli.StringFlag{
  27. Name: "password-file",
  28. Usage: `path to the <file> containing the password to decrypt the
  29. intermediate private key.`,
  30. },
  31. cli.StringFlag{
  32. Name: "issuer-password-file",
  33. Usage: `path to the <file> containing the password to decrypt the
  34. certificate issuer private key used in the RA mode.`,
  35. },
  36. cli.StringFlag{
  37. Name: "resolver",
  38. Usage: "address of a DNS resolver to be used instead of the default.",
  39. },
  40. cli.StringFlag{
  41. Name: "token",
  42. Usage: "token used to enable the linked ca.",
  43. EnvVar: "STEP_CA_TOKEN",
  44. },
  45. },
  46. }
  47. // AppAction is the action used when the top command runs.
  48. func appAction(ctx *cli.Context) error {
  49. passFile := ctx.String("password-file")
  50. issuerPassFile := ctx.String("issuer-password-file")
  51. resolver := ctx.String("resolver")
  52. token := ctx.String("token")
  53. // If zero cmd line args show help, if >1 cmd line args show error.
  54. if ctx.NArg() == 0 {
  55. return cli.ShowAppHelp(ctx)
  56. }
  57. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  58. return err
  59. }
  60. configFile := ctx.Args().Get(0)
  61. config, err := config.LoadConfiguration(configFile)
  62. if err != nil {
  63. fatal(err)
  64. }
  65. if config.AuthorityConfig != nil {
  66. if token == "" && strings.EqualFold(config.AuthorityConfig.DeploymentType, pki.LinkedDeployment.String()) {
  67. return errors.New(`'step-ca' requires the '--token' flag for linked deploy type.
  68. To get a linked authority token:
  69. 1. Log in or create a Certificate Manager account at ` + "\033[1mhttps://u.step.sm/linked\033[0m" + `
  70. 2. Add a new authority and select "Link a step-ca instance"
  71. 3. Follow instructions in browser to start 'step-ca' using the '--token' flag
  72. `)
  73. }
  74. }
  75. var password []byte
  76. if passFile != "" {
  77. if password, err = ioutil.ReadFile(passFile); err != nil {
  78. fatal(errors.Wrapf(err, "error reading %s", passFile))
  79. }
  80. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  81. }
  82. var issuerPassword []byte
  83. if issuerPassFile != "" {
  84. if issuerPassword, err = ioutil.ReadFile(issuerPassFile); err != nil {
  85. fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
  86. }
  87. issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
  88. }
  89. // replace resolver if requested
  90. if resolver != "" {
  91. net.DefaultResolver.PreferGo = true
  92. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  93. return net.Dial(network, resolver)
  94. }
  95. }
  96. srv, err := ca.New(config,
  97. ca.WithConfigFile(configFile),
  98. ca.WithPassword(password),
  99. ca.WithIssuerPassword(issuerPassword),
  100. ca.WithLinkedCAToken(token))
  101. if err != nil {
  102. fatal(err)
  103. }
  104. go ca.StopReloaderHandler(srv)
  105. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  106. fatal(err)
  107. }
  108. return nil
  109. }
  110. // fatal writes the passed error on the standard error and exits with the exit
  111. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  112. // stack trace of the error.
  113. func fatal(err error) {
  114. if os.Getenv("STEPDEBUG") == "1" {
  115. fmt.Fprintf(os.Stderr, "%+v\n", err)
  116. } else {
  117. fmt.Fprintln(os.Stderr, err)
  118. }
  119. os.Exit(2)
  120. }