Skip to content

Commit 19cb83f

Browse files
committed
feat: allow whitelist configurations to be loaded from database
1 parent 91b005a commit 19cb83f

8 files changed

+155
-7
lines changed

lib/pact_broker/config/load.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'pact_broker/configuration'
22
require 'pact_broker/logging'
33
require 'pact_broker/config/setting'
4+
require 'pact_broker/config/space_delimited_string_list'
45

56
module PactBroker
67
module Config
@@ -49,6 +50,8 @@ def get_value_from_setting setting
4950
Integer(setting.value)
5051
when 'float'
5152
Float(setting.value)
53+
when 'space_delimited_string_list'
54+
SpaceDelimitedStringList.parse(setting.value)
5255
when 'boolean'
5356
setting.value == "1"
5457
end

lib/pact_broker/config/save.rb

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def get_db_type setting_name
4949
'boolean'
5050
when String, nil
5151
'string'
52+
when SpaceDelimitedStringList
53+
'space_delimited_string_list'
5254
when Array, Hash
5355
'json'
5456
when Integer
@@ -69,6 +71,8 @@ def get_db_value setting_name
6971
"1"
7072
when FalseClass
7173
"0"
74+
when SpaceDelimitedStringList
75+
val.to_s
7276
when Array, Hash
7377
val.to_json
7478
else
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module PactBroker
2+
module Config
3+
class SpaceDelimitedStringList < Array
4+
5+
def initialize list
6+
super(list)
7+
end
8+
9+
def self.parse(string)
10+
array = (string || '').split(' ').collect do | word |
11+
if word[0] == '/' and word[-1] == '/'
12+
Regexp.new(word[1..-2])
13+
else
14+
word
15+
end
16+
end
17+
SpaceDelimitedStringList.new(array)
18+
end
19+
20+
def to_s
21+
collect do | word |
22+
if word.is_a?(Regexp)
23+
"/#{word.source}/"
24+
else
25+
word
26+
end
27+
end.join(' ')
28+
end
29+
end
30+
end
31+
end

lib/pact_broker/configuration.rb

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'pact_broker/error'
2+
require 'pact_broker/config/space_delimited_string_list'
23

34
module PactBroker
45

@@ -23,15 +24,19 @@ class Configuration
2324
:check_for_potential_duplicate_pacticipant_names,
2425
:webhook_retry_schedule,
2526
:semver_formats,
26-
:disable_ssl_verification
27+
:disable_ssl_verification,
28+
:webhook_http_method_whitelist,
29+
:webhook_scheme_whitelist,
30+
:webhook_host_whitelist,
31+
:base_equality_only_on_content_that_affects_verification_results
2732
]
2833

2934
attr_accessor :log_dir, :database_connection, :auto_migrate_db, :use_hal_browser, :html_pact_renderer
3035
attr_accessor :validate_database_connection_config, :enable_diagnostic_endpoints, :version_parser, :sha_generator
3136
attr_accessor :use_case_sensitive_resource_names, :order_versions_by_date
3237
attr_accessor :check_for_potential_duplicate_pacticipant_names
33-
attr_accessor :webhook_http_method_whitelist, :webhook_scheme_whitelist, :webhook_host_whitelist
3438
attr_accessor :webhook_retry_schedule
39+
attr_reader :webhook_http_method_whitelist, :webhook_scheme_whitelist, :webhook_host_whitelist
3540
attr_accessor :semver_formats
3641
attr_accessor :enable_public_badge_access, :shields_io_base_url
3742
attr_accessor :disable_ssl_verification
@@ -68,8 +73,7 @@ def self.default_configuration
6873
config.version_parser = PactBroker::Versions::ParseSemanticVersion
6974
config.sha_generator = PactBroker::Pacts::GenerateSha
7075
config.base_equality_only_on_content_that_affects_verification_results = false
71-
# Not recommended to set this to true unless there is no way to
72-
# consistently extract an orderable object from the consumer application version number.
76+
# TODO change this to true
7377
config.order_versions_by_date = false
7478
config.semver_formats = ["%M.%m.%p%s%d", "%M.%m", "%M"]
7579
config.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200] #10 sec, 1 min, 2 min, 5 min, 10 min, 20 min => 38 minutes
@@ -171,8 +175,29 @@ def load_from_database!
171175
PactBroker::Config::Load.call(self)
172176
end
173177

178+
def webhook_http_method_whitelist= webhook_http_method_whitelist
179+
@webhook_http_method_whitelist = parse_space_delimited_string_list_property('webhook_http_method_whitelist', webhook_http_method_whitelist)
180+
end
181+
182+
def webhook_scheme_whitelist= webhook_scheme_whitelist
183+
@webhook_scheme_whitelist = parse_space_delimited_string_list_property('webhook_scheme_whitelist', webhook_scheme_whitelist)
184+
end
185+
186+
def webhook_host_whitelist= webhook_host_whitelist
187+
@webhook_host_whitelist = parse_space_delimited_string_list_property('webhook_host_whitelist', webhook_host_whitelist)
188+
end
189+
174190
private
175191

192+
def parse_space_delimited_string_list_property property_name, property_value
193+
case property_value
194+
when String then Config::SpaceDelimitedStringList.parse(property_value)
195+
when Array then Config::SpaceDelimitedStringList.new(property_value)
196+
else
197+
raise ConfigurationError.new("Pact Broker configuration property `#{property_name}` must be a space delimited String or an Array")
198+
end
199+
end
200+
176201
def create_logger path
177202
FileUtils::mkdir_p File.dirname(path)
178203
logger = Logger.new(path)

spec/lib/pact_broker/config/load_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Config
77
describe ".call" do
88

99
class MockConfig
10-
attr_accessor :foo, :bar, :nana, :meep, :lalala, :meow, :peebo
10+
attr_accessor :foo, :bar, :nana, :meep, :lalala, :meow, :peebo, :whitelist
1111
end
1212

1313
before do
@@ -19,6 +19,7 @@ class MockConfig
1919
Setting.create(name: 'meow', type: 'boolean', value: "0")
2020
Setting.create(name: 'peebo', type: 'string', value: nil)
2121
Setting.create(name: 'unknown', type: 'string', value: nil)
22+
Setting.create(name: 'whitelist', type: 'space_delimited_string_list', value: 'foo bar')
2223
end
2324

2425
let(:configuration) { MockConfig.new }
@@ -60,6 +61,11 @@ class MockConfig
6061
expect(configuration.peebo).to eq nil
6162
end
6263

64+
it "loads a space_delimited_string_list" do
65+
subject
66+
expect(configuration.whitelist).to eq ["foo", "bar"]
67+
end
68+
6369
it "does not load a setting where the Configuration object does not have a matching property" do
6470
allow(Load.logger).to receive(:warn)
6571
expect(Load.logger).to receive(:warn).with("Could not load configuration setting \"unknown\" as there is no matching attribute on the Configuration class")

spec/lib/pact_broker/config/save_spec.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
require 'pact_broker/config/save'
22
require 'pact_broker/configuration'
3+
require 'pact_broker/config/space_delimited_string_list'
34

45
module PactBroker
56
module Config
67
describe Save do
78

89
describe "#call" do
9-
let(:setting_names) { [:foo, :bar, :wiffle, :meep, :flop, :peebo, :lalala, :meow] }
10+
let(:setting_names) { [:foo, :bar, :wiffle, :meep, :flop, :peebo, :lalala, :meow, :whitelist] }
1011
let(:configuration) do
1112
double("PactBroker::Configuration",
1213
foo: true,
@@ -16,7 +17,8 @@ module Config
1617
flop: nil,
1718
peebo: 1,
1819
lalala: 1.2,
19-
meow: Object.new)
20+
meow: Object.new,
21+
whitelist: SpaceDelimitedStringList.parse("foo bar"))
2022
end
2123

2224
subject { Save.call(configuration, setting_names) }
@@ -70,6 +72,13 @@ module Config
7072
expect(setting.value).to eq '1.2'
7173
end
7274

75+
it "saves a SpaceDelimitedStringList" do
76+
subject
77+
setting = Setting.find(name: 'whitelist')
78+
expect(setting.type).to eq 'space_delimited_string_list'
79+
expect(setting.value).to eq 'foo bar'
80+
end
81+
7382
it "does not save an arbitrary object to the database" do
7483
allow(Save.logger).to receive(:warn)
7584
expect(Save.logger).to receive(:warn).with("Could not save configuration setting \"meow\" to database as the class Object is not supported.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'pact_broker/config/space_delimited_string_list'
2+
3+
module PactBroker
4+
module Config
5+
describe SpaceDelimitedStringList do
6+
describe "parse" do
7+
subject { SpaceDelimitedStringList.parse(input) }
8+
9+
context "when input is ''" do
10+
let(:input) { "" }
11+
12+
it { is_expected.to eq [] }
13+
14+
its(:to_s) { is_expected.to eq input }
15+
end
16+
17+
context "when input is 'foo bar'" do
18+
let(:input) { "foo bar" }
19+
20+
it { is_expected.to eq ["foo", "bar"] }
21+
22+
it { is_expected.to be_a SpaceDelimitedStringList }
23+
24+
its(:to_s) { is_expected.to eq input }
25+
end
26+
27+
context "when input is '/foo.*/'" do
28+
let(:input) { "/foo.*/" }
29+
30+
it { is_expected.to eq [/foo.*/] }
31+
32+
its(:to_s) { is_expected.to eq input }
33+
end
34+
35+
context "when input is '/foo\\.*/' (note double backslash)" do
36+
let(:input) { "/foo\\.*/" }
37+
38+
it { is_expected.to eq [/foo\.*/] }
39+
40+
its(:to_s) { is_expected.to eq input }
41+
end
42+
end
43+
end
44+
end
45+
end

spec/lib/pact_broker/configuration_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,32 @@ module PactBroker
1616
expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(pact, options)
1717
PactBroker.configuration.html_pact_renderer.call pact, options
1818
end
19+
end
20+
21+
describe "webhook_http_method_whitelist" do
22+
it "allows setting the whitelist by a string" do
23+
PactBroker.configuration.webhook_http_method_whitelist = "foo"
24+
expect(PactBroker.configuration.webhook_http_method_whitelist).to be_a Config::SpaceDelimitedStringList
25+
end
26+
27+
it "allows setting the whitelist by an array" do
28+
PactBroker.configuration.webhook_http_method_whitelist = ["foo"]
29+
expect(PactBroker.configuration.webhook_http_method_whitelist).to be_a Config::SpaceDelimitedStringList
30+
end
31+
end
1932

33+
describe "webhook_scheme_whitelist" do
34+
it "allows setting the whitelist by a string" do
35+
PactBroker.configuration.webhook_scheme_whitelist = "foo"
36+
expect(PactBroker.configuration.webhook_scheme_whitelist).to be_a Config::SpaceDelimitedStringList
37+
end
38+
end
39+
40+
describe "webhook_host_whitelist" do
41+
it "allows setting the whitelist by a string" do
42+
PactBroker.configuration.webhook_host_whitelist = "foo"
43+
expect(PactBroker.configuration.webhook_host_whitelist).to be_a Config::SpaceDelimitedStringList
44+
end
2045
end
2146

2247
describe "SETTING_NAMES" do

0 commit comments

Comments
 (0)