0
0
Fork 0

Add credentials to redis sentinel configuration (#31768)

This commit is contained in:
David Roetzel 2024-09-05 16:06:58 +02:00 committed by GitHub
parent b4b639ee4a
commit f85694acfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 23 deletions

View file

@ -57,39 +57,50 @@ class Mastodon::RedisConfiguration
def setup_config(prefix: nil, defaults: {})
prefix = "#{prefix}REDIS_"
url = ENV.fetch("#{prefix}URL", nil)
user = ENV.fetch("#{prefix}USER", nil)
password = ENV.fetch("#{prefix}PASSWORD", nil)
host = ENV.fetch("#{prefix}HOST", defaults[:host])
port = ENV.fetch("#{prefix}PORT", defaults[:port])
db = ENV.fetch("#{prefix}DB", defaults[:db])
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
sentinel_port = ENV.fetch("#{prefix}SENTINEL_PORT", 26_379)
sentinel_list = ENV.fetch("#{prefix}SENTINELS", nil)
url = ENV.fetch("#{prefix}URL", nil)
user = ENV.fetch("#{prefix}USER", nil)
password = ENV.fetch("#{prefix}PASSWORD", nil)
host = ENV.fetch("#{prefix}HOST", defaults[:host])
port = ENV.fetch("#{prefix}PORT", defaults[:port])
db = ENV.fetch("#{prefix}DB", defaults[:db])
return { url:, driver: } if url
sentinels = parse_sentinels(sentinel_list, default_port: sentinel_port)
sentinel_options = setup_sentinels(prefix, default_user: user, default_password: password)
if name.present? && sentinels.present?
host = name
if sentinel_options.present?
host = sentinel_options[:name]
port = nil
db ||= 0
else
sentinels = nil
end
url = construct_uri(host, port, db, user, password)
if url.present?
{ url:, driver:, name:, sentinels: }
{ url:, driver: }.merge(sentinel_options)
else
# Fall back to base config. This has defaults for the URL
# so this cannot lead to an endless loop.
# Fall back to base config, which has defaults for the URL
# so this cannot lead to endless recursion.
base
end
end
def setup_sentinels(prefix, default_user: nil, default_password: nil)
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
sentinel_port = ENV.fetch("#{prefix}SENTINEL_PORT", 26_379)
sentinel_list = ENV.fetch("#{prefix}SENTINELS", nil)
sentinel_username = ENV.fetch("#{prefix}SENTINEL_USERNAME", default_user)
sentinel_password = ENV.fetch("#{prefix}SENTINEL_PASSWORD", default_password)
sentinels = parse_sentinels(sentinel_list, default_port: sentinel_port)
if name.present? && sentinels.present?
{ name:, sentinels:, sentinel_username:, sentinel_password: }
else
{}
end
end
def construct_uri(host, port, db, user, password)
return nil if host.blank?