-
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.
feat: allow POST access to the 'pacts for verification' endpoint for …
…the read only user (#22) * feat: allow SQL log level and warn duration to be set via environment variables and hush the 'table not found' errors that freak people out * feat: allow POST access to the 'pacts for verification' endpoint for the read only user * chore: freeze * chore: downcase sql log level
- Loading branch information
Showing
10 changed files
with
155 additions
and
61 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
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
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
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
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
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,7 +1,29 @@ | ||
# This changes the ERROR logs that occur when | ||
# Sequel doesn't know if a table/view exists or not to DEBUG, | ||
# so that they don't freak newbies out when they start up the | ||
# broker for the first time. | ||
|
||
require 'delegate' | ||
|
||
class DatabaseLogger < SimpleDelegator | ||
def info *args | ||
__getobj__().debug(*args) | ||
def error *args | ||
if error_is_about_table_not_existing?(args) | ||
__getobj__().debug(*reassure_people_that_this_is_expected(args)) | ||
else | ||
__getobj__().error(*args) | ||
end | ||
end | ||
|
||
def error_is_about_table_not_existing?(args) | ||
args.first.is_a?(String) && | ||
( args.first.include?("PG::UndefinedTable") || | ||
args.first.include?("no such table") || | ||
args.first.include?("no such view")) | ||
end | ||
|
||
def reassure_people_that_this_is_expected(args) | ||
message = args.shift | ||
message = message + " Don't panic. This happens when Sequel doesn't know if a table/view exists or not." | ||
[message] + 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
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,55 @@ | ||
require 'rack' | ||
require_relative 'resource_access_rules' | ||
|
||
class PactBrokerResourceAccessPolicy | ||
READ_METHODS = %w{GET OPTIONS HEAD}.freeze | ||
ALL_METHODS = %w{GET POST PUT PATCH DELETE HEAD OPTIONS}.freeze | ||
POST = 'POST'.freeze | ||
|
||
ALL_PATHS = %r{.*}.freeze | ||
PACT_BADGE_PATH = %r{^/pacts/provider/[^/]+/consumer/.*/badge(?:\.[A-Za-z]+)?$}.freeze | ||
MATRIX_BADGE_PATH = %r{^/matrix/provider/[^/]+/latest/[^/]+/consumer/[^/]+/latest/[^/]+/badge(?:\.[A-Za-z]+)?$}.freeze | ||
HEARTBEAT_PATH = %r{^/diagnostic/status/heartbeat$}.freeze | ||
PACTS_FOR_VERIFICATION_PATH = %r{^/pacts/provider/[^/]+/for-verification$}.freeze | ||
|
||
PUBLIC = 0 | ||
READ = 1 | ||
WRITE = 2 | ||
|
||
def initialize(resource_access_rules) | ||
@resource_access_rules = resource_access_rules | ||
end | ||
|
||
def public_access_allowed?(env) | ||
resource_access_rules.access_allowed?(env, PUBLIC) | ||
end | ||
|
||
def read_access_allowed?(env) | ||
resource_access_rules.access_allowed?(env, READ) | ||
end | ||
|
||
def self.build(allow_public_read_access, allow_public_access_to_heartbeat) | ||
rules = [ | ||
[WRITE, ALL_METHODS, ALL_PATHS], | ||
[READ, READ_METHODS, ALL_PATHS], | ||
[READ, [POST], PACTS_FOR_VERIFICATION_PATH], | ||
[PUBLIC, READ_METHODS, PACT_BADGE_PATH], | ||
[PUBLIC, READ_METHODS, MATRIX_BADGE_PATH] | ||
] | ||
|
||
if allow_public_access_to_heartbeat | ||
rules.unshift([PUBLIC, READ_METHODS, HEARTBEAT_PATH]) | ||
end | ||
|
||
if allow_public_read_access | ||
rules.unshift([PUBLIC, READ_METHODS, ALL_PATHS]) | ||
rules.unshift([PUBLIC, [POST], PACTS_FOR_VERIFICATION_PATH]) | ||
end | ||
|
||
PactBrokerResourceAccessPolicy.new(ResourceAccessRules.new(rules)) | ||
end | ||
|
||
private | ||
|
||
attr_reader :resource_access_rules | ||
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,34 @@ | ||
require 'rack' | ||
|
||
class ResourceAccessRules | ||
PATH_INFO = Rack::PATH_INFO | ||
REQUEST_METHOD = Rack::REQUEST_METHOD | ||
|
||
def initialize(rules) | ||
@rules = rules | ||
end | ||
|
||
def access_allowed?(env, level) | ||
!!rules.find do | rule_level, allowed_methods, path_pattern | | ||
level_allowed?(level, rule_level) && | ||
method_allowed?(env, allowed_methods) && | ||
path_allowed?(env, path_pattern) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :rules | ||
|
||
def level_allowed?(level, rule_level) | ||
level >= rule_level | ||
end | ||
|
||
def path_allowed?(env, pattern) | ||
env[PATH_INFO] =~ pattern | ||
end | ||
|
||
def method_allowed?(env, allowed_methods) | ||
allowed_methods.include?(env[REQUEST_METHOD]) | ||
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