-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters