main.go 5.9 KB

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