info.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Package info implements the info command.
  2. package info
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/cloudflare/cfssl/api/client"
  7. "github.com/cloudflare/cfssl/cli"
  8. "github.com/cloudflare/cfssl/cli/sign"
  9. "github.com/cloudflare/cfssl/errors"
  10. "github.com/cloudflare/cfssl/helpers"
  11. "github.com/cloudflare/cfssl/info"
  12. goerr "errors"
  13. )
  14. var infoUsageTxt = `cfssl info -- get info about a remote signer
  15. Usage:
  16. Get info about a remote signer:
  17. cfssl info -remote remote_host [-label label] [-profile profile] [-label label]
  18. Flags:
  19. `
  20. var infoFlags = []string{"remote", "label", "profile", "config"}
  21. func getInfoFromRemote(c cli.Config) (resp *info.Resp, err error) {
  22. req := new(info.Req)
  23. req.Label = c.Label
  24. req.Profile = c.Profile
  25. cert, err := helpers.LoadClientCertificate(c.MutualTLSCertFile, c.MutualTLSKeyFile)
  26. if err != nil {
  27. return
  28. }
  29. remoteCAs, err := helpers.LoadPEMCertPool(c.TLSRemoteCAs)
  30. if err != nil {
  31. return
  32. }
  33. serv := client.NewServerTLS(c.Remote, helpers.CreateTLSConfig(remoteCAs, cert))
  34. reqJSON, _ := json.Marshal(req)
  35. resp, err = serv.Info(reqJSON)
  36. if err != nil {
  37. return
  38. }
  39. _, err = helpers.ParseCertificatePEM([]byte(resp.Certificate))
  40. if err != nil {
  41. return
  42. }
  43. return
  44. }
  45. func getInfoFromConfig(c cli.Config) (resp *info.Resp, err error) {
  46. s, err := sign.SignerFromConfig(c)
  47. if err != nil {
  48. return
  49. }
  50. req := new(info.Req)
  51. req.Label = c.Label
  52. req.Profile = c.Profile
  53. resp, err = s.Info(*req)
  54. if err != nil {
  55. return
  56. }
  57. return
  58. }
  59. func infoMain(args []string, c cli.Config) (err error) {
  60. if len(args) > 0 {
  61. return goerr.New("argument is provided but not defined; please refer to the usage by flag -h.")
  62. }
  63. var resp *info.Resp
  64. if c.Remote != "" {
  65. resp, err = getInfoFromRemote(c)
  66. if err != nil {
  67. return
  68. }
  69. } else if c.CFG != nil {
  70. resp, err = getInfoFromConfig(c)
  71. if err != nil {
  72. return
  73. }
  74. } else {
  75. return goerr.New("Either -remote or -config must be given. Refer to cfssl info -h for usage.")
  76. }
  77. respJSON, err := json.Marshal(resp)
  78. if err != nil {
  79. return errors.NewBadRequest(err)
  80. }
  81. fmt.Print(string(respJSON))
  82. return nil
  83. }
  84. // Command assembles the definition of Command 'info'
  85. var Command = &cli.Command{
  86. UsageText: infoUsageTxt,
  87. Flags: infoFlags,
  88. Main: infoMain,
  89. }