From 00b6c46f1ea0ec93de5cb98a0aa93ce4d013eb44 Mon Sep 17 00:00:00 2001 From: Christoph Pitschmann Date: Mon, 22 Feb 2021 00:04:52 +0100 Subject: [PATCH] feat: 38 add docker environment variable PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS (#39) * feat: 38 add docker environment variable PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS feat: do not print database password, if present * fix: move password masking from config.ru to docker_configuration.rb fix: copy&paste issue in the test suite --- README.md | 1 + pact_broker/config.ru | 1 + pact_broker/docker_configuration.rb | 13 ++++++++++++- spec/docker_configuration_spec.rb | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f4800f3..7417838 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Set the environment variable `PACT_BROKER_LOG_LEVEL` to one of `DEBUG`, `INFO`, ## Other webhook settings * `PACT_BROKER_WEBHOOK_RETRY_SCHEDULE` - a space delimited list of integers specifying the number of seconds after which to retry webhook requests when they fail. Defaults to `10 60 120 300 600 1200`. This does not normally need to be changed. +* `PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS` - a space delimited list of successful http codes (e.g. `200 201 301`). Defaults to `200 201 202 203 204 205 206`. If webhook call returns the response with http code that is listed in the success codes then the operation is considered as a success, otherwise the webhook will be re-triggered based on `PACT_BROKER_WEBHOOK_RETRY_SCHEDULE` configuration. ## Other environment variables diff --git a/pact_broker/config.ru b/pact_broker/config.ru index a0c51be..5a5412f 100644 --- a/pact_broker/config.ru +++ b/pact_broker/config.ru @@ -21,6 +21,7 @@ app = PactBroker::App.new do | config | config.webhook_http_method_whitelist = dc.webhook_http_method_whitelist config.webhook_scheme_whitelist = dc.webhook_scheme_whitelist config.webhook_retry_schedule = dc.webhook_retry_schedule + config.webhook_http_code_success = dc.webhook_http_code_success config.base_equality_only_on_content_that_affects_verification_results = dc.base_equality_only_on_content_that_affects_verification_results config.order_versions_by_date = dc.order_versions_by_date config.disable_ssl_verification = dc.disable_ssl_verification diff --git a/pact_broker/docker_configuration.rb b/pact_broker/docker_configuration.rb index f407cfc..ef1a9c8 100644 --- a/pact_broker/docker_configuration.rb +++ b/pact_broker/docker_configuration.rb @@ -9,7 +9,14 @@ def initialize env, default_configuration def pact_broker_environment_variables pact_broker_environment_variable_names.sort.each_with_object({}) do | name, hash | - hash[name] = name =~ /password/i ? "*****" : @env[name] + value = @env[name] + # special case: suppress password of database connection string, if present + if name == "PACT_BROKER_DATABASE_URL" && value =~ /:\/\/[^:]+:[^@]+@/ + hash[name] = value.sub(/(:\/\/[^:]+):[^@]+@/, '\1:*****@') + else + hash[name] = name =~ /password/i ? "*****" : value + end + end end @@ -30,6 +37,10 @@ def webhook_http_method_whitelist space_delimited_string_list_or_default(:webhook_http_method_whitelist) end + def webhook_http_code_success + space_delimited_integer_list_or_default(:webhook_http_code_success) + end + def webhook_retry_schedule space_delimited_integer_list_or_default(:webhook_retry_schedule) end diff --git a/spec/docker_configuration_spec.rb b/spec/docker_configuration_spec.rb index 90092ae..3fbb1f0 100644 --- a/spec/docker_configuration_spec.rb +++ b/spec/docker_configuration_spec.rb @@ -10,6 +10,7 @@ { "PACT_BROKER_WEBHOOK_HOST_WHITELIST" => host_whitelist, "PACT_BROKER_WEBHOOK_RETRY_SCHEDULE" => retry_schedule, + "PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS" => http_code_success, "PACT_BROKER_ORDER_VERSIONS_BY_DATE" => "false" } end @@ -18,9 +19,12 @@ let(:retry_schedule) { "" } + let(:http_code_success) { "" } + let(:default_configuration) do instance_double('default configuration', webhook_host_whitelist: 'default', + webhook_http_code_success: 'default', webhook_retry_schedule: 'default' ) end @@ -111,6 +115,7 @@ it { expect(subject.database_configuration[:database]).to eq "pactbroker" } it { expect(subject.database_configuration[:encoding]).to eq "utf8" } it { expect(subject.database_configuration[:port]).to eq 5432 } + it { expect(subject.pact_broker_environment_variables["PACT_BROKER_DATABASE_URL"]).to eq "postgresql://pactbrokeruser:*****@localhost:5432/pactbroker" } end context "using a configured environment variable name and an arbitrary env var" do @@ -212,6 +217,18 @@ end end + describe "webhook_http_code_success" do + context "when PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS is '200 202 301 302'" do + let(:http_code_success) { "200 202 301 302" } + its(:webhook_http_code_success) { is_expected.to eq [200, 202, 301, 302] } + end + + context "when PACT_BROKER_WEBHOOK_HTTP_CODE_SUCCESS is ''" do + let(:http_code_success) { "" } + its(:webhook_http_code_success) { is_expected.to eq 'default' } + end + end + describe "webhook_retry_schedule" do context "when PACT_BROKER_WEBHOOK_RETRY_SCHEDULE is '1 2 3'" do let(:retry_schedule) { "1 2 3" }