cfssl-bundle.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "github.com/cloudflare/cfssl/cli"
  7. "github.com/cloudflare/cfssl/cli/bundle"
  8. "github.com/cloudflare/cfssl/config"
  9. )
  10. // main defines the bundle usage and registers all defined commands and flags.
  11. func main() {
  12. var bundleFlagSet = flag.NewFlagSet("bundle", flag.ExitOnError)
  13. var c cli.Config
  14. var usageText = `cfssl-bundle -- create a certificate bundle that contains the client cert
  15. Usage of bundle:
  16. - Bundle local certificate files
  17. bundle -cert file [-ca-bundle file] [-int-bundle file] [-int-dir dir] [-metadata file] [-key keyfile] [-flavor optimal|ubiquitous|force] [-password password]
  18. - Bundle certificate from remote server.
  19. bundle -domain domain_name [-ip ip_address] [-ca-bundle file] [-int-bundle file] [-int-dir dir] [-metadata file]
  20. Flags:
  21. `
  22. registerFlags(&c, bundleFlagSet)
  23. bundleFlagSet.Usage = func() {
  24. fmt.Fprintf(os.Stderr, "\t%s", usageText)
  25. for _, name := range bundle.Command.Flags {
  26. if f := bundleFlagSet.Lookup(name); f != nil {
  27. printDefaultValue(f)
  28. }
  29. }
  30. }
  31. args := os.Args[1:]
  32. bundleFlagSet.Parse(args)
  33. args = bundleFlagSet.Args()
  34. var err error
  35. c.CFG, err = config.LoadFile(c.ConfigFile)
  36. if c.ConfigFile != "" && err != nil {
  37. fmt.Fprintf(os.Stderr, "Failed to load config file: %v", err)
  38. }
  39. if err := bundle.Command.Main(args, c); err != nil {
  40. fmt.Fprintln(os.Stderr, err)
  41. }
  42. }
  43. // printDefaultValue is a helper function to print out a user friendly
  44. // usage message of a flag. It's useful since we want to write customized
  45. // usage message on selected subsets of the global flag set. It is
  46. // borrowed from standard library source code. Since flag value type is
  47. // not exported, default string flag values are printed without
  48. // quotes. The only exception is the empty string, which is printed as "".
  49. func printDefaultValue(f *flag.Flag) {
  50. format := " -%s=%s: %s\n"
  51. if f.DefValue == "" {
  52. format = " -%s=%q: %s\n"
  53. }
  54. fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
  55. }
  56. // registerFlags defines all cfssl command flags and associates their values with variables.
  57. func registerFlags(c *cli.Config, f *flag.FlagSet) {
  58. f.StringVar(&c.CertFile, "cert", "", "Client certificate that contains the public key")
  59. f.StringVar(&c.KeyFile, "key", "", "private key for the certificate")
  60. f.StringVar(&c.CABundleFile, "ca-bundle", "", "path to root certificate store")
  61. f.StringVar(&c.IntBundleFile, "int-bundle", "", "path to intermediate certificate store")
  62. f.StringVar(&c.Flavor, "flavor", "ubiquitous", "Bundle Flavor: ubiquitous, optimal and force.")
  63. f.StringVar(&c.IntDir, "int-dir", "", "specify intermediates directory")
  64. f.StringVar(&c.Metadata, "metadata", "", "Metadata file for root certificate presence. The content of the file is a json dictionary (k,v): each key k is SHA-1 digest of a root certificate while value v is a list of key store filenames.")
  65. f.StringVar(&c.Domain, "domain", "", "remote server domain name")
  66. f.StringVar(&c.IP, "ip", "", "remote server ip")
  67. f.StringVar(&c.Password, "password", "0", "Password for accessing PKCS #12 data passed to bundler")
  68. }