Skip to content

Commit 58b6e50

Browse files
authored
Merge pull request #890 from estolfo/RUBY-1226-sessions
RUBY-1226 Sessions implementation
2 parents ea27aa1 + 9769cbe commit 58b6e50

File tree

85 files changed

+3902
-612
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3902
-612
lines changed

lib/mongo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
require 'mongo/index'
3838
require 'mongo/server'
3939
require 'mongo/server_selector'
40+
require 'mongo/session'
4041
require 'mongo/socket'
4142
require 'mongo/uri'
4243
require 'mongo/version'

lib/mongo/auth/cr.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def initialize(user)
5555
def login(connection)
5656
conversation = Conversation.new(user)
5757
reply = connection.dispatch([ conversation.start(connection) ])
58+
connection.update_cluster_time(Operation::Result.new(reply))
5859
reply = connection.dispatch([ conversation.continue(reply, connection) ])
60+
connection.update_cluster_time(Operation::Result.new(reply))
5961
conversation.finalize(reply, connection)
6062
end
6163
end

lib/mongo/auth/cr/conversation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def continue(reply, connection = nil)
5959
if connection && connection.features.op_msg_enabled?
6060
selector = LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce))
6161
selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
62+
cluster_time = connection.mongos? && connection.cluster_time
63+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
6264
Protocol::Msg.new([:none], {}, selector)
6365
else
6466
Protocol::Query.new(
@@ -98,6 +100,8 @@ def finalize(reply, connection = nil)
98100
def start(connection = nil)
99101
if connection && connection.features.op_msg_enabled?
100102
selector = Auth::GET_NONCE.merge(Protocol::Msg::DATABASE_IDENTIFIER => user.auth_source)
103+
cluster_time = connection.mongos? && connection.cluster_time
104+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
101105
Protocol::Msg.new([:none], {}, selector)
102106
else
103107
Protocol::Query.new(

lib/mongo/auth/ldap.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def initialize(user)
5454
# @since 2.0.0
5555
def login(connection)
5656
conversation = Conversation.new(user)
57-
conversation.finalize(connection.dispatch([ conversation.start(connection) ]))
57+
reply = connection.dispatch([ conversation.start(connection) ])
58+
connection.update_cluster_time(Operation::Result.new(reply))
59+
conversation.finalize(reply)
5860
end
5961
end
6062
end

lib/mongo/auth/ldap/conversation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def start(connection = nil)
6565
if connection && connection.features.op_msg_enabled?
6666
selector = LOGIN.merge(payload: payload, mechanism: LDAP::MECHANISM)
6767
selector[Protocol::Msg::DATABASE_IDENTIFIER] = Auth::EXTERNAL
68+
cluster_time = connection.mongos? && connection.cluster_time
69+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
6870
Protocol::Msg.new([:none], {}, selector)
6971
else
7072
Protocol::Query.new(

lib/mongo/auth/scram.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def initialize(user)
5656
def login(connection)
5757
conversation = Conversation.new(user)
5858
reply = connection.dispatch([ conversation.start(connection) ])
59+
connection.update_cluster_time(Operation::Result.new(reply))
5960
reply = connection.dispatch([ conversation.continue(reply, connection) ])
61+
connection.update_cluster_time(Operation::Result.new(reply))
6062
until reply.documents[0][Conversation::DONE]
6163
reply = connection.dispatch([ conversation.finalize(reply, connection) ])
64+
connection.update_cluster_time(Operation::Result.new(reply))
6265
end
6366
reply
6467
end

lib/mongo/auth/scram/conversation.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def continue(reply, connection = nil)
121121
if connection && connection.features.op_msg_enabled?
122122
selector = CLIENT_CONTINUE_MESSAGE.merge(payload: client_final_message, conversationId: id)
123123
selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
124+
cluster_time = connection.mongos? && connection.cluster_time
125+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
124126
Protocol::Msg.new([:none], {}, selector)
125127
else
126128
Protocol::Query.new(
@@ -150,6 +152,8 @@ def finalize(reply, connection = nil)
150152
if connection && connection.features.op_msg_enabled?
151153
selector = CLIENT_CONTINUE_MESSAGE.merge(payload: client_empty_message, conversationId: id)
152154
selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
155+
cluster_time = connection.mongos? && connection.cluster_time
156+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
153157
Protocol::Msg.new([:none], {}, selector)
154158
else
155159
Protocol::Query.new(
@@ -176,6 +180,8 @@ def start(connection = nil)
176180
if connection && connection.features.op_msg_enabled?
177181
selector = CLIENT_FIRST_MESSAGE.merge(payload: client_first_message, mechanism: SCRAM::MECHANISM)
178182
selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
183+
cluster_time = connection.mongos? && connection.cluster_time
184+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
179185
Protocol::Msg.new([:none], {}, selector)
180186
else
181187
Protocol::Query.new(

lib/mongo/auth/user/view.rb

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class View
2525
# @return [ Database ] database The view's database.
2626
attr_reader :database
2727

28-
def_delegators :database, :cluster, :read_preference
28+
def_delegators :database, :cluster, :read_preference, :client
2929
def_delegators :cluster, :next_primary
3030

3131
# Create a new user in the database.
@@ -36,15 +36,20 @@ class View
3636
# @param [ Auth::User, String ] user_or_name The user object or user name.
3737
# @param [ Hash ] options The user options.
3838
#
39+
# @option options [ Session ] :session The session to use for the operation.
40+
#
3941
# @return [ Result ] The command response.
4042
#
4143
# @since 2.0.0
4244
def create(user_or_name, options = {})
4345
user = generate(user_or_name, options)
44-
Operation::Write::CreateUser.new(
45-
user: user,
46-
db_name: database.name
47-
).execute(next_primary)
46+
client.send(:with_session, options) do |session|
47+
Operation::Write::CreateUser.new(
48+
user: user,
49+
db_name: database.name,
50+
session: session
51+
).execute(next_primary)
52+
end
4853
end
4954

5055
# Initialize the new user view.
@@ -65,15 +70,21 @@ def initialize(database)
6570
# view.remove('user')
6671
#
6772
# @param [ String ] name The user name.
73+
# @param [ Hash ] options The options for the remove operation.
74+
#
75+
# @option options [ Session ] :session The session to use for the operation.
6876
#
6977
# @return [ Result ] The command response.
7078
#
7179
# @since 2.0.0
72-
def remove(name)
73-
Operation::Write::RemoveUser.new(
74-
user_name: name,
75-
db_name: database.name
76-
).execute(next_primary)
80+
def remove(name, options = {})
81+
client.send(:with_session, options) do |session|
82+
Operation::Write::RemoveUser.new(
83+
user_name: name,
84+
db_name: database.name,
85+
session: session
86+
).execute(next_primary)
87+
end
7788
end
7889

7990
# Update a user in the database.
@@ -84,15 +95,20 @@ def remove(name)
8495
# @param [ Auth::User, String ] user_or_name The user object or user name.
8596
# @param [ Hash ] options The user options.
8697
#
98+
# @option options [ Session ] :session The session to use for the operation.
99+
#
87100
# @return [ Result ] The response.
88101
#
89102
# @since 2.0.0
90103
def update(user_or_name, options = {})
91-
user = generate(user_or_name, options)
92-
Operation::Write::UpdateUser.new(
93-
user: user,
94-
db_name: database.name
95-
).execute(next_primary)
104+
client.send(:with_session, options) do |session|
105+
user = generate(user_or_name, options)
106+
Operation::Write::UpdateUser.new(
107+
user: user,
108+
db_name: database.name,
109+
session: session
110+
).execute(next_primary)
111+
end
96112
end
97113

98114
# Get info for a particular user in the database.
@@ -101,21 +117,27 @@ def update(user_or_name, options = {})
101117
# view.info('emily')
102118
#
103119
# @param [ String ] name The user name.
120+
# @param [ Hash ] options The options for the info operation.
121+
#
122+
# @option options [ Session ] :session The session to use for the operation.
104123
#
105124
# @return [ Hash ] A document containing information on a particular user.
106125
#
107126
# @since 2.1.0
108-
def info(name)
109-
user_query(name).documents
127+
def info(name, options = {})
128+
user_query(name, options).documents
110129
end
111130

112131
private
113132

114-
def user_query(name)
115-
Operation::Commands::UserQuery.new(
116-
user_name: name,
117-
db_name: database.name
118-
).execute(next_primary)
133+
def user_query(name, options = {})
134+
client.send(:with_session, options) do |session|
135+
Operation::Commands::UserQuery.new(
136+
user_name: name,
137+
db_name: database.name,
138+
session: session
139+
).execute(next_primary)
140+
end
119141
end
120142

121143
def generate(user, options)

lib/mongo/auth/x509.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def initialize(user)
5555
# @since 2.0.0
5656
def login(connection)
5757
conversation = Conversation.new(user)
58-
conversation.finalize(connection.dispatch([ conversation.start(connection) ]))
58+
reply = connection.dispatch([ conversation.start(connection) ])
59+
connection.update_cluster_time(Operation::Result.new(reply))
60+
conversation.finalize(reply)
5961
end
6062
end
6163
end

lib/mongo/auth/x509/conversation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def start(connection = nil)
6767
if connection && connection.features.op_msg_enabled?
6868
selector = login
6969
selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
70+
cluster_time = connection.mongos? && connection.cluster_time
71+
selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
7072
Protocol::Msg.new([:none], {}, selector)
7173
else
7274
Protocol::Query.new(

0 commit comments

Comments
 (0)