main.go 6.2 KB

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