tests_post-install_7_plugin.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #! /usr/bin/env bash
  2. # -*- coding: utf-8 -*-
  3. source tests_base.sh
  4. MOCK_SERVER_PID=""
  5. function backup () {
  6. cat /etc/crowdsec/profiles.yaml > ./backup_profiles.yaml
  7. cat /etc/crowdsec/notifications/http.yaml > ./backup_http.yaml
  8. }
  9. function restore_backup () {
  10. cat ./backup_profiles.yaml | sudo tee /etc/crowdsec/profiles.yaml > /dev/null
  11. cat ./backup_http.yaml | sudo tee /etc/crowdsec/notifications/http.yaml > /dev/null
  12. }
  13. function clear_backup() {
  14. rm ./backup_profiles.yaml
  15. rm ./backup_http.yaml
  16. }
  17. function modify_config() {
  18. PLUGINS_DIR=$(sudo find /usr -type d -wholename "*"crowdsec/plugins)
  19. sed -i "s#/usr/local/lib/crowdsec/plugins#${PLUGINS_DIR}#g" ./config/config.yaml
  20. cat ./config/config.yaml | sed 's/group: nogroup/group: '$(groups nobody | cut -d ':' -f2 | tr -d ' ')'/' | sudo tee /etc/crowdsec/config.yaml > /dev/null
  21. cat ./config/http.yaml | sudo tee /etc/crowdsec/notifications/http.yaml > /dev/null
  22. cat ./config/profiles.yaml | sudo tee /etc/crowdsec/profiles.yaml > /dev/null
  23. ${SYSTEMCTL} restart crowdsec
  24. sleep 5s
  25. }
  26. function setup_tests() {
  27. backup
  28. cscli decisions delete --all
  29. modify_config
  30. python3 -u mock_http_server.py > mock_http_server_logs.log &
  31. count=0
  32. while ! nc -z localhost 9999; do
  33. sleep 0.5
  34. ((count ++))
  35. if [[ $count == 41 ]]; then
  36. fail "mock server not up after 20s"
  37. fi
  38. done
  39. MOCK_SERVER_PID=$!
  40. }
  41. function cleanup_tests() {
  42. restore_backup
  43. clear_backup
  44. kill -9 $MOCK_SERVER_PID
  45. rm mock_http_server_logs.log
  46. ${SYSTEMCTL} restart crowdsec
  47. sleep 5s
  48. }
  49. function run_tests() {
  50. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  51. if [[ $log_line_count -ne "0" ]] ; then
  52. cleanup_tests
  53. fail "expected 0 log lines fom mock http server before adding decisions"
  54. fi
  55. sleep 5s
  56. ${CSCLI} decisions add --ip 1.2.3.4 --duration 30s
  57. ${CSCLI} decisions add --ip 1.2.3.5 --duration 30s
  58. sleep 5s
  59. cat mock_http_server_logs.log
  60. log_line_count=$(cat mock_http_server_logs.log | wc -l)
  61. if [[ $log_line_count -ne "1" ]] ; then
  62. cleanup_tests
  63. fail "expected 1 log line from http server"
  64. fi
  65. total_alerts=$(cat mock_http_server_logs.log | jq .request_body | jq length)
  66. if [[ $total_alerts -ne "2" ]] ; then
  67. cleanup_tests
  68. fail "expected to receive 2 alerts in the request body from plugin"
  69. fi
  70. first_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[0].decisions[0].value)
  71. if [[ $first_received_ip != "1.2.3.4" ]] ; then
  72. cleanup_tests
  73. fail "expected to receive IP 1.2.3.4 as value of first decision"
  74. fi
  75. second_received_ip=$(cat mock_http_server_logs.log | jq -r .request_body[1].decisions[0].value)
  76. if [[ $second_received_ip != "1.2.3.5" ]] ; then
  77. cleanup_tests
  78. fail "expected to receive IP 1.2.3.5 as value of second decision"
  79. fi
  80. }
  81. setup_tests
  82. run_tests
  83. cleanup_tests