main.go 6.1 KB

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