main.go 5.2 KB

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