papi.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "time"
  4. "github.com/crowdsecurity/crowdsec/pkg/apiserver"
  5. "github.com/crowdsecurity/crowdsec/pkg/database"
  6. "github.com/crowdsecurity/crowdsec/pkg/types"
  7. "github.com/pkg/errors"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/spf13/cobra"
  10. "gopkg.in/tomb.v2"
  11. )
  12. func NewPapiCmd() *cobra.Command {
  13. var cmdLapi = &cobra.Command{
  14. Use: "papi [action]",
  15. Short: "Manage interaction with Polling API (PAPI)",
  16. Args: cobra.MinimumNArgs(1),
  17. DisableAutoGenTag: true,
  18. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  19. if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
  20. return errors.Wrap(err, "Local API is disabled, please run this command on the local API machine")
  21. }
  22. if csConfig.API.Server.OnlineClient == nil {
  23. log.Fatalf("no configuration for Central API in '%s'", *csConfig.FilePath)
  24. }
  25. if csConfig.API.Server.OnlineClient.Credentials.PapiURL == "" {
  26. log.Fatalf("no PAPI URL in configuration")
  27. }
  28. return nil
  29. },
  30. }
  31. cmdLapi.AddCommand(NewPapiStatusCmd())
  32. cmdLapi.AddCommand(NewPapiSyncCmd())
  33. return cmdLapi
  34. }
  35. func NewPapiStatusCmd() *cobra.Command {
  36. cmdCapiStatus := &cobra.Command{
  37. Use: "status",
  38. Short: "Get status of the Polling API",
  39. Args: cobra.MinimumNArgs(0),
  40. DisableAutoGenTag: true,
  41. Run: func(cmd *cobra.Command, args []string) {
  42. var err error
  43. dbClient, err = database.NewClient(csConfig.DbConfig)
  44. if err != nil {
  45. log.Fatalf("unable to initialize database client : %s", err)
  46. }
  47. apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig)
  48. if err != nil {
  49. log.Fatalf("unable to initialize API client : %s", err)
  50. }
  51. papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
  52. if err != nil {
  53. log.Fatalf("unable to initialize PAPI client : %s", err)
  54. }
  55. perms, err := papi.GetPermissions()
  56. if err != nil {
  57. log.Fatalf("unable to get PAPI permissions: %s", err)
  58. }
  59. var lastTimestampStr *string
  60. lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
  61. if err != nil {
  62. lastTimestampStr = types.StrPtr("never")
  63. }
  64. log.Infof("You can successfully interact with Polling API (PAPI)")
  65. log.Infof("Console plan: %s", perms.Plan)
  66. log.Infof("Last order received: %s", *lastTimestampStr)
  67. log.Infof("PAPI subscriptions:")
  68. for _, sub := range perms.Categories {
  69. log.Infof(" - %s", sub)
  70. }
  71. },
  72. }
  73. return cmdCapiStatus
  74. }
  75. func NewPapiSyncCmd() *cobra.Command {
  76. cmdCapiSync := &cobra.Command{
  77. Use: "sync",
  78. Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
  79. Args: cobra.MinimumNArgs(0),
  80. DisableAutoGenTag: true,
  81. Run: func(cmd *cobra.Command, args []string) {
  82. var err error
  83. t := tomb.Tomb{}
  84. dbClient, err = database.NewClient(csConfig.DbConfig)
  85. if err != nil {
  86. log.Fatalf("unable to initialize database client : %s", err)
  87. }
  88. apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig)
  89. if err != nil {
  90. log.Fatalf("unable to initialize API client : %s", err)
  91. }
  92. t.Go(apic.Push)
  93. papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
  94. if err != nil {
  95. log.Fatalf("unable to initialize PAPI client : %s", err)
  96. }
  97. t.Go(papi.SyncDecisions)
  98. err = papi.PullOnce(time.Time{})
  99. if err != nil {
  100. log.Fatalf("unable to sync decisions: %s", err)
  101. }
  102. log.Infof("Sending acknowledgements to CAPI")
  103. apic.Shutdown()
  104. papi.Shutdown()
  105. t.Wait()
  106. time.Sleep(5 * time.Second) //FIXME: the push done by apic.Push is run inside a sub goroutine, sleep to make sure it's done
  107. },
  108. }
  109. return cmdCapiSync
  110. }