Commit 1966312c authored by Sean McGivern's avatar Sean McGivern

Merge branch '14507-initialize-redis-store' into 'master'

Build the Redis::Store instance and use it in session_store

See merge request gitlab-org/gitlab!73552
parents 530df3c2 890eccf8
......@@ -19,10 +19,25 @@ cookie_key = if Rails.env.development?
"_gitlab_session"
end
sessions_config = Gitlab::Redis::SharedState.params
sessions_config[:namespace] = Gitlab::Redis::SharedState::SESSION_NAMESPACE
if Gitlab::Utils.to_boolean(ENV['GITLAB_REDIS_STORE_WITH_SESSION_STORE'], default: true)
store = Gitlab::Redis::SharedState.store(
namespace: Gitlab::Redis::SharedState::SESSION_NAMESPACE
)
Gitlab::Application.config.session_store(
Gitlab::Application.config.session_store(
:redis_store, # Using the cookie_store would enable session replay attacks.
redis_store: store,
key: cookie_key,
secure: Gitlab.config.gitlab.https,
httponly: true,
expires_in: Settings.gitlab['session_expire_delay'] * 60,
path: Rails.application.config.relative_url_root.presence || '/'
)
else
sessions_config = Gitlab::Redis::SharedState.params
sessions_config[:namespace] = Gitlab::Redis::SharedState::SESSION_NAMESPACE
Gitlab::Application.config.session_store(
:redis_store, # Using the cookie_store would enable session replay attacks.
servers: sessions_config,
key: cookie_key,
......@@ -30,4 +45,5 @@ Gitlab::Application.config.session_store(
httponly: true,
expires_in: Settings.gitlab['session_expire_delay'] * 60,
path: Rails.application.config.relative_url_root.presence || '/'
)
)
end
......@@ -17,7 +17,7 @@ module Gitlab
module Redis
class Wrapper
class << self
delegate :params, :url, to: :new
delegate :params, :url, :store, to: :new
def with
pool.with { |redis| yield redis }
......@@ -126,6 +126,10 @@ module Gitlab
sentinels && !sentinels.empty?
end
def store(extras = {})
::Redis::Store::Factory.create(redis_store_options.merge(extras))
end
private
def redis_store_options
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Session initializer for GitLab' do
subject { Gitlab::Application.config }
let(:load_session_store) do
load Rails.root.join('config/initializers/session_store.rb')
end
describe 'config#session_store' do
context 'when the GITLAB_REDIS_STORE_WITH_SESSION_STORE env is not set' do
before do
stub_env('GITLAB_REDIS_STORE_WITH_SESSION_STORE', nil)
end
it 'initialized as a redis_store with a proper Redis::Store instance' do
expect(subject).to receive(:session_store).with(:redis_store, a_hash_including(redis_store: kind_of(::Redis::Store)))
load_session_store
end
end
context 'when the GITLAB_REDIS_STORE_WITH_SESSION_STORE env is disabled' do
before do
stub_env('GITLAB_REDIS_STORE_WITH_SESSION_STORE', false)
end
it 'initialized as a redis_store with a proper servers configuration' do
expect(subject).to receive(:session_store).with(:redis_store, a_hash_including(servers: kind_of(Hash)))
load_session_store
end
end
end
end
......@@ -87,6 +87,43 @@ RSpec.shared_examples "redis_shared_examples" do
end
end
describe '.store' do
let(:rails_env) { 'development' }
subject { described_class.new(rails_env).store }
shared_examples 'redis store' do
it 'instantiates Redis::Store' do
is_expected.to be_a(::Redis::Store)
expect(subject.to_s).to eq("Redis Client connected to #{host} against DB #{redis_database}")
end
context 'with the namespace' do
let(:namespace) { 'namespace_name' }
subject { described_class.new(rails_env).store(namespace: namespace) }
it "uses specified namespace" do
expect(subject.to_s).to eq("Redis Client connected to #{host} against DB #{redis_database} with namespace #{namespace}")
end
end
end
context 'with old format' do
it_behaves_like 'redis store' do
let(:config_file_name) { config_old_format_host }
let(:host) { "localhost:#{redis_port}" }
end
end
context 'with new format' do
it_behaves_like 'redis store' do
let(:config_file_name) { config_new_format_host }
let(:host) { "development-host:#{redis_port}" }
end
end
end
describe '.params' do
subject { described_class.new(rails_env).params }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment