main.go 5.8 KB

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