Skip to content

Commit 7e19e61

Browse files
p-mongop
andcommitted
Fix RUBY-2055 Driver sends null pwd field in createUser when password is not specified (#1609)
* Shorten lines to 79 columns * RUBY-2055 fix user creation without password * Use dashes for second level headings * Give an example for creating an x.509 user Co-authored-by: Oleg Pudeyev <p@users.noreply.github.com>
1 parent 658a618 commit 7e19e61

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

lib/mongo/auth/user.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def sasl_prepped_password
151151
# authorized for.
152152
# @option options [ String ] :user The user name.
153153
# @option options [ String ] :password The user's password.
154+
# @option options [ String ] :pwd Legacy option for the user's password.
155+
# If :password and :pwd are both specified, :password takes precedence.
154156
# @option options [ Symbol ] :auth_mech The authorization mechanism.
155157
# @option options [ Array<String>, Array<Hash> ] roles The user roles.
156158
# @option options [ String ] :client_key The user's client key cached from a previous
@@ -196,7 +198,11 @@ def initialize(options)
196198
#
197199
# @since 2.0.0
198200
def spec
199-
{ pwd: password, roles: roles }
201+
{roles: roles}.tap do |spec|
202+
if password
203+
spec[:pwd] = password
204+
end
205+
end
200206
end
201207

202208
private

spec/mongo/auth/user/view_spec.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
describe Mongo::Auth::User::View do
44

5+
let(:database) { root_authorized_client.database }
6+
57
let(:view) do
6-
described_class.new(root_authorized_client.database)
8+
described_class.new(database)
79
end
810

911
before do
@@ -12,6 +14,39 @@
1214

1315
describe '#create' do
1416

17+
context 'when password is not provided' do
18+
19+
let(:database) { root_authorized_client.use('$external').database }
20+
21+
let(:username) { 'passwordless-user' }
22+
23+
let(:response) do
24+
view.create(
25+
username,
26+
# https://stackoverflow.com/questions/55939832/mongodb-external-database-cannot-create-new-user-with-user-defined-role
27+
roles: [{role: 'read', db: 'admin'}],
28+
)
29+
end
30+
31+
before do
32+
begin
33+
view.remove(username)
34+
rescue Mongo::Error::OperationFailure
35+
# can be user not found, ignore
36+
end
37+
end
38+
39+
it 'creates the user' do
40+
view.info(username).should == []
41+
42+
lambda do
43+
response
44+
end.should_not raise_error
45+
46+
view.info(username).first['user'].should == username
47+
end
48+
end
49+
1550
context 'when a session is not used' do
1651

1752
let!(:response) do

spec/mongo/auth/user_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,16 @@
284284
end
285285
end
286286
end
287+
288+
describe '#spec' do
289+
context 'when no password and no roles are set' do
290+
let(:user) do
291+
described_class.new(user: 'foo')
292+
end
293+
294+
it 'is a hash with empty roles' do
295+
user.spec.should == {roles: []}
296+
end
297+
end
298+
end
287299
end

0 commit comments

Comments
 (0)