github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/db/instance-mysql (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eu
     4  script_name=$0
     5  DB_BACKEND=$(echo "${script_name}" | cut -d- -f2)
     6  export DB_BACKEND
     7  
     8  die() {
     9      echo >&2 "$@"
    10      exit 1
    11  }
    12  
    13  MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
    14  MYSQL_PORT=${MYSQL_PORT:-3306}
    15  MYSQL_PASSWORD=${MYSQL_PASSWORD:-password}
    16  MYSQL_USER=${MYSQL_USER:-root}
    17  
    18  about() {
    19      die "usage: ${script_name} [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
    20  }
    21  
    22  check_requirements() {
    23      if ! command -v mysql >/dev/null; then
    24          die "missing required program 'mysql' as a mysql client (package mariadb-client-core-10.6 on debian like system)"
    25      fi
    26  }
    27  
    28  silence_password_warning() {
    29      ( ( ( "$@" >&9 ) 2>&1 \
    30          | grep -F -v "[Warning] Using a password on the command line interface can be insecure." ) >&2 ) 9>&1 || [[ $? == 1 ]]
    31  }
    32  
    33  exec_sql() {
    34      cmd="${1?Missing required sql command}"
    35  
    36      silence_password_warning \
    37          mysql \
    38          "--host=${MYSQL_HOST}" \
    39          "--user=${MYSQL_USER}" \
    40          "--port=${MYSQL_PORT}" \
    41          "--password=${MYSQL_PASSWORD}" <<< "${cmd}"
    42  }
    43  
    44  setup() {
    45      exec_sql "DROP DATABASE IF EXISTS crowdsec_test;"
    46      exec_sql "CREATE DATABASE crowdsec_test;"
    47      exec_sql "DROP USER IF EXISTS crowdsec_test;"
    48      exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
    49      exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
    50  }
    51  
    52  dump() {
    53      backup_file="${1?Missing file to backup database to}"
    54  
    55      args=(mysqldump)
    56      if mysqldump --column-statistics 2>&1 | grep -q -v 'unknown option'; then
    57          args+=("--column-statistics=0")
    58      fi
    59      args+=("--host=${MYSQL_HOST}" "--port=${MYSQL_PORT}" "--user=${MYSQL_USER}" "--password=${MYSQL_PASSWORD}" --databases crowdsec_test)
    60  
    61      silence_password_warning "${args[@]}" > "${backup_file}"
    62  }
    63  
    64  restore() {
    65      backup_file="${1?missing file to restore database from}"
    66      [[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
    67  
    68      silence_password_warning \
    69          mysql \
    70          "--host=${MYSQL_HOST}" \
    71          "--user=${MYSQL_USER}" \
    72          "--port=${MYSQL_PORT}" \
    73          "--password=${MYSQL_PASSWORD}" < "${backup_file}"
    74  
    75      exec_sql "DROP USER IF EXISTS 'crowdsec_test';"
    76      exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
    77      exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
    78  }
    79  
    80  config_yaml() {
    81      MYSQL_PORT=${MYSQL_PORT} MYSQL_HOST=${MYSQL_HOST} yq e '
    82          .db_config.type=strenv(DB_BACKEND)|
    83          .db_config.user="crowdsec_test" |
    84          .db_config.password="crowdsec_test" |
    85          .db_config.db_name="crowdsec_test"  |
    86          .db_config.host=strenv(MYSQL_HOST) |
    87          .db_config.port=env(MYSQL_PORT) |
    88          del(.db_config.db_path)
    89      ' -i "${CONFIG_YAML}"
    90  }
    91  
    92  [[ $# -lt 1 ]] && about
    93  
    94  check_requirements
    95  
    96  case "$1" in
    97      setup)
    98          setup
    99          ;;
   100      config-yaml)
   101          config_yaml
   102          ;;
   103      dump)
   104          shift
   105          dump "$@"
   106          ;;
   107      restore)
   108          shift
   109          restore "$@"
   110          ;;
   111      exec_sql)
   112          shift
   113          #
   114          # This command is meant to run a query against the the crowdsec database.
   115          # The exec_sql() function is more generic and is also used for database setup and backups.
   116          #
   117          # For this reason, we select the database here.
   118          #
   119          exec_sql "use crowdsec_test; $@"
   120          ;;
   121      *)
   122          about
   123          ;;
   124  esac;