Skip to content

Commit b1e7c15

Browse files
committed
add basic_auth config option
In favor of this `basic_auth_user` and `basic_auth_password` have been removed. Example: ```ruby basic_auth: ->(topic) { username = ENV["TOPIC_#{topic.singularize.upcase}_USER"] password = ENV["SECRET_PASSWORD"] "#{username}:#{password}" } ``` It should always return a string in the format `USERNAME:PASSWORD`. Instead of using a proc this string can be set directly: ```ruby basic_auth: "foo:bar" ```
1 parent 60708be commit b1e7c15

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# MagicPipe Changelog
22

3+
## Unreleased
4+
* Allow to set the `basic_auth` config as a proc which gets the topic name passed.
5+
Example:
6+
```ruby
7+
basic_auth: ->(topic) {
8+
username = ENV["TOPIC_#{topic.singularize.upcase}_USER"]
9+
password = ENV["SECRET_PASSWORD"]
10+
"#{username}:#{password}"
11+
}
12+
```
13+
It should always return a string in the format `USERNAME:PASSWORD`. Instead
14+
of using a proc this string can be set directly:
15+
```ruby
16+
basic_auth: "foo:bar"
17+
```
18+
In favor of this `basic_auth_user` and `basic_auth_password` have been removed.
19+
320
## v0.2.0
421

522
Enhancing the HTTPS transport:

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ $magic_pipe = MagicPipe.build do |mp|
141141
}
142142
mp.https_transport_options = {
143143
url: "https://my.receiver.service/foo",
144-
basic_auth_user: "bar",
144+
basic_auth: "bar:foo",
145145
}
146146
mp.sqs_transport_options = {
147147
queue: "my_data_stream"
@@ -217,8 +217,7 @@ $magic_pipe = MagicPipe.build do |mp|
217217
url: "https://my.receiver.service/messages",
218218
dynamic_path_builder: -> (topic) { topic }
219219

220-
basic_auth_user: "foo",
221-
basic_auth_password: "bar",
220+
basic_auth: "foo:bar",
222221

223222
timeout: 2,
224223
open_timeout: 3,

lib/magic_pipe/config.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def set_https_defaults
7474
#
7575
dynamic_path_builder: nil,
7676

77-
basic_auth_user: "missing",
78-
basic_auth_password: "x",
77+
basic_auth: "missing:x",
7978
timeout: 2,
8079
open_timeout: 3,
8180
}

lib/magic_pipe/transports/https.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def initialize(config, metrics)
2121
# So that it can be retried?
2222
#
2323
def submit(payload, metadata)
24+
username, password = basic_auth(metadata[:topic])
25+
@conn.basic_auth(username, password || "x")
2426
@conn.post do |r|
2527
path = dynamic_path(metadata[:topic])
2628
r.url(path) if path
@@ -45,12 +47,14 @@ def url
4547
@options.fetch(:url)
4648
end
4749

48-
def basic_auth_user
49-
@options.fetch(:basic_auth_user)
50-
end
51-
52-
def basic_auth_password
53-
@options.fetch(:basic_auth_password)
50+
def basic_auth(topic)
51+
user_auth = @options.fetch(:basic_auth)
52+
credentials = if user_auth.respond_to?(:call)
53+
user_auth.call(topic)
54+
else
55+
user_auth
56+
end
57+
credentials.split(':')
5458
end
5559

5660
def timeout
@@ -78,7 +82,6 @@ def user_agent
7882
def build_connection
7983
Faraday.new(url) do |f|
8084
f.request :retry, max: 2, interval: 0.1, backoff_factor: 2
81-
f.request :basic_auth, basic_auth_user, basic_auth_password
8285

8386
f.headers['Content-Type'] = content_type
8487
f.headers['User-Agent'] = user_agent

spec/magic_pipe/config_spec.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
actual = subject.https_transport_options
3636

3737
expect(actual[:url]).to_not be_nil
38-
expect(actual[:basic_auth_user]).to_not be_nil
39-
expect(actual[:basic_auth_password]).to_not be_nil
38+
expect(actual[:basic_auth]).to_not be_nil
4039
expect(actual[:timeout]).to_not be_nil
4140
expect(actual[:open_timeout]).to_not be_nil
4241

@@ -59,8 +58,7 @@
5958
expect(actual[:url]).to eq "http://foo.bar"
6059
expect(actual[:dynamic_path_builder]).to eq fn
6160

62-
expect(actual[:basic_auth_user]).to_not be_nil
63-
expect(actual[:basic_auth_password]).to_not be_nil
61+
expect(actual[:basic_auth]).to_not be_nil
6462
expect(actual[:timeout]).to_not be_nil
6563
expect(actual[:open_timeout]).to_not be_nil
6664
end

spec/magic_pipe/transports/https_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
let(:https_options) do
88
{
99
url: base_url,
10-
basic_auth_user: basic_auth_user,
11-
basic_auth_password: "x",
10+
basic_auth: "#{basic_auth_user}:x",
1211
}
1312
end
1413

@@ -108,5 +107,17 @@ def self.it_submits_a_request_with_the_correct_data
108107

109108
it_submits_a_request_with_the_correct_data
110109
end
110+
111+
describe "when using a dynamic `basic_auth`" do
112+
let(:target_url) { base_url }
113+
let(:https_options) do
114+
super().merge(
115+
basic_auth: -> (topic) { "test-#{topic}:foobar" }
116+
)
117+
end
118+
let(:auth_header) { "Basic " + Base64.strict_encode64("test-marsupials:foobar") }
119+
120+
it_submits_a_request_with_the_correct_data
121+
end
111122
end
112123
end

0 commit comments

Comments
 (0)