main.go 5.7 KB

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