Skip to content

Commit 3a72a29

Browse files
Merge pull request #1948 from dmitry-sinina/cdr_processor_http_auth_fix
cdr_processors: fix http basic auth support
2 parents ba03bb9 + 08a94e0 commit 3a72a29

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

app/lib/cdr_processor/processors/cdr_http_base.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ def send_http_request(payload)
8181
else
8282
kwargs[:body] = http_body(payload)
8383
end
84-
response = HTTPX.public_send(http_method, http_url, **kwargs)
84+
client = HTTPX
85+
if @params['auth_user'].present?
86+
client = client.plugin(:basic_auth).basic_auth(@params['auth_user'], @params['auth_password'].to_s)
87+
end
88+
response = client.public_send(http_method, http_url, **kwargs)
8589
response.raise_for_status
8690
response
8791
end

config/cdr_processors.yml.distr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ cdr_http:
2525
mail_subject: "Exception happened in CDR http"
2626
url: "http://example.com/api/cdrs"
2727
method: POST
28+
# auth_user: user
29+
# auth_password: password
2830
# may be an array:
2931
# cdr_fields: [
3032
# 'id', 'src_prefix_in', 'src_prefix_out', 'dst_prefix_in', 'dst_prefix_out', 'time_start',
@@ -64,6 +66,8 @@ cdr_clickhouse:
6466
mail_from: "yeti-cdr-billing@example.com"
6567
mail_subject: "Exception happened in CDR http"
6668
url: "http://clickhouse.example.com"
69+
# auth_user: user
70+
# auth_password: password
6771
clickhouse_db: main
6872
clickhouse_table: cdrs
6973
# may be an array:

spec/lib/cdr_processor/processors/cdr_clickhouse_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@
8383
end
8484
end
8585

86+
context 'with basic auth credentials' do
87+
let(:config) { super().merge(auth_user: 'yeti', auth_password: 'secret') }
88+
89+
let!(:stub_clickhouse_request) do
90+
WebMock.stub_request(:post, config[:url])
91+
.with(
92+
query: expected_query,
93+
body: expected_body,
94+
headers: { 'Authorization' => "Basic #{Base64.strict_encode64('yeti:secret')}" }
95+
)
96+
.and_return(status: response_status, body: nil)
97+
end
98+
99+
it 'sends request with basic auth header' do
100+
subject
101+
expect(stub_clickhouse_request).to have_been_requested
102+
end
103+
end
104+
86105
context 'permit array attribute' do
87106
let(:config) { super().merge cdr_fields: %w[id local_tag] }
88107
let(:expected_body) do

spec/lib/cdr_processor/processors/cdr_http_batch_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@
9595
end
9696
end
9797

98+
context 'with basic auth credentials' do
99+
let(:config) { super().merge('auth_user' => 'yeti', 'auth_password' => 'secret') }
100+
101+
it 'sends request with basic auth header' do
102+
subject
103+
expect(WebMock).to have_requested(:post, config['url']).once
104+
.with(headers: { 'Authorization' => "Basic #{Base64.strict_encode64('yeti:secret')}" })
105+
end
106+
end
107+
98108
context 'permit array attribute' do
99109
let(:cdr_fields) { ['id'] }
100110

spec/lib/cdr_processor/processors/cdr_http_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@
8383
end
8484
end
8585

86+
context 'with basic auth credentials' do
87+
let(:config) { super().merge('auth_user' => 'yeti', 'auth_password' => 'secret') }
88+
89+
it 'sends requests with basic auth header' do
90+
subject
91+
expect(WebMock).to have_requested(:post, config['url']).times(2)
92+
.with(headers: { 'Authorization' => "Basic #{Base64.strict_encode64('yeti:secret')}" })
93+
end
94+
end
95+
8696
context 'permit array attribute' do
8797
let(:cdr_fields) { ['id'] }
8898

0 commit comments

Comments
 (0)