app.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package commands
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "unicode"
  11. "github.com/pkg/errors"
  12. "github.com/smallstep/certificates/authority/config"
  13. "github.com/smallstep/certificates/ca"
  14. "github.com/smallstep/certificates/pki"
  15. "github.com/urfave/cli"
  16. "go.step.sm/cli-utils/errs"
  17. "go.step.sm/cli-utils/step"
  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> [**--password-file**=<file>]
  24. [**--ssh-host-password-file**=<file>] [**--ssh-user-password-file**=<file>]
  25. [**--issuer-password-file**=<file>] [**--resolver**=<addr>]`,
  26. Flags: []cli.Flag{
  27. cli.StringFlag{
  28. Name: "password-file",
  29. Usage: `path to the <file> containing the password to decrypt the
  30. intermediate private key.`,
  31. },
  32. cli.StringFlag{
  33. Name: "ssh-host-password-file",
  34. Usage: `path to the <file> containing the password to decrypt the
  35. private key used to sign SSH host certificates. If the flag is not passed it
  36. will default to --password-file.`,
  37. },
  38. cli.StringFlag{
  39. Name: "ssh-user-password-file",
  40. Usage: `path to the <file> containing the password to decrypt the
  41. private key used to sign SSH user certificates. If the flag is not passed it
  42. will default to --password-file.`,
  43. },
  44. cli.StringFlag{
  45. Name: "issuer-password-file",
  46. Usage: `path to the <file> containing the password to decrypt the
  47. certificate issuer private key used in the RA mode.`,
  48. },
  49. cli.StringFlag{
  50. Name: "resolver",
  51. Usage: "address of a DNS resolver to be used instead of the default.",
  52. },
  53. cli.StringFlag{
  54. Name: "token",
  55. Usage: "token used to enable the linked ca.",
  56. EnvVar: "STEP_CA_TOKEN",
  57. },
  58. cli.BoolFlag{
  59. Name: "quiet",
  60. Usage: "disable startup information",
  61. EnvVar: "STEP_CA_QUIET",
  62. },
  63. cli.StringFlag{
  64. Name: "context",
  65. Usage: "The name of the authority's context.",
  66. EnvVar: "STEP_CA_CONTEXT",
  67. },
  68. },
  69. }
  70. // AppAction is the action used when the top command runs.
  71. func appAction(ctx *cli.Context) error {
  72. passFile := ctx.String("password-file")
  73. sshHostPassFile := ctx.String("ssh-host-password-file")
  74. sshUserPassFile := ctx.String("ssh-user-password-file")
  75. issuerPassFile := ctx.String("issuer-password-file")
  76. resolver := ctx.String("resolver")
  77. token := ctx.String("token")
  78. quiet := ctx.Bool("quiet")
  79. if ctx.NArg() > 1 {
  80. return errs.TooManyArguments(ctx)
  81. }
  82. if caCtx := ctx.String("context"); caCtx != "" {
  83. if err := step.Contexts().SetCurrent(caCtx); err != nil {
  84. return err
  85. }
  86. }
  87. var configFile string
  88. if ctx.NArg() > 0 {
  89. configFile = ctx.Args().Get(0)
  90. } else {
  91. configFile = step.CaConfigFile()
  92. }
  93. cfg, err := config.LoadConfiguration(configFile)
  94. if err != nil {
  95. fatal(err)
  96. }
  97. if cfg.AuthorityConfig != nil {
  98. if token == "" && strings.EqualFold(cfg.AuthorityConfig.DeploymentType, pki.LinkedDeployment.String()) {
  99. return errors.New(`'step-ca' requires the '--token' flag for linked deploy type.
  100. To get a linked authority token:
  101. 1. Log in or create a Certificate Manager account at ` + "\033[1mhttps://u.step.sm/linked\033[0m" + `
  102. 2. Add a new authority and select "Link a step-ca instance"
  103. 3. Follow instructions in browser to start 'step-ca' using the '--token' flag
  104. `)
  105. }
  106. }
  107. var password []byte
  108. if passFile != "" {
  109. if password, err = os.ReadFile(passFile); err != nil {
  110. fatal(errors.Wrapf(err, "error reading %s", passFile))
  111. }
  112. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  113. }
  114. var sshHostPassword []byte
  115. if sshHostPassFile != "" {
  116. if sshHostPassword, err = os.ReadFile(sshHostPassFile); err != nil {
  117. fatal(errors.Wrapf(err, "error reading %s", sshHostPassFile))
  118. }
  119. sshHostPassword = bytes.TrimRightFunc(sshHostPassword, unicode.IsSpace)
  120. }
  121. var sshUserPassword []byte
  122. if sshUserPassFile != "" {
  123. if sshUserPassword, err = os.ReadFile(sshUserPassFile); err != nil {
  124. fatal(errors.Wrapf(err, "error reading %s", sshUserPassFile))
  125. }
  126. sshUserPassword = bytes.TrimRightFunc(sshUserPassword, unicode.IsSpace)
  127. }
  128. var issuerPassword []byte
  129. if issuerPassFile != "" {
  130. if issuerPassword, err = os.ReadFile(issuerPassFile); err != nil {
  131. fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
  132. }
  133. issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
  134. }
  135. // replace resolver if requested
  136. if resolver != "" {
  137. net.DefaultResolver.PreferGo = true
  138. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  139. return net.Dial(network, resolver)
  140. }
  141. }
  142. srv, err := ca.New(cfg,
  143. ca.WithConfigFile(configFile),
  144. ca.WithPassword(password),
  145. ca.WithSSHHostPassword(sshHostPassword),
  146. ca.WithSSHUserPassword(sshUserPassword),
  147. ca.WithIssuerPassword(issuerPassword),
  148. ca.WithLinkedCAToken(token),
  149. ca.WithQuiet(quiet))
  150. if err != nil {
  151. fatal(err)
  152. }
  153. go ca.StopReloaderHandler(srv)
  154. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  155. fatal(err)
  156. }
  157. return nil
  158. }
  159. // fatal writes the passed error on the standard error and exits with the exit
  160. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  161. // stack trace of the error.
  162. func fatal(err error) {
  163. if os.Getenv("STEPDEBUG") == "1" {
  164. fmt.Fprintf(os.Stderr, "%+v\n", err)
  165. } else {
  166. fmt.Fprintln(os.Stderr, err)
  167. }
  168. os.Exit(2)
  169. }