Skip to content

Commit

Permalink
feat: allow public access to the heartbeat URL to be configured via a…
Browse files Browse the repository at this point in the history
…n environment variable
  • Loading branch information
bethesque committed Mar 12, 2018
1 parent ffa5a97 commit a3d8fb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions container/etc/nginx/main.d/pactbroker-env.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env PACT_BROKER_DATABASE_NAME;
env PACT_BROKER_DATABASE_PORT;
env PACT_BROKER_BASIC_AUTH_USERNAME;
env PACT_BROKER_BASIC_AUTH_PASSWORD;
env PACT_BROKER_PUBLIC_HEARTBEAT;
env PACT_BROKER_LOG_LEVEL;
env http_proxy;
env https_proxy;
Expand Down
18 changes: 14 additions & 4 deletions pact_broker/basic_auth.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class BasicAuth
PATH_INFO = 'PATH_INFO'
BADGE_PATH = %r{^/pacts/provider/[^/]+/consumer/.*/badge(?:\.[A-Za-z]+)?$}
PATH_INFO = 'PATH_INFO'.freeze
BADGE_PATH = %r{^/pacts/provider/[^/]+/consumer/.*/badge(?:\.[A-Za-z]+)?$}.freeze
HEARTBEAT_PATH = "/diagnostic/status/heartbeat".freeze

def initialize(app, username, password)
def initialize(app, username, password, allow_public_access_to_heartbeat)
@app = app
@expected_username = username
@expected_password = password
@allow_public_access_to_heartbeat = allow_public_access_to_heartbeat

@app_with_auth = Rack::Auth::Basic.new(app, "Restricted area") do |username, password|
username == @expected_username && password == @expected_password
Expand All @@ -21,6 +23,14 @@ def call(env)
end

def use_basic_auth?(env)
!(env[PATH_INFO] =~ BADGE_PATH)
!(is_badge_path?(env) || is_heartbeat_and_public_access_allowed(env))
end

def is_badge_path?(env)
env[PATH_INFO] =~ BADGE_PATH
end

def is_heartbeat_and_public_access_allowed?
@allow_public_access_to_heartbeat && env[PATH_INFO] == BADGE_PATH
end
end
3 changes: 2 additions & 1 deletion pact_broker/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ PactBroker.configuration.load_from_database!
basic_auth_username = ENV.fetch('PACT_BROKER_BASIC_AUTH_USERNAME','')
basic_auth_password = ENV.fetch('PACT_BROKER_BASIC_AUTH_PASSWORD', '')
use_basic_auth = basic_auth_username != '' && basic_auth_password != ''
allow_public_access_to_heartbeat = ENV.fetch('PACT_BROKER_PUBLIC_HEARTBEAT', '') == 'true'

if use_basic_auth
app = BasicAuth.new(app, basic_auth_username, basic_auth_password)
app = BasicAuth.new(app, basic_auth_username, basic_auth_password, allow_public_access_to_heartbeat)
end

run app
9 changes: 9 additions & 0 deletions script/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fi
[ -z "${PACT_CONT_NAME}" ] && PACT_CONT_NAME="broker-app"
[ -z "${PSQL_CONT_NAME}" ] && PSQL_CONT_NAME="postgres"
[ -z "${PACT_BROKER_DATABASE_ADAPTER}" ] && PACT_BROKER_DATABASE_ADAPTER="postgres"
[ -z "${PACT_BROKER_PUBLIC_HEARTBEAT}" ] && PACT_BROKER_PUBLIC_HEARTBEAT="true"

echo "Will build the pact broker"
docker build -t=dius/pact_broker .
Expand Down Expand Up @@ -159,6 +160,7 @@ docker run --privileged --name=${PACT_CONT_NAME} -d -p ${PORT_BIND} \
-e PACT_BROKER_DATABASE_PORT=${PACT_BROKER_DATABASE_PORT} \
-e PACT_BROKER_BASIC_AUTH_USERNAME=${PACT_BROKER_BASIC_AUTH_USERNAME} \
-e PACT_BROKER_BASIC_AUTH_PASSWORD=${PACT_BROKER_BASIC_AUTH_PASSWORD} \
-e PACT_BROKER_PUBLIC_HEARTBEAT=${PACT_BROKER_PUBLIC_HEARTBEAT} \
-e PACT_BROKER_LOG_LEVEL=INFO \
dius/pact_broker
sleep 1 && docker logs ${PACT_CONT_NAME}
Expand Down Expand Up @@ -223,4 +225,11 @@ if [[ "${response_code}" -ne '200' ]]; then
die "Expected response code to be 200, but was ${response_code}"
fi
echo "Checking that the heartbeat URL can be accessed without basic auth"
response_code=$(curl -s -o /dev/null -w "%{http_code}" http://${test_ip}:${EXTERN_BROKER_PORT}/diagnostic/status/heartbeat)
if [[ "${response_code}" -ne '200' ]]; then
die "Expected response code to be 200, but was ${response_code}"
fi
echo "SUCCESS: All tests passed!"

0 comments on commit a3d8fb8

Please sign in to comment.