bundle.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Package bundle implements the bundle command.
  2. package bundle
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/cloudflare/cfssl/bundler"
  7. "github.com/cloudflare/cfssl/cli"
  8. "github.com/cloudflare/cfssl/ubiquity"
  9. )
  10. // Usage text of 'cfssl bundle'
  11. var bundlerUsageText = `cfssl bundle -- create a certificate bundle that contains the client cert
  12. Usage of bundle:
  13. - Bundle local certificate files
  14. cfssl bundle -cert file [-ca-bundle file] [-int-bundle file] [-int-dir dir] [-metadata file] [-key keyfile] [-flavor optimal|ubiquitous|force] [-password password]
  15. - Bundle certificate from remote server.
  16. cfssl bundle -domain domain_name [-ip ip_address] [-ca-bundle file] [-int-bundle file] [-int-dir dir] [-metadata file]
  17. Flags:
  18. `
  19. // flags used by 'cfssl bundle'
  20. var bundlerFlags = []string{"cert", "key", "ca-bundle", "int-bundle", "flavor", "int-dir", "metadata", "domain", "ip", "password"}
  21. // bundlerMain is the main CLI of bundler functionality.
  22. func bundlerMain(args []string, c cli.Config) (err error) {
  23. bundler.IntermediateStash = c.IntDir
  24. ubiquity.LoadPlatforms(c.Metadata)
  25. flavor := bundler.BundleFlavor(c.Flavor)
  26. var b *bundler.Bundler
  27. // If it is a force bundle, don't require ca bundle and intermediate bundle
  28. // Otherwise, initialize a bundler with CA bundle and intermediate bundle.
  29. if flavor == bundler.Force {
  30. b = &bundler.Bundler{}
  31. } else {
  32. b, err = bundler.NewBundler(c.CABundleFile, c.IntBundleFile)
  33. if err != nil {
  34. return
  35. }
  36. }
  37. var bundle *bundler.Bundle
  38. if c.CertFile != "" {
  39. if c.CertFile == "-" {
  40. var certPEM, keyPEM []byte
  41. certPEM, err = cli.ReadStdin(c.CertFile)
  42. if err != nil {
  43. return
  44. }
  45. if c.KeyFile != "" {
  46. keyPEM, err = cli.ReadStdin(c.KeyFile)
  47. if err != nil {
  48. return
  49. }
  50. }
  51. bundle, err = b.BundleFromPEMorDER(certPEM, keyPEM, flavor, "")
  52. if err != nil {
  53. return
  54. }
  55. } else {
  56. // Bundle the client cert
  57. bundle, err = b.BundleFromFile(c.CertFile, c.KeyFile, flavor, c.Password)
  58. if err != nil {
  59. return
  60. }
  61. }
  62. } else if c.Domain != "" {
  63. bundle, err = b.BundleFromRemote(c.Domain, c.IP, flavor)
  64. if err != nil {
  65. return
  66. }
  67. } else {
  68. return errors.New("Must specify bundle target through -cert or -domain")
  69. }
  70. marshaled, err := bundle.MarshalJSON()
  71. if err != nil {
  72. return
  73. }
  74. fmt.Printf("%s", marshaled)
  75. return
  76. }
  77. // Command assembles the definition of Command 'bundle'
  78. var Command = &cli.Command{UsageText: bundlerUsageText, Flags: bundlerFlags, Main: bundlerMain}