acl.sh.in 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash -x
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. BASICURL="http://127.0.0.1"
  4. BASICURLS="https://127.0.0.1"
  5. NETDATA_VARLIB_DIR="/var/lib/netdata"
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[0;43m'
  9. NOCOLOR='\033[0m'
  10. #change the previous acl file and with a new
  11. #and store it on a new file
  12. change_file(){
  13. sed "s/$1/$2/g" netdata.cfg > "$4"
  14. }
  15. NETDATAPID=""
  16. change_ssl_file(){
  17. KEYROW="ssl key = $3/key.pem"
  18. CERTROW="ssl certificate = $3/cert.pem"
  19. sed "s@ssl key =@$KEYROW@g" netdata.ssl.cfg > tmp
  20. sed "s@ssl certificate =@$CERTROW@g" tmp > tmp2
  21. sed "s/$1/$2/g" tmp2 > "$4"
  22. }
  23. run_acl_tests() {
  24. #Give a time for netdata start properly
  25. sleep 2
  26. curl -v -k --tls-max 1.2 --create-dirs -o index.html "$2" 2> log_index.txt
  27. curl -v -k --tls-max 1.2 --create-dirs -o netdata.txt "$2/netdata.conf" 2> log_nc.txt
  28. curl -v -k --tls-max 1.2 --create-dirs -o badge.csv "$2/api/v1/badge.svg?chart=cpu.cpu0_interrupts" 2> log_badge.txt
  29. curl -v -k --tls-max 1.2 --create-dirs -o info.txt "$2/api/v1/info" 2> log_info.txt
  30. curl -H "X-Auth-Token: $1" -v -k --tls-max 1.2 --create-dirs -o health.csv "$2/api/v1/manage/health?cmd=LIST" 2> log_health.txt
  31. TOT=$(grep -c "HTTP/1.1 301" log_*.txt | cut -d: -f2| grep -c 1)
  32. if [ "$TOT" -ne "$4" ]; then
  33. echo -e "${RED}I got a wrong number of redirects($TOT) when SSL is activated, It was expected $4 ${NOCOLOR}"
  34. rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
  35. kill $NETDATAPID
  36. exit 1
  37. elif [ "$TOT" -eq "$4" ] && [ "$4" -ne "0" ]; then
  38. echo -e "${YELLOW}I got the correct number of redirects($4) when SSL is activated and I try to access with HTTP. ${NOCOLOR}"
  39. return
  40. fi
  41. TOT=$(grep -c "HTTP/1.1 200 OK" log_* | cut -d: -f2| grep -c 1)
  42. if [ "$TOT" -ne "$3" ]; then
  43. echo -e "${RED}I got a wrong number of \"200 OK\" from the queries, it was expected $3. ${NOCOLOR}"
  44. kill $NETDATAPID
  45. rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
  46. exit 1
  47. fi
  48. echo -e "${GREEN}ACLs were applied correctly ${NOCOLOR}"
  49. }
  50. CONF=$(grep "bind" netdata.cfg)
  51. MUSER=$(grep run netdata.cfg | cut -d= -f2|sed 's/^[ \t]*//')
  52. openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -sha512 -subj "/C=US/ST=Denied/L=Somewhere/O=Dis/CN=www.example.com" -keyout key.pem -out cert.pem
  53. chown "$MUSER" key.pem cert.pem
  54. CWD=$(pwd)
  55. if [ -f "${NETDATA_VARLIB_DIR}/netdata.api.key" ] ;then
  56. read -r TOKEN < "${NETDATA_VARLIB_DIR}/netdata.api.key"
  57. else
  58. TOKEN="NULL"
  59. fi
  60. change_file "$CONF" " bind to = *" "$CWD" "netdata.conf.test0"
  61. netdata -c "netdata.conf.test0" -D &
  62. NETDATAPID=$!
  63. run_acl_tests $TOKEN "$BASICURL:19999" 5 0
  64. kill $NETDATAPID
  65. change_ssl_file "$CONF" " bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management *:20001=dashboard|registry|netdata.conf^SSL=optional *:20002=dashboard|registry" "$CWD" "netdata.conf.test1"
  66. netdata -c "netdata.conf.test1" -D &
  67. NETDATAPID=$!
  68. run_acl_tests $TOKEN "$BASICURL:19999" 5 5
  69. run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
  70. run_acl_tests $TOKEN "$BASICURL:20000" 4 5
  71. run_acl_tests $TOKEN "$BASICURLS:20000" 4 0
  72. run_acl_tests $TOKEN "$BASICURL:20001" 4 0
  73. run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
  74. run_acl_tests $TOKEN "$BASICURL:20002" 3 5
  75. run_acl_tests $TOKEN "$BASICURLS:20002" 3 0
  76. kill $NETDATAPID
  77. change_ssl_file "$CONF" " bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management *:20001=dashboard|registry|netdata.conf^SSL=force *:20002=dashboard|registry" "$CWD" "netdata.conf.test2"
  78. netdata -c "netdata.conf.test2" -D &
  79. NETDATAPID=$!
  80. run_acl_tests $TOKEN "$BASICURL:19999" 5 5
  81. run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
  82. run_acl_tests $TOKEN "$BASICURL:20000" 4 5
  83. run_acl_tests $TOKEN "$BASICURLS:20000" 4 0
  84. run_acl_tests $TOKEN "$BASICURL:20001" 4 5
  85. run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
  86. run_acl_tests $TOKEN "$BASICURL:20002" 3 5
  87. run_acl_tests $TOKEN "$BASICURLS:20002" 3 0
  88. kill $NETDATAPID
  89. change_ssl_file "$CONF" " bind to = *=dashboard|registry|badges|management|netdata.conf *:20000=dashboard|registry|badges|management^SSL=optional *:20001=dashboard|registry|netdata.conf^SSL=force" "$CWD" "netdata.conf.test3"
  90. netdata -c "netdata.conf.test3" -D &
  91. NETDATAPID=$!
  92. run_acl_tests $TOKEN "$BASICURL:19999" 5 5
  93. run_acl_tests $TOKEN "$BASICURLS:19999" 5 0
  94. run_acl_tests $TOKEN "$BASICURL:20000" 4 0
  95. run_acl_tests $TOKEN "$BASICURLS:20000" 4 0
  96. run_acl_tests $TOKEN "$BASICURL:20001" 4 5
  97. run_acl_tests $TOKEN "$BASICURLS:20001" 4 0
  98. kill $NETDATAPID
  99. rm log_* netdata.conf.test* netdata.txt health.csv index.html badge.csv tmp* key.pem cert.pem info.txt
  100. echo "All the tests were successful ${NOCOLOR}"