Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bb2cdabe
Commit
bb2cdabe
authored
May 16, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
8f391eef
3b211391
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
11 deletions
+84
-11
app/models/active_session.rb
app/models/active_session.rb
+38
-11
spec/models/active_session_spec.rb
spec/models/active_session_spec.rb
+46
-0
No files found.
app/models/active_session.rb
View file @
bb2cdabe
...
@@ -53,7 +53,7 @@ class ActiveSession
...
@@ -53,7 +53,7 @@ class ActiveSession
def
self
.
list
(
user
)
def
self
.
list
(
user
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
cleaned_up_lookup_entries
(
redis
,
user
.
id
).
map
do
|
entry
|
cleaned_up_lookup_entries
(
redis
,
user
).
map
do
|
entry
|
# rubocop:disable Security/MarshalLoad
# rubocop:disable Security/MarshalLoad
Marshal
.
load
(
entry
)
Marshal
.
load
(
entry
)
# rubocop:enable Security/MarshalLoad
# rubocop:enable Security/MarshalLoad
...
@@ -78,7 +78,7 @@ class ActiveSession
...
@@ -78,7 +78,7 @@ class ActiveSession
def
self
.
cleanup
(
user
)
def
self
.
cleanup
(
user
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
cleaned_up_lookup_entries
(
redis
,
user
.
id
)
cleaned_up_lookup_entries
(
redis
,
user
)
end
end
end
end
...
@@ -90,25 +90,52 @@ class ActiveSession
...
@@ -90,25 +90,52 @@ class ActiveSession
"
#{
Gitlab
::
Redis
::
SharedState
::
USER_SESSIONS_LOOKUP_NAMESPACE
}
:
#{
user_id
}
"
"
#{
Gitlab
::
Redis
::
SharedState
::
USER_SESSIONS_LOOKUP_NAMESPACE
}
:
#{
user_id
}
"
end
end
def
self
.
cleaned_up_lookup_entries
(
redis
,
user_id
)
def
self
.
list_sessions
(
user
)
lookup_key
=
lookup_key_name
(
user_id
)
sessions_from_ids
(
session_ids_for_user
(
user
))
end
def
self
.
session_ids_for_user
(
user
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
smembers
(
lookup_key_name
(
user
.
id
))
end
end
def
self
.
sessions_from_ids
(
session_ids
)
return
[]
if
session_ids
.
empty?
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
session_keys
=
session_ids
.
map
{
|
session_id
|
"
#{
Gitlab
::
Redis
::
SharedState
::
SESSION_NAMESPACE
}
:
#{
session_id
}
"
}
redis
.
mget
(
session_keys
).
compact
.
map
do
|
raw_session
|
# rubocop:disable Security/MarshalLoad
Marshal
.
load
(
raw_session
)
# rubocop:enable Security/MarshalLoad
end
end
end
session_ids
=
redis
.
smembers
(
lookup_key
)
def
self
.
raw_active_session_entries
(
session_ids
,
user_id
)
return
[]
if
session_ids
.
empty?
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
entry_keys
=
session_ids
.
map
{
|
session_id
|
key_name
(
user_id
,
session_id
)
}
entry_keys
=
session_ids
.
map
{
|
session_id
|
key_name
(
user_id
,
session_id
)
}
return
[]
if
entry_keys
.
empty?
entries
=
redis
.
mget
(
entry_keys
)
redis
.
mget
(
entry_keys
)
end
end
session_ids_and_entries
=
session_ids
.
zip
(
entries
)
def
self
.
cleaned_up_lookup_entries
(
redis
,
user
)
session_ids
=
session_ids_for_user
(
user
)
entries
=
raw_active_session_entries
(
session_ids
,
user
.
id
)
# remove expired keys.
# remove expired keys.
# only the single key entries are automatically expired by redis, the
# only the single key entries are automatically expired by redis, the
# lookup entries in the set need to be removed manually.
# lookup entries in the set need to be removed manually.
session_ids_and_entries
=
session_ids
.
zip
(
entries
)
session_ids_and_entries
.
reject
{
|
_session_id
,
entry
|
entry
}.
each
do
|
session_id
,
_entry
|
session_ids_and_entries
.
reject
{
|
_session_id
,
entry
|
entry
}.
each
do
|
session_id
,
_entry
|
redis
.
srem
(
lookup_key
,
session_id
)
redis
.
srem
(
lookup_key
_name
(
user
.
id
)
,
session_id
)
end
end
session_ids_and_entries
.
select
{
|
_session_id
,
entry
|
entry
}.
map
{
|
_session_id
,
entry
|
entry
}
entries
.
compact
end
end
end
end
spec/models/active_session_spec.rb
View file @
bb2cdabe
...
@@ -88,6 +88,52 @@ RSpec.describe ActiveSession, :clean_gitlab_redis_shared_state do
...
@@ -88,6 +88,52 @@ RSpec.describe ActiveSession, :clean_gitlab_redis_shared_state do
end
end
end
end
describe
'.list_sessions'
do
it
'uses the ActiveSession lookup to return original sessions'
do
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
"session:gitlab:6919a6f1bb119dd7396fadc38fd18d0d"
,
Marshal
.
dump
({
_csrf_token:
'abcd'
}))
redis
.
sadd
(
"session:lookup:user:gitlab:
#{
user
.
id
}
"
,
%w[
6919a6f1bb119dd7396fadc38fd18d0d
59822c7d9fcdfa03725eff41782ad97d
]
)
end
expect
(
ActiveSession
.
list_sessions
(
user
)).
to
eq
[{
_csrf_token:
'abcd'
}]
end
end
describe
'.session_ids_for_user'
do
it
'uses the user lookup table to return session ids'
do
session_ids
=
[
'59822c7d9fcdfa03725eff41782ad97d'
]
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
sadd
(
"session:lookup:user:gitlab:
#{
user
.
id
}
"
,
session_ids
)
end
expect
(
ActiveSession
.
session_ids_for_user
(
user
)).
to
eq
(
session_ids
)
end
end
describe
'.sessions_from_ids'
do
it
'uses the ActiveSession lookup to return original sessions'
do
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
"session:gitlab:6919a6f1bb119dd7396fadc38fd18d0d"
,
Marshal
.
dump
({
_csrf_token:
'abcd'
}))
end
expect
(
ActiveSession
.
sessions_from_ids
([
'6919a6f1bb119dd7396fadc38fd18d0d'
])).
to
eq
[{
_csrf_token:
'abcd'
}]
end
it
'avoids a redis lookup for an empty array'
do
expect
(
Gitlab
::
Redis
::
SharedState
).
not_to
receive
(
:with
)
expect
(
ActiveSession
.
sessions_from_ids
([])).
to
eq
([])
end
end
describe
'.set'
do
describe
'.set'
do
it
'sets a new redis entry for the user session and a lookup entry'
do
it
'sets a new redis entry for the user session and a lookup entry'
do
ActiveSession
.
set
(
user
,
request
)
ActiveSession
.
set
(
user
,
request
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment