test_env.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. BASE="./tests"
  3. usage() {
  4. echo "Usage:"
  5. echo " ./wizard.sh -h Display this help message."
  6. echo " ./test_env.sh -d ./tests Create test environment in './tests' folder"
  7. exit 0
  8. }
  9. while [[ $# -gt 0 ]]
  10. do
  11. key="${1}"
  12. case ${key} in
  13. -d|--directory)
  14. BASE=${2}
  15. shift #past argument
  16. shift
  17. ;;
  18. -h|--help)
  19. usage
  20. exit 0
  21. ;;
  22. *) # unknown option
  23. log_err "Unknown argument ${key}."
  24. usage
  25. exit 1
  26. ;;
  27. esac
  28. done
  29. BASE=$(realpath $BASE)
  30. DATA_DIR="$BASE/data"
  31. LOG_DIR="$BASE/logs/"
  32. CONFIG_DIR="$BASE/config"
  33. CONFIG_FILE="$BASE/dev.yaml"
  34. CSCLI_DIR="$CONFIG_DIR/crowdsec-cli"
  35. PARSER_DIR="$CONFIG_DIR/parsers"
  36. PARSER_S00="$PARSER_DIR/s00-raw"
  37. PARSER_S01="$PARSER_DIR/s01-parse"
  38. PARSER_S02="$PARSER_DIR/s02-enrich"
  39. SCENARIOS_DIR="$CONFIG_DIR/scenarios"
  40. POSTOVERFLOWS_DIR="$CONFIG_DIR/postoverflows"
  41. HUB_DIR="$CONFIG_DIR/hub"
  42. PLUGINS="http slack splunk email"
  43. PLUGINS_DIR="plugins"
  44. NOTIF_DIR="notifications"
  45. log_info() {
  46. msg=$1
  47. date=$(date +%x:%X)
  48. echo -e "[$date][INFO] $msg"
  49. }
  50. create_arbo() {
  51. mkdir -p "$BASE"
  52. mkdir -p "$DATA_DIR"
  53. mkdir -p "$LOG_DIR"
  54. mkdir -p "$CONFIG_DIR"
  55. mkdir -p "$PARSER_DIR"
  56. mkdir -p "$PARSER_S00"
  57. mkdir -p "$PARSER_S01"
  58. mkdir -p "$PARSER_S02"
  59. mkdir -p "$SCENARIOS_DIR"
  60. mkdir -p "$POSTOVERFLOWS_DIR"
  61. mkdir -p "$CSCLI_DIR"
  62. mkdir -p "$HUB_DIR"
  63. mkdir -p "$CONFIG_DIR/$NOTIF_DIR/$plugin"
  64. mkdir -p "$BASE/$PLUGINS_DIR"
  65. }
  66. copy_files() {
  67. cp "./config/profiles.yaml" "$CONFIG_DIR"
  68. cp "./config/simulation.yaml" "$CONFIG_DIR"
  69. cp "./cmd/crowdsec/crowdsec" "$BASE"
  70. cp "./cmd/crowdsec-cli/cscli" "$BASE"
  71. cp -r "./config/patterns" "$CONFIG_DIR"
  72. cp "./config/acquis.yaml" "$CONFIG_DIR"
  73. touch "$CONFIG_DIR"/local_api_credentials.yaml
  74. touch "$CONFIG_DIR"/online_api_credentials.yaml
  75. envsubst < "./config/dev.yaml" > $BASE/dev.yaml
  76. for plugin in $PLUGINS
  77. do
  78. cp $PLUGINS_DIR/$NOTIF_DIR/$plugin/notification-$plugin $BASE/$PLUGINS_DIR/notification-$plugin
  79. cp $PLUGINS_DIR/$NOTIF_DIR/$plugin/$plugin.yaml $CONFIG_DIR/$NOTIF_DIR/$plugin.yaml
  80. done
  81. }
  82. setup() {
  83. $BASE/cscli -c "$CONFIG_FILE" hub update
  84. $BASE/cscli -c "$CONFIG_FILE" collections install crowdsecurity/linux
  85. }
  86. setup_api() {
  87. $BASE/cscli -c "$CONFIG_FILE" machines add test -p testpassword -f $CONFIG_DIR/local_api_credentials.yaml --force
  88. }
  89. main() {
  90. log_info "Creating test arboresence in $BASE"
  91. create_arbo
  92. log_info "Arboresence created"
  93. log_info "Copying needed files for tests environment"
  94. copy_files
  95. log_info "Files copied"
  96. log_info "Setting up configurations"
  97. CURRENT_PWD=$(pwd)
  98. cd $BASE
  99. setup_api
  100. setup
  101. cd $CURRENT_PWD
  102. log_info "Environment is ready in $BASE"
  103. }
  104. main