app.go 6.1 KB

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