Skip to content

Commit f0cc2d1

Browse files
committed
Merge pull request #601 from estolfo/RUBY-890-auth
RUBY-890 Adjustments to auth and test suite
2 parents 3aece88 + 7c6f0da commit f0cc2d1

File tree

12 files changed

+86
-95
lines changed

12 files changed

+86
-95
lines changed

lib/mongo/auth.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require 'mongo/auth/executable'
1615
require 'mongo/auth/cr'
1716
require 'mongo/auth/ldap'
1817
require 'mongo/auth/scram'

lib/mongo/auth/cr.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class CR
24-
include Executable
24+
25+
# The authentication mechinism string.
26+
#
27+
# @since 2.0.0
28+
MECHANISM = 'MONGODB-CR'.freeze
29+
30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::CR.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
2544

2645
# Log the user in on the given connection.
2746
#
@@ -34,7 +53,7 @@ class CR
3453
#
3554
# @since 2.0.0
3655
def login(connection)
37-
conversation = Conversation.new(user, auth_database(connection))
56+
conversation = Conversation.new(user)
3857
reply = connection.dispatch([ conversation.start ])
3958
reply = connection.dispatch([ conversation.continue(reply) ])
4059
conversation.finalize(reply)

lib/mongo/auth/cr/conversation.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Conversation
5656
def continue(reply)
5757
validate!(reply)
5858
Protocol::Query.new(
59-
database,
59+
user.auth_source,
6060
Database::COMMAND,
6161
LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce)),
6262
limit: -1
@@ -89,7 +89,11 @@ def finalize(reply)
8989
#
9090
# @since 2.0.0
9191
def start
92-
Protocol::Query.new(database, Database::COMMAND, Auth::GET_NONCE, limit: -1)
92+
Protocol::Query.new(
93+
user.auth_source,
94+
Database::COMMAND,
95+
Auth::GET_NONCE,
96+
limit: -1)
9397
end
9498

9599
# Create the new conversation.
@@ -101,9 +105,8 @@ def start
101105
# @param [ String ] database The database to authenticate against.
102106
#
103107
# @since 2.0.0
104-
def initialize(user, database)
108+
def initialize(user)
105109
@user = user
106-
@database = database
107110
end
108111

109112
private

lib/mongo/auth/executable.rb

Lines changed: 0 additions & 52 deletions
This file was deleted.

lib/mongo/auth/ldap.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class LDAP
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'PLAIN'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::LDAP.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
3144
# Log the user in on the given connection.
3245
#
3346
# @example Log the user in.

lib/mongo/auth/scram.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class SCRAM
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'SCRAM-SHA-1'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::SCRAM.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
44+
3145
# Log the user in on the given connection.
3246
#
3347
# @example Log the user in.

lib/mongo/auth/scram/conversation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Conversation
111111
def continue(reply)
112112
validate_first_message!(reply)
113113
Protocol::Query.new(
114-
Database::ADMIN,
114+
user.auth_source,
115115
Database::COMMAND,
116116
CLIENT_CONTINUE_MESSAGE.merge(payload: client_final_message, conversationId: id),
117117
limit: -1
@@ -133,7 +133,7 @@ def continue(reply)
133133
def finalize(reply)
134134
validate_final_message!(reply)
135135
Protocol::Query.new(
136-
Database::ADMIN,
136+
user.auth_source,
137137
Database::COMMAND,
138138
CLIENT_CONTINUE_MESSAGE.merge(payload: client_empty_message, conversationId: id),
139139
limit: -1
@@ -151,7 +151,7 @@ def finalize(reply)
151151
# @since 2.0.0
152152
def start
153153
Protocol::Query.new(
154-
Database::ADMIN,
154+
user.auth_source,
155155
Database::COMMAND,
156156
CLIENT_FIRST_MESSAGE.merge(payload: client_first_message, mechanism: SCRAM::MECHANISM),
157157
limit: -1

lib/mongo/auth/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def hashed_password
134134
#
135135
# @since 2.0.0
136136
def initialize(options)
137-
@auth_source = options[:auth_source] || options[:database] || Database::ADMIN
138137
@database = options[:database] || Database::ADMIN
138+
@auth_source = options[:auth_source] || @database
139139
@name = options[:user]
140140
@password = options[:password] || options[:pwd]
141141
@mechanism = options[:auth_mech] || :mongodb_cr

lib/mongo/auth/x509.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class X509
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'MONGODB-X509'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::X509.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
44+
3145
# Log the user in on the given connection.
3246
#
3347
# @example Log the user in.

lib/mongo/server/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Connection
2222
include Connectable
2323
extend Forwardable
2424

25-
# @return [ Mongo::Auth::CR, Mongo::Auth::X509, Mongo::Auth:LDAP ]
25+
# @return [ Mongo::Auth::CR, Mongo::Auth::X509, Mongo::Auth:LDAP, Mongo::Auth::SCRAM ]
2626
# authenticator The authentication strategy.
2727
attr_reader :authenticator
2828

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
begin
4949
# Adds the test user to the test database with permissions on all
5050
# databases that will be used in the test suite.
51-
ADMIN_AUTHORIZED_CLIENT.database.users.create(TEST_USER)
51+
ADMIN_AUTHORIZED_TEST_CLIENT.database.users.create(TEST_USER)
5252
rescue Exception => e
5353
unless write_command_enabled?
5454
# If we are on versions less than 2.6, we need to create a user for
5555
# each database, since the users are not stored in the admin database
5656
# but in the system.users collection on the datbases themselves. Also,
5757
# roles in versions lower than 2.6 can only be strings, not hashes.
58-
begin ROOT_AUTHORIZED_CLIENT.database.users.create(TEST_READ_WRITE_USER); rescue; end
58+
begin ADMIN_AUTHORIZED_TEST_CLIENT.database.users.create(TEST_READ_WRITE_USER); rescue; end
5959
end
6060
end
6161
end

spec/support/authorization.rb

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#
5555
# @since 2.0.0
5656
ROOT_USER = Mongo::Auth::User.new(
57-
database: Mongo::Database::ADMIN,
5857
user: ROOT_USER_NAME,
5958
password: ROOT_USER_PWD,
6059
roles: [
@@ -112,20 +111,6 @@
112111
write: WRITE_CONCERN
113112
)
114113

115-
# Provides an authorized mongo client on the default test database for the
116-
# default root system administrator.
117-
#
118-
# @since 2.0.0
119-
ROOT_AUTHORIZED_CLIENT = Mongo::Client.new(
120-
ADDRESSES,
121-
auth_source: Mongo::Database::ADMIN,
122-
database: TEST_DB,
123-
user: ROOT_USER.name,
124-
password: ROOT_USER.password,
125-
max_pool_size: 1,
126-
write: WRITE_CONCERN
127-
)
128-
129114
# Provides an unauthorized mongo client on the default test database.
130115
#
131116
# @since 2.0.0
@@ -147,13 +132,15 @@
147132
write: WRITE_CONCERN
148133
)
149134

150-
# Get an authorized client on the admin database logged in as the admin
135+
# Get an authorized client on the test database logged in as the admin
151136
# root user.
152137
#
153138
# @since 2.0.0
154-
ADMIN_AUTHORIZED_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
139+
ADMIN_AUTHORIZED_TEST_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
155140
user: ROOT_USER.name,
156-
password: ROOT_USER.password
141+
password: ROOT_USER.password,
142+
database: TEST_DB,
143+
auth_source: Mongo::Database::ADMIN
157144
)
158145

159146
module Authorization
@@ -181,12 +168,6 @@ def self.included(context)
181168
# @since 2.0.0
182169
context.let(:authorized_client) { AUTHORIZED_CLIENT }
183170

184-
# Provides an authorized mongo client on the default test database for the
185-
# default root system administrator.
186-
#
187-
# @since 2.0.0
188-
context.let(:root_authorized_client) { ROOT_AUTHORIZED_CLIENT }
189-
190171
# Provides an unauthorized mongo client on the default test database.
191172
#
192173
# @since 2.0.0
@@ -198,11 +179,11 @@ def self.included(context)
198179
# @since 2.0.0
199180
context.let!(:admin_unauthorized_client) { ADMIN_UNAUTHORIZED_CLIENT }
200181

201-
# Get an authorized client on the admin database logged in as the admin
182+
# Get an authorized client on the test database logged in as the admin
202183
# root user.
203184
#
204185
# @since 2.0.0
205-
context.let!(:admin_authorized_client) { ADMIN_AUTHORIZED_CLIENT }
186+
context.let!(:root_authorized_client) { ADMIN_AUTHORIZED_TEST_CLIENT }
206187

207188
# Gets the default test collection from the authorized client.
208189
#

0 commit comments

Comments
 (0)