Skip to content

Commit b201149

Browse files
committed
Add SCRAM-SHA-256 limited support
- auth_mechanism parameter for mongodb_user - use password instead of password_hash if SCRAM-SHA-256, because password digestion is on the server - insync is not verified for the password
1 parent 45d9bf4 commit b201149

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ Administrator user name
561561
##### `admin_password`
562562
Administrator user password
563563

564+
##### `admin_auth_mechanism`
565+
Administrator authentication mechanism.
566+
scram_sha_256 password synchronization verification is not supported.
567+
Default: 'scram_sha_1'
568+
564569
##### `admin_roles`
565570
Administrator user roles
566571

@@ -648,6 +653,12 @@ For more information please refer to [MongoDB Authentication Process](http://doc
648653
##### `password`
649654
Plain-text user password (will be hashed)
650655

656+
##### `auth_mechanism`
657+
Authentication mechanism.
658+
Can be either 'scram_sha_1' or 'scram_sha_256'.
659+
scram_sha_256 password synchronization verification is not supported.
660+
Default: 'scram_sha_1'
661+
651662
##### `roles`
652663
Array with user roles as string.
653664
Roles will be granted to user's database if no alternative database is explicitly defined.

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,25 @@ def create
5353

5454
command = {
5555
createUser: @resource[:username],
56-
pwd: password_hash,
5756
customData: {
5857
createdBy: "Puppet Mongodb_user['#{@resource[:name]}']"
5958
},
6059
roles: role_hashes(@resource[:roles], @resource[:database]),
61-
digestPassword: false
6260
}
6361

6462
if mongo_4? || mongo_5?
65-
# SCRAM-SHA-256 requires digestPassword to be true.
66-
command[:mechanisms] = ['SCRAM-SHA-1']
63+
if @resource[:auth_mechanism] == :scram_sha_256
64+
command[:mechanisms] = ['SCRAM-SHA-256']
65+
command[:pwd] = @resource[:password]
66+
command[:digestPassword] = true
67+
else
68+
command[:mechanisms] = ['SCRAM-SHA-1']
69+
command[:pwd] = password_hash
70+
command[:digestPassword] = false
71+
end
72+
else
73+
command[:pwd] = password_hash
74+
command[:digestPassword] = false
6775
end
6876

6977
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
@@ -112,6 +120,10 @@ def password=(value)
112120
digestPassword: true
113121
}
114122

123+
if mongo_4? || mongo_5?
124+
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1']
125+
end
126+
115127
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
116128
end
117129
end

lib/puppet/type/mongodb_user.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def to_s?(value)
5353
end
5454

5555
newproperty(:password_hash) do
56-
desc 'The password hash of the user. Use mongodb_password() for creating hash. Only available on MongoDB 3.0 and later.'
56+
desc 'The password hash of the user. Use mongodb_password() for creating hash. Only available on MongoDB 3.0 and later. SCRAM-SHA-256 authentication mechanism is not supported.'
5757
defaultto do
5858
if @resource[:password].nil?
5959
raise Puppet::Error, "Property 'password_hash' must be set. Use mongodb_password() for creating hash." if provider.database == :absent
@@ -90,10 +90,18 @@ def to_s?(_value = @is)
9090
end
9191

9292
def insync?(_is)
93+
return true if @resource[:auth_mechanism] == :scram_sha_256
94+
9395
should_to_s == to_s?
9496
end
9597
end
9698

99+
newparam(:auth_mechanism) do
100+
desc 'Authentication mechanism. Password verification is not supported with SCRAM-SHA-256.'
101+
defaultto :scram_sha_1
102+
newvalues(:scram_sha_256, :scram_sha_1)
103+
end
104+
97105
newproperty(:scram_credentials) do
98106
desc 'The SCRAM-SHA-1 credentials of a user. These are read only and change when password or password_hash changes.'
99107
end
@@ -115,6 +123,8 @@ def insync?(_is)
115123
err("Either 'password_hash' or 'password' should be provided")
116124
elsif !self[:password_hash].nil? && !self[:password].nil?
117125
err("Only one of 'password_hash' or 'password' should be provided")
126+
elsif !self[:password_hash].nil? && self[:auth_mechanism] == :scram_sha_256
127+
err("'password_hash' is not supported with SCRAM-SHA-256 authentication mechanism")
118128
end
119129
if should(:scram_credentials)
120130
raise("The parameter 'scram_credentials' is read-only and cannot be changed")

manifests/db.pp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# == Parameters
66
#
77
# user - Database username.
8+
# auth_mechanism - Authentication mechanism. scram_sha_256 password verification is not supported. Defaults to 'scram_sha_1'.
89
# db_name - Database name. Defaults to $name.
910
# password_hash - Hashed password. Hex encoded md5 hash of "$username:mongo:$password".
1011
# password - Plain text user password. This is UNSAFE, use 'password_hash' instead.
@@ -13,11 +14,12 @@
1314
#
1415
define mongodb::db (
1516
String $user,
16-
String $db_name = $name,
17-
Optional[Variant[String[1], Sensitive[String[1]]]] $password_hash = undef,
18-
Optional[Variant[String[1], Sensitive[String[1]]]] $password = undef,
19-
Array[String] $roles = ['dbAdmin'],
20-
Integer[0] $tries = 10,
17+
Enum['scram_sha_1', 'scram_sha_256'] $auth_mechanism = 'scram_sha_1',
18+
String $db_name = $name,
19+
Optional[Variant[String[1], Sensitive[String[1]]]] $password_hash = undef,
20+
Optional[Variant[String[1], Sensitive[String[1]]]] $password = undef,
21+
Array[String] $roles = ['dbAdmin'],
22+
Integer[0] $tries = 10,
2123
) {
2224
unless $facts['mongodb_is_master'] == 'false' { # lint:ignore:quoted_booleans
2325
mongodb_database { $db_name:
@@ -35,12 +37,23 @@
3537
fail("Parameter 'password_hash' or 'password' should be provided to mongodb::db.")
3638
}
3739

40+
if $auth_mechanism == 'scram_sha_256' {
41+
$password_config = {
42+
password => $password,
43+
}
44+
} else {
45+
$password_config = {
46+
password_hash => $hash,
47+
}
48+
}
49+
3850
mongodb_user { "User ${user} on db ${db_name}":
39-
ensure => present,
40-
password_hash => $hash,
41-
username => $user,
42-
database => $db_name,
43-
roles => $roles,
51+
ensure => present,
52+
username => $user,
53+
database => $db_name,
54+
roles => $roles,
55+
auth_mechanism => $auth_mechanism,
56+
* => $password_config,
4457
}
4558
}
4659
}

manifests/params.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
$restart = true
1212
$create_admin = false
1313
$admin_username = 'admin'
14+
$admin_auth_mechanism = 'scram_sha_1'
1415
$admin_roles = [
1516
'userAdmin', 'readWrite', 'dbAdmin', 'dbAdminAnyDatabase', 'readAnyDatabase',
1617
'readWriteAnyDatabase', 'userAdminAnyDatabase', 'clusterAdmin',

manifests/server.pp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
Boolean $create_admin = $mongodb::params::create_admin,
7777
String $admin_username = $mongodb::params::admin_username,
7878
Optional[Variant[String, Sensitive[String]]] $admin_password = undef,
79+
Enum['scram_sha_1', 'scram_sha_256'] $admin_auth_mechanism = $mongodb::params::admin_auth_mechanism,
7980
Boolean $handle_creds = $mongodb::params::handle_creds,
8081
Boolean $store_creds = $mongodb::params::store_creds,
8182
Array $admin_roles = $mongodb::params::admin_roles,
@@ -105,9 +106,10 @@
105106
}
106107
if $create_admin and ($service_ensure == 'running' or $service_ensure == true) {
107108
mongodb::db { 'admin':
108-
user => $admin_username,
109-
password => $admin_password_unsensitive,
110-
roles => $admin_roles,
109+
user => $admin_username,
110+
auth_mechanism => $admin_auth_mechanism,
111+
password => $admin_password_unsensitive,
112+
roles => $admin_roles,
111113
}
112114

113115
# Make sure it runs before other DB creation

0 commit comments

Comments
 (0)