app.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.StringFlag{
  59. Name: "context",
  60. Usage: "The name of the authority's context.",
  61. EnvVar: "STEP_CA_CONTEXT",
  62. },
  63. },
  64. }
  65. // AppAction is the action used when the top command runs.
  66. func appAction(ctx *cli.Context) error {
  67. passFile := ctx.String("password-file")
  68. sshHostPassFile := ctx.String("ssh-host-password-file")
  69. sshUserPassFile := ctx.String("ssh-user-password-file")
  70. issuerPassFile := ctx.String("issuer-password-file")
  71. resolver := ctx.String("resolver")
  72. token := ctx.String("token")
  73. if ctx.NArg() > 1 {
  74. return errs.TooManyArguments(ctx)
  75. }
  76. if caCtx := ctx.String("context"); caCtx != "" {
  77. if err := step.Contexts().SetCurrent(caCtx); err != nil {
  78. return err
  79. }
  80. }
  81. var configFile string
  82. if ctx.NArg() > 0 {
  83. configFile = ctx.Args().Get(0)
  84. } else {
  85. configFile = step.CaConfigFile()
  86. }
  87. cfg, err := config.LoadConfiguration(configFile)
  88. if err != nil {
  89. fatal(err)
  90. }
  91. if cfg.AuthorityConfig != nil {
  92. if token == "" && strings.EqualFold(cfg.AuthorityConfig.DeploymentType, pki.LinkedDeployment.String()) {
  93. return errors.New(`'step-ca' requires the '--token' flag for linked deploy type.
  94. To get a linked authority token:
  95. 1. Log in or create a Certificate Manager account at ` + "\033[1mhttps://u.step.sm/linked\033[0m" + `
  96. 2. Add a new authority and select "Link a step-ca instance"
  97. 3. Follow instructions in browser to start 'step-ca' using the '--token' flag
  98. `)
  99. }
  100. }
  101. var password []byte
  102. if passFile != "" {
  103. if password, err = os.ReadFile(passFile); err != nil {
  104. fatal(errors.Wrapf(err, "error reading %s", passFile))
  105. }
  106. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  107. }
  108. var sshHostPassword []byte
  109. if sshHostPassFile != "" {
  110. if sshHostPassword, err = os.ReadFile(sshHostPassFile); err != nil {
  111. fatal(errors.Wrapf(err, "error reading %s", sshHostPassFile))
  112. }
  113. sshHostPassword = bytes.TrimRightFunc(sshHostPassword, unicode.IsSpace)
  114. }
  115. var sshUserPassword []byte
  116. if sshUserPassFile != "" {
  117. if sshUserPassword, err = os.ReadFile(sshUserPassFile); err != nil {
  118. fatal(errors.Wrapf(err, "error reading %s", sshUserPassFile))
  119. }
  120. sshUserPassword = bytes.TrimRightFunc(sshUserPassword, unicode.IsSpace)
  121. }
  122. var issuerPassword []byte
  123. if issuerPassFile != "" {
  124. if issuerPassword, err = os.ReadFile(issuerPassFile); err != nil {
  125. fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
  126. }
  127. issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
  128. }
  129. // replace resolver if requested
  130. if resolver != "" {
  131. net.DefaultResolver.PreferGo = true
  132. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  133. return net.Dial(network, resolver)
  134. }
  135. }
  136. srv, err := ca.New(cfg,
  137. ca.WithConfigFile(configFile),
  138. ca.WithPassword(password),
  139. ca.WithSSHHostPassword(sshHostPassword),
  140. ca.WithSSHUserPassword(sshUserPassword),
  141. ca.WithIssuerPassword(issuerPassword),
  142. ca.WithLinkedCAToken(token))
  143. if err != nil {
  144. fatal(err)
  145. }
  146. go ca.StopReloaderHandler(srv)
  147. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  148. fatal(err)
  149. }
  150. return nil
  151. }
  152. // fatal writes the passed error on the standard error and exits with the exit
  153. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  154. // stack trace of the error.
  155. func fatal(err error) {
  156. if os.Getenv("STEPDEBUG") == "1" {
  157. fmt.Fprintf(os.Stderr, "%+v\n", err)
  158. } else {
  159. fmt.Fprintln(os.Stderr, err)
  160. }
  161. os.Exit(2)
  162. }