broker_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package csplugin
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "path"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  11. "github.com/crowdsecurity/crowdsec/pkg/models"
  12. "github.com/crowdsecurity/crowdsec/pkg/types"
  13. "github.com/pkg/errors"
  14. "github.com/stretchr/testify/assert"
  15. "gopkg.in/tomb.v2"
  16. )
  17. var testPath string
  18. func setPluginPermTo744() {
  19. setPluginPermTo("744")
  20. }
  21. func setPluginPermTo722() {
  22. setPluginPermTo("722")
  23. }
  24. func setPluginPermTo724() {
  25. setPluginPermTo("724")
  26. }
  27. func TestGetPluginNameAndTypeFromPath(t *testing.T) {
  28. setUp()
  29. defer tearDown()
  30. type args struct {
  31. path string
  32. }
  33. tests := []struct {
  34. name string
  35. args args
  36. want string
  37. want1 string
  38. wantErr bool
  39. }{
  40. {
  41. name: "valid plugin name, single dash",
  42. args: args{
  43. path: path.Join(testPath, "notification-gitter"),
  44. },
  45. want: "notification",
  46. want1: "gitter",
  47. wantErr: false,
  48. },
  49. {
  50. name: "invalid plugin name",
  51. args: args{
  52. path: "./tests/gitter",
  53. },
  54. want: "",
  55. want1: "",
  56. wantErr: true,
  57. },
  58. {
  59. name: "valid plugin name, multiple dash",
  60. args: args{
  61. path: "./tests/notification-instant-slack",
  62. },
  63. want: "notification-instant",
  64. want1: "slack",
  65. wantErr: false,
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. got, got1, err := getPluginTypeAndSubtypeFromPath(tt.args.path)
  71. if (err != nil) != tt.wantErr {
  72. t.Errorf("getPluginNameAndTypeFromPath() error = %v, wantErr %v", err, tt.wantErr)
  73. return
  74. }
  75. if got != tt.want {
  76. t.Errorf("getPluginNameAndTypeFromPath() got = %v, want %v", got, tt.want)
  77. }
  78. if got1 != tt.want1 {
  79. t.Errorf("getPluginNameAndTypeFromPath() got1 = %v, want %v", got1, tt.want1)
  80. }
  81. })
  82. }
  83. }
  84. func TestListFilesAtPath(t *testing.T) {
  85. setUp()
  86. defer tearDown()
  87. type args struct {
  88. path string
  89. }
  90. tests := []struct {
  91. name string
  92. args args
  93. want []string
  94. wantErr bool
  95. }{
  96. {
  97. name: "valid directory",
  98. args: args{
  99. path: testPath,
  100. },
  101. want: []string{
  102. path.Join(testPath, "notification-gitter"),
  103. path.Join(testPath, "slack"),
  104. },
  105. },
  106. {
  107. name: "invalid directory",
  108. args: args{
  109. path: "./foo/bar/",
  110. },
  111. wantErr: true,
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. got, err := listFilesAtPath(tt.args.path)
  117. if (err != nil) != tt.wantErr {
  118. t.Errorf("listFilesAtPath() error = %v, wantErr %v", err, tt.wantErr)
  119. return
  120. }
  121. if !reflect.DeepEqual(got, tt.want) {
  122. t.Errorf("listFilesAtPath() = %v, want %v", got, tt.want)
  123. }
  124. })
  125. }
  126. }
  127. func TestBrokerInit(t *testing.T) {
  128. tests := []struct {
  129. name string
  130. action func()
  131. errContains string
  132. wantErr bool
  133. procCfg csconfig.PluginCfg
  134. }{
  135. {
  136. name: "valid config",
  137. action: setPluginPermTo744,
  138. wantErr: false,
  139. },
  140. {
  141. name: "group writable binary",
  142. wantErr: true,
  143. errContains: "notification-dummy is world writable",
  144. action: setPluginPermTo722,
  145. },
  146. {
  147. name: "group writable binary",
  148. wantErr: true,
  149. errContains: "notification-dummy is group writable",
  150. action: setPluginPermTo724,
  151. },
  152. {
  153. name: "no plugin dir",
  154. wantErr: true,
  155. errContains: "no such file or directory",
  156. action: tearDown,
  157. },
  158. {
  159. name: "no plugin binary",
  160. wantErr: true,
  161. errContains: "binary for plugin dummy_default not found",
  162. action: func() {
  163. err := os.Remove(path.Join(testPath, "notification-dummy"))
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. },
  168. },
  169. {
  170. name: "only specify user",
  171. wantErr: true,
  172. errContains: "both plugin user and group must be set",
  173. procCfg: csconfig.PluginCfg{
  174. User: "123445555551122toto",
  175. },
  176. action: setPluginPermTo744,
  177. },
  178. {
  179. name: "only specify group",
  180. wantErr: true,
  181. errContains: "both plugin user and group must be set",
  182. procCfg: csconfig.PluginCfg{
  183. Group: "123445555551122toto",
  184. },
  185. action: setPluginPermTo744,
  186. },
  187. {
  188. name: "Fails to run as root",
  189. wantErr: true,
  190. errContains: "operation not permitted",
  191. procCfg: csconfig.PluginCfg{
  192. User: "root",
  193. Group: "root",
  194. },
  195. action: setPluginPermTo744,
  196. },
  197. {
  198. name: "Invalid user and group",
  199. wantErr: true,
  200. errContains: "unknown user toto1234",
  201. procCfg: csconfig.PluginCfg{
  202. User: "toto1234",
  203. Group: "toto1234",
  204. },
  205. action: setPluginPermTo744,
  206. },
  207. {
  208. name: "Valid user and invalid group",
  209. wantErr: true,
  210. errContains: "unknown group toto1234",
  211. procCfg: csconfig.PluginCfg{
  212. User: "nobody",
  213. Group: "toto1234",
  214. },
  215. action: setPluginPermTo744,
  216. },
  217. }
  218. for _, test := range tests {
  219. t.Run(test.name, func(t *testing.T) {
  220. defer tearDown()
  221. buildDummyPlugin()
  222. if test.action != nil {
  223. test.action()
  224. }
  225. pb := PluginBroker{}
  226. profiles := csconfig.NewDefaultConfig().API.Server.Profiles
  227. profiles = append(profiles, &csconfig.ProfileCfg{
  228. Notifications: []string{"dummy_default"},
  229. })
  230. err := pb.Init(&test.procCfg, profiles, &csconfig.ConfigurationPaths{
  231. PluginDir: testPath,
  232. NotificationDir: "./tests/notifications",
  233. })
  234. defer pb.Kill()
  235. if test.wantErr {
  236. assert.ErrorContains(t, err, test.errContains)
  237. } else {
  238. assert.NoError(t, err)
  239. }
  240. })
  241. }
  242. }
  243. func TestBrokerRun(t *testing.T) {
  244. buildDummyPlugin()
  245. setPluginPermTo744()
  246. defer tearDown()
  247. procCfg := csconfig.PluginCfg{}
  248. pb := PluginBroker{}
  249. profiles := csconfig.NewDefaultConfig().API.Server.Profiles
  250. profiles = append(profiles, &csconfig.ProfileCfg{
  251. Notifications: []string{"dummy_default"},
  252. })
  253. err := pb.Init(&procCfg, profiles, &csconfig.ConfigurationPaths{
  254. PluginDir: testPath,
  255. NotificationDir: "./tests/notifications",
  256. })
  257. assert.NoError(t, err)
  258. tomb := tomb.Tomb{}
  259. go pb.Run(&tomb)
  260. defer pb.Kill()
  261. assert.NoFileExists(t, "./out")
  262. defer os.Remove("./out")
  263. pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
  264. pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
  265. time.Sleep(time.Second * 4)
  266. assert.FileExists(t, "./out")
  267. assert.Equal(t, types.GetLineCountForFile("./out"), 2)
  268. }
  269. func buildDummyPlugin() {
  270. dir, err := os.MkdirTemp("./tests", "cs_plugin_test")
  271. if err != nil {
  272. log.Fatal(err)
  273. }
  274. cmd := exec.Command("go", "build", "-o", path.Join(dir, "notification-dummy"), "../../plugins/notifications/dummy/")
  275. if err := cmd.Run(); err != nil {
  276. log.Fatal(err)
  277. }
  278. testPath = dir
  279. }
  280. func setPluginPermTo(perm string) {
  281. if err := exec.Command("chmod", perm, path.Join(testPath, "notification-dummy")).Run(); err != nil {
  282. log.Fatal(errors.Wrapf(err, "chmod 744 %s", path.Join(testPath, "notification-dummy")))
  283. }
  284. }
  285. func setUp() {
  286. dir, err := os.MkdirTemp("./", "cs_plugin_test")
  287. if err != nil {
  288. log.Fatal(err)
  289. }
  290. _, err = os.Create(path.Join(dir, "slack"))
  291. if err != nil {
  292. log.Fatal(err)
  293. }
  294. _, err = os.Create(path.Join(dir, "notification-gitter"))
  295. if err != nil {
  296. log.Fatal(err)
  297. }
  298. err = os.Mkdir(path.Join(dir, "dummy_dir"), 0666)
  299. if err != nil {
  300. log.Fatal(err)
  301. }
  302. testPath = dir
  303. }
  304. func tearDown() {
  305. err := os.RemoveAll(testPath)
  306. if err != nil {
  307. log.Fatal(err)
  308. }
  309. }