main.go 6.0 KB

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