app.go 4.9 KB

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