4
0

main.go 5.3 KB

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