test_wizard_upgrade.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #! /usr/bin/env bash
  2. # -*- coding: utf-8 -*-
  3. # Codes
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. NC='\033[0m'
  7. OK_STR="${GREEN}OK${NC}"
  8. FAIL_STR="${RED}FAIL${NC}"
  9. CURRENT_FOLDER=$(pwd)
  10. BOUNCER_VERSION="v0.0.6"
  11. RELEASE_FOLDER=""
  12. HUB_AVAILABLE_PARSERS="/etc/crowdsec/hub/parsers"
  13. HUB_AVAILABLE_SCENARIOS="/etc/crowdsec/hub/scenarios"
  14. HUB_AVAILABLE_COLLECTIONS="/etc/crowdsec/hub/collections"
  15. HUB_AVAILABLE_PO="/etc/crowdsec/hub/postoverflows"
  16. HUB_ENABLED_PARSERS="/etc/crowdsec/parsers"
  17. HUB_ENABLED_SCENARIOS="/etc/crowdsec/scenarios"
  18. HUB_ENABLED_COLLECTIONS="/etc/crowdsec/collections"
  19. HUB_ENABLED_PO="/etc/crowdsec/postoverflows"
  20. ACQUIS_FILE="/etc/crowdsec/acquis.yaml"
  21. PROFILE_FILE="/etc/crowdsec/profiles.yaml"
  22. CONFIG_FILE="/etc/crowdsec/config.yaml"
  23. LOCAL_API_FILE="/etc/crowdsec/local_api_credentials.yaml"
  24. ONLINE_API_FILE="/etc/crowdsec/online_api_credentials.yaml"
  25. SIMULATION_FILE="/etc/crowdsec/simulation.yaml"
  26. DB_FILE="/var/lib/crowdsec/data/crowdsec.db"
  27. SYSTEMD_FILE="/etc/systemd/system/crowdsec.service"
  28. BOUNCER_FOLDER="/etc/crowdsec/cs-firewall-bouncer"
  29. MUST_FAIL=0
  30. function init
  31. {
  32. which git > /dev/null
  33. if [ $? -ne 0 ]; then
  34. echo "git is needed this test, exiting ..."
  35. fi
  36. if [[ -z ${RELEASE_FOLDER} ]];
  37. then
  38. cd ..
  39. BUILD_VERSION=${CROWDSEC_VERSION} make release
  40. if [ $? != 0 ]; then
  41. echo "Unable to make the release (make sure you have go installed), exiting"
  42. exit 1
  43. fi
  44. RELEASE_FOLDER="crowdsec-${CROWDSEC_VERSION}"
  45. fi
  46. cp -r ${RELEASE_FOLDER} ${CURRENT_FOLDER}
  47. cd ${CURRENT_FOLDER}
  48. echo "[*] Installing crowdsec (bininstall)"
  49. cd ${RELEASE_FOLDER}/
  50. ./wizard.sh --bininstall
  51. cd ${CURRENT_FOLDER}
  52. cscli hub update
  53. cscli collections install crowdsecurity/sshd
  54. cscli postoverflows install crowdsecurity/cdn-whitelist
  55. cscli machines add -a
  56. systemctl start crowdsec
  57. echo "[*] Install firewall bouncer"
  58. wget https://github.com/crowdsecurity/cs-firewall-bouncer/releases/download/${BOUNCER_VERSION}/cs-firewall-bouncer.tgz
  59. tar xzvf cs-firewall-bouncer.tgz
  60. cd cs-firewall-bouncer-${BOUNCER_VERSION}/
  61. (echo "iptables" | sudo ./install.sh) || (echo "Unable to install cs-firewall-bouncer" && exit 1)
  62. cd ${CURRENT_FOLDER}
  63. echo "[*] Tainting parser /etc/crowdsec/parsers/s01-parse/sshd-logs.yaml"
  64. echo " # test taint parser" >> /etc/crowdsec/parsers/s01-parse/sshd-logs.yaml
  65. echo "[*] Tainting scenario /etc/crowdsec/scenarios/ssh-bf.yaml"
  66. echo " # test taint scenario" >> /etc/crowdsec/scenarios/ssh-bf.yaml
  67. echo "[*] Tainting postoverflow /etc/crowdsec/postoverflows/s01-whitelist/cdn-whitelist.yaml"
  68. echo " # test taint postoverflow" >> /etc/crowdsec/postoverflows/s01-whitelist/cdn-whitelist.yaml
  69. echo "[*] Tainting new systemd configuration file"
  70. echo " # test taint systemd file" >> ${RELEASE_FOLDER}/config/crowdsec.service
  71. echo "[*] Tainting profile file"
  72. echo " # test taint profile file" >> ${PROFILE_FILE}
  73. echo "[*] Tainting acquis file"
  74. echo " # test taint acquis file" >> ${ACQUIS_FILE}
  75. echo "[*] Tainting local_api_creds file"
  76. echo " # test taint local_api_creds file" >> ${LOCAL_API_FILE}
  77. echo "[*] Tainting online_api_creds file"
  78. echo " # test taint online_api_creds file" >> ${ONLINE_API_FILE}
  79. echo "[*] Tainting config file"
  80. echo " # test taint config file" >> ${CONFIG_FILE}
  81. echo "[*] Tainting simulation file"
  82. echo " # test taint simulation file" >> ${SIMULATION_FILE}
  83. echo "[*] Adding a decision"
  84. cscli decisions add -i 1.2.3.4
  85. find ${HUB_ENABLED_PARSERS} -type l -exec md5sum "{}" + >> parsers_enabled.md5
  86. find ${HUB_ENABLED_SCENARIOS} -type l -exec md5sum "{}" + >> scenarios_enabled.md5
  87. find ${HUB_ENABLED_COLLECTIONS} -type l -exec md5sum "{}" + >> collections_enabled.md5
  88. find ${HUB_ENABLED_PO} -type l -exec md5sum "{}" + >> po_enabled.md5
  89. md5sum ${ACQUIS_FILE} >> acquis.md5
  90. md5sum ${PROFILE_FILE} >> profile.md5
  91. md5sum ${LOCAL_API_FILE} >> local_api_creds.md5
  92. md5sum ${ONLINE_API_FILE} >> online_api_creds.md5
  93. md5sum ${CONFIG_FILE} >> config.md5
  94. md5sum ${SIMULATION_FILE} >> simulation.md5
  95. md5sum ${DB_FILE} >> db.md5
  96. md5sum ${SYSTEMD_FILE} >> systemd.md5
  97. echo "[*] Setup done"
  98. echo "[*] Launching the upgrade"
  99. cd ${RELEASE_FOLDER}/
  100. ./wizard.sh --upgrade --force
  101. cd ${CURRENT_FOLDER}
  102. echo "[*] Upgrade done, checking results"
  103. }
  104. function down
  105. {
  106. cd ${RELEASE_FOLDER}/
  107. ./wizard.sh --uninstall
  108. cd ${CURRENT_FOLDER}
  109. rm -rf crowdsec-v*
  110. rm -rf cs-firewall-bouncer-*
  111. rm -f crowdsec-release.tgz
  112. rm -f cs-firewall-bouncer.tgz
  113. rm -- *.md5
  114. }
  115. function assert_equal
  116. {
  117. echo ""
  118. if [ "$1" = "$2" ]; then
  119. echo -e "Status - ${GREEN}OK${NC}"
  120. else
  121. echo -e "Status - ${RED}FAIL${NC}"
  122. echo "Details:"
  123. echo ""
  124. diff <(echo "$1" ) <(echo "$2")
  125. MUST_FAIL=1
  126. fi
  127. echo "-----------------------------------------------------------------------"
  128. }
  129. function assert_not_equal
  130. {
  131. echo ""
  132. if [ "$1" != "$2" ]; then
  133. echo -e "Status - ${GREEN}OK${NC}"
  134. else
  135. echo -e "Status - ${RED}FAIL${NC}"
  136. echo "Details:"
  137. echo ""
  138. diff <(echo "$1" ) <(echo "$2")
  139. MUST_FAIL=1
  140. fi
  141. echo "-----------------------------------------------------------------------"
  142. }
  143. function assert_folder_exists
  144. {
  145. echo ""
  146. if [ -d "${BOUNCER_FOLDER}" ]
  147. then
  148. echo -e "Status - ${GREEN}OK${NC}"
  149. else
  150. echo -e "Status - ${RED}FAIL${NC}"
  151. echo "Folder '$1' doesn't exist, but should"
  152. MUST_FAIL=1
  153. fi
  154. echo "-----------------------------------------------------------------------"
  155. }
  156. function test_enabled_parsers
  157. {
  158. echo $FUNCNAME
  159. new=$(find ${HUB_ENABLED_PARSERS} -type f -exec md5sum "{}" +)
  160. old=$(cat parsers_enabled.md5)
  161. assert_equal "$new" "$old"
  162. }
  163. function test_enabled_scenarios
  164. {
  165. echo $FUNCNAME
  166. new=$(find ${HUB_ENABLED_SCENARIOS} -type f -exec md5sum "{}" +)
  167. old=$(cat scenarios_enabled.md5)
  168. assert_equal "$new" "$old"
  169. }
  170. function test_enabled_collections
  171. {
  172. echo $FUNCNAME
  173. new=$(find ${HUB_ENABLED_COLLECTIONS} -type f -exec md5sum "{}" +)
  174. old=$(cat collections_enabled.md5)
  175. assert_equal "$new" "$old"
  176. }
  177. function test_enabled_po
  178. {
  179. echo $FUNCNAME
  180. new=$(find ${HUB_ENABLED_PO} -type f -exec md5sum "{}" +)
  181. old=$(cat po_enabled.md5)
  182. assert_equal "$new" "$old"
  183. }
  184. function test_config_file
  185. {
  186. echo $FUNCNAME
  187. new=$(find ${CONFIG_FILE} -type f -exec md5sum "{}" +)
  188. old=$(cat config.md5)
  189. assert_equal "$new" "$old"
  190. }
  191. function test_acquis_file
  192. {
  193. echo $FUNCNAME
  194. new=$(find ${ACQUIS_FILE} -type f -exec md5sum "{}" +)
  195. old=$(cat acquis.md5)
  196. assert_equal "$new" "$old"
  197. }
  198. function test_local_api_creds_file
  199. {
  200. echo $FUNCNAME
  201. new=$(find ${LOCAL_API_FILE} -type f -exec md5sum "{}" +)
  202. old=$(cat local_api_creds.md5)
  203. assert_equal "$new" "$old"
  204. }
  205. function test_online_api_creds_file
  206. {
  207. echo $FUNCNAME
  208. new=$(find ${ONLINE_API_FILE} -type f -exec md5sum "{}" +)
  209. old=$(cat online_api_creds.md5)
  210. assert_equal "$new" "$old"
  211. }
  212. function test_profile_file
  213. {
  214. echo $FUNCNAME
  215. new=$(find ${PROFILE_FILE} -type f -exec md5sum "{}" +)
  216. old=$(cat profile.md5)
  217. assert_equal "$new" "$old"
  218. }
  219. function test_db_file
  220. {
  221. echo $FUNCNAME
  222. new=$(find ${DB_FILE} -type f -exec md5sum "{}" +)
  223. old=$(cat db.md5)
  224. assert_equal "$new" "$old"
  225. }
  226. function test_simulation_file
  227. {
  228. echo $FUNCNAME
  229. new=$(find ${SIMULATION_FILE} -type f -exec md5sum "{}" +)
  230. old=$(cat simulation.md5)
  231. assert_equal "$new" "$old"
  232. }
  233. function test_systemd_file
  234. {
  235. echo $FUNCNAME
  236. new=$(find ${SYSTEMD_FILE} -type f -exec md5sum "{}" +)
  237. old=$(cat systemd.md5)
  238. assert_not_equal "$new" "$old"
  239. }
  240. function test_bouncer_dir
  241. {
  242. echo $FUNCNAME
  243. assert_folder_exists ${BOUNCER_FOLDER}
  244. }
  245. function start_test
  246. {
  247. echo ""
  248. echo "-----------------------------------------------------------------------"
  249. test_enabled_parsers
  250. test_enabled_scenarios
  251. test_enabled_collections
  252. test_enabled_po
  253. test_config_file
  254. test_acquis_file
  255. test_online_api_creds_file
  256. test_local_api_creds_file
  257. test_profile_file
  258. test_simulation_file
  259. test_db_file
  260. test_systemd_file
  261. test_bouncer_dir
  262. }
  263. usage() {
  264. echo "Usage:"
  265. echo ""
  266. echo " ./test_wizard_upgrade.sh -h Display this help message."
  267. echo " ./test_wizard_upgrade.sh Run all the testsuite. Go must be available to make the release"
  268. echo " ./test_wizard_upgrade.sh --release <path_to_release_folder> If go is not installed, please provide a path to the crowdsec-vX.Y.Z release folder"
  269. echo ""
  270. exit 0
  271. }
  272. while [[ $# -gt 0 ]]
  273. do
  274. key="${1}"
  275. case ${key} in
  276. --version|-v)
  277. CROWDSEC_VERSION="${2}"
  278. shift #past argument
  279. shift
  280. ;;
  281. --release|-r)
  282. RELEASE_FOLDER="${2}"
  283. shift #past argument
  284. shift
  285. ;;
  286. -h|--help)
  287. usage
  288. exit 0
  289. ;;
  290. *) # unknown option
  291. echo "Unknown argument ${key}."
  292. usage
  293. exit 1
  294. ;;
  295. esac
  296. done
  297. init
  298. start_test
  299. down
  300. if [ ${MUST_FAIL} -eq 1 ]
  301. then
  302. exit 1
  303. fi