main.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "html"
  6. "log"
  7. "math/rand"
  8. "net/http"
  9. "os"
  10. "reflect"
  11. "regexp"
  12. "strconv"
  13. "time"
  14. // Server profiler
  15. _ "net/http/pprof"
  16. "github.com/smallstep/certificates/authority"
  17. "github.com/smallstep/certificates/commands"
  18. "github.com/urfave/cli"
  19. "go.step.sm/cli-utils/command"
  20. "go.step.sm/cli-utils/command/version"
  21. "go.step.sm/cli-utils/config"
  22. "go.step.sm/cli-utils/usage"
  23. // Enabled kms interfaces.
  24. _ "github.com/smallstep/certificates/kms/awskms"
  25. _ "github.com/smallstep/certificates/kms/cloudkms"
  26. _ "github.com/smallstep/certificates/kms/softkms"
  27. _ "github.com/smallstep/certificates/kms/sshagentkms"
  28. // Experimental kms interfaces.
  29. _ "github.com/smallstep/certificates/kms/pkcs11"
  30. _ "github.com/smallstep/certificates/kms/yubikey"
  31. // Enabled cas interfaces.
  32. _ "github.com/smallstep/certificates/cas/cloudcas"
  33. _ "github.com/smallstep/certificates/cas/softcas"
  34. )
  35. // commit and buildTime are filled in during build by the Makefile
  36. var (
  37. BuildTime = "N/A"
  38. Version = "N/A"
  39. )
  40. func init() {
  41. config.Set("Smallstep CA", Version, BuildTime)
  42. authority.GlobalVersion.Version = Version
  43. rand.Seed(time.Now().UnixNano())
  44. }
  45. // appHelpTemplate contains the modified template for the main app
  46. var appHelpTemplate = `## NAME
  47. **{{.HelpName}}** -- {{.Usage}}
  48. ## USAGE
  49. {{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .Commands}} <command>{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
  50. ## DESCRIPTION
  51. {{.Description}}{{end}}{{if .VisibleCommands}}
  52. ## COMMANDS
  53. {{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
  54. |||
  55. |---|---|{{range .VisibleCommands}}
  56. | **{{join .Names ", "}}** | {{.Usage}} |{{end}}
  57. {{end}}{{if .VisibleFlags}}{{end}}
  58. ## OPTIONS
  59. {{range $index, $option := .VisibleFlags}}{{if $index}}
  60. {{end}}{{$option}}
  61. {{end}}{{end}}{{if .Copyright}}{{if len .Authors}}
  62. ## AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
  63. {{range $index, $author := .Authors}}{{if $index}}
  64. {{end}}{{$author}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
  65. ## ONLINE
  66. This documentation is available online at https://smallstep.com/docs/certificates
  67. ## VERSION
  68. {{.Version}}{{end}}{{end}}
  69. ## COPYRIGHT
  70. {{.Copyright}}
  71. ## FEEDBACK ` +
  72. html.UnescapeString("&#"+strconv.Itoa(128525)+";") + " " +
  73. html.UnescapeString("&#"+strconv.Itoa(127867)+";") +
  74. `
  75. The **step-ca** utility is not instrumented for usage statistics. It does not phone home.
  76. But your feedback is extremely valuable. Any information you can provide regarding how you’re using **step-ca** helps.
  77. Please send us a sentence or two, good or bad: **feedback@smallstep.com** or https://github.com/smallstep/certificates/discussions.
  78. {{end}}
  79. `
  80. func main() {
  81. // Override global framework components
  82. cli.VersionPrinter = func(c *cli.Context) {
  83. version.Command(c)
  84. }
  85. cli.AppHelpTemplate = appHelpTemplate
  86. cli.SubcommandHelpTemplate = usage.SubcommandHelpTemplate
  87. cli.CommandHelpTemplate = usage.CommandHelpTemplate
  88. cli.HelpPrinter = usage.HelpPrinter
  89. cli.FlagNamePrefixer = usage.FlagNamePrefixer
  90. cli.FlagStringer = stringifyFlag
  91. // Configure cli app
  92. app := cli.NewApp()
  93. app.Name = "step-ca"
  94. app.HelpName = "step-ca"
  95. app.Version = config.Version()
  96. app.Usage = "an online certificate authority for secure automated certificate management"
  97. app.UsageText = `**step-ca** <config> [**--password-file**=<file>] [**--resolver**=<addr>] [**--help**] [**--version**]`
  98. app.Description = `**step-ca** runs the Step Online Certificate Authority
  99. (Step CA) using the given configuration.
  100. See the README.md for more detailed configuration documentation.
  101. ## POSITIONAL ARGUMENTS
  102. <config>
  103. : File that configures the operation of the Step CA; this file is generated
  104. when you initialize the Step CA using 'step ca init'
  105. ## EXIT CODES
  106. This command will run indefinitely on success and return \>0 if any error occurs.
  107. ## EXAMPLES
  108. These examples assume that you have already initialized your PKI by running
  109. 'step ca init'. If you have not completed this step please see the 'Getting Started'
  110. section of the README.
  111. Run the Step CA and prompt for password:
  112. '''
  113. $ step-ca $STEPPATH/config/ca.json
  114. '''
  115. Run the Step CA and read the password from a file - this is useful for
  116. automating deployment:
  117. '''
  118. $ step-ca $STEPPATH/config/ca.json --password-file ./password.txt
  119. '''`
  120. app.Flags = append(app.Flags, commands.AppCommand.Flags...)
  121. app.Flags = append(app.Flags, cli.HelpFlag)
  122. app.Copyright = "(c) 2018-2020 Smallstep Labs, Inc."
  123. // All non-successful output should be written to stderr
  124. app.Writer = os.Stdout
  125. app.ErrWriter = os.Stderr
  126. app.Commands = command.Retrieve()
  127. // Start the golang debug logger if environment variable is set.
  128. // See https://golang.org/pkg/net/http/pprof/
  129. debugProfAddr := os.Getenv("STEP_PROF_ADDR")
  130. if debugProfAddr != "" {
  131. go func() {
  132. log.Println(http.ListenAndServe(debugProfAddr, nil))
  133. }()
  134. }
  135. app.Action = func(_ *cli.Context) error {
  136. // Hack to be able to run a the top action as a subcommand
  137. set := flag.NewFlagSet(app.Name, flag.ContinueOnError)
  138. set.Parse(os.Args)
  139. ctx := cli.NewContext(app, set, nil)
  140. return commands.AppCommand.Run(ctx)
  141. }
  142. if err := app.Run(os.Args); err != nil {
  143. if os.Getenv("STEPDEBUG") == "1" {
  144. fmt.Fprintf(os.Stderr, "%+v\n", err)
  145. } else {
  146. fmt.Fprintln(os.Stderr, err)
  147. }
  148. os.Exit(1)
  149. }
  150. }
  151. func flagValue(f cli.Flag) reflect.Value {
  152. fv := reflect.ValueOf(f)
  153. for fv.Kind() == reflect.Ptr {
  154. fv = reflect.Indirect(fv)
  155. }
  156. return fv
  157. }
  158. var placeholderString = regexp.MustCompile(`<.*?>`)
  159. func stringifyFlag(f cli.Flag) string {
  160. fv := flagValue(f)
  161. usage := fv.FieldByName("Usage").String()
  162. placeholder := placeholderString.FindString(usage)
  163. if placeholder == "" {
  164. switch f.(type) {
  165. case cli.BoolFlag, cli.BoolTFlag:
  166. default:
  167. placeholder = "<value>"
  168. }
  169. }
  170. return cli.FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder) + "\t" + usage
  171. }