Skip to content

Commit

Permalink
Merge pull request #169 from tpitale/sh-support-sentinel-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tpitale authored Jun 9, 2024
2 parents b4e8e97 + b7d14ec commit d52c8ed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/mail_room/arbitration/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
module MailRoom
module Arbitration
class Redis
Options = Struct.new(:redis_url, :namespace, :sentinels) do
Options = Struct.new(:redis_url, :namespace, :sentinels, :sentinel_username, :sentinel_password) do
def initialize(mailbox)
redis_url = mailbox.arbitration_options[:redis_url] || "redis://localhost:6379"
namespace = mailbox.arbitration_options[:namespace]
sentinels = mailbox.arbitration_options[:sentinels]
sentinel_username = mailbox.arbitration_options[:sentinel_username]
sentinel_password = mailbox.arbitration_options[:sentinel_password]

if namespace
warn <<~MSG
Expand All @@ -16,7 +18,7 @@ def initialize(mailbox)
MSG
end

super(redis_url, namespace, sentinels)
super(redis_url, namespace, sentinels, sentinel_username, sentinel_password)
end
end

Expand Down Expand Up @@ -48,6 +50,8 @@ def client
sentinels = options.sentinels
redis_options = { url: options.redis_url }
redis_options[:sentinels] = sentinels if sentinels
redis_options[:sentinel_username] = options.sentinel_username if options.sentinel_username
redis_options[:sentinel_password] = options.sentinel_password if options.sentinel_password

redis = ::Redis.new(redis_options)

Expand Down
8 changes: 6 additions & 2 deletions lib/mail_room/delivery/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ module Delivery
# Sidekiq Delivery method
# @author Douwe Maan
class Sidekiq
Options = Struct.new(:redis_url, :namespace, :sentinels, :queue, :worker, :logger, :redis_db) do
Options = Struct.new(:redis_url, :namespace, :sentinels, :queue, :worker, :logger, :redis_db, :sentinel_username, :sentinel_password) do
def initialize(mailbox)
redis_url = mailbox.delivery_options[:redis_url] || "redis://localhost:6379"
redis_db = mailbox.delivery_options[:redis_db] || 0
namespace = mailbox.delivery_options[:namespace]
sentinels = mailbox.delivery_options[:sentinels]
sentinel_username = mailbox.delivery_options[:sentinel_username]
sentinel_password = mailbox.delivery_options[:sentinel_password]
queue = mailbox.delivery_options[:queue] || "default"
worker = mailbox.delivery_options[:worker]
logger = mailbox.logger
Expand All @@ -25,7 +27,7 @@ def initialize(mailbox)
MSG
end

super(redis_url, namespace, sentinels, queue, worker, logger, redis_db)
super(redis_url, namespace, sentinels, queue, worker, logger, redis_db, sentinel_username, sentinel_password)
end
end

Expand Down Expand Up @@ -55,6 +57,8 @@ def client
sentinels = options.sentinels
redis_options = { url: options.redis_url, db: options.redis_db }
redis_options[:sentinels] = sentinels if sentinels
redis_options[:sentinel_username] = options.sentinel_username if options.sentinel_username
redis_options[:sentinel_password] = options.sentinel_password if options.sentinel_password

redis = ::Redis.new(redis_options)

Expand Down
24 changes: 24 additions & 0 deletions spec/lib/arbitration/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@
expect(raw_client.config.password).to eq('mypassword')
expect(raw_client.config.sentinels.map(&:server_url)).to eq(["redis://10.0.0.1:26379"])
end

context 'with separate Sentinel username and password' do
let(:sentinel_username) { 'my-sentinel-user' }
let(:sentinel_password) { 'my-sentinel-pass' }
let(:mailbox) {
build_mailbox(
arbitration_options: {
redis_url: redis_url,
sentinels: sentinels,
sentinel_username: sentinel_username,
sentinel_password: sentinel_password
}
)
}

it 'client uses Sentinel username and password' do
expect(raw_client.config).to be_a RedisClient::SentinelConfig
expect(raw_client.config.password).to eq('mypassword')

sentinels = raw_client.config.sentinels
expect(sentinels.map(&:username).uniq).to eq([sentinel_username])
expect(sentinels.map(&:password).uniq).to eq([sentinel_password])
end
end
end
end
end
27 changes: 26 additions & 1 deletion spec/lib/delivery/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,32 @@
expect(raw_client.config.password).to eq('mypassword')
expect(raw_client.config.sentinels.map(&:server_url)).to eq(["redis://10.0.0.1:26379"])
end
end

context 'with separate Sentinel username and password' do
let(:sentinel_username) { 'my-sentinel-user' }
let(:sentinel_password) { 'my-sentinel-pass' }
let(:mailbox) {
build_mailbox(
delivery_method: :sidekiq,
delivery_options: {
redis_url: redis_url,
sentinels: sentinels,
sentinel_username: sentinel_username,
sentinel_password: sentinel_password
}
)
}

it 'client uses Sentinel username and password' do
expect(raw_client.config).to be_a RedisClient::SentinelConfig
expect(raw_client.config.password).to eq('mypassword')

sentinels = raw_client.config.sentinels

expect(sentinels.map(&:username).uniq).to eq([sentinel_username])
expect(sentinels.map(&:password).uniq).to eq([sentinel_password])
end
end
end
end
end

0 comments on commit d52c8ed

Please sign in to comment.