main.go 5.6 KB

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