diff --git a/pact_broker/basic_auth.rb b/pact_broker/basic_auth.rb new file mode 100644 index 00000000..c8bfc495 --- /dev/null +++ b/pact_broker/basic_auth.rb @@ -0,0 +1,26 @@ +class BasicAuth + PATH_INFO = 'PATH_INFO' + BADGE_PATH = %r{^/pacts/provider/[^/]+/consumer/.*/badge(?:\.[A-Za-z]+)?$} + + def initialize(app, username, password) + @app = app + @expected_username = username + @expected_password = password + + @app_with_auth = Rack::Auth::Basic.new(app, "Restricted area") do |username, password| + username == @expected_username && password == @expected_password + end + end + + def call(env) + if use_basic_auth? env + @app_with_auth.call(env) + else + @app.call(env) + end + end + + def use_basic_auth?(env) + !(env[PATH_INFO] =~ BADGE_PATH) + end +end diff --git a/pact_broker/config.ru b/pact_broker/config.ru index 722c3cb3..e1a87b2d 100644 --- a/pact_broker/config.ru +++ b/pact_broker/config.ru @@ -1,48 +1,23 @@ -require 'fileutils' require 'logger' require 'sequel' require 'pact_broker' -require 'delegate' - -class DatabaseLogger < SimpleDelegator - def info *args - __getobj__().debug(*args) - end -end - -if defined?(PhusionPassenger) - PhusionPassenger.on_event(:starting_worker_process) do |forked| - if forked - Sequel::DATABASES.each { |db| db.disconnect } - end - end -end - -database_adapter = ENV.fetch('PACT_BROKER_DATABASE_ADAPTER','') != '' ? ENV['PACT_BROKER_DATABASE_ADAPTER'] : 'postgres' - -DATABASE_CREDENTIALS = { - adapter: database_adapter, - user: ENV['PACT_BROKER_DATABASE_USERNAME'], - password: ENV['PACT_BROKER_DATABASE_PASSWORD'], - host: ENV['PACT_BROKER_DATABASE_HOST'], - database: ENV['PACT_BROKER_DATABASE_NAME'] -} - -if ENV['PACT_BROKER_DATABASE_PORT'] =~ /^\d+$/ - DATABASE_CREDENTIALS[:port] = ENV['PACT_BROKER_DATABASE_PORT'].to_i -end - -if ENV.fetch('PACT_BROKER_BASIC_AUTH_USERNAME','') != '' && ENV.fetch('PACT_BROKER_BASIC_AUTH_PASSWORD', '') != '' - use Rack::Auth::Basic, "Restricted area" do |username, password| - username == ENV['PACT_BROKER_BASIC_AUTH_USERNAME'] && password == ENV['PACT_BROKER_BASIC_AUTH_PASSWORD'] - end -end +require_relative 'basic_auth' +require_relative 'database_connection' +require_relative 'passenger_config' app = PactBroker::App.new do | config | config.logger = ::Logger.new($stdout) config.logger.level = Logger::WARN - config.database_connection = Sequel.connect(DATABASE_CREDENTIALS.merge(logger: DatabaseLogger.new(config.logger), encoding: 'utf8')) + config.database_connection = create_database_connection(config.logger) config.database_connection.timezone = :utc end +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 != '' + +if use_basic_auth + app = BasicAuth.new(app, basic_auth_username, basic_auth_password) +end + run app diff --git a/pact_broker/database_connection.rb b/pact_broker/database_connection.rb new file mode 100644 index 00000000..ae79739f --- /dev/null +++ b/pact_broker/database_connection.rb @@ -0,0 +1,20 @@ +require 'sequel' +require_relative 'database_logger' + +def create_database_connection(logger) + database_adapter = ENV.fetch('PACT_BROKER_DATABASE_ADAPTER','') != '' ? ENV['PACT_BROKER_DATABASE_ADAPTER'] : 'postgres' + + credentials = { + adapter: database_adapter, + user: ENV['PACT_BROKER_DATABASE_USERNAME'], + password: ENV['PACT_BROKER_DATABASE_PASSWORD'], + host: ENV['PACT_BROKER_DATABASE_HOST'], + database: ENV['PACT_BROKER_DATABASE_NAME'] + } + + if ENV['PACT_BROKER_DATABASE_PORT'] =~ /^\d+$/ + credentials[:port] = ENV['PACT_BROKER_DATABASE_PORT'].to_i + end + + Sequel.connect(credentials.merge(logger: DatabaseLogger.new(logger), encoding: 'utf8')) +end diff --git a/pact_broker/database_logger.rb b/pact_broker/database_logger.rb new file mode 100644 index 00000000..b47a6c0b --- /dev/null +++ b/pact_broker/database_logger.rb @@ -0,0 +1,7 @@ +require 'delegate' + +class DatabaseLogger < SimpleDelegator + def info *args + __getobj__().debug(*args) + end +end diff --git a/pact_broker/passenger_config.rb b/pact_broker/passenger_config.rb new file mode 100644 index 00000000..e45727e6 --- /dev/null +++ b/pact_broker/passenger_config.rb @@ -0,0 +1,7 @@ +if defined?(PhusionPassenger) + PhusionPassenger.on_event(:starting_worker_process) do |forked| + if forked + Sequel::DATABASES.each { |db| db.disconnect } + end + end +end diff --git a/script/test.sh b/script/test.sh index 90c436af..effc21b7 100755 --- a/script/test.sh +++ b/script/test.sh @@ -56,14 +56,14 @@ fi [ -z "${PACT_BROKER_PORT}" ] && PACT_BROKER_PORT=80 [ -z "${PSQL_WAIT_TIMEOUT}" ] && PSQL_WAIT_TIMEOUT="10s" [ -z "${PACT_WAIT_TIMEOUT}" ] && PACT_WAIT_TIMEOUT="15s" -[ -z "${PACT_CONT_NAME}" ] && PACT_CONT_NAME="broker_app" +[ -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" echo "Will build the pact broker" docker build -t=dius/pact_broker . -# Stop and remove any running broker_app container instances before updating +# Stop and remove any running broker-app container instances before updating if docker ps -a | grep ${PACT_CONT_NAME}; then echo "" echo "Stopping and removing running instance of pact broker container"