Class: PactBroker::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/configuration.rb

Constant Summary collapse

SAVABLE_SETTING_NAMES =
[
  :order_versions_by_date,
  :use_case_sensitive_resource_names,
  :enable_public_badge_access,
  :shields_io_base_url,
  :check_for_potential_duplicate_pacticipant_names,
  :webhook_retry_schedule,
  :semver_formats,
  :disable_ssl_verification,
  :webhook_http_method_whitelist,
  :webhook_scheme_whitelist,
  :webhook_host_whitelist,
  :base_equality_only_on_content_that_affects_verification_results,
  :seed_example_data,
  :badge_provider_mode,
  :warning_error_class_names,
  :base_url,
  :log_dir,
  :allow_missing_migration_files,
  :auto_migrate_db_data,
  :use_rack_protection,
  :metrics_sql_statement_timeout
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



67
68
69
70
71
72
73
74
# File 'lib/pact_broker/configuration.rb', line 67

def initialize
  @before_resource_hook = ->(resource){}
  @after_resource_hook = ->(resource){}
  @authenticate_with_basic_auth = nil
  @authorize = nil
  @api_error_reporters = []
  @semantic_logger = SemanticLogger["root"]
end

Instance Attribute Details

#allow_missing_migration_filesObject

Returns the value of attribute allow_missing_migration_files.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def allow_missing_migration_files
  @allow_missing_migration_files
end

#api_error_reportersObject (readonly)

Returns the value of attribute api_error_reporters.



59
60
61
# File 'lib/pact_broker/configuration.rb', line 59

def api_error_reporters
  @api_error_reporters
end

#auto_migrate_dbObject

Returns the value of attribute auto_migrate_db.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def auto_migrate_db
  @auto_migrate_db
end

#auto_migrate_db_dataObject

Returns the value of attribute auto_migrate_db_data.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def auto_migrate_db_data
  @auto_migrate_db_data
end

#badge_provider_modeObject

Returns the value of attribute badge_provider_mode.



55
56
57
# File 'lib/pact_broker/configuration.rb', line 55

def badge_provider_mode
  @badge_provider_mode
end

#base_equality_only_on_content_that_affects_verification_resultsObject

Returns the value of attribute base_equality_only_on_content_that_affects_verification_results.



58
59
60
# File 'lib/pact_broker/configuration.rb', line 58

def base_equality_only_on_content_that_affects_verification_results
  @base_equality_only_on_content_that_affects_verification_results
end

#base_resource_class_factoryObject

Returns the value of attribute base_resource_class_factory.



61
62
63
# File 'lib/pact_broker/configuration.rb', line 61

def base_resource_class_factory
  @base_resource_class_factory
end

#base_urlObject

Returns the value of attribute base_url.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def base_url
  @base_url
end

#check_for_potential_duplicate_pacticipant_namesObject

Returns the value of attribute check_for_potential_duplicate_pacticipant_names.



50
51
52
# File 'lib/pact_broker/configuration.rb', line 50

def check_for_potential_duplicate_pacticipant_names
  @check_for_potential_duplicate_pacticipant_names
end

#content_security_policyObject

Returns the value of attribute content_security_policy.



57
58
59
# File 'lib/pact_broker/configuration.rb', line 57

def content_security_policy
  @content_security_policy
end

#custom_loggerObject (readonly)

Returns the value of attribute custom_logger.



60
61
62
# File 'lib/pact_broker/configuration.rb', line 60

def custom_logger
  @custom_logger
end

#database_connectionObject

Returns the value of attribute database_connection.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def database_connection
  @database_connection
end

#disable_ssl_verificationObject

Returns the value of attribute disable_ssl_verification.



56
57
58
# File 'lib/pact_broker/configuration.rb', line 56

def disable_ssl_verification
  @disable_ssl_verification
end

#enable_diagnostic_endpointsObject

Returns the value of attribute enable_diagnostic_endpoints.



47
48
49
# File 'lib/pact_broker/configuration.rb', line 47

def enable_diagnostic_endpoints
  @enable_diagnostic_endpoints
end

#enable_public_badge_accessObject

Returns the value of attribute enable_public_badge_access.



55
56
57
# File 'lib/pact_broker/configuration.rb', line 55

def enable_public_badge_access
  @enable_public_badge_access
end

#example_data_seederObject

Returns the value of attribute example_data_seeder.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def example_data_seeder
  @example_data_seeder
end

#hal_browser_content_security_policy_overridesObject

Returns the value of attribute hal_browser_content_security_policy_overrides.



57
58
59
# File 'lib/pact_broker/configuration.rb', line 57

def hal_browser_content_security_policy_overrides
  @hal_browser_content_security_policy_overrides
end

#html_pact_rendererObject

Returns the value of attribute html_pact_renderer.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def html_pact_renderer
  @html_pact_renderer
end

#log_dirObject

Returns the value of attribute log_dir.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def log_dir
  @log_dir
end

#metrics_sql_statement_timeoutObject

Returns the value of attribute metrics_sql_statement_timeout.



62
63
64
# File 'lib/pact_broker/configuration.rb', line 62

def metrics_sql_statement_timeout
  @metrics_sql_statement_timeout
end

#order_versions_by_dateObject

Returns the value of attribute order_versions_by_date.



48
49
50
# File 'lib/pact_broker/configuration.rb', line 48

def order_versions_by_date
  @order_versions_by_date
end

#policy_builderObject

Returns the value of attribute policy_builder.



61
62
63
# File 'lib/pact_broker/configuration.rb', line 61

def policy_builder
  @policy_builder
end

#policy_scope_builderObject

Returns the value of attribute policy_scope_builder.



61
62
63
# File 'lib/pact_broker/configuration.rb', line 61

def policy_scope_builder
  @policy_scope_builder
end

#seed_example_dataObject

Returns the value of attribute seed_example_data.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def seed_example_data
  @seed_example_data
end

#semver_formatsObject

Returns the value of attribute semver_formats.



54
55
56
# File 'lib/pact_broker/configuration.rb', line 54

def semver_formats
  @semver_formats
end

#sha_generatorObject

Returns the value of attribute sha_generator.



47
48
49
# File 'lib/pact_broker/configuration.rb', line 47

def sha_generator
  @sha_generator
end

#shields_io_base_urlObject

Returns the value of attribute shields_io_base_url.



55
56
57
# File 'lib/pact_broker/configuration.rb', line 55

def shields_io_base_url
  @shields_io_base_url
end

#use_case_sensitive_resource_namesObject

Returns the value of attribute use_case_sensitive_resource_names.



48
49
50
# File 'lib/pact_broker/configuration.rb', line 48

def use_case_sensitive_resource_names
  @use_case_sensitive_resource_names
end

#use_hal_browserObject

Returns the value of attribute use_hal_browser.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def use_hal_browser
  @use_hal_browser
end

#use_rack_protectionObject

Returns the value of attribute use_rack_protection.



46
47
48
# File 'lib/pact_broker/configuration.rb', line 46

def use_rack_protection
  @use_rack_protection
end

#user_agentObject

Returns the value of attribute user_agent.



52
53
54
# File 'lib/pact_broker/configuration.rb', line 52

def user_agent
  @user_agent
end

#validate_database_connection_configObject

Returns the value of attribute validate_database_connection_config.



47
48
49
# File 'lib/pact_broker/configuration.rb', line 47

def validate_database_connection_config
  @validate_database_connection_config
end

#version_parserObject

Returns the value of attribute version_parser.



47
48
49
# File 'lib/pact_broker/configuration.rb', line 47

def version_parser
  @version_parser
end

#warning_error_class_namesObject

Returns the value of attribute warning_error_class_names.



49
50
51
# File 'lib/pact_broker/configuration.rb', line 49

def warning_error_class_names
  @warning_error_class_names
end

#webhook_host_whitelistObject

Returns the value of attribute webhook_host_whitelist.



53
54
55
# File 'lib/pact_broker/configuration.rb', line 53

def webhook_host_whitelist
  @webhook_host_whitelist
end

#webhook_http_code_successObject

Returns the value of attribute webhook_http_code_success.



53
54
55
# File 'lib/pact_broker/configuration.rb', line 53

def webhook_http_code_success
  @webhook_http_code_success
end

#webhook_http_method_whitelistObject

Returns the value of attribute webhook_http_method_whitelist.



53
54
55
# File 'lib/pact_broker/configuration.rb', line 53

def webhook_http_method_whitelist
  @webhook_http_method_whitelist
end

#webhook_retry_scheduleObject

Returns the value of attribute webhook_retry_schedule.



51
52
53
# File 'lib/pact_broker/configuration.rb', line 51

def webhook_retry_schedule
  @webhook_retry_schedule
end

#webhook_scheme_whitelistObject

Returns the value of attribute webhook_scheme_whitelist.



53
54
55
# File 'lib/pact_broker/configuration.rb', line 53

def webhook_scheme_whitelist
  @webhook_scheme_whitelist
end

Class Method Details

.default_configurationObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pact_broker/configuration.rb', line 76

def self.default_configuration
  require 'pact_broker/versions/parse_semantic_version'
  require 'pact_broker/pacts/generate_sha'

  config = Configuration.new
  config.log_dir = File.expand_path("./log")
  config.auto_migrate_db = true
  config.auto_migrate_db_data = true
  config.allow_missing_migration_files = false
  config.use_rack_protection = true
  config.use_hal_browser = true
  config.validate_database_connection_config = true
  config.enable_diagnostic_endpoints = true
  config.enable_public_badge_access = false # For security
  config.shields_io_base_url = "https://img.shields.io".freeze
  config.badge_provider_mode = :proxy # other option is :redirect
  config.use_case_sensitive_resource_names = true
  config.html_pact_renderer = default_html_pact_render
  config.version_parser = PactBroker::Versions::ParseSemanticVersion
  config.sha_generator = PactBroker::Pacts::GenerateSha
  config.seed_example_data = true
  config.example_data_seeder = lambda do
    require 'pact_broker/db/seed_example_data'
    PactBroker::DB::SeedExampleData.call
  end
  config.user_agent = "Pact Broker v#{PactBroker::VERSION}"
  config.base_equality_only_on_content_that_affects_verification_results = true
  config.order_versions_by_date = true
  config.semver_formats = ["%M.%m.%p%s%d", "%M.%m", "%M"]
  config.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200] #10 sec, 1 min, 2 min, 5 min, 10 min, 20 min => 38 minutes
  config.check_for_potential_duplicate_pacticipant_names = true
  config.disable_ssl_verification = false
  config.webhook_http_method_whitelist = ['POST']
  config.webhook_http_code_success = [200, 201, 202, 203, 204, 205, 206]
  config.webhook_scheme_whitelist = ['https']
  config.webhook_host_whitelist = []
  # TODO get rid of unsafe-inline
  config.content_security_policy = {
    script_src: "'self' 'unsafe-inline'",
    style_src: "'self' 'unsafe-inline'",
    img_src: "'self' data: #{URI(config.shields_io_base_url).host}",
    font_src: "'self' data:",
    base_uri: "'self'",
    frame_src: "'self'",
    frame_ancestors: "'self'"
  }
  config.hal_browser_content_security_policy_overrides = {
    script_src: "'self' 'unsafe-inline' 'unsafe-eval'",
    frame_ancestors: "'self'"
  }
  config.policy_builder = -> (object) { DefaultPolicy.new(nil, object) }
  config.policy_scope_builder = -> (scope) { scope }
  config.base_resource_class_factory = -> () {
    require 'pact_broker/api/resources/default_base_resource'
    PactBroker::Api::Resources::DefaultBaseResource
  }
  config.warning_error_class_names = ['Sequel::ForeignKeyConstraintViolation', 'PG::QueryCanceled']
  config.metrics_sql_statement_timeout = 30
  config
end

.default_html_pact_renderObject



151
152
153
154
155
156
# File 'lib/pact_broker/configuration.rb', line 151

def self.default_html_pact_render
  lambda { |pact, options|
    require 'pact_broker/api/renderers/html_pact_renderer'
    PactBroker::Api::Renderers::HtmlPactRenderer.call pact, options
  }
end

Instance Method Details

#add_api_error_reporter(&block) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/pact_broker/configuration.rb', line 210

def add_api_error_reporter &block
  if block_given?
    unless block.arity == 2
      raise ConfigurationError.new("api_error_notfifier block must accept two arguments, 'error' and 'options'")
    end
    @api_error_reporters << block
    nil
  end
end

#after_resource(&block) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/pact_broker/configuration.rb', line 202

def after_resource &block
  if block_given?
    @after_resource_hook = block
  else
    @after_resource_hook
  end
end

#authenticate(&block) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/pact_broker/configuration.rb', line 166

def authenticate &block
  if block_given?
    @authenticate = block
  else
    @authenticate
  end
end

#authenticate_with_basic_auth(&block) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/pact_broker/configuration.rb', line 174

def authenticate_with_basic_auth &block
  if block_given?
    @authenticate_with_basic_auth = block
  else
    @authenticate_with_basic_auth
  end
end

#authentication_configured?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/pact_broker/configuration.rb', line 162

def authentication_configured?
  !!authenticate || !!authenticate_with_basic_auth
end

#authorization_configured?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/pact_broker/configuration.rb', line 182

def authorization_configured?
  !!authorize
end

#authorize(&block) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/pact_broker/configuration.rb', line 186

def authorize &block
  if block_given?
    @authorize = block
  else
    @authorize
  end
end

#before_resource(&block) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/pact_broker/configuration.rb', line 194

def before_resource &block
  if block_given?
    @before_resource_hook = block
  else
    @before_resource_hook
  end
end

#enable_badge_resources=(enable_badge_resources) ⇒ Object



224
225
226
227
# File 'lib/pact_broker/configuration.rb', line 224

def enable_badge_resources= enable_badge_resources
  puts "Pact Broker configuration property `enable_badge_resources` is deprecated. Please use `enable_public_badge_access`"
  self.enable_public_badge_access = enable_badge_resources
end

#load_from_database!Object



235
236
237
238
239
# File 'lib/pact_broker/configuration.rb', line 235

def load_from_database!
  # Can't require a Sequel::Model class before the connection has been set
  require 'pact_broker/config/load'
  PactBroker::Config::Load.call(self)
end

#log_configurationObject



145
146
147
148
149
# File 'lib/pact_broker/configuration.rb', line 145

def log_configuration
  SAVABLE_SETTING_NAMES.sort.each do | setting |
    logger.info "PactBroker.configuration.#{setting}=#{PactBroker.configuration.send(setting).inspect}"
  end
end

#loggerObject



137
138
139
# File 'lib/pact_broker/configuration.rb', line 137

def logger
  custom_logger || @semantic_logger
end

#logger=(logger) ⇒ Object



141
142
143
# File 'lib/pact_broker/configuration.rb', line 141

def logger= logger
  @custom_logger = logger
end

#save_to_databaseObject



229
230
231
232
233
# File 'lib/pact_broker/configuration.rb', line 229

def save_to_database
  # Can't require a Sequel::Model class before the connection has been set
  require 'pact_broker/config/save'
  PactBroker::Config::Save.call(self, SAVABLE_SETTING_NAMES)
end

#show_backtrace_in_error_response?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/pact_broker/configuration.rb', line 158

def show_backtrace_in_error_response?
  !!(ENV['RACK_ENV'] && ENV['RACK_ENV'].downcase != 'production')
end

#show_webhook_response?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/pact_broker/configuration.rb', line 220

def show_webhook_response?
  webhook_host_whitelist.any?
end

#warning_error_classesObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/pact_broker/configuration.rb', line 257

def warning_error_classes
  warning_error_class_names.collect do | class_name |
    begin
      Object.const_get(class_name)
    rescue NameError => e
      logger.warn("Class #{class_name} couldn't be loaded as a warning error class (#{e.class} - #{e.message}). Ignoring.")
      nil
    end
  end.compact
end