Skip to content

Commit

Permalink
feat: enable public badge access
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 2, 2017
1 parent 85ab047 commit ab8fffd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 39 deletions.
26 changes: 26 additions & 0 deletions pact_broker/basic_auth.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 12 additions & 37 deletions pact_broker/config.ru
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions pact_broker/database_connection.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions pact_broker/database_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'delegate'

class DatabaseLogger < SimpleDelegator
def info *args
__getobj__().debug(*args)
end
end
7 changes: 7 additions & 0 deletions pact_broker/passenger_config.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions script/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ab8fffd

Please sign in to comment.