main.go 5.2 KB

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