health_check-3.1.0/0000755000004100000410000000000014054112036014141 5ustar www-datawww-datahealth_check-3.1.0/.travis.yml0000644000004100000410000001062114054112036016252 0ustar www-datawww-datadest: xenial os: linux # Released April 2018, Support ended: April 2023 # ruby 2.4.9, 2.5.3, 2.5.7, 2.6.5 (default) and 2.7.0 pre-installed on travis addons: apt: packages: - net-tools cache: bundler language: ruby notifications: email: on_success: change on_failure: always before_install: - gem update --system $RUBYGEMS_VERSION - gem --version - gem install bundler ${BUNDLER_VERSION:+-v} ${BUNDLER_VERSION} - gem install smarter_bundler - bundle --version - mkdir -p tmp/bundle bundler_args: "--binstubs" script: ./test/test_with_railsapp jobs: fast_finish: true allow_failures: - rvm: 3.0.1 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - rvm: 3.0.1 gemfile: test/rails_edge.gemfile env: - RAILS_VERSION=edge - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED - rvm: ruby-head gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED - rvm: ruby-head gemfile: test/rails_edge.gemfile env: - RAILS_VERSION=edge - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED include: # ------------------------------------- # Standard # ruby 5.0 Jun. 2016 - rvm: 2.2.2 gemfile: test/rails_5.0.gemfile env: - RAILS_VERSION=5.0 - MIDDLEWARE=no - RUBYGEMS_VERSION=2.7.11 - BUNDLER_VERSION=1.17.3 - rvm: 2.3.6 gemfile: test/rails_5.0.gemfile env: - RAILS_VERSION=5.0 - MIDDLEWARE=no # rails 5.1 sometime before May 2017 - rvm: 2.2.2 gemfile: test/rails_5.1.gemfile env: - RAILS_VERSION=5.1 - MIDDLEWARE=no - RUBYGEMS_VERSION=2.7.11 - BUNDLER_VERSION=1.17.3 - rvm: 2.3.6 gemfile: test/rails_5.1.gemfile env: - RAILS_VERSION=5.1 - MIDDLEWARE=no - BUNDLER_VERSION=1.17.3 - rvm: 2.4.3 gemfile: test/rails_5.1.gemfile env: - RAILS_VERSION=5.1 - MIDDLEWARE=no # ------------------------------------- # Middleware # ruby 5.0 Jun. 2016 - rvm: 2.2.2 gemfile: test/rails_5.0.gemfile env: - RAILS_VERSION=5.0 - MIDDLEWARE=yes - RUBYGEMS_VERSION=2.7.11 - BUNDLER_VERSION=1.17.3 - rvm: 2.3.6 gemfile: test/rails_5.0.gemfile env: - RAILS_VERSION=5.0 - MIDDLEWARE=yes # rails 5.1 sometime before May 2017 - rvm: 2.2.2 gemfile: test/rails_5.1.gemfile env: - RAILS_VERSION=5.1 - MIDDLEWARE=yes - RUBYGEMS_VERSION=2.7.11 - BUNDLER_VERSION=1.17.3 - rvm: 2.4.3 gemfile: test/rails_5.1.gemfile env: - RAILS_VERSION=5.1 - MIDDLEWARE=yes # rails 5.2 April 2018 # ------------------------------------- # INSTALLED AS MIDDLEWARE # rails 6.0 - rvm: 2.5.0 gemfile: test/rails_6.0.gemfile env: - RAILS_VERSION=6.0 - MIDDLEWARE=YES - rvm: 2.6.7 gemfile: test/rails_6.0.gemfile env: - RAILS_VERSION=6.0 - MIDDLEWARE=YES # Not testing past recommended ruby version as listed on https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html # -------------------------------------- - rvm: 2.5.0 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - rvm: 2.6.6 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - rvm: 2.7.3 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - rvm: 3.0.1 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED # ------------------ # FIXME # ------------------ - rvm: 3.0.1 gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - rvm: 3.0.1 gemfile: test/rails_edge.gemfile env: - RAILS_VERSION=edge - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED - rvm: ruby-head gemfile: test/rails_6.1.gemfile env: - RAILS_VERSION=6.1 - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED - rvm: ruby-head gemfile: test/rails_edge.gemfile env: - RAILS_VERSION=edge - MIDDLEWARE=YES - SMTP_STARTTLS=DISABLED health_check-3.1.0/test/0000755000004100000410000000000014054112036015120 5ustar www-datawww-datahealth_check-3.1.0/test/provision_vagrant0000644000004100000410000000546114054112036020623 0ustar www-datawww-data#!/bin/bash case `id` in *root*) ;; *) exec echo Must be run as root ;; esac chruby_version=0.3.9 chruby=chruby-${chruby_version} set -x set -eE -o functrace report_failure() { local lineno=$2 local fn=$3 local exitstatus=$4 local msg=$5 local lineno_fns=${1% 0} if [[ $lineno_fns != "0" ]] ; then lineno="${lineno} ${lineno_fns}" fi if [[ $exitstatus == 0 ]] ; then echo "${BASH_SOURCE[1]}: Finished!" else echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" fi } trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR id pwd export DEBIAN_FRONTEND=noninteractive find /tmp/got-apt-update -mtime -1 || ( apt-get update && touch /tmp/got-apt-update ) apt install --yes -q build-essential ruby ruby-dev sqlite3 libsqlite3-dev nodejs git git-core apt install --yes -q gcc autoconf bison libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev apt install --yes -q libgdbm3 # useful when debugging apt install --yes -q silversearcher-ag vim exuberant-ctags apt install --yes -q unattended-upgrades unattended-upgrade # The following is not required for testing, install if you are doing manual tests with extra gems # apt install --yes -q mysql-client mysql-server libmysqlclient-dev # apt install --yes -q libcurl4-openssl-dev libncurses5-dev libxml2-dev libxslt1-dev ( echo Install chruby [ -s ${chruby}.tar.gz ] || wget -q -O ${chruby}.tar.gz https://github.com/postmodern/chruby/archive/v${chruby_version}.tar.gz [ -d ${chruby} ] || tar -xzf ${chruby}.tar.gz cd ${chruby}/ ./scripts/setup.sh cat > /etc/profile.d/chruby.sh <<'EOF' if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then source /usr/local/share/chruby/chruby.sh #source /usr/local/share/chruby/auto fi EOF chmod a+r /etc/profile.d/chruby.sh ) ( [ -d ruby-build ] || git clone https://github.com/rbenv/ruby-build.git which ruby-build || PREFIX=/usr/local ./ruby-build/install.sh mkdir -p /opt/rubies for v in 2.2.2 do [ -x /opt/rubies/$v/bin/ruby ] || ( ruby-build $v /opt/rubies/$v ) [ -x /opt/rubies/${v}/bin/bundle ] || ( /opt/rubies/${v}/bin/gem install bundler -v '<2.0' ) done for v in 2.3.8 2.4.10 2.5.9 2.5.0 2.6.6 2.6.7 2.7.1 2.7.3 3.0.1 do [ -x /opt/rubies/$v/bin/ruby ] || ( ruby-build $v /opt/rubies/$v ) [ -x /opt/rubies/$v/bin/bundle ] || ( /opt/rubies/$v/bin/gem install bundler ) done ) echo Setup system ruby which bundle || gem install bundler || gem install bundler -v '<2.0' which bundle || gem install bundler -v '< 2.0' bundle --version set +x cat < 5.1.0' gem 'rake', '>= 0.8.7' # spring-watcher-listen was resolved to 2.0.1, which depends on # listen was resolved to 3.1.5, which depends on # ruby_dep # and ruby_dep 1.5 requires 2.2.3 or later gem 'ruby_dep', '~> 1.3.0' # REQUIRED gem 'listen', '<3.1.2' # REQUIRED group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED health_check-3.1.0/test/init_variables0000644000004100000410000000354114054112036020041 0ustar www-datawww-data#!/bin/bash # Any failure causes exit set -eE -o functrace report_failure() { local lineno=$2 local fn=$3 local exitstatus=$4 local msg=$5 local lineno_fns=${1% 0} if [[ $lineno_fns != "0" ]] ; then lineno="${lineno} ${lineno_fns}" fi if [[ $exitstatus == 0 ]] ; then echo "${BASH_SOURCE[1]}: Finished!" else echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" fi } trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR echo Setting RAILS_ENV=test RACK_ENV=test export RAILS_ENV=test RACK_ENV=test base_dir=$PWD tmp_dir=$base_dir/tmp railsapp=$tmp_dir/railsapp custom_file="$railsapp/tmp/custom_check.ok" catchall_file="$railsapp/tmp/catchall_route.enabled" success=successful rehash='' rbenv_which='which' if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then echo "Detected user installed rvm" elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then echo "Detected root installed rvm" elif [[ -d "$HOME/.rbenv" ]] ; then echo "Detected rbenv: `rbenv version`" rehash='rbenv rehash' rbenv_which='rbenv which' else printf "Note: Neither rvm nor rbenv was not found.\n" fi echo "Checking required commands exist:" for cmd in bash gem egrep ls tail kill find cpio do echo -n " " which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 ) done for cmd in ruby gem do echo -n " " $rbenv_which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 ) done rails="rails" rake="rake" if [ -x $base_dir/test/bin/rails ] then rails="$base_dir/test/bin/rails" rake="$base_dir/test/bin/rake" export PATH="$base_dir/test/bin:$PATH" fi if [ -x $railsapp/bin/rails ] then rails="$railsapp/bin/rails" rake="$railsapp/bin/rake" export PATH="$railsapp/bin:$PATH" fi echo "Using rails=$rails, rake=$rake" health_check-3.1.0/test/test_with_railsapp0000755000004100000410000006051214054112036020757 0ustar www-datawww-data#!/bin/bash route_prefix=medical_check # Any failure causes exit set -eE -o functrace report_failure() { local lineno=$2 local fn=$3 local exitstatus=$4 local msg=$5 local lineno_fns=${1% 0} if [[ $lineno_fns != "0" ]] ; then lineno="${lineno} ${lineno_fns}" fi if [[ $exitstatus == 0 ]] ; then echo "${BASH_SOURCE[1]}: Finished!" else echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" fi } trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR # Any failure causes exit set -eE -o functrace report_failure() { local lineno=$2 local fn=$3 local exitstatus=$4 local msg=$5 local lineno_fns=${1% 0} if [[ $lineno_fns != "0" ]] ; then lineno="${lineno} ${lineno_fns}" fi if [[ $exitstatus == 0 ]] ; then echo "${BASH_SOURCE[1]}: Finished!" else echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" fi } trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR export DISABLE_SPRING=1 cleanup_db() { echo Dropping database ... $rake db:drop echo Removing migrations ... rm -f db/migrate/* db/schema.rb case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in [0-9]*) echo 'Jruby requires the database to be created before the server is started: running rake db:migrate' $rake db:migrate ;; esac } case "$1" in redo) . test/init_variables cd $railsapp cleanup_db actual_rails_version=`$rails -v` ;; *) . test/setup_railsapp $1 ;; esac run_test=$2 cd $railsapp date > $custom_file rm -f $catchall_file case `egrep '^[^#]*MiddlewareHealthcheck' config/application.rb` in '') export has_middleware=false ;; ?*) export has_middleware=true ;; esac testurl="$base_dir/test/testurl" fake_smtp_server="$base_dir/test/fake_smtp_server" server_pid='' fake_smtp_pid='' pick_a_port() { while : do port=`expr 10000 + $RANDOM` # Check Tcp ports in Listen mode with No address resolution if (netstat -tln | egrep ":${port} .*:"); then echo "(Skipping used port)" else break fi done } start_server() { # restart migration list rm -rf db/migrate db/schema.rb mkdir -p db/migrate # Increment port each time to make sure we have not trouble with address/port already allocated pick_a_port host=http://127.0.0.1:${port} bundle_prefix='' if [ -f Gemfile ] then bundle_prefix='bundle exec' fi server_arg=${RAILS_SERVER:-webrick} case "$actual_rails_version" in *' '[12345].*) ;; *) server_arg="-u $server_arg" ;; esac echo "start_server called using: `env | egrep '^RAILS|^RACK|^PATH='` $bundle_prefix $server_arg" case "$server_arg" in *puma) $bundle_prefix puma -b tcp://127.0.0.1:$port & ;; *passenger) $bundle_prefix passenger start -p $port & ;; *thin) $bundle_prefix thin start -p $port & ;; *unicorn) $bundle_prefix unicorn_rails -l 127.0.0.1:$port & ;; *) if [ -x script/server ] then echo Starting server on port $port using $bundle_prefix ./script/server ... $bundle_prefix ./script/server $server_arg -p $port & else echo Starting server on port $port using $rails s ... $bundle_prefix $rails server $server_arg -p $port & fi ;; esac server_pid=$! echo Server pid: $server_pid sleep 3 echo echo 'Checking server is up ...' for i in 1 2 3 4 5 6 do if $testurl ${host}/static.txt ; then break fi if kill -0 $server_pid ; then echo "waiting ${i} ..." else echo "ERROR: Server has died!!" exit 3 fi done } stop_server() { case "$server_pid" in [0-9]*) echo ======================================================== echo "Killing rails server [pid: $server_pid]" kill -QUIT $server_pid || echo server has already exited .. if [ -x bin/spring ] ; then echo Stopping spring ... bin/spring stop || echo spring had already exited .. fi sleep 1 kill -9 $server_pid || echo server had already exited ... sleep 1 # needed for unicorn - it doesn't die when it is supposed to killall "$server_arg" || echo server and child processes had already stopped ... ;; esac case "$fake_smtp_pid" in [0-9]*) echo ======================================================== echo "Killing fake smtp server [pid: $fake_smtp_pid]" kill -QUIT $fake_smtp_pid || echo fake_smtp had already exited .. sleep 2 kill -9 $fake_smtp_pid || echo fake_smtp had already exited .. ;; esac server_pid='' fake_smtp_pid='' ps -f echo Waiting for sub processes to complete ... wait echo Finished waiting for sub processes, sleeping 2 seconds ... sleep 2 } finish() { set +e echo ======================================================== echo TEST ${1:-FAILED} echo ======================================================== echo Result of: ls -lR $railsapp/log $railsapp/db ls -lR $railsapp/log $railsapp/db if [ -s $railsapp/log/test.log ] then echo ======================================================== echo Last 50 lines of test log tail -50 $railsapp/log/test.log fi if [ -s $railsapp/log/production.log ] then echo ======================================================== echo Last 50 lines of production log tail -50 $railsapp/log/production.log fi stop_server trap "" 0 echo ======================================================== ps uxf || echo ps failed echo ======================================================== echo TEST ${1:-FAILED}, exiting with status ${2:-2} echo ======================================================== exit ${2:-2} } trap "finish FAILED 1" 0 common_tests() { test_no=$1 if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: CHECKING routes exist..." $rake routes | tee /tmp/t$$ echo case `egrep ${route_prefix} /tmp/t$$ || true` in '') echo WARNING - routes for ${route_prefix} not listed! ;; esac echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING can get a static file ..." case "$RAILS_ENV=`egrep '^\s*config.serve_static_[asetfil]* *= *false' config/environments/${RAILS_ENV}.rb`" in production*static*false*) echo " SKIPPED (disabled in production)" ;; *) grep serve_static_files config/environments/${RAILS_ENV}.rb config/[a-z]*.rb || echo no serve_static_files entry $testurl ${host}/static.txt 200 text/plain STATIC-FILE ;; esac echo fi rm -f tmp/health_check_success.txt tmp/health_check_failure.txt test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING can get an example controller ..." $testurl ${host}/example 200 text/plain 'example page' echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING direct call to catchall method on example controller ..." $testurl ${host}/example/catchall 200 text/plain 'catch all route' echo fi if [ -f tmp/health_check_success.txt ] ; then echo "FAIL tmp/health_check_success.txt exists on line $LINENO" else echo "PASS tmp/health_check_success.txt is missing as expected on line $LINENO" fi if [ -f tmp/health_check_failure.txt ] ; then echo "FAIL tmp/health_check_failure.txt exists on line $LINENO" else echo "PASS tmp/health_check_failure.txt is missing as expected on line $LINENO" fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass with no database migrations ..." ls db/migrate rm -f tmp/health_check_success.txt tmp/health_check_failure.txt $testurl ${host}/${route_prefix}/migration 200 text/plain $success if [ -f tmp/health_check_success.txt ] ; then echo "PASS tmp/health_check_success.txt exists as expected on line $LINENO" cat tmp/health_check_success.txt else echo "FAIL tmp/health_check_success.txt is missing on line $LINENO" fi if [ -f tmp/health_check_failure.txt ] ; then echo "FAIL tmp/health_check_failure.txt exists on line $LINENO" else echo "PASS tmp/health_check_failure.txt is missing as expected on line $LINENO" fi echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should fail without initial database migration ..." cp $base_dir/test/migrate/nine/* db/migrate ls db/migrate rm -f tmp/health_check_success.txt tmp/health_check_failure.txt $testurl ${host}/${route_prefix}/migration 550 text/plain failed if [ -f tmp/health_check_success.txt ] ; then echo "FAIL tmp/health_check_success.txt exists on line $LINENO" else echo "PASS tmp/health_check_success.txt is missing as expected on line $LINENO" fi if [ -f tmp/health_check_failure.txt ] ; then echo "PASS tmp/health_check_failure.txt exists as expected on line $LINENO" cat tmp/health_check_failure.txt else echo "FAIL tmp/health_check_failure.txt is missing on line $LINENO" fi echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should pass without initial database migration (since it ignores the difference) ..." $testurl ${host}/${route_prefix}/database 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/site should pass ..." $testurl ${host}/${route_prefix}/site 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after initial database migration ..." $rake db:migrate $testurl ${host}/${route_prefix}/migration 200 text/plain $success echo fi #test with coruppted DB rm -rf db.bak cp -R db db.bak for f in db/*.sqlite3 do echo CORRUPTED > $f done test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should fail if the database has been corrupted ..." $testurl ${host}/${route_prefix}/database 550 text/plain failed: echo fi export HIDE_ERROR_RESPONSE=true stop_server start_server test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should have response body 'health_check failed' but no error details if include_error_in_response_body is false" $testurl ${host}/${route_prefix}/database 550 text/plain 'health_check failed' failed: echo fi unset HIDE_ERROR_RESPONSE stop_server start_server test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/site should pass ..." $testurl ${host}/${route_prefix}/site 200 text/plain $success if $has_middleware; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should pass ..." $testurl ${host}/${route_prefix}/middleware_site 200 text/plain $success fi echo fi # Restore database cp -f db.bak/*.sqlite3 db/ test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should fail without all migrations ..." cp $base_dir/test/migrate/twelve/* db/migrate ls db/migrate $testurl ${host}/${route_prefix}/migration 550 text/plain failed echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after both database migrations ..." $rake db:migrate $testurl ${host}/${route_prefix}/migration 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after both database migrations ..." $rake db:migrate $testurl ${host}/${route_prefix}/migration 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/email should fail without smtp available ..." $testurl ${host}/${route_prefix}/email 550 text/plain failed echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/email should pass with smtp available ..." $fake_smtp_server & fake_smtp_pid=$! sleep 5 $testurl ${host}/${route_prefix}/email 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should fail without smtp available ..." $testurl ${host}/${route_prefix} 550 text/plain failed echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/all should fail without smtp available ..." $testurl ${host}/${route_prefix} 550 text/plain failed echo fi kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should pass with smtp available ..." $fake_smtp_server & fake_smtp_pid=$! sleep 5 $testurl ${host}/${route_prefix} 200 text/plain $success echo fi kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/all should pass with smtp available ..." $fake_smtp_server & fake_smtp_pid=$! sleep 5 $testurl ${host}/${route_prefix}/all 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/pass should pass ..." $testurl ${host}/${route_prefix}/pass 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom should pass ..." $testurl ${host}/${route_prefix}/custom 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.html should pass (returning plain text) ..." $testurl ${host}/${route_prefix}/custom.html 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.json should pass ..." $testurl ${host}/${route_prefix}/custom.json 200 application/json '"healthy":true' $testurl ${host}/${route_prefix}/custom.json 200 application/json "\"message\":\"$success\"" echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.xml should pass ..." $testurl ${host}/${route_prefix}/custom.xml 200 application/xml 'true' $testurl ${host}/${route_prefix}/custom.xml 200 application/xml "$success" echo fi test_no=`expr 1 + $test_no` rm -f $custom_file if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom should fail when custom returns string ..." $testurl ${host}/${route_prefix}/custom 550 text/plain failed echo fi if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/pass should pass even if other custom test returns string ..." $testurl ${host}/${route_prefix}/pass 200 text/plain $success echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should fail when custom check fails ..." $testurl ${host}/${route_prefix} 550 text/plain "$custom_file is missing!" echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}.json (all) should fail when custom check fails ..." $testurl ${host}/${route_prefix}.json 555 application/json '"healthy":false' $testurl ${host}/${route_prefix}.json 555 application/json "$custom_file is missing!" echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}.xml (all) should fail when custom check fails ..." $testurl ${host}/${route_prefix}.xml 555 application/xml 'false' echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then if $has_middleware; then echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should pass ..." $testurl ${host}/${route_prefix}/middleware_site 200 text/plain $success else echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should fail ..." $testurl ${host}/${route_prefix}/middleware_site 550 text/plain failed fi echo fi test_no=`expr 1 + $test_no` if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then echo "${test_no}[line $LINENO]: TESTING log files to check for deprecation warnings ..." if egrep ' is deprecated|DEPRECATION WARNING' $railsapp/log/[tp][er][so][td]*.log then echo Found deprecation warnings - failed test exit 99 fi fi date > $custom_file } # required for rails 4.1+ in production mode export SECRET_KEY_BASE=cf2f49c38a3fe67416ddf680f4f3187c0fce7dd1b9b117b34d195df75b274e08a04877e23803b2fdf1aa9a655269d94bc4888aa325cf7e721cc47368cfe56a80 # required for rails 5 to server static files export RAILS_SERVE_STATIC_FILES=on export IP_WHITELIST='123.123.123.123' unset AUTH_USER unset AUTH_PASSWORD case "$run_test" in ''|[12]) echo ======================================================== echo TESTING whitelist ban WITHOUT CATCHALL in test env echo ======================================================== export RAILS_ENV=test RACK_ENV=test start_server echo '1: TESTING controller prohibited by ip...' expected_status=403 $testurl ${host}/${route_prefix}/site $expected_status if $has_middleware; then echo echo '2: TESTING middleware prohibited by ip...' expected_status=403 $testurl ${host}/${route_prefix}/middleware $expected_status fi ;; esac export IP_WHITELIST='' export AUTH_USER='someone' export AUTH_PASSWORD='secret' case "$run_test" in ''|[3456789]|10) echo ======================================================== echo TESTING basic auth, no whitelist WITHOUT CATCHALL in test env echo ======================================================== export RAILS_ENV=test RACK_ENV=test case "$run_trest" in '') stop_server cleanup_db ;; esac start_server expected_status=401 echo '3: TESTING controller without authentication insists on authentication ...' AUTH_USER= $testurl ${host}/${route_prefix}/site $expected_status echo '4: TESTING controller with wrong password insists on authentication ...' AUTH_PASSWORD=wrong $testurl ${host}/${route_prefix}/site $expected_status echo '5: TESTING controller with wrong user insists on authentication ...' AUTH_USER=wrong $testurl ${host}/${route_prefix}/site $expected_status echo '6: TESTING controller with authentication works ...' expected_status=200 $testurl ${host}/${route_prefix}/site $expected_status if $has_middleware; then echo echo '7: TESTING middleware without authentication insists on authentication ...' expected_status=401 AUTH_USER= $testurl ${host}/${route_prefix}/middleware $expected_status echo echo '8: TESTING middleware with wrong password insists on authentication ...' AUTH_PASSWORD=wrong $testurl ${host}/${route_prefix}/middleware $expected_status echo echo '9: TESTING middleware with wrong user insists on authentication ...' AUTH_USER=wrong $testurl ${host}/${route_prefix}/middleware $expected_status echo echo '10: TESTING middleware with authentication works ...' expected_status=200 $testurl ${host}/${route_prefix}/middleware $expected_status else echo echo "Skipped middleware tests as it is not configured..." fi ;; esac unset AUTH_USER unset AUTH_PASSWORD case "$run_test" in ''|1??) echo ======================================================== echo TESTING WITHOUT CATCHALL, no whitelist or user in test env echo ======================================================== export RAILS_ENV=test RACK_ENV=test case "$run_trest" in '') stop_server cleanup_db ;; esac start_server # get a static file echo echo 'TESTING no catchall route active ...' expected_status=404,500,502 $testurl ${host}/another/url $expected_status echo 'TESTING default route has been overriden ...' expected_status=404,500,502 $testurl ${host}/health_check/site $expected_status common_tests 100 ;; esac export IP_WHITELIST='127.0.0.1' export AUTH_USER='someone' export AUTH_PASSWORD='secret' case "$run_test" in ''|2??) echo ======================================================== echo TESTING WITH CATCHALL with whitelist and user in ${RAILS_ENV2:-production} env echo ======================================================== export RAILS_ENV=${RAILS_ENV2:-production} RACK_ENV=${RAILS_ENV2:-production} case "$run_trest" in '') stop_server cleanup_db ;; esac date > $catchall_file start_server echo echo 'TESTING catchall route active ...' $testurl ${host}/another/url 200 text/plain 'catch all route' echo common_tests 200 ;; esac rm -f $catchall_file finish PASSED 0 exit 0 # vi: sw=4 ai sm: health_check-3.1.0/test/fake_smtp_server0000755000004100000410000000745314054112036020416 0ustar www-datawww-data#!/usr/bin/env ruby require 'socket' require 'openssl' class FakeSmtpServer def initialize(port) @port = port @socket = TCPServer.new(@port) @client = @orig_client = nil end def start return unless @client.nil? puts "fake_smtp_server: Waiting for one connection to port #{@port} ..." @client = @socket.accept send '220 dummy-smtp.example.com SMTP' cmd = receive while cmd !~ /^QUIT\r/ if cmd =~ /^HELO(.*)\r/ if ENV['FAIL_SMTP'] == 'HELO' send '550 Access Denied – Invalid HELO name' else send '250-Welcome to a dummy smtp server' unless ENV['SMTP_STARTTLS'] == 'DISABLED' send '250-STARTTLS' end send '250-AUTH PLAIN LOGIN' send '250 Ok' end elsif cmd =~ /^AUTH(.*)\r/ if ENV['FAIL_SMTP'] == 'AUTH' send '535 5.7.8 Authentication credentials invalid' else send '235 2.7.0 Authentication successful' end elsif cmd =~ /^STARTTLS\r/ if ENV['SMTP_STARTTLS'] == 'DISABLED' send '502 STARTTLS is disabled!' end send '220 Ready to start TLS' if ENV['FAIL_SMTP'] == 'STARTTLS' cmd = receive return close end @orig_client = @client @client = tlsconnect(@client) else send '502 I am so dumb I only understand HELO, AUTH, STARTTLS and QUIT which always return a success status' end cmd = receive end send '221 Bye Bye' close end private def close @client.close unless @client.nil? @orig_client.close unless @orig_client.nil? end def send(line) @client.puts line puts "-> #{line}" end def receive line = @client.gets puts "<- #{line}" line end def ssl_socket(client, context) OpenSSL::SSL::SSLSocket.new(client, context) end def ssl_context @_ssl_context ||= begin key, cert = generate_certificate context = OpenSSL::SSL::SSLContext.new context.key = key context.cert = cert context.verify_mode = OpenSSL::SSL::VERIFY_NONE context.min_version = nil context end end # Pass socket from TCPServer.new accept def tlsconnect(client) ssl_client = ssl_socket(client, ssl_context) puts '=> TLS connection started' ssl_client.accept puts '=> TLS connection established' ssl_client end def generate_certificate key = OpenSSL::PKey::RSA.new(2048) name = OpenSSL::X509::Name.parse('CN=localhost') cert = OpenSSL::X509::Certificate.new cert.version = 2 cert.serial = 0 cert.not_before = Time.now cert.not_after = Time.now + 3600 cert.public_key = key.public_key cert.subject = name extension_factory = OpenSSL::X509::ExtensionFactory.new nil, cert cert.add_extension extension_factory.create_extension('basicConstraints', 'CA:FALSE', true) cert.add_extension extension_factory.create_extension('keyUsage', 'keyEncipherment,dataEncipherment,digitalSignature') cert.add_extension extension_factory.create_extension('subjectKeyIdentifier', 'hash') cert.issuer = name cert.sign key, OpenSSL::Digest::SHA256.new [key, cert] end end FakeSmtpServer.new(3555).start puts 'fake_smtp_server: Exiting now the conversation has finished.' exit 0 # Tested with irb script: # require 'net/smtp' # # status = '' # begin # if @skip_external_checks # status = '250' # else # smtp = Net::SMTP.new('localhost', 3555) # smtp.enable_starttls # smtp.open_timeout = 10 # smtp.read_timeout = 10 # smtp.start('domain', 'user_name', 'password', :plain) do # status = smtp.helo('domain').status # end # end # rescue Exception => ex # status = ex.to_s # end # (status =~ /^250/) ? 'PASS' : "FAILED SMTP: #{status || 'unexpected error'}. " health_check-3.1.0/test/rails_6.1.gemfile0000644000004100000410000000135414054112036020153 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION gem 'rails', '~> 6.1.0' gem 'rake', '>= 0.8.7' group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', require: !ENV['RESQUE'].nil? # REQUIRED # s3 check gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED gem 'webpacker', '~> 4.0.7' # REQUIRED gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 gem 'webrick' # REQUIRED for ruby 3.0 health_check-3.1.0/test/rails_edge.gemfile0000644000004100000410000000177514054112036020562 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' # Bundle edge Rails instead: ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION gem 'rails' gem 'rake' gem 'rack' group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3' end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED # Initial Gemfile has therubyracer commented out gem 'therubyrhino', platform: :jruby # REQUIRED gem 'therubyracer', platform: :ruby # REQUIRED gem 'webpacker', '~> 4.0.7' # REQUIRED gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 gem 'webrick' # REQUIRED for ruby 3.0 health_check-3.1.0/test/testurl0000755000004100000410000000353014054112036016551 0ustar www-datawww-data#!/usr/bin/env ruby require 'net/http' require 'uri' def open(url) parsed_uri = URI.parse(url) http = Net::HTTP.new(parsed_uri.host, parsed_uri.port) req = Net::HTTP::Get.new(parsed_uri.request_uri) req.basic_auth(ENV['AUTH_USER'], ENV['AUTH_PASSWORD'].to_s) if ENV['AUTH_USER'] http.request(req) end response = open(ARGV[0]) rescue nil unless response i=0 print "waiting.." while i < 120 and not response #puts 'RESPONSE:', response.inspect print "." STDOUT.flush i += 1 sleep(1) response = open(ARGV[0]) rescue nil end unless response puts "\nFAIL: timed out after waiting #{i} seconds" exit 9 end puts "\n got url content after waiting #{i} seconds" end page_content = response.body puts " response code: #{response.code} #{response.message}" response.header.each_header {|key,value| puts " #{key}: #{value}" } puts " body: #{page_content}" if ARGV[1] and ARGV[1] != '' if ARGV[1].split(',').include?(response.code) puts "PASS (response code was #{response.code} which matched #{ARGV[1]})" else puts "FAIL (response code was #{response.code}, expected #{ARGV[1]})" exit 1 end end if ARGV[2] and ARGV[2] != '' if response.content_type == ARGV[2] puts "PASS (content type was #{response.content_type})" else puts "FAIL (content type was #{response.content_type}, expected #{ARGV[2]})" exit 2 end end if ARGV[3] and ARGV[3] != '' if page_content.to_s.include? ARGV[3] puts "PASS (found #{ARGV[3]})" else puts "FAIL (expected to find #{ARGV[3]}) - page contents:" , page_content.to_s, 'END-OF-CONTENTS' exit 3 end end if ARGV[4] and ARGV[4] != '' if !page_content.to_s.include? ARGV[4] puts "PASS (did not find #{ARGV[4]})" else puts "FAIL (found #{ARGV[4]}) - page contents:" , page_content.to_s, 'END-OF-CONTENTS' exit 3 end end exit 0 health_check-3.1.0/test/rails_6.0.gemfile0000644000004100000410000000146214054112036020152 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION gem 'rails', '~> 6.0.0' gem 'rake', '>= 0.8.7' gem 'listen', '<3.1.2' # REQUIRED group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED gem 'webpacker', '~> 4.0.7' # REQUIRED health_check-3.1.0/test/rails_5.2.gemfile0000644000004100000410000000174614054112036020160 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION gem 'rails', '~> 5.2.0' gem 'rake', '>= 0.8.7' # spring-watcher-listen was resolved to 2.0.1, which depends on # listen was resolved to 3.1.5, which depends on # ruby_dep # and ruby_dep 1.5 requires 2.2.3 or later gem 'ruby_dep', '~> 1.3.0' # REQUIRED gem 'listen', '<3.1.2' # REQUIRED group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED health_check-3.1.0/test/rails_6.2.gemfile0000644000004100000410000000154514054112036020156 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION gem 'rails', '~> 6.2.0' gem 'rake', '>= 0.8.7' group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED gem 'webpacker', '~> 4.0.7' # REQUIRED gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 gem 'webrick' # REQUIRED for ruby 3.0 health_check-3.1.0/test/migrate/0000755000004100000410000000000014054112036016550 5ustar www-datawww-datahealth_check-3.1.0/test/migrate/twelve/0000755000004100000410000000000014054112036020056 5ustar www-datawww-datahealth_check-3.1.0/test/migrate/twelve/9_create_countries.rb0000644000004100000410000000030714054112036024171 0ustar www-datawww-dataclass CreateCountries < ActiveRecord::Migration[5.0] def self.up create_table :countries do |t| t.column :name, :string end end def self.down drop_table :countries end end health_check-3.1.0/test/migrate/twelve/011_create_roles.roles.rb0000644000004100000410000000027314054112036024560 0ustar www-datawww-dataclass CreateRoles < ActiveRecord::Migration[5.0] def self.up create_table :roles do |t| t.column :name, :string end end def self.down drop_table :roles end end health_check-3.1.0/test/migrate/twelve/012_create_users.rb0000644000004100000410000000031114054112036023444 0ustar www-datawww-dataclass CreateUsers < ActiveRecord::Migration[5.0] def self.up create_table "users", force: true do |t| t.column :name, :string end end def self.down drop_table :users end end health_check-3.1.0/test/migrate/nine/0000755000004100000410000000000014054112036017501 5ustar www-datawww-datahealth_check-3.1.0/test/migrate/nine/9_create_countries.rb0000644000004100000410000000030714054112036023614 0ustar www-datawww-dataclass CreateCountries < ActiveRecord::Migration[5.0] def self.up create_table :countries do |t| t.column :name, :string end end def self.down drop_table :countries end end health_check-3.1.0/test/migrate/empty/0000755000004100000410000000000014054112036017706 5ustar www-datawww-datahealth_check-3.1.0/test/migrate/empty/do_not_remove.txt0000644000004100000410000000001414054112036023301 0ustar www-datawww-dataDon't removehealth_check-3.1.0/test/rails_5.0.gemfile0000644000004100000410000000141714054112036020151 0ustar www-datawww-data# Gemfile for health_test testing source 'https://rubygems.org' ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION gem 'rails', '~> 5.0.0' gem 'rake', '>= 0.8.7' gem 'listen', '<3.1.2' # REQUIRED group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', "~> 1.3.7" end gem 'shoulda' end # redis based checks gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED # s3 check gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED health_check-3.1.0/test/setup_railsapp0000755000004100000410000004170314054112036020106 0ustar www-datawww-data#!/bin/bash route_prefix=medical_check # Any failure causes exit set -eE -o functrace report_failure() { local lineno=$2 local fn=$3 local exitstatus=$4 local msg=$5 local lineno_fns=${1% 0} if [[ $lineno_fns != "0" ]] ; then lineno="${lineno} ${lineno_fns}" fi if [[ $exitstatus == 0 ]] ; then echo "${BASH_SOURCE[1]}: Finished!" else echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" fi } trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR case "$1" in [0-9]*) export BUNDLE_GEMFILE=$PWD/test/rails_$1.gemfile ;; esac while : do case "$BUNDLE_GEMFILE" in */test/rails_[Qq].gemfile) echo "Aborting..." exit 2 ;; */test/rails_edge.gemfile|*/test/rails_[0-9].[0-9]*.gemfile) if [ -f "$BUNDLE_GEMFILE" ]; then break fi ;; esac echo "== SELECT GEMFILE ==" echo echo "Please select the gemfile for the required rails series:" (cd test ; ls rails*gemfile | ruby -p -e '$_.sub!(/rails_(.*).gemfile/, " \\1")' ) echo echo -n "Enter choice (or q to quit): " read x export BUNDLE_GEMFILE=$PWD/test/rails_$x.gemfile done if [ -z "$MIDDLEWARE" ]; then echo -n "Add as middleware [N/y] : " read MIDDLEWARE export MIDDLEWARE fi rm -rf tmp/Gemfile* tmp/railsapp tmp/bin tmp/gems test/bin test/rails*.gemfile.lock mkdir -p tmp/gems . test/init_variables if $rbenv_which bundle ; then echo Bundler is installed else gem install bundler ${BUNDLER_VERSION:+-v ${BUNDLER_VERSION}} $rehash fi echo "Running bundle with BUNDLE_GEMFILE=$BUNDLE_GEMFILE ..." if ! smarter_bundle ; then echo "Test aborted (missing required gems)" exit 2 elif [ ! -s $BUNDLE_GEMFILE.lock ] ; then echo "Error: smarter_bundler return OK status BUT lock file ($BUNDLE_GEMFILE.lock) is missing!" exit 3 else echo bundle passed - lock file contains: cat $BUNDLE_GEMFILE.lock echo fi $rehash rails="$base_dir/test/bin/rails" rake="$base_dir/test/bin/rake" echo Checking $rails is present ... [ -f $rails -a -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} exec rake rails:update:bin || echo '(ignored rake rails:update:bin exit status)' [ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs railties || echo '(ignored bundle exit status)' [ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rails || echo '(ignored bundle exit status)' if [ ! -f $rails ]; then echo "Test aborted (unable to create $rails)" exit 2 fi if [ ! -f $rake ]; then echo "Running bundle binstubs rake ..." if ! bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rake || [ ! -f $rake ]; then echo "Test aborted (unable to create $rake)" exit 2 fi fi actual_rails_version=`$rails -v` [ -d lib/health_check ] || exec echo setup_railsapp MUST be executed in the base of the health_check gem/clone of git repository export GEM_PATH="$tmp_dir/gems:`gem environment gempath`" echo Set GEM_PATH=$GEM_PATH echo Installing health_check as a gem into $tmp_dir/gems rm -f pkg/health_check-*.gem if env GEM_HOME=$tmp_dir/gems $rake install then echo rake install passed else echo rake install failed! running gem install pkg/health_check-*.gem manually to see error message: env GEM_HOME=$tmp_dir/gems gem install pkg/health_check-*.gem echo gem install worked, but flagging it as a FAIL anyway since rake install failed! exit 2 fi echo Gems in tmp/gems: ls tmp/gems echo Environment: env | egrep 'TRAVIS|RAILS|RUBY|_ENV|GEM|BUNDLE' || echo "No relevant variables set" cd $tmp_dir case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in [0-9]*) db=jdbcsqlite3 # Appears to need a bit extra time ;; *) db=sqlite3 ;; esac echo "Creating $actual_rails_version app in $tmp_dir/railsapp using adapter $db" case "$actual_rails_version" in *' '[345].*) args="--skip-bundle -d $db" case "$BUNDLE_GEMFILE" in *rails_edge.gemfile) $rails new railsapp $args --edge ;; *) $rails new railsapp $args ;; esac ;; *' '[6].*) args="--skip-bundle -d $db --skip-git --skip-bootsnap" case "$BUNDLE_GEMFILE" in *rails_edge.gemfile) $rails new railsapp $args --edge ;; *) $rails new railsapp $args ;; esac ;; *) echo "Unknown rails version" ;; esac cd $railsapp [ -z "$rehash" ] || rbenv local `rbenv version-name` echo "Changed current directory to railsapp root: $railsapp" echo "Fixing rdoc require in Rakefile if needed" ruby -p -i.bak -e '$_.gsub!(/rake.rdoctask/, "rdoc/task")' Rakefile echo "Configuring mailer to point to fake_smtp_server port 3555" cat >> config/environment.rb <<'!EOF!' ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { address: "localhost", port: 3555 } !EOF! echo Adding an initializer for health_check gem ... mkdir -p config/initializers tee config/initializers/health_check.rb <> Gemfile case "$RAILS_SERVER" in webrick|'') echo "Using default webrick server" ;; *) echo "Adding $RAILS_SERVER gem to Gemfile (for use as server)" echo "gem '$RAILS_SERVER'" >> Gemfile ;; esac TAB=$'\t' QUOTES='"'"'" case "$actual_rails_version" in *' '5.0*) if egrep -i 'gem.*sqlite3' Gemfile ; then # Can't do this as a require as we may go back to testing JRuby echo Force sqlite to 1.3.13+ version for Rails 5.0 ... gem=sqlite3 sed -i.bak -e "s/^\([ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]${gem}[${QUOTES}]\)\(.*\)$/\1, '~> 1.3.13' # overriden: \2/" Gemfile fi ;; esac if egrep -q REQUIRED ${INITIAL_BUNDLE_GEMFILE} ; then sed -n "s/^[ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]\([^${QUOTES}]*\)[${QUOTES}].*REQUIRED.*/\1/p" ${INITIAL_BUNDLE_GEMFILE} | while read gem do echo "Commenting out gem '$gem' line in Gemfile" sed -i.bak -e "s/^\([ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]${gem}[${QUOTES}].*\)$/# overriden by REQUIRED below: \1/" Gemfile done echo Adding Required gems egrep REQUIRED ${INITIAL_BUNDLE_GEMFILE} | tee -a Gemfile else echo No required gems to be added to Gemfile fi echo echo ================= $PWD/Gemfile =================== cat Gemfile echo echo ================================================== echo running smarter_bundle install smarter_bundle install $rehash if egrep webpacker Gemfile && [ ! -f config/webpacker.yml ] ; then echo completing setup by running rails webpacker:install ... $rails webpacker:install fi echo "Using binstubs in $railsapp/bin for rails and rake commands" rails="$railsapp/bin/rails" rake="$railsapp/bin/rake" echo Checking $rails is present ... [ -f $rails -a -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} exec rake rails:update:bin || echo '(ignored rake rails:update:bin exit status)' [ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs railties || echo '(ignored bundle exit status)' [ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rails || echo '(ignored bundle exit status)' if [ ! -f $rails ]; then echo "Test aborted (unable to create $rails)" exit 2 fi echo Checking $rake is present ... [ -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rake || echo '(ignored bundle exit status)' if [ ! -f $rake ]; then echo "Test aborted (unable to create $rake)" exit 2 fi $rehash # Fix for rvm, otherwise bundle run from rails create fails export PATH="`pwd`/bin:$PATH" echo ================= $PWD/Gemfile.lock =================== cat Gemfile.lock echo ================================================== echo for e in test ${RAILS_ENV2:-production} do if [ -f config/environments/$e.rb ]; then echo ======== config/environments/$e.rb ================ sed -i.bak -e 's/config.serve_static_assets = false/config.serve_static_assets = true # NOTE: health_check test: changed to true/' \ -e 's/config.active_record.migration_error = :page_load/# & # NOTE: health_check test: commented out/' \ config/environments/$e.rb cat config/environments/$e.rb || true echo fi done if egrep -q 'bootsnap.setup' config/boot.rb ; then echo Commenting out bootsnap from config/boot.rb sed -i.bak -e 's/^\(.*bootsnap.setup\)/# \1/' config/boot.rb echo "============== $PWD/config/boot.rb =============" cat config/boot.rb echo ================================================== fi rm -rf tmp/cache/bootsnap* echo echo "============== $PWD/config/environment.rb =============" cat config/environment.rb echo echo ========================================================= case $db in jdbcsqlite3) for e in test ${RAILS_ENV2:-production} do echo export RAILS_ENV=$e RACK_ENV=$e echo "Jruby requires the database to be created before the server is started: running RAILS_ENV=$e rake db:migrate" $rake db:migrate echo done ;; esac echo STATIC-FILE > public/static.txt cat > public/ajax_example.html <<'EOF' Example static and dynamic calls to health_check

Static calls

Dynamic calls

EOF cat > app/controllers/example_controller.rb <<'EOF' class ExampleController < ApplicationController def index render plain: 'example page' end def catchall render plain: 'catch all route' end end EOF echo ======================================= case "$MIDDLEWARE" in [Yy]*) if [ -s config/application.rb ]; then mv -f config/application.rb config/application.rb-old ( ruby -n -e 'print if not /^ *end/..9999' config/application.rb-old | tee /tmp/t$$ if [ ! -s /tmp/t$$ ]; then echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 sed -e '/^ *end/,$d' config/application.rb-old fi echo " # -----------------------------------------" echo " # START OF SECTION FOR TESTING HEALTH_CHECK" echo " config.middleware.insert_after Rails::Rack::Logger, HealthCheck::MiddlewareHealthcheck" echo " # END OF SECTION FOR TESTING HEALTH_CHECK" echo " # ---------------------------------------" ruby -n -e 'print if /^ *end/..9999' config/application.rb-old | tee /tmp/t$$ if [ ! -s /tmp/t$$ ]; then echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 sed -n -e '/^ *end/,$p' config/application.rb-old fi ) 3>&1 > config/application.rb #echo =============== config/application.rb-old ======================== #cat config/application.rb-old echo =============== $PWD/config/application.rb ======================== cat config/application.rb else echo FAILED: NO config/application.rb file!! exit 2 fi echo ======================================= ;; esac if [ -s config/routes.rb ]; then mv -f config/routes.rb config/routes.rb-old ( ruby -n -e 'print if not /^end/..9999' config/routes.rb-old | tee /tmp/t$$ if [ ! -s /tmp/t$$ ]; then echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 sed -e '/^end/,$d' config/routes.rb-old fi # rails 3.0+ echo " # -----------------------------------------" echo " # START OF SECTION FOR TESTING HEALTH_CHECK" echo " get 'example/catchall(.:format)', controller: :example, action: :catchall" echo " get 'example(.:format)', controller: :example, action: :index" echo " if File.exists?('$catchall_file')" echo " health_check_routes" echo " # CATCH ALL ROUTE" echo " get '*path', controller: :example, action: :catchall" echo " end" echo " # END OF SECTION FOR TESTING HEALTH_CHECK" echo " # ---------------------------------------" ruby -n -e 'print if /^end/..9999' config/routes.rb-old | tee /tmp/t$$ if [ ! -s /tmp/t$$ ]; then echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 sed -n -e '/^end/,$p' config/routes.rb-old fi ) 3>&1 > config/routes.rb #echo =============== config/routes.rb-old ======================== #cat config/routes.rb-old echo =============== config/routes.rb ======================== cat config/routes.rb else echo FAILED: NO config/routes.rb file!! exit 2 fi echo ======================================= echo echo "Created $actual_rails_version app in $railsapp using adapter $db" echo -n "Using " ruby --version health_check-3.1.0/init.rb0000644000004100000410000000003014054112036015422 0ustar www-datawww-datarequire 'health_check' health_check-3.1.0/.gitignore0000644000004100000410000000061114054112036016127 0ustar www-datawww-data# Suggested by bundler *.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp .ruby-version ## PROJECT::SPECIFIC bin/ test/bin/ railsapps/ test/*.gemfile.lock ,* .vagrant ubuntu*console.log # See: https://gist.github.com/ianheggie/9327010 # for Global git ignore for OS/IDE/temp/backup files health_check-3.1.0/.document0000644000004100000410000000007414054112036015761 0ustar www-datawww-dataREADME.rdoc lib/**/*.rb bin/* features/**/*.feature LICENSE health_check-3.1.0/health_check.gemspec0000644000004100000410000000244714054112036020117 0ustar www-datawww-data# -*- encoding: utf-8 -*- lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'health_check/version' Gem::Specification.new do |gem| gem.name = "health_check" gem.version = HealthCheck::VERSION gem.required_rubygems_version = Gem::Requirement.new(">= 0") if gem.respond_to? :required_rubygems_version= gem.authors = ["Ian Heggie"] gem.email = ["ian@heggie.biz"] gem.summary = %q{Simple health check of Rails app for uptime monitoring with Pingdom, NewRelic, EngineYard etc.} gem.description = <<-EOF Simple health check of Rails app for uptime monitoring with Pingdom, NewRelic, EngineYard etc. EOF gem.homepage = "https://github.com/ianheggie/health_check" gem.license = "MIT" gem.files = `git ls-files`.split($/) gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.extra_rdoc_files = [ "README.rdoc" ] gem.require_paths = ["lib"] gem.required_ruby_version = '>= 2.2.2' gem.add_dependency(%q, [">= 5.0"]) gem.add_development_dependency(%q, [">= 0.1.0"]) gem.add_development_dependency(%q, [">= 0.8.3"]) gem.add_development_dependency(%q, ["~> 2.11.0"]) gem.add_development_dependency(%q, [">= 1.2"]) end health_check-3.1.0/Rakefile0000644000004100000410000000116114054112036015605 0ustar www-datawww-datarequire "bundler/gem_tasks" #require 'rubygems' require 'rake' #tests as gem task :test do exec '/bin/bash', './test/test_with_railsapp' end task default: :test begin gem 'rdoc' require 'rdoc/task' Rake::RDocTask.new do |rdoc| version = HealthCheck::VERSION rdoc.rdoc_dir = 'rdoc' rdoc.title = "health_check #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('CHANGELOG') rdoc.rdoc_files.include('MIT-LICENSE') rdoc.rdoc_files.include('lib/**/*.rb') end rescue Gem::LoadError puts "rdoc (or a dependency) not available. Install it with: gem install rdoc" end health_check-3.1.0/lib/0000755000004100000410000000000014054112036014707 5ustar www-datawww-datahealth_check-3.1.0/lib/health_check/0000755000004100000410000000000014054112036017311 5ustar www-datawww-datahealth_check-3.1.0/lib/health_check/version.rb0000644000004100000410000000005314054112036021321 0ustar www-datawww-datamodule HealthCheck VERSION = "3.1.0" end health_check-3.1.0/lib/health_check/redis_health_check.rb0000644000004100000410000000124014054112036023423 0ustar www-datawww-data# frozen_string_literal: true module HealthCheck class RedisHealthCheck extend BaseHealthCheck class << self def check raise "Wrong configuration. Missing 'redis' gem" unless defined?(::Redis) client.ping == 'PONG' ? '' : "Redis.ping returned #{res.inspect} instead of PONG" rescue Exception => err create_error 'redis', err.message ensure client.close if client.connected? end def client @client ||= Redis.new( { url: HealthCheck.redis_url, password: HealthCheck.redis_password }.reject { |k, v| v.nil? } ) end end end end health_check-3.1.0/lib/health_check/health_check_routes.rb0000644000004100000410000000071414054112036023643 0ustar www-datawww-datamodule ActionDispatch::Routing class Mapper def health_check_routes(prefix = nil) HealthCheck::Engine.routes_explicitly_defined = true add_health_check_routes(prefix) end def add_health_check_routes(prefix = nil) HealthCheck.uri = prefix if prefix match "#{HealthCheck.uri}(/:checks)(.:format)", controller: 'health_check/health_check', action: :index, via: [:get, :post], defaults: { format: 'txt' } end end end health_check-3.1.0/lib/health_check/middleware_health_check.rb0000644000004100000410000000675114054112036024446 0ustar www-datawww-data# Copyright (c) 2010-2021 Ian Heggie, released under the MIT license. # See MIT-LICENSE for details. require 'ipaddr' module HealthCheck class MiddlewareHealthcheck def initialize(app) @app = app end def call(env) (response_type, middleware_checks, full_stack_checks) = parse_env(env) if response_type if error_response = (ip_blocked(env) || not_authenticated(env)) return error_response end HealthCheck.installed_as_middleware = true errors = '' begin # Process the checks to be run from middleware errors = HealthCheck::Utils.process_checks(middleware_checks, true) # Process remaining checks through the full stack if there are any unless full_stack_checks.empty? return @app.call(env) end rescue => e errors = e.message.blank? ? e.class.to_s : e.message.to_s end healthy = errors.blank? msg = healthy ? HealthCheck.success : "health_check failed: #{errors}" if response_type == 'xml' content_type = 'text/xml' msg = { healthy: healthy, message: msg }.to_xml error_code = HealthCheck.http_status_for_error_object elsif response_type == 'json' content_type = 'application/json' msg = { healthy: healthy, message: msg }.to_json error_code = HealthCheck.http_status_for_error_object else content_type = 'text/plain' error_code = HealthCheck.http_status_for_error_text end [ (healthy ? 200 : error_code), { 'Content-Type' => content_type }, [msg] ] else @app.call(env) end end protected def parse_env(env) uri = env['PATH_INFO'] if uri =~ /^\/#{Regexp.escape HealthCheck.uri}(\/([-_0-9a-zA-Z]*))?(\.(\w*))?$/ checks = $2.to_s == '' ? ['standard'] : $2.split('_') response_type = $4.to_s middleware_checks = checks & HealthCheck.middleware_checks full_stack_checks = (checks - HealthCheck.middleware_checks) - ['and'] [response_type, middleware_checks, full_stack_checks ] end end def ip_blocked(env) return false if HealthCheck.origin_ip_whitelist.blank? req = Rack::Request.new(env) request_ipaddr = IPAddr.new(req.ip) unless HealthCheck.origin_ip_whitelist.any? { |addr| IPAddr.new(addr).include? request_ipaddr } [ HealthCheck.http_status_for_ip_whitelist_error, { 'Content-Type' => 'text/plain' }, [ 'Health check is not allowed for the requesting IP' ] ] end end def not_authenticated(env) return false unless HealthCheck.basic_auth_username && HealthCheck.basic_auth_password auth = MiddlewareHealthcheck::Request.new(env) if auth.provided? && auth.basic? && Rack::Utils.secure_compare(HealthCheck.basic_auth_username, auth.username) && Rack::Utils.secure_compare(HealthCheck.basic_auth_password, auth.password) env['REMOTE_USER'] = auth.username return false end [ 401, { 'Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Health Check"' }, [ ] ] end class Request < Rack::Auth::AbstractRequest def basic? "basic" == scheme end def credentials @credentials ||= params.unpack("m*").first.split(/:/, 2) end def username credentials.first end def password credentials.last end end end end health_check-3.1.0/lib/health_check/base_health_check.rb0000644000004100000410000000017114054112036023231 0ustar www-datawww-datamodule BaseHealthCheck def create_error(check_type, error_message) "[#{check_type} - #{error_message}] " end end health_check-3.1.0/lib/health_check/resque_health_check.rb0000644000004100000410000000063214054112036023625 0ustar www-datawww-datamodule HealthCheck class ResqueHealthCheck extend BaseHealthCheck def self.check unless defined?(::Resque) raise "Wrong configuration. Missing 'resque' gem" end res = ::Resque.redis.ping res == 'PONG' ? '' : "Resque.redis.ping returned #{res.inspect} instead of PONG" rescue Exception => e create_error 'resque-redis', e.message end end end health_check-3.1.0/lib/health_check/sidekiq_health_check.rb0000644000004100000410000000066314054112036023756 0ustar www-datawww-datamodule HealthCheck class SidekiqHealthCheck extend BaseHealthCheck def self.check unless defined?(::Sidekiq) raise "Wrong configuration. Missing 'sidekiq' gem" end ::Sidekiq.redis do |r| res = r.ping res == 'PONG' ? '' : "Sidekiq.redis.ping returned #{res.inspect} instead of PONG" end rescue Exception => e create_error 'sidekiq-redis', e.message end end end health_check-3.1.0/lib/health_check/elasticsearch_health_check.rb0000644000004100000410000000065514054112036025140 0ustar www-datawww-datamodule HealthCheck class ElasticsearchHealthCheck extend BaseHealthCheck def self.check unless defined?(::Elasticsearch) raise "Wrong configuration. Missing 'elasticsearch' gem" end res = ::Elasticsearch::Client.new.ping res == true ? '' : "Elasticsearch returned #{res.inspect} instead of true" rescue Exception => e create_error 'elasticsearch', e.message end end end health_check-3.1.0/lib/health_check/utils.rb0000644000004100000410000001545314054112036021006 0ustar www-datawww-data# Copyright (c) 2010-2013 Ian Heggie, released under the MIT license. # See MIT-LICENSE for details. module HealthCheck class Utils @@default_smtp_settings = { address: "localhost", port: 25, domain: 'localhost.localdomain', user_name: nil, password: nil, authentication: nil, enable_starttls_auto: true } cattr_accessor :default_smtp_settings # process an array containing a list of checks def self.process_checks(checks, called_from_middleware = false) errors = '' checks.each do |check| case check when 'and', 'site' # do nothing when "database" HealthCheck::Utils.get_database_version when "email" errors << HealthCheck::Utils.check_email when "emailconf" errors << HealthCheck::Utils.check_email if HealthCheck::Utils.mailer_configured? when "migrations", "migration" if defined?(ActiveRecord::Migration) and ActiveRecord::Migration.respond_to?(:check_pending!) # Rails 4+ begin ActiveRecord::Migration.check_pending! rescue ActiveRecord::PendingMigrationError => ex errors << ex.message end else database_version = HealthCheck::Utils.get_database_version migration_version = HealthCheck::Utils.get_migration_version if database_version.to_i != migration_version.to_i errors << "Current database version (#{database_version}) does not match latest migration (#{migration_version}). " end end when 'cache' errors << HealthCheck::Utils.check_cache when 'resque-redis-if-present' errors << HealthCheck::ResqueHealthCheck.check if defined?(::Resque) when 'sidekiq-redis-if-present' errors << HealthCheck::SidekiqHealthCheck.check if defined?(::Sidekiq) when 'redis-if-present' errors << HealthCheck::RedisHealthCheck.check if defined?(::Redis) when 's3-if-present' errors << HealthCheck::S3HealthCheck.check if defined?(::Aws) when 'elasticsearch-if-present' errors << HealthCheck::ElasticsearchHealthCheck.check if defined?(::Elasticsearch) when 'resque-redis' errors << HealthCheck::ResqueHealthCheck.check when 'sidekiq-redis' errors << HealthCheck::SidekiqHealthCheck.check when 'redis' errors << HealthCheck::RedisHealthCheck.check when 's3' errors << HealthCheck::S3HealthCheck.check when 'elasticsearch' errors << HealthCheck::ElasticsearchHealthCheck.check when 'rabbitmq' errors << HealthCheck::RabbitMQHealthCheck.check when "standard" errors << HealthCheck::Utils.process_checks(HealthCheck.standard_checks, called_from_middleware) when "middleware" errors << "Health check not called from middleware - probably not installed as middleware." unless called_from_middleware when "custom" HealthCheck.custom_checks.each do |name, list| list.each do |custom_check| errors << custom_check.call(self) end end when "all", "full" errors << HealthCheck::Utils.process_checks(HealthCheck.full_checks, called_from_middleware) else if HealthCheck.custom_checks.include? check HealthCheck.custom_checks[check].each do |custom_check| errors << custom_check.call(self) end else return "invalid argument to health_test." end end errors << '. ' unless errors == '' || errors.end_with?('. ') end return errors.strip rescue => e return e.message end def self.db_migrate_path # Lazy initialisation so Rails.root will be defined @@db_migrate_path ||= File.join(::Rails.root, 'db', 'migrate') end def self.db_migrate_path=(value) @@db_migrate_path = value end def self.mailer_configured? defined?(ActionMailer::Base) && (ActionMailer::Base.delivery_method != :smtp || HealthCheck::Utils.default_smtp_settings != ActionMailer::Base.smtp_settings) end def self.get_database_version ActiveRecord::Migrator.current_version if defined?(ActiveRecord) end def self.get_migration_version(dir = self.db_migrate_path) latest_migration = nil Dir[File.join(dir, "[0-9]*_*.rb")].each do |f| l = f.scan(/0*([0-9]+)_[_.a-zA-Z0-9]*.rb/).first.first rescue -1 latest_migration = l if !latest_migration || l.to_i > latest_migration.to_i end latest_migration end def self.check_email case ActionMailer::Base.delivery_method when :smtp HealthCheck::Utils.check_smtp(ActionMailer::Base.smtp_settings, HealthCheck.smtp_timeout) when :sendmail HealthCheck::Utils.check_sendmail(ActionMailer::Base.sendmail_settings) else '' end end def self.check_sendmail(settings) File.executable?(settings[:location]) ? '' : 'no sendmail executable found. ' end def self.check_smtp(settings, timeout) status = '' begin if @skip_external_checks status = '250' else smtp = Net::SMTP.new(settings[:address], settings[:port]) smtp.enable_starttls if settings[:enable_starttls_auto] smtp.open_timeout = timeout smtp.read_timeout = timeout smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do status = smtp.helo(settings[:domain]).status end end rescue Exception => ex status = ex.to_s end (status =~ /^250/) ? '' : "SMTP: #{status || 'unexpected error'}. " end def self.check_cache t = Time.now.to_i value = "ok #{t}" ret = ::Rails.cache.read('__health_check_cache_test__') if ret.to_s =~ /^ok (\d+)$/ diff = ($1.to_i - t).abs return('Cache expiry is broken. ') if diff > 30 elsif ret return 'Cache is returning garbage. ' end if ::Rails.cache.write('__health_check_cache_test__', value, expires_in: 2.seconds) ret = ::Rails.cache.read('__health_check_cache_test__') if ret =~ /^ok (\d+)$/ diff = ($1.to_i - t).abs (diff < 2 ? '' : 'Out of date cache or time is skewed. ') else 'Unable to read from cache. ' end else 'Unable to write to cache. ' end end end end health_check-3.1.0/lib/health_check/rabbitmq_health_check.rb0000644000004100000410000000060514054112036024122 0ustar www-datawww-datamodule HealthCheck class RabbitMQHealthCheck extend BaseHealthCheck def self.check unless defined?(::Bunny) raise "Wrong configuration. Missing 'bunny' gem" end connection = Bunny.new(HealthCheck.rabbitmq_config) connection.start connection.close '' rescue Exception => e create_error 'rabbitmq', e.message end end end health_check-3.1.0/lib/health_check/s3_health_check.rb0000644000004100000410000000351414054112036022650 0ustar www-datawww-datamodule HealthCheck class S3HealthCheck extend BaseHealthCheck class << self def check unless defined?(::Aws) raise "Wrong configuration. Missing 'aws-sdk' or 'aws-sdk-s3' gem" end return create_error 's3', 'Could not connect to aws' if aws_s3_client.nil? HealthCheck.buckets.each do |bucket_name, permissions| if permissions.nil? # backward compatible permissions = [:R, :W, :D] end permissions.each do |permision| begin send(permision, bucket_name) rescue Exception => e raise "bucket:#{bucket_name}, permission:#{permision} - #{e.message}" end end end '' rescue Exception => e create_error 's3', e.message end private # We already assume you are using Rails. Let's also assume you have an initializer # created for your Aws config. We will set the region here so you can use an # instance profile and simply set the region in your environment. def configure_client ::Aws.config[:s3] = { force_path_style: true } ::Aws.config[:region] ||= ENV['AWS_REGION'] || ENV['DEFAULT_AWS_REGION'] ::Aws::S3::Client.new end def aws_s3_client @aws_s3_client ||= configure_client end def R(bucket) aws_s3_client.list_objects(bucket: bucket) end def W(bucket) aws_s3_client.put_object(bucket: bucket, key: "healthcheck_#{::Rails.application.class.parent_name}", body: Time.new.to_s) end def D(bucket) aws_s3_client.delete_object(bucket: bucket, key: "healthcheck_#{::Rails.application.class.parent_name}") end end end end health_check-3.1.0/lib/health_check/health_check_controller.rb0000644000004100000410000000655514054112036024516 0ustar www-datawww-data# Copyright (c) 2010-2021 Ian Heggie, released under the MIT license. # See MIT-LICENSE for details. require "ipaddr" module HealthCheck class HealthCheckController < ActionController::Base layout false if self.respond_to? :layout before_action :check_origin_ip before_action :authenticate def index last_modified = Time.now.utc max_age = HealthCheck.max_age if max_age > 1 last_modified = Time.at((last_modified.to_f / max_age).floor * max_age).utc end is_public = (max_age > 1) && ! HealthCheck.basic_auth_username if stale?(last_modified: last_modified, public: is_public) checks = params[:checks] ? params[:checks].split('_') : ['standard'] checks -= HealthCheck.middleware_checks if HealthCheck.installed_as_middleware begin errors = HealthCheck::Utils.process_checks(checks) rescue Exception => e errors = e.message.blank? ? e.class.to_s : e.message.to_s end response.headers['Cache-Control'] = "must-revalidate, max-age=#{max_age}" if errors.blank? send_response true, nil, :ok, :ok if HealthCheck.success_callbacks HealthCheck.success_callbacks.each do |callback| callback.call(checks) end end else msg = HealthCheck.include_error_in_response_body ? "#{HealthCheck.failure}: #{errors}" : nil send_response false, msg, HealthCheck.http_status_for_error_text, HealthCheck.http_status_for_error_object # Log a single line as some uptime checkers only record that it failed, not the text returned msg = "#{HealthCheck.failure}: #{errors}" logger.send(HealthCheck.log_level, msg) if logger && HealthCheck.log_level if HealthCheck.failure_callbacks HealthCheck.failure_callbacks.each do |callback| callback.call(checks, msg) end end end end end protected def send_response(healthy, msg, text_status, obj_status) msg ||= healthy ? HealthCheck.success : HealthCheck.failure obj = { healthy: healthy, message: msg} respond_to do |format| format.html { render plain: msg, status: text_status, content_type: 'text/plain' } format.json { render json: obj, status: obj_status } format.xml { render xml: obj, status: obj_status } format.any { render plain: msg, status: text_status, content_type: 'text/plain' } end end def authenticate return unless HealthCheck.basic_auth_username && HealthCheck.basic_auth_password authenticate_or_request_with_http_basic('Health Check') do |username, password| username == HealthCheck.basic_auth_username && password == HealthCheck.basic_auth_password end end def check_origin_ip request_ipaddr = IPAddr.new(HealthCheck.accept_proxied_requests ? request.remote_ip : request.ip) unless HealthCheck.origin_ip_whitelist.blank? || HealthCheck.origin_ip_whitelist.any? { |addr| IPAddr.new(addr).include? request_ipaddr } render plain: 'Health check is not allowed for the requesting IP', status: HealthCheck.http_status_for_ip_whitelist_error, content_type: 'text/plain' end end # turn cookies for CSRF off def protect_against_forgery? false end end end health_check-3.1.0/lib/health_check.rb0000644000004100000410000000735514054112036017650 0ustar www-datawww-data# Copyright (c) 2010-2013 Ian Heggie, released under the MIT license. # See MIT-LICENSE for details. module HealthCheck class Engine < ::Rails::Engine cattr_accessor :routes_explicitly_defined end # Log level mattr_accessor :log_level self.log_level = 'info' # Text output upon success mattr_accessor :success self.success = "success" # Text output upon failure mattr_accessor :failure self.failure = "health_check failed" # Timeout in seconds used when checking smtp server mattr_accessor :smtp_timeout self.smtp_timeout = 30.0 # http status code used when plain text error message is output mattr_accessor :http_status_for_error_text self.http_status_for_error_text = 500 # http status code used when an error object is output (json or xml) mattr_accessor :http_status_for_error_object self.http_status_for_error_object = 500 # http status code used when the ip is not allowed for the request mattr_accessor :http_status_for_ip_whitelist_error self.http_status_for_ip_whitelist_error = 403 # check remote_ip rather than ip for ip whitelist mattr_accessor :accept_proxied_requests self.accept_proxied_requests = false # ips allowed to perform requests mattr_accessor :origin_ip_whitelist self.origin_ip_whitelist = [] # max-age of response in seconds # cache-control is public when max_age > 1 and basic authentication is used mattr_accessor :max_age self.max_age = 1 # s3 buckets mattr_accessor :buckets self.buckets = {} # rabbitmq mattr_accessor :rabbitmq_config self.rabbitmq_config = {} # health check uri path mattr_accessor :uri self.uri = 'health_check' # Basic Authentication mattr_accessor :basic_auth_username, :basic_auth_password self.basic_auth_username = nil self.basic_auth_password = nil # Array of custom check blocks mattr_accessor :custom_checks mattr_accessor :full_checks mattr_accessor :standard_checks self.custom_checks = { } self.full_checks = ['database', 'migrations', 'custom', 'email', 'cache', 'redis-if-present', 'sidekiq-redis-if-present', 'resque-redis-if-present', 's3-if-present', 'elasticsearch-if-present'] self.standard_checks = [ 'database', 'migrations', 'custom', 'emailconf' ] # Middleware based checks mattr_accessor :middleware_checks self.middleware_checks = [ 'middleware' ] mattr_accessor :installed_as_middleware # Allow non-standard redis url and password mattr_accessor :redis_url self.redis_url = ENV['REDIS_URL'] mattr_accessor :redis_password self.redis_password = 'some-password' # Include the error in the response body. # You should only do this where your /health_check endpoint is NOT open to the public internet mattr_accessor :include_error_in_response_body self.include_error_in_response_body = false # used for on_failure and on_success mattr_accessor :success_callbacks mattr_accessor :failure_callbacks def self.add_custom_check(name = 'custom', &block) custom_checks[name] ||= [ ] custom_checks[name] << block end def self.on_success(&block) success_callbacks ||= [ ] success_callbacks << block end def self.on_failure(&block) failure_callbacks ||= [ ] failure_callbacks << block end def self.setup yield self end end require 'health_check/version' require 'health_check/base_health_check' require 'health_check/resque_health_check' require 'health_check/s3_health_check' require 'health_check/redis_health_check' require 'health_check/elasticsearch_health_check' require 'health_check/sidekiq_health_check' require 'health_check/utils' require 'health_check/health_check_controller' require 'health_check/health_check_routes' require 'health_check/middleware_health_check' require 'health_check/rabbitmq_health_check' # vi: sw=2 sm ai: health_check-3.1.0/README.rdoc0000644000004100000410000004057514054112036015762 0ustar www-datawww-data= health_check gem Simple health check of Rails 5.x and 6.x apps for use with Pingdom, NewRelic, EngineYard etc. The basic goal is to quickly check that rails is up and running and that it has access to correctly configured resources (database, email gateway) Check the latest README {master}[https://github.com/ianheggie/health_check/tree/master] for other versions Use gem versions for stable releases: * ~> 3.x for Rails 5.x and 6.x (etc until there is a proven need to split off another branch), * ~> 2.x for Rails 4.x, * ~> 1.7 for Rails 3.x, * ~> 1.6.3 for Rails 2.3 Note: it is best to pin to a specific commit if using a development branch as sometimes tests break. health_check provides various monitoring URIs, for example: curl localhost:3000/health_check success curl localhost:3000/health_check/all.json {"healthy":true,"message":"success"} curl localhost:3000/health_check/database_cache_migration.xml true success You may also issue POST calls instead of GET to these urls. On failure (detected by health_check) a 500 http status is returned with a simple explanation of the failure (if include_error_in_response_body is true) curl localhost:3000/health_check/fail health_check failed: invalid argument to health_test. The health_check controller disables sessions for versions that eagerly load sessions. == Checks * standard (default) - site, database and migrations checks are run plus email if ActionMailer is defined and it is not using the default configuration * all / full - all checks are run (can be overriden in config block) * cache - checks that a value can be written to the cache * custom - runs checks added via config.add_custom_check * database - checks that the current migration level can be read from the database * email - basic check of email - :test returns true, :sendmail checks file is present and executable, :smtp sends HELO command to server and checks response * migration - checks that the database migration level matches that in db/migrations * rabbitmq - RabbitMQ Health Check * redis / redis-if-present - checks Redis connectivity * resque-redis / resque-redis-if-present - checks Resque connectivity to Redis * s3 / s3-if-present - checks proper permissions to s3 buckets * sidekiq-redis / sidekiq-redis-if-present - checks Sidekiq connectivity to Redis * elasticsearch / elasticsearch-if-present - checks Elasticsearch connectivity * site - checks rails is running sufficiently to render text Some checks have a *-if-present form, which only runs the check if the corresponding library has been required. The email gateway is not checked unless the smtp settings have been changed. Specify full or include email in the list of checks to verify the smtp settings (eg use 127.0.0.1 instead of localhost). Note: rails also checks migrations by default in development mode and throws an ActiveRecord::PendingMigrationError exception (http error 500) if there is an error == Installation Add the following line to Gemfile (after the rails gems are listed) gem 'health_check' And then execute bundle Or install it yourself as: gem install health_check == Configuration To change the configuration of health_check, create a file `config/initializers/health_check.rb` and add a configuration block like: HealthCheck.setup do |config| # uri prefix (no leading slash) config.uri = 'health_check' # Text output upon success config.success = 'success' # Text output upon failure config.failure = 'health_check failed' # Disable the error message to prevent /health_check from leaking # sensitive information config.include_error_in_response_body = false # Log level (success or failure message with error details is sent to rails log unless this is set to nil) config.log_level = 'info' # Timeout in seconds used when checking smtp server config.smtp_timeout = 30.0 # http status code used when plain text error message is output # Set to 200 if you want your want to distinguish between partial (text does not include success) and # total failure of rails application (http status of 500 etc) config.http_status_for_error_text = 500 # http status code used when an error object is output (json or xml) # Set to 200 if you want to distinguish between partial (healthy property == false) and # total failure of rails application (http status of 500 etc) config.http_status_for_error_object = 500 # bucket names to test connectivity - required only if s3 check used, access permissions can be mixed config.buckets = {'bucket_name' => [:R, :W, :D]} # You can customize which checks happen on a standard health check, eg to set an explicit list use: config.standard_checks = [ 'database', 'migrations', 'custom' ] # Or to exclude one check: config.standard_checks -= [ 'emailconf' ] # You can set what tests are run with the 'full' or 'all' parameter config.full_checks = ['database', 'migrations', 'custom', 'email', 'cache', 'redis', 'resque-redis', 'sidekiq-redis', 's3'] # Add one or more custom checks that return a blank string if ok, or an error message if there is an error config.add_custom_check do CustomHealthCheck.perform_check # any code that returns blank on success and non blank string upon failure end # Add another custom check with a name, so you can call just specific custom checks. This can also be run using # the standard 'custom' check. # You can define multiple tests under the same name - they will be run one after the other. config.add_custom_check('sometest') do CustomHealthCheck.perform_another_check # any code that returns blank on success and non blank string upon failure end # max-age of response in seconds # cache-control is public when max_age > 1 and basic_auth_username is not set # You can force private without authentication for longer max_age by # setting basic_auth_username but not basic_auth_password config.max_age = 1 # Protect health endpoints with basic auth # These default to nil and the endpoint is not protected config.basic_auth_username = 'my_username' config.basic_auth_password = 'my_password' # Whitelist requesting IPs by a list of IP and/or CIDR ranges, either IPv4 or IPv6 (uses IPAddr.include? method to check) # Defaults to blank which allows any IP config.origin_ip_whitelist = %w(123.123.123.123 10.11.12.0/24 2400:cb00::/32) # Use ActionDispatch::Request's remote_ip method when behind a proxy to pick up the real remote IP for origin_ip_whitelist check # Otherwise uses Rack::Request's ip method (the default, and always used by Middleware), which is more susceptable to spoofing # See https://stackoverflow.com/questions/10997005/whats-the-difference-between-request-remote-ip-and-request-ip-in-rails config.accept_proxied_requests = false # http status code used when the ip is not allowed for the request config.http_status_for_ip_whitelist_error = 403 # rabbitmq config.rabbitmq_config = {} # When redis url/password is non-standard config.redis_url = 'redis_url' # default ENV['REDIS_URL'] # Only included if set, as url can optionally include passwords as well config.redis_password = 'redis_password' # default ENV['REDIS_PASSWORD'] # Failure Hooks to do something more ... # checks lists the checks requested config.on_failure do |checks, msg| # log msg somewhere end config.on_success do |checks| # flag that everything is well end end You may call add_custom_check multiple times with different tests. These tests will be included in the default list ("standard"). If you have a catchall route then add the following line above the catch all route (in `config/routes.rb`): health_check_routes === Installing As Middleware Install health_check as middleware if you want to sometimes ignore exceptions from later parts of the Rails middleware stack, eg DB connection errors from QueryCache. The "middleware" check will fail if you have not installed health_check as middleware. To install health_check as middleware add the following line to the config/application.rb: config.middleware.insert_after Rails::Rack::Logger, HealthCheck::MiddlewareHealthcheck Note: health_check is installed as a full rails engine even if it has been installed as middleware. This is so the remaining checks continue to run through the complete rails stack. You can also adjust what checks are run from middleware, eg if you want to exclude the checking of the database etc, then set config.middleware_checks = ['middleware', 'standard', 'custom'] config.standard_checks = ['middleware', 'custom'] Middleware checks are run first, and then full stack checks. When installed as middleware, exceptions thrown when running the full stack tests are formatted in the standard way. == Uptime Monitoring Use a website monitoring service to check the url regularly for the word "success" (without the quotes) rather than just a 200 http status so that any substitution of a different server or generic information page should also be reported as an error. If an error is encounted, the text "health_check failed: some error message/s" will be returned and the http status will be 500. See * Pingdom Website Monitoring - https://www.pingdom.com * NewRelic Availability Monitoring - http://newrelic.com/docs/features/availability-monitoring-faq * Engine Yard's guide - https://support.cloud.engineyard.com/entries/20996821-monitor-application-uptime (although the guide is based on fitter_happier plugin it will also work with this gem) * Nagios check_http (with -s success) - https://www.nagios-plugins.org/doc/man/check_http.html * Any other montoring service that can be set to check for the word success in the text returned from a url === Requesting Json and XML responses Health_check will respond with an encoded hash object if json or xml is requested. Either set the HTTP Accept header or append .json or .xml to the url. The hash contains two keys: * healthy - true if requested checks pass (boolean) * message - text message ("success" or error message) The following commands curl -v localhost:3000/health_check.json curl -v localhost:3000/health_check/email.json curl -v -H "Accept: application/json" localhost:3000/health_check Will return a result with Content-Type: application/json and body like: {"healthy":true,"message":"success"} These following commands curl -v localhost:3000/health_check.xml curl -v localhost:3000/health_check/migration_cache.xml curl -v -H "Accept: text/xml" localhost:3000/health_check/cache Will return a result with Content-Type: application/xml and body like: true success See https://github.com/ianheggie/health_check/wiki/Ajax-Example for an Ajax example == Silencing log output It is recomended that you use silencer, lograge or one of the other log filtering gems. For example, with lograge use the following to exclude health_check from being logged: config.lograge.ignore_actions = ["HealthCheck::HealthCheckController#index"] Likewise you will probably want to exclude health_check from monitoring systems like newrelic. == Caching Cache-control is set with * public if max_age is > 1 and basic_auth_username is not set (otherwise private) * no-cache * must-revalidate * max-age (default 1) Last-modified is set to the current time (rounded down to a multiple of max_age when max_age > 1) == Known Issues * See https://github.com/ianheggie/health_check/issues * No inline documentation for methods * rvm gemsets breaks the test - specifically rvm use 1.9.3 works but rvm gemset use ruby-1.9.3-p385@health_check --create triggers a "Could not find gem 'coffee-rails (~> 3.2.1) ruby' in the gems available on this machine." error in the last call to bundle (installing health_check as a gem via a path into the temp railsapp) == Similar projects * fitter_happier plugin by atmos - plugin with similar goals, but not compatible with uptime, and does not check email gateway * HealthBit - inspired by this gem but with a fresh start as a simpler rack only application, no travis CI tests (yet?) but looks interesting. == Testing === Automated testing and other checks * {Gem Version}[http://badge.fury.io/rb/health_check] - Latest Gem * {}[https://travis-ci.org/ianheggie/health_check] - Travis CI * {}[https://codeclimate.com/github/ianheggie/health_check] - Code quality * {}[https://gemnasium.com/ianheggie/health_check] - Gem dependencies === Manual testing The instructions have been changed to using a vagrant virtual box for consistent results. Install vagrant 1.9.7 or later and virtual_box or other local virtual machine provider. Add the vagrant plugin called vbguest. vagrant plugin install vagrant-vbguest Create a temp directory for throw away testing, and clone the health_check gem into it mkdir -p ~/tmp cd ~/tmp git clone https://github.com/ianheggie/health_check.git ~/tmp/health_check The Vagrantfile includes provisioning rules to install chruby (ruby version control), ruby-build will also be installed and run to build various rubies under /opt/rubies. Use vagrant ssh to connect to the virtual box and run tests. The test script will package up and install the gem under a temporary path, create a dummy rails app configured for sqlite, install the gem, and then run up tests against the server. This will require TCP port 3456 to be free. Cd to the checked out health_check directory and then run the test as follows: cd ~/tmp/health_check vagrant up # this will also run vagrant provision and take some time # chruby and various ruby versions will be installed vagrant ssh cd /vagrant # the current directory on your host is mounted here on the virtual machine chruby 2.2.2 # or some other ruby version (run chruby with no arguments to see the current list) test/test_with_railsapp exit # from virtual machine when finished The script will first call `test/setup_railsapp` to setup a rails app with health_check installed and then run up the rails server and perform veraious tests. The script `test/setup_railsapp` will prompt you for which gemfile under test you wish to use to install the appropriate rails version, and then setup tmp/railsapp accordingly. The command `rake test` will also launch these tests, except it cannot install the bundler and rake gems if they are missing first (unlike test/test_with_railsapp) == Copyright Copyright (c) 2010-2021 Ian Heggie, released under the MIT license. See MIT-LICENSE for details. == Contributors Thanks go to the various people who have given feedback and suggestions via the issues list and pull requests. === Contributing Use gem versions for stable releases, or github branch / commits for development versions: * for Rails 5.x and 6.x use feature branched off master {master}[https://github.com/ianheggie/health_check/tree/master] for development; * for Rails 4.x use feature branches off the {rails4}[https://github.com/ianheggie/health_check/tree/rails4] stable branch for development; * for Rails 3.x use feature branches off the {rails3}[https://github.com/ianheggie/health_check/tree/rails3] stable branch for development; * for Rails 2.3 use feature branches off the {rails2.3}[https://github.com/ianheggie/health_check/tree/rails2.3] stable branch for development; 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Create a test that confirms your changes work 4. Update README.rdoc to explain enhancements, or add succinct comment in code when fixing bugs 5. Commit your changes (`git commit -am 'Add some feature'`) 6. Push to the branch (`git push origin my-new-feature`) 7. Create new Pull Request (Code with BDD tests and documentation are highly favoured) Feedback welcome! Especially with suggested replacement code, tests and documentation health_check-3.1.0/config/0000755000004100000410000000000014054112036015406 5ustar www-datawww-datahealth_check-3.1.0/config/routes.rb0000644000004100000410000000020214054112036017246 0ustar www-datawww-dataunless HealthCheck::Engine.routes_explicitly_defined ::Rails.application.routes.draw do add_health_check_routes() end end health_check-3.1.0/Vagrantfile0000644000004100000410000000156014054112036016330 0ustar www-datawww-data# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. config.vm.box = "ubuntu/focal64" # set auto_update to false, if you do NOT want to check the correct # additions version when booting this machine config.vbguest.auto_update = false # do NOT download the iso file from a webserver config.vbguest.no_remote = true # provision with a shell script. config.vm.provision "shell", path: "./test/provision_vagrant" config.vm.provider "virtualbox" do |v| # travis allocates 7.5 GB, but this is sufficient v.memory = 2048 v.cpus = 2 end # if File.file?('.git') && IO.read('.git') =~ %r{\Agitdir: (.+)/.git/worktrees.*} # # Handle git worktrees ... # path = $1 # config.vm.synced_folder path, path # end end health_check-3.1.0/Gemfile0000644000004100000410000000100614054112036015431 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in health_check.gemspec gemspec group :development, :test do if defined?(JRUBY_VERSION) gem 'jruby-openssl' gem 'activerecord-jdbcsqlite3-adapter' else gem 'sqlite3', '~> 1.3.7' end # run travis-lint to check .travis.yml gem 'travis-lint' # mime-types 2.0 requires Ruby version >= 1.9.2 # mime-types 3.0 requires Ruby version >= 2.0 gem 'mime-types', defined?(JRUBY_VERSION) || RUBY_VERSION < '2.0' ? '< 3' : '>= 3.0' end health_check-3.1.0/MIT-LICENSE0000644000004100000410000000206014054112036015573 0ustar www-datawww-dataCopyright (c) 2010-2013 Ian Heggie MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. health_check-3.1.0/CHANGELOG0000644000004100000410000001410614054112036015355 0ustar www-datawww-data= Change Log = * 3.1.0 - 26 May 2021 * Updated README to clarify railsN branch status (they are intended to be stable, development to be a feature branch off them or master) * Updated README with all the settings * Updated all the branches to work with the latest travis and gem changes, as some gems needed to be locked down. * Updated to test rails 6.0 and 6.1 * Got all travis tests passing * Removed old, unused branches * Merged PR Fix broken Cache-Control headers #93 from felixbuenemann * Merged PR S3 should not depend on Rails secrets file #77 by natefaerber * Merged PR RabbitMQ Health check #98 from rhuanbarreto * Merged PR Use remote_ip to accept proxy-forwarded requests #102 by alessio-signorini but made it optional * Fixed up failure setting to match previous output on error, and use it as a prefix when the error message is also output (not by default) * Always log the error to rails log even if not including in html response * Merged PR ensure REDIS connections are closed #88 from yld * Merged PR more robust cache check #90 from masciugo * Merged PR Add log_level config setting which defaults to 'info'. #97 from FloHeinle * Merged PR get rid of old school hash rockets syntax #92 from DmytroStepaniuk * Merged PR Converted whitelist testing to use IPAddr objects. #64 jordanyaker * Added on_success and on_failure callbacks * Makes sure errors are seperated by a period and a space and a period always ends the list of errors * 3.0.0 * First release on rails5 branch * Depends on railties rather than rails so it can be used with trimmed down stacks * Corrected ruby version required to match rails * Cleaned up README * redis_url now defaults to nil (url determined by redis gem) * Cleaned out rails 4.0 dependent code * Cleaned up test code and updated to rails 5 standards, uses smarter_bundler to handle gem ruby version isssues * Added rails 5.1 test * Split all releases to this rails* branchs - master is only for edge development * 2.7.0 * Add ability to check health of redis when url is non-standard redis url * 2.6.0 * Add named custom checks * 2.5.0 * Added whitelist for IP# (Thanks Fernando Alvarez) * reworked whitelist PR * Expanded tests for whitelist and basic authentication * reworked middleware, simplified error codes, added whitelist and basic authentication into middleware * Removed unit tests as they where aonly applicable under rails 2.3 when installed in vendor/plugins * #55 by mnoack - correct binstubs arg in test * #54 by gkop - Lograge config snippet works with Rails 4.2.7.1, lograge 0.4.1 * Used ideas from #52 - use middleware to catch Rails stack exceptions * #51 by tokenshift - Fixing NameError on `basic_auth_username`. * Changes to address #50 by fillphafftek - allow standard check to be run from middleware if configured to do so, removed requirement for "middleware" to be passed in url for middleware tests * 2.4.0 * Added tests for middleware * Changed contributed middleware code to conform to existing url scheme * Allow both GET and POST calls * Prefer config.uri for changing route prefix * 2.3.0 * Fix route reload issue * Various fixes to get tests working with bundle/jruby and other gem issues * Document additional branches * Fix route reload issue (auto routing previosuly conflicted with devise) * Removed ref to rails 2.3, 3.* * 2.2.1 * Adjust private/public cache-control based on max_age set * 2.2.0 * Add max_age so we can control the caching of responses, don't run tests if Last-modified still matches * Added basic auth - Thanks omadahealth * A few macinations due to gem changes and avoidning triggering deprecation notices * Add single quote to README to make the configuration file valid - Thanks Ryan Selk * Fixed README formatting * 2.1.0 * Updated contributed tests so there is both the forced check and a *-if-present check which tests if the gem's class is loaded * Added resque-redis check - Thanks filiphaftek * In addition to adding a test file to S3, we will also try to delete it to confirm that delete operations are possible - Thanks Anton Dimitrov * Added redis, sidekiq-redis and s3 health-checks - Thanks Filip * Fix render options - Thanks Yuji Hanamura * Fix to always return a 200 status code on success rather than 304 (adds Last-Modified) - Thanks macgregordennis * Added Rails 5.0 tests * 2.0.0 - Removed silence - recommend to use a log filtering gem instead * 1.4.1 - Rails 4 and route changes * Now handles routes being generated multiple times by some gem / rails / ruby combinations - Previously multiple calls to health_check_routes where ignored, now explicit calls to health_check_route always adds the route but flags that it doesn't have to be added again on the end of the list * Uses ActiveRecord::Migration.check_pending! if available and returns the message if an exception is raised (Rails 4.0+) * Simplified routing rules down to one rule for Rails 3.0+ * Includes some changes for rails 4.1 (edge) - but still a work in progress * 1.3.1 - Include changes from contributers: * Migrations with dots are now handled * the list of checks for "full" / "all" can be configured * 1.2.0 - The gem can now be configured, including timeouts, status codes and text returned on success - Customn checks can be added via initializer like config.add_custom_check { CustomCheckClass.a_custom_check } - You can now request the response to be json or xml (via url or Content-accepted header) - reduced tests to the versions of ruby recomended for the different versions of rails * 1.1.2 - Change to bundler support for building gems, as jeweler gem was broken by v2.0.0 of rubygems * 1.1.0 - Include cache check (Thanks to https://github.com/mgomes1 ) and some changes to test setup to workaround and diagnose test failures under rvm * 1.0.2 - Included travis config and gemfiles used in travis tests in gem and changes to test setup so that gem test * 1.x - Includes Rails 3.x suppprt as an Engine * 0.x - Rails 2.3