Class: PactBroker::Configuration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



44
45
46
47
48
49
50
51
# File 'lib/pact_broker/configuration.rb', line 44

def initialize
  @runtime_configuration = PactBroker::Config::RuntimeConfiguration.new
  @before_resource_hook = ->(resource){}
  @after_resource_hook = ->(resource){}
  @authenticate_with_basic_auth = nil
  @authorize = nil
  @api_error_reporters = []
end

Instance Attribute Details

#api_error_reportersObject

Returns the value of attribute api_error_reporters.



36
37
38
# File 'lib/pact_broker/configuration.rb', line 36

def api_error_reporters
  @api_error_reporters
end

#base_resource_class_factoryObject

Returns the value of attribute base_resource_class_factory.



38
39
40
# File 'lib/pact_broker/configuration.rb', line 38

def base_resource_class_factory
  @base_resource_class_factory
end

#content_security_policyObject

Returns the value of attribute content_security_policy.



35
36
37
# File 'lib/pact_broker/configuration.rb', line 35

def content_security_policy
  @content_security_policy
end

#custom_loggerObject (readonly)

Returns the value of attribute custom_logger.



37
38
39
# File 'lib/pact_broker/configuration.rb', line 37

def custom_logger
  @custom_logger
end

#database_connectionObject

Returns the value of attribute database_connection.



32
33
34
# File 'lib/pact_broker/configuration.rb', line 32

def database_connection
  @database_connection
end

#example_data_seederObject

Returns the value of attribute example_data_seeder.



33
34
35
# File 'lib/pact_broker/configuration.rb', line 33

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.



35
36
37
# File 'lib/pact_broker/configuration.rb', line 35

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.



34
35
36
# File 'lib/pact_broker/configuration.rb', line 34

def html_pact_renderer
  @html_pact_renderer
end

#policy_builderObject

Returns the value of attribute policy_builder.



38
39
40
# File 'lib/pact_broker/configuration.rb', line 38

def policy_builder
  @policy_builder
end

#policy_scope_builderObject

Returns the value of attribute policy_scope_builder.



38
39
40
# File 'lib/pact_broker/configuration.rb', line 38

def policy_scope_builder
  @policy_scope_builder
end

#runtime_configurationObject

Returns the value of attribute runtime_configuration.



42
43
44
# File 'lib/pact_broker/configuration.rb', line 42

def runtime_configuration
  @runtime_configuration
end

#sha_generatorObject

Returns the value of attribute sha_generator.



34
35
36
# File 'lib/pact_broker/configuration.rb', line 34

def sha_generator
  @sha_generator
end

#version_parserObject

Returns the value of attribute version_parser.



34
35
36
# File 'lib/pact_broker/configuration.rb', line 34

def version_parser
  @version_parser
end

Class Method Details

.default_configurationObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pact_broker/configuration.rb', line 53

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

  config = Configuration.new
  config.html_pact_renderer = default_html_pact_render
  config.version_parser = PactBroker::Versions::ParseSemanticVersion
  config.sha_generator = PactBroker::Pacts::GenerateSha
  config.example_data_seeder = lambda do
    require "pact_broker/db/seed_example_data"
    PactBroker::DB::SeedExampleData.call
  end

  # 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
end

.default_html_pact_renderObject



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

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



214
215
216
217
218
219
220
221
222
# File 'lib/pact_broker/configuration.rb', line 214

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



206
207
208
209
210
211
212
# File 'lib/pact_broker/configuration.rb', line 206

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

#authenticate(&block) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/pact_broker/configuration.rb', line 170

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

#authenticate_with_basic_auth(&block) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/pact_broker/configuration.rb', line 178

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)


166
167
168
# File 'lib/pact_broker/configuration.rb', line 166

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

#authorization_configured?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/pact_broker/configuration.rb', line 186

def authorization_configured?
  !!authorize
end

#authorize(&block) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/pact_broker/configuration.rb', line 190

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

#before_resource(&block) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/pact_broker/configuration.rb', line 198

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

#enable_badge_resources=(enable_badge_resources) ⇒ Object



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

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



233
234
235
236
237
# File 'lib/pact_broker/configuration.rb', line 233

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(runtime_configuration)
end

#log_configurationObject



148
149
150
151
152
153
# File 'lib/pact_broker/configuration.rb', line 148

def log_configuration
  logger.info "------------------------------------------------------------------------"
  logger.info "PACT BROKER CONFIGURATION:"
  runtime_configuration.log_configuration(logger)
  logger.info "------------------------------------------------------------------------"
end

#loggerObject



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

def logger
  custom_logger || logger_from_runtime_configuration
end

#logger=(logger) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/pact_broker/configuration.rb', line 140

def logger= logger
  if @default_appender && SemanticLogger.appenders.include?(@default_appender)
    SemanticLogger.remove_appender(@default_appender)
    @default_appender = nil
  end
  @custom_logger = logger
end

#logger_from_runtime_configurationObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pact_broker/configuration.rb', line 121

def logger_from_runtime_configuration
  @logger_from_runtime_configuration ||= begin
    runtime_configuration.validate_logging_attributes!
    SemanticLogger.default_level = runtime_configuration.log_level
    if runtime_configuration.log_stream == :file
      path = runtime_configuration.log_dir + "/pact_broker.log"
      FileUtils.mkdir_p(runtime_configuration.log_dir)
      @default_appender = SemanticLogger.add_appender(file_name: path, formatter: runtime_configuration.log_format)
    else
      @default_appender = SemanticLogger.add_appender(io: $stdout, formatter: runtime_configuration.log_format)
    end
    @logger_from_runtime_configuration = SemanticLogger["pact-broker"]
  end
end

#override_runtime_configuration!(overrides) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pact_broker/configuration.rb', line 97

def override_runtime_configuration!(overrides)
  new_runtime_configuration = runtime_configuration.dup
  valid_overrides = {}
  invalid_overrides = {}

  overrides.each do | key, value |
    if new_runtime_configuration.respond_to?("#{key}=")
      valid_overrides[key] = Anyway::AutoCast.call(value)
    else
      invalid_overrides[key] = Anyway::AutoCast.call(value)
    end
  end

  if logger.debug?
    logger.debug("Overridding runtime configuration", payload: { overrides: valid_overrides, ignoring: invalid_overrides })
  end

  valid_overrides.each do | key, value |
    new_runtime_configuration.public_send("#{key}=", value)
  end

  self.runtime_configuration = new_runtime_configuration
end

#show_backtrace_in_error_response?Boolean

Returns:

  • (Boolean)


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

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

#show_webhook_response?Boolean

Returns:

  • (Boolean)


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

def show_webhook_response?
  webhook_host_whitelist&.any?
end

#with_runtime_configuration_overrides(overrides) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/pact_broker/configuration.rb', line 89

def with_runtime_configuration_overrides(overrides)
  original_runtime_configuration = runtime_configuration
  self.runtime_configuration = override_runtime_configuration!(overrides)
  yield
ensure
  self.runtime_configuration = original_runtime_configuration
end