Skip to content
代码片段 群组 项目
未验证 提交 ea5d36e7 编辑于 作者: Balasankar "Balu" C's avatar Balasankar "Balu" C
浏览文件

Support specifying TLS settings in resque.yml


Changelog: added
Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
上级 e460fd24
No related branches found
No related tags found
无相关合并请求
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
# #
development: development:
url: redis://localhost:6379 url: redis://localhost:6379
# ssl_params:
# ca_path: "/path/to/dir/with/certs"
# ca_file: "/path/to/ca.crt"
# cert_file: "/path/to/client.crt"
# key_file: "/path/to/client.key"
# sentinels: # sentinels:
# - # -
# host: localhost # host: localhost
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
module Gitlab module Gitlab
module Redis module Redis
class Wrapper class Wrapper
InvalidPathError = Class.new(StandardError)
class << self class << self
delegate :params, :url, :store, to: :new delegate :params, :url, :store, to: :new
...@@ -122,12 +124,14 @@ def redis_store_options ...@@ -122,12 +124,14 @@ def redis_store_options
config = raw_config_hash config = raw_config_hash
config[:instrumentation_class] ||= self.class.instrumentation_class config[:instrumentation_class] ||= self.class.instrumentation_class
if config[:cluster].present? result = if config[:cluster].present?
config[:db] = 0 # Redis Cluster only supports db 0 config[:db] = 0 # Redis Cluster only supports db 0
config config
else else
parse_redis_url(config) parse_redis_url(config)
end end
parse_client_tls_options(result)
end end
def parse_redis_url(config) def parse_redis_url(config)
...@@ -153,6 +157,33 @@ def parse_redis_url(config) ...@@ -153,6 +157,33 @@ def parse_redis_url(config)
end end
end end
def parse_client_tls_options(config)
return config unless config&.key?(:ssl_params)
# Only cert_file and key_file are handled in this method. ca_file and
# ca_path are Strings, so they can be passed as-is. cert_store is not
# currently supported.
cert_file = config[:ssl_params].delete(:cert_file)
key_file = config[:ssl_params].delete(:key_file)
unless ::File.exist?(cert_file)
raise InvalidPathError,
"Certificate file #{cert_file} specified in in `resque.yml` does not exist."
end
config[:ssl_params][:cert] = OpenSSL::X509::Certificate.new(File.read(cert_file))
unless ::File.exist?(key_file)
raise InvalidPathError,
"Key file #{key_file} specified in in `resque.yml` does not exist."
end
config[:ssl_params][:key] = OpenSSL::PKey.read(File.read(key_file))
config
end
def raw_config_hash def raw_config_hash
config_data = fetch_config config_data = fetch_config
......
...@@ -365,6 +365,90 @@ ...@@ -365,6 +365,90 @@
end end
end end
describe "#parse_client_tls_options" do
let(:dummy_certificate) { OpenSSL::X509::Certificate.new }
let(:dummy_key) { OpenSSL::PKey::RSA.new }
let(:resque_yaml_config_without_tls) { { url: 'redis://localhost:6379' } }
let(:resque_yaml_config_with_tls) do
{
url: 'rediss://localhost:6380',
ssl_params: {
cert_file: '/tmp/client.crt',
key_file: '/tmp/client.key'
}
}
end
let(:parsed_config_with_tls) do
{
url: 'rediss://localhost:6380',
ssl_params: {
cert: dummy_certificate,
key: dummy_key
}
}
end
before do
allow(::File).to receive(:exist?).and_call_original
allow(::File).to receive(:read).and_call_original
end
context 'when configuration does not have TLS related options' do
it 'returns the coniguration as-is' do
expect(subject.send(:parse_client_tls_options,
resque_yaml_config_without_tls)).to eq(resque_yaml_config_without_tls)
end
end
context 'when specified certificate file does not exist' do
before do
allow(::File).to receive(:exist?).with("/tmp/client.crt").and_return(false)
allow(::File).to receive(:exist?).with("/tmp/client.key").and_return(true)
end
it 'raises error about missing certificate file' do
expect do
subject.send(:parse_client_tls_options,
resque_yaml_config_with_tls)
end.to raise_error(Gitlab::Redis::Wrapper::InvalidPathError,
"Certificate file /tmp/client.crt specified in in `resque.yml` does not exist.")
end
end
context 'when specified key file does not exist' do
before do
allow(::File).to receive(:exist?).with("/tmp/client.crt").and_return(true)
allow(::File).to receive(:read).with("/tmp/client.crt").and_return("DUMMY_CERTIFICATE")
allow(OpenSSL::X509::Certificate).to receive(:new).with("DUMMY_CERTIFICATE").and_return(dummy_certificate)
allow(::File).to receive(:exist?).with("/tmp/client.key").and_return(false)
end
it 'raises error about missing key file' do
expect do
subject.send(:parse_client_tls_options,
resque_yaml_config_with_tls)
end.to raise_error(Gitlab::Redis::Wrapper::InvalidPathError,
"Key file /tmp/client.key specified in in `resque.yml` does not exist.")
end
end
context 'when configuration valid TLS related options' do
before do
allow(::File).to receive(:exist?).with("/tmp/client.crt").and_return(true)
allow(::File).to receive(:exist?).with("/tmp/client.key").and_return(true)
allow(::File).to receive(:read).with("/tmp/client.crt").and_return("DUMMY_CERTIFICATE")
allow(::File).to receive(:read).with("/tmp/client.key").and_return("DUMMY_KEY")
allow(OpenSSL::X509::Certificate).to receive(:new).with("DUMMY_CERTIFICATE").and_return(dummy_certificate)
allow(OpenSSL::PKey).to receive(:read).with("DUMMY_KEY").and_return(dummy_key)
end
it "converts cert_file and key_file appropriately" do
expect(subject.send(:parse_client_tls_options, resque_yaml_config_with_tls)).to eq(parsed_config_with_tls)
end
end
end
describe '#fetch_config' do describe '#fetch_config' do
before do before do
FileUtils.mkdir_p(File.join(rails_root, 'config')) FileUtils.mkdir_p(File.join(rails_root, 'config'))
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册