main.go 6.7 KB

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