main.go 7.0 KB

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