cfssl-certinfo.go 2.6 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/certinfo"
  8. "github.com/cloudflare/cfssl/config"
  9. _ "github.com/go-sql-driver/mysql" // import to support MySQL
  10. _ "github.com/lib/pq" // import to support Postgres
  11. _ "github.com/mattn/go-sqlite3" // import to support SQLite3
  12. )
  13. // main defines the newkey usage and registers all defined commands and flags.
  14. func main() {
  15. var certinfoFlagSet = flag.NewFlagSet("certinfo", flag.ExitOnError)
  16. var c cli.Config
  17. registerFlags(&c, certinfoFlagSet)
  18. var usageText = `cfssl-certinfo -- output certinfo about the given cert
  19. Usage of certinfo:
  20. - Data from local certificate files
  21. certinfo -cert file
  22. - Data from certificate from remote server.
  23. certinfo -domain domain_name
  24. - Data from CA storage
  25. certinfo -serial serial_number -aki authority_key_id (requires -db-config)
  26. Flags:
  27. `
  28. certinfoFlagSet.Usage = func() {
  29. fmt.Fprintf(os.Stderr, "\t%s", usageText)
  30. for _, name := range certinfo.Command.Flags {
  31. if f := certinfoFlagSet.Lookup(name); f != nil {
  32. printDefaultValue(f)
  33. }
  34. }
  35. }
  36. args := os.Args[1:]
  37. certinfoFlagSet.Parse(args)
  38. args = certinfoFlagSet.Args()
  39. var err error
  40. c.CFG, err = config.LoadFile(c.ConfigFile)
  41. if c.ConfigFile != "" && err != nil {
  42. fmt.Fprintf(os.Stderr, "Failed to load config file: %v", err)
  43. }
  44. if err := certinfo.Command.Main(args, c); err != nil {
  45. fmt.Fprintln(os.Stderr, err)
  46. }
  47. }
  48. // printDefaultValue is a helper function to print out a user friendly
  49. // usage message of a flag. It's useful since we want to write customized
  50. // usage message on selected subsets of the global flag set. It is
  51. // borrowed from standard library source code. Since flag value type is
  52. // not exported, default string flag values are printed without
  53. // quotes. The only exception is the empty string, which is printed as "".
  54. func printDefaultValue(f *flag.Flag) {
  55. format := " -%s=%s: %s\n"
  56. if f.DefValue == "" {
  57. format = " -%s=%q: %s\n"
  58. }
  59. fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
  60. }
  61. // registerFlags defines all cfssl command flags and associates their values with variables.
  62. func registerFlags(c *cli.Config, f *flag.FlagSet) {
  63. f.StringVar(&c.CertFile, "cert", "", "Client certificate that contains the public key")
  64. f.StringVar(&c.Domain, "domain", "", "remote server domain name")
  65. f.StringVar(&c.Serial, "serial", "", "certificate serial number")
  66. f.StringVar(&c.AKI, "aki", "", "certificate issuer (authority) key identifier")
  67. f.StringVar(&c.DBConfigFile, "db-config", "", "certificate db configuration file")
  68. }