main.go 7.0 KB

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