app.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. )
  18. // AppCommand is the action used as the top action.
  19. var AppCommand = cli.Command{
  20. Name: "start",
  21. Action: appAction,
  22. UsageText: `**step-ca** <config> [**--password-file**=<file>]
  23. [**--ssh-host-password-file**=<file>] [**--ssh-user-password-file**=<file>]
  24. [**--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: "ssh-host-password-file",
  33. Usage: `path to the <file> containing the password to decrypt the
  34. private key used to sign SSH host certificates. If the flag is not passed it
  35. will default to --password-file.`,
  36. },
  37. cli.StringFlag{
  38. Name: "ssh-user-password-file",
  39. Usage: `path to the <file> containing the password to decrypt the
  40. private key used to sign SSH user certificates. If the flag is not passed it
  41. will default to --password-file.`,
  42. },
  43. cli.StringFlag{
  44. Name: "issuer-password-file",
  45. Usage: `path to the <file> containing the password to decrypt the
  46. certificate issuer private key used in the RA mode.`,
  47. },
  48. cli.StringFlag{
  49. Name: "resolver",
  50. Usage: "address of a DNS resolver to be used instead of the default.",
  51. },
  52. cli.StringFlag{
  53. Name: "token",
  54. Usage: "token used to enable the linked ca.",
  55. EnvVar: "STEP_CA_TOKEN",
  56. },
  57. },
  58. }
  59. // AppAction is the action used when the top command runs.
  60. func appAction(ctx *cli.Context) error {
  61. passFile := ctx.String("password-file")
  62. sshHostPassFile := ctx.String("ssh-host-password-file")
  63. sshUserPassFile := ctx.String("ssh-user-password-file")
  64. issuerPassFile := ctx.String("issuer-password-file")
  65. resolver := ctx.String("resolver")
  66. token := ctx.String("token")
  67. // If zero cmd line args show help, if >1 cmd line args show error.
  68. if ctx.NArg() == 0 {
  69. return cli.ShowAppHelp(ctx)
  70. }
  71. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  72. return err
  73. }
  74. configFile := ctx.Args().Get(0)
  75. cfg, err := config.LoadConfiguration(configFile)
  76. if err != nil {
  77. fatal(err)
  78. }
  79. if cfg.AuthorityConfig != nil {
  80. if token == "" && strings.EqualFold(cfg.AuthorityConfig.DeploymentType, pki.LinkedDeployment.String()) {
  81. return errors.New(`'step-ca' requires the '--token' flag for linked deploy type.
  82. To get a linked authority token:
  83. 1. Log in or create a Certificate Manager account at ` + "\033[1mhttps://u.step.sm/linked\033[0m" + `
  84. 2. Add a new authority and select "Link a step-ca instance"
  85. 3. Follow instructions in browser to start 'step-ca' using the '--token' flag
  86. `)
  87. }
  88. }
  89. var password []byte
  90. if passFile != "" {
  91. if password, err = os.ReadFile(passFile); err != nil {
  92. fatal(errors.Wrapf(err, "error reading %s", passFile))
  93. }
  94. password = bytes.TrimRightFunc(password, unicode.IsSpace)
  95. }
  96. var sshHostPassword []byte
  97. if sshHostPassFile != "" {
  98. if sshHostPassword, err = os.ReadFile(sshHostPassFile); err != nil {
  99. fatal(errors.Wrapf(err, "error reading %s", sshHostPassFile))
  100. }
  101. sshHostPassword = bytes.TrimRightFunc(sshHostPassword, unicode.IsSpace)
  102. }
  103. var sshUserPassword []byte
  104. if sshUserPassFile != "" {
  105. if sshUserPassword, err = os.ReadFile(sshUserPassFile); err != nil {
  106. fatal(errors.Wrapf(err, "error reading %s", sshUserPassFile))
  107. }
  108. sshUserPassword = bytes.TrimRightFunc(sshUserPassword, unicode.IsSpace)
  109. }
  110. var issuerPassword []byte
  111. if issuerPassFile != "" {
  112. if issuerPassword, err = os.ReadFile(issuerPassFile); err != nil {
  113. fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
  114. }
  115. issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
  116. }
  117. // replace resolver if requested
  118. if resolver != "" {
  119. net.DefaultResolver.PreferGo = true
  120. net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
  121. return net.Dial(network, resolver)
  122. }
  123. }
  124. srv, err := ca.New(cfg,
  125. ca.WithConfigFile(configFile),
  126. ca.WithPassword(password),
  127. ca.WithSSHHostPassword(sshHostPassword),
  128. ca.WithSSHUserPassword(sshUserPassword),
  129. ca.WithIssuerPassword(issuerPassword),
  130. ca.WithLinkedCAToken(token))
  131. if err != nil {
  132. fatal(err)
  133. }
  134. go ca.StopReloaderHandler(srv)
  135. if err = srv.Run(); err != nil && err != http.ErrServerClosed {
  136. fatal(err)
  137. }
  138. return nil
  139. }
  140. // fatal writes the passed error on the standard error and exits with the exit
  141. // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
  142. // stack trace of the error.
  143. func fatal(err error) {
  144. if os.Getenv("STEPDEBUG") == "1" {
  145. fmt.Fprintf(os.Stderr, "%+v\n", err)
  146. } else {
  147. fmt.Fprintln(os.Stderr, err)
  148. }
  149. os.Exit(2)
  150. }