File tree 3 files changed +45
-29
lines changed
lib/puppet/provider/mongodb_user
unit/puppet/provider/mongodb_user
3 files changed +45
-29
lines changed Original file line number Diff line number Diff line change 9
9
def self . instances
10
10
require 'json'
11
11
12
- if db_ismaster
13
- script = 'EJSON.stringify(db.system.users.find().toArray())'
14
- # A hack to prevent prefetching failures until admin user is created
15
- script = "try {#{ script } } catch (e) { if (e.message.match(/requires authentication/) || e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled
16
-
17
- out = mongo_eval ( script )
18
- return [ ] if auth_enabled && ( out . include? ( 'requires authentication' ) || out . include? ( 'not authorized on admin' ) )
19
-
20
- users = JSON . parse out
21
-
22
- users . map do |user |
23
- new ( name : user [ '_id' ] ,
24
- ensure : :present ,
25
- username : user [ 'user' ] ,
26
- database : user [ 'db' ] ,
27
- roles : from_roles ( user [ 'roles' ] , user [ 'db' ] ) ,
28
- password_hash : user [ 'credentials' ] [ 'MONGODB-CR' ] ,
29
- scram_credentials : user [ 'credentials' ] [ 'SCRAM-SHA-1' ] )
30
- end
31
- else
32
- Puppet . warning 'User info is available only from master host'
33
- [ ]
12
+ script = 'EJSON.stringify(db.system.users.find().toArray())'
13
+ # A hack to prevent prefetching failures until admin user is created
14
+ script = "try {#{ script } } catch (e) { if (e.message.match(/requires authentication/) || e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled
15
+
16
+ out = mongo_eval ( script )
17
+ return [ ] if auth_enabled && ( out . include? ( 'requires authentication' ) || out . include? ( 'not authorized on admin' ) )
18
+
19
+ users = JSON . parse out
20
+
21
+ users . map do |user |
22
+ new ( name : user [ '_id' ] ,
23
+ ensure : :present ,
24
+ username : user [ 'user' ] ,
25
+ database : user [ 'db' ] ,
26
+ roles : from_roles ( user [ 'roles' ] , user [ 'db' ] ) ,
27
+ password_hash : user [ 'credentials' ] [ 'MONGODB-CR' ] ,
28
+ scram_credentials : user [ 'credentials' ] [ 'SCRAM-SHA-1' ] )
34
29
end
35
30
end
36
31
Original file line number Diff line number Diff line change @@ -73,6 +73,20 @@ class { 'mongodb::globals':
73
73
expect ( r . stdout ) . to match %r{some value}
74
74
end
75
75
end
76
+
77
+ it 'create a user' do
78
+ pp = <<-EOS
79
+ mongodb_user {'testuser':
80
+ ensure => present,
81
+ password_hash => mongodb_password('testuser', 'passw0rd'),
82
+ database => 'testdb',
83
+ roles => ['readWrite', 'dbAdmin'],
84
+ }
85
+ EOS
86
+
87
+ apply_manifest_on ( hosts , pp , catch_failures : true )
88
+ apply_manifest_on ( hosts , pp , catch_changes : true )
89
+ end
76
90
end
77
91
78
92
describe 'mongodb::server with replset_members' do
@@ -347,5 +361,19 @@ class { 'mongodb::globals':
347
361
expect ( r . stdout ) . to match %r{created_by_puppet}
348
362
end
349
363
end
364
+
365
+ it 'create a user' do
366
+ pp = <<-EOS
367
+ mongodb_user {'testuser':
368
+ ensure => present,
369
+ password_hash => mongodb_password('testuser', 'passw0rd'),
370
+ database => 'testdb',
371
+ roles => ['readWrite', 'dbAdmin'],
372
+ }
373
+ EOS
374
+
375
+ apply_manifest_on ( hosts , pp , catch_failures : true )
376
+ apply_manifest_on ( hosts , pp , catch_changes : true )
377
+ end
350
378
end
351
379
end
Original file line number Diff line number Diff line change 71
71
end
72
72
end
73
73
74
- describe 'empty self.instances from slave' do
75
- it 'doesn`t retrun array of users' do
76
- allow ( provider . class ) . to receive ( :db_ismaster ) . and_return ( false )
77
- expect ( provider . class . instances ) . to be_empty
78
- end
79
- end
80
-
81
74
describe 'create' do
82
75
it 'creates a user' do
83
76
cmd_json = <<-EOS . gsub ( %r{^\s *} , '' ) . gsub ( %r{$\n } , '' )
You can’t perform that action at this time.
0 commit comments