main.go 7.1 KB

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