|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +### |
| 4 | +# DEFAULT VALUES |
| 5 | +### |
| 6 | + |
| 7 | +MODE_HELP="defines how the logstash-modsecurity configuration should be deployed (has to be one off 'symlink', 'file')" |
| 8 | +MODE="symlink" |
| 9 | + |
| 10 | +TARGET_HELP="defines, where the logstash-modsecurity config should be deployed |
| 11 | + For MODE='symlink' TARGET is expected to be a directory (where the symlink to the source files are created) |
| 12 | + For MODE='file' TARGET is expected to be a file (resulting file of concat operation of all source files) |
| 13 | + If TARGET does not exists, it will be created." |
| 14 | +TARGET="/etc/logstash/conf.d" |
| 15 | + |
| 16 | +SOURCEDIR_HELP="defines, where the logstash-modsecurity config is found, this should point to the directory, where the git clone of logstash-modsecurity is placed." |
| 17 | +SOURCEDIR="" |
| 18 | + |
| 19 | +MODULES_HELP="contains the selected rule ids, which should be deployed" |
| 20 | +declare -a MODULES=( |
| 21 | + "0000_header.conf" |
| 22 | + "1000_input_stdin_example.conf" |
| 23 | +# "1010_input_file_example.conf" |
| 24 | + "2000_filter_sections_split.conf" |
| 25 | + "2010_filter_section_a_parse.conf" |
| 26 | + "2020_filter_section_b_parse_request_line.conf" |
| 27 | + "2021_filter_section_b_headers_key-value.conf" |
| 28 | +# "2029_filter_section_b_example_header_Cookie.conf" |
| 29 | +# "2029_filter_section_b_example_header_X-Forwarded-For.conf" |
| 30 | +# "2029_filter_section_b_example_splitt_all_cockies.conf" |
| 31 | + "2030_filter_section_c_parse.conf" |
| 32 | +# "2040_filter_section_d_example.conf" |
| 33 | +# "2050_filter_section_e_example.conf" |
| 34 | + "2060_filter_section_f_parse_request_line.conf" |
| 35 | + "2061_filter_section_f_parse_headers.conf" |
| 36 | + "2062_filter_section_f_headers_key-value.conf" |
| 37 | +# "2070_filter_section_g_example.conf" |
| 38 | + "2080_filter_section_h_parse_messages_to_auditLogTrailerMessages.conf" |
| 39 | + "2081_filter_section_h_convert_to_key-value.conf" |
| 40 | + "2082_filter_section_h_extract_stopwatch.conf" |
| 41 | +# "2089_filter_section_h_example_geoip.conf" |
| 42 | +# "2089_filter_section_h_example_severities.conf" |
| 43 | +# "2090_filter_section_i_example.conf" |
| 44 | +# "2100_filter_section_j_example.conf" |
| 45 | + "2110_filter_section_k_parse_matchedRules.conf" |
| 46 | + "2500_filter_cleanup.conf" |
| 47 | + "3000_output_stdout_example.conf" |
| 48 | +) |
| 49 | + |
| 50 | +### |
| 51 | +# END DEFAULT VALUES |
| 52 | +### |
| 53 | + |
| 54 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 55 | + echo "Usage: $0 [-h | --help] [config-file]" |
| 56 | + echo " -h, --help : print usage" |
| 57 | + echo |
| 58 | + echo "MODE ${MODE_HELP}" |
| 59 | + echo "TARGET ${TARGET_HELP}" |
| 60 | + echo "SOURCEDIR ${SOURCEDIR_HELP}" |
| 61 | + echo "MODULES ${MODULES_HELP}" |
| 62 | + echo |
| 63 | + echo "For an example config file see logstash-modsecurity.cfg.example" |
| 64 | + echo |
| 65 | + echo "Without config-file, default settings are used" |
| 66 | + echo "MODE: ${MODE}" |
| 67 | + echo "TARGET: ${TARGET}" |
| 68 | + echo "SOURCEDIR: ${SOURCEDIR}" |
| 69 | + echo "MODULES:" |
| 70 | + for MODULE in "${MODULES[@]}" |
| 71 | + do |
| 72 | + echo "* ${MODULE}" |
| 73 | + done |
| 74 | + echo |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 78 | +# Source config in /etc |
| 79 | +if [[ -r /etc/logstash-modsecurity.conf ]]; then |
| 80 | + echo "Source config from /etc/logstash-modsecurity.cfg" |
| 81 | + source /etc/logstash-modsecurity.conf |
| 82 | +fi |
| 83 | + |
| 84 | +# Source config in argument $1 if present |
| 85 | +if [[ -n "$1" ]]; then |
| 86 | + if [[ -r "$1" ]]; then |
| 87 | + echo "Source config from $1" |
| 88 | + source $1 |
| 89 | + else |
| 90 | + echo "ERROR: Unable to read config file: $1" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | +fi |
| 94 | + |
| 95 | +# Validate MODE and TARGET |
| 96 | +if [[ "${MODE}" == "symlink" || "${MODE}" == "file" ]]; then |
| 97 | + if [[ "${MODE}" == "symlink" ]]; then |
| 98 | + if [[ ! -e "${TARGET}" ]]; then |
| 99 | + echo "Create directory: ${TARGET}" |
| 100 | + mkdir -p ${TARGET} |
| 101 | + if [[ "$?" -ne "0" ]]; then |
| 102 | + echo "ERROR: Unable to create ${TARGET}" |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | + else |
| 106 | + if [[ ! -d "${TARGET}" ]]; then |
| 107 | + echo "ERROR: MODE is 'symlink' but TARGET is not a directory, TARGET is ${TAGET}" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + fi |
| 111 | + else |
| 112 | + # MODE == "file" |
| 113 | + if [[ ! -e "${TARGET}" ]]; then |
| 114 | + TARGETDIR=`dirname ${TARGET}` |
| 115 | + if [[ ! -d "${TARGETDIR}" ]]; then |
| 116 | + echo "Create target directory: ${TARGETDIR}" |
| 117 | + mkdir -p ${TARGETDIR} |
| 118 | + if [[ "$?" -ne "0" ]]; then |
| 119 | + echo "ERROR: Unable to create ${TARGETDIR}" |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + touch ${TARGET} |
| 123 | + if [[ "$?" -ne "0" ]]; then |
| 124 | + echo "ERROR: Unable to create ${TARGET}" |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + fi |
| 128 | + else |
| 129 | + if [[ ! -w "${TARGET}" || ! -f "${TARGET}" ]]; then |
| 130 | + echo "ERROR: MODE is 'file', TARGET exists, but is not a writeable file, TARGET is ${TARGET}" |
| 131 | + exit 1 |
| 132 | + else |
| 133 | + >${TARGET} |
| 134 | + fi |
| 135 | + fi |
| 136 | + fi |
| 137 | +else |
| 138 | + echo "ERROR: MODE is expected to by one of 'symlink' or 'file', MODE is ${MODE}" |
| 139 | + exit 1 |
| 140 | +fi |
| 141 | + |
| 142 | +# Get SOURCEDIR from script location, if not specified |
| 143 | +if [[ -z "${SOURCEDIR}" ]]; then |
| 144 | + pushd `dirname $0` > /dev/null |
| 145 | + SOURCEDIR=`pwd -P` |
| 146 | + popd > /dev/null |
| 147 | +else |
| 148 | + if [[ ! -d "${SOURCEDIR}" ]]; then |
| 149 | + echo "ERROR: SOURCEDIR is not a directory." |
| 150 | + exit 1 |
| 151 | + fi |
| 152 | +fi |
| 153 | + |
| 154 | +# Print current settings |
| 155 | +echo "MODE: ${MODE}" |
| 156 | +echo "TARGET: ${TARGET}" |
| 157 | +echo "SOURCEDIR: ${SOURCEDIR}" |
| 158 | +echo |
| 159 | + |
| 160 | +for FILE in "${MODULES[@]}" |
| 161 | +do |
| 162 | + echo "process ${FILE}" |
| 163 | + if [[ ! -r "${SOURCEDIR}/${FILE}" ]]; then |
| 164 | + echo "ERROR: Unable to read ${SOURCEDIR}/${FILE}" |
| 165 | + fi |
| 166 | + |
| 167 | + if [[ "$MODE" == "symlink" ]]; then |
| 168 | + ln -s ${SOURCEDIR}/${FILE} ${TARGET}/${FILE} |
| 169 | + if [[ "$?" -ne "0" ]]; then |
| 170 | + echo "ERROR: Unable to create symlink ${TARGET}/${FILE}" |
| 171 | + fi |
| 172 | + else |
| 173 | + # MODE == "file" |
| 174 | + cat ${SOURCEDIR}/${FILE} >> ${TARGET} |
| 175 | + if [[ "$?" -ne "0" ]]; then |
| 176 | + echo "ERROR: Unable to concat ${SOURCEDIR}/${FILE} to ${TARGET}/${FILE}" |
| 177 | + fi |
| 178 | + fi |
| 179 | +done |
0 commit comments