github.com/crowdsecurity/crowdsec@v1.6.1/scripts/test_env.sh (about)

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