main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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>]
  99. [**--ssh-host-password-file**=<file>] [**--ssh-user-password-file**=<file>]
  100. [**--issuer-password-file**=<file>] [**--resolver**=<addr>] [**--help**] [**--version**]`
  101. app.Description = `**step-ca** runs the Step Online Certificate Authority
  102. (Step CA) using the given configuration.
  103. See the README.md for more detailed configuration documentation.
  104. ## POSITIONAL ARGUMENTS
  105. <config>
  106. : File that configures the operation of the Step CA; this file is generated
  107. when you initialize the Step CA using 'step ca init'
  108. ## EXIT CODES
  109. This command will run indefinitely on success and return \>0 if any error occurs.
  110. ## EXAMPLES
  111. These examples assume that you have already initialized your PKI by running
  112. 'step ca init'. If you have not completed this step please see the 'Getting Started'
  113. section of the README.
  114. Run the Step CA and prompt for password:
  115. '''
  116. $ step-ca $STEPPATH/config/ca.json
  117. '''
  118. Run the Step CA and read the password from a file - this is useful for
  119. automating deployment:
  120. '''
  121. $ step-ca $STEPPATH/config/ca.json --password-file ./password.txt
  122. '''`
  123. app.Flags = append(app.Flags, commands.AppCommand.Flags...)
  124. app.Flags = append(app.Flags, cli.HelpFlag)
  125. app.Copyright = "(c) 2018-2020 Smallstep Labs, Inc."
  126. // All non-successful output should be written to stderr
  127. app.Writer = os.Stdout
  128. app.ErrWriter = os.Stderr
  129. app.Commands = command.Retrieve()
  130. // Start the golang debug logger if environment variable is set.
  131. // See https://golang.org/pkg/net/http/pprof/
  132. debugProfAddr := os.Getenv("STEP_PROF_ADDR")
  133. if debugProfAddr != "" {
  134. go func() {
  135. log.Println(http.ListenAndServe(debugProfAddr, nil))
  136. }()
  137. }
  138. app.Action = func(_ *cli.Context) error {
  139. // Hack to be able to run a the top action as a subcommand
  140. set := flag.NewFlagSet(app.Name, flag.ContinueOnError)
  141. set.Parse(os.Args)
  142. ctx := cli.NewContext(app, set, nil)
  143. return commands.AppCommand.Run(ctx)
  144. }
  145. if err := app.Run(os.Args); err != nil {
  146. if os.Getenv("STEPDEBUG") == "1" {
  147. fmt.Fprintf(os.Stderr, "%+v\n", err)
  148. } else {
  149. fmt.Fprintln(os.Stderr, err)
  150. }
  151. os.Exit(1)
  152. }
  153. }
  154. func flagValue(f cli.Flag) reflect.Value {
  155. fv := reflect.ValueOf(f)
  156. for fv.Kind() == reflect.Ptr {
  157. fv = reflect.Indirect(fv)
  158. }
  159. return fv
  160. }
  161. var placeholderString = regexp.MustCompile(`<.*?>`)
  162. func stringifyFlag(f cli.Flag) string {
  163. fv := flagValue(f)
  164. usage := fv.FieldByName("Usage").String()
  165. placeholder := placeholderString.FindString(usage)
  166. if placeholder == "" {
  167. switch f.(type) {
  168. case cli.BoolFlag, cli.BoolTFlag:
  169. default:
  170. placeholder = "<value>"
  171. }
  172. }
  173. return cli.FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder) + "\t" + usage
  174. }