Skip to content

Commit 044cfd8

Browse files
Ramesh SenchaRamesh7
Ramesh Sencha
authored andcommitted
(CONT-567) allow deferred function for password
1 parent ef1a455 commit 044cfd8

File tree

12 files changed

+126
-85
lines changed

12 files changed

+126
-85
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# This function exists for usage of a role password that is a deferred function
4+
Puppet::Functions.create_function(:'sqlserver::password') do
5+
dispatch :password do
6+
optional_param 'Any', :pass
7+
return_type 'Any'
8+
end
9+
10+
def password(pass)
11+
pass
12+
end
13+
end

manifests/login.pp

+13-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,21 @@
7676
'absent' => 'delete',
7777
}
7878

79+
$parameters = {
80+
'password' => Deferred('sqlserver::password', [$password]),
81+
'disabled' => $disabled,
82+
'login_type' => $login_type,
83+
'login' => $login,
84+
'default_language' => $default_language,
85+
'default_database' => $default_database,
86+
'check_policy' => $check_policy,
87+
'check_expiration' => $check_expiration,
88+
'svrroles' => $svrroles,
89+
}
90+
7991
sqlserver_tsql { "login-${instance}-${login}":
8092
instance => $instance,
81-
command => template("sqlserver/${_create_delete}/login.sql.erb"),
93+
command => stdlib::deferrable_epp("sqlserver/${_create_delete}/login.sql.epp", $parameters),
8294
onlyif => template('sqlserver/query/login_exists.sql.erb'),
8395
require => Sqlserver::Config[$instance],
8496
}

manifests/user.pp

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@
6262
'absent' => 'delete',
6363
}
6464

65+
$parameters = {
66+
'password' => Deferred('sqlserver::password', [$password]),
67+
'database' => $database,
68+
'user' => $user,
69+
'login' => $login,
70+
'default_schema' => $default_schema,
71+
}
72+
6573
sqlserver_tsql { "user-${instance}-${database}-${user}":
6674
instance => $instance,
67-
command => template("sqlserver/${create_delete}/user.sql.erb"),
75+
command => stdlib::deferrable_epp("sqlserver/${create_delete}/user.sql.epp", $parameters),
6876
onlyif => template('sqlserver/query/user_exists.sql.erb'),
6977
require => Sqlserver::Config[$instance],
7078
}

metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": [
1111
{
1212
"name": "puppetlabs/stdlib",
13-
"version_requirement": ">= 4.13.1 < 9.0.0"
13+
"version_requirement": ">= 8.4.0 < 9.0.0"
1414
},
1515
{
1616
"name": "puppetlabs/powershell",

spec/functions/password_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'sqlserver::password' do
6+
it { is_expected.to run.with_params('password').and_return('password') }
7+
it { is_expected.to run.with_params(nil).and_return(nil) }
8+
end

templates/create/login.sql.epp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
DECLARE
2+
@is_disabled as tinyint = <%= if $disabled {1} else {0} %>,
3+
@login_type as varchar(255) = NULL;
4+
5+
SET @login_type = (SELECT [type] FROM sys.server_principals where name = '<%= $login %>')
6+
IF (@login_type IS NULL)
7+
BEGIN
8+
-- Create the login
9+
CREATE LOGIN [<%= $login %>]
10+
<% if $login_type !~ /WINDOWS_LOGIN/ { -%>
11+
WITH
12+
PASSWORD = '<%= $password %>',
13+
CHECK_EXPIRATION = <% if $check_expiration { %>ON<% } else { %>OFF<% } %>,
14+
CHECK_POLICY = <% if $check_policy { %>ON<% } else { %>OFF<% } %>,
15+
<% } else { -%>
16+
FROM WINDOWS WITH
17+
<% } -%>
18+
DEFAULT_LANGUAGE = [<%= $default_language %>],
19+
DEFAULT_DATABASE = [<%= $default_database %>];
20+
-- Fetch the login type
21+
SET @login_type = (SELECT [type] FROM sys.server_principals where name = '<%= $login %>')
22+
END
23+
24+
IF (@login_type = 'G')
25+
BEGIN
26+
-- Windows Group type logins can only be granted/denied connection
27+
IF @is_disabled = 0 GRANT CONNECT SQL TO [<%= $login %>]
28+
ELSE DENY CONNECT SQL TO [<%= $login %>]
29+
END
30+
ELSE
31+
BEGIN
32+
IF @is_disabled = 0 ALTER LOGIN [<%= $login %>] ENABLE
33+
ELSE ALTER LOGIN [<%= $login %>] DISABLE
34+
END
35+
36+
ALTER LOGIN [<%= $login %>] WITH
37+
<% if $login_type != 'WINDOWS_LOGIN' { -%>
38+
CHECK_EXPIRATION = <% if $check_expiration { %>ON<% } else { %>OFF<% } %>,
39+
CHECK_POLICY = <% if $check_policy { %>ON<% } else { %>OFF<% } %>,
40+
<% } -%>
41+
DEFAULT_LANGUAGE = [<%= $default_language %>],
42+
DEFAULT_DATABASE = [<%= $default_database %>];
43+
44+
<% $svrroles.each |String $role, Any $enable_bit| { -%>
45+
IF (SELECT COUNT(me.role_principal_id) from sys.server_role_members me
46+
JOIN sys.server_principals rol ON me.role_principal_id = rol.principal_id
47+
JOIN sys.server_principals pri ON me.member_principal_id = pri.principal_id
48+
WHERE rol.type_desc = 'SERVER_ROLE'
49+
AND rol.name = '<%= $role %>'
50+
AND pri.name = '<%= $login %>') != <%= $enable_bit %>
51+
BEGIN
52+
<% if ($enable_bit == '1') or ($enable_bit == 1) { -%>
53+
ALTER SERVER ROLE [<%= $role %>] ADD MEMBER [<%= $login %>];
54+
<% } else { -%>
55+
ALTER SERVER ROLE [<%= $role %>] DROP MEMBER [<%= $login %>];
56+
<% } -%>
57+
END
58+
<% } -%>

templates/create/login.sql.erb

-58
This file was deleted.

templates/create/user.sql.epp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
USE [<%= $database %>];
2+
<% if $password { %>
3+
IF EXISTS(select containment from sys.databases WHERE name = '<%= $database %>' AND containment = 0)
4+
THROW 51000, 'Database must be contained in order to use passwords', 10
5+
<% } %>
6+
CREATE USER [<%= $user %>]
7+
<% if $login { -%>
8+
FROM LOGIN [<%= $login %>]
9+
<% } else { -%>
10+
<% if $password { -%>
11+
WITH PASSWORD = '<%= $password %>'
12+
<% } -%>
13+
<% } -%>
14+
<% if $default_schema { -%>
15+
<% if $password { -%>,<% } else { -%>
16+
WITH <% } -%>
17+
DEFAULT_SCHEMA = <%= $default_schema %>
18+
<% } -%>

templates/create/user.sql.erb

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
USE master;
2-
IF exists(select * from sys.server_principals where name = '<%= @login %>')
2+
IF exists(select * from sys.server_principals where name = '<%= $login %>')
33
BEGIN
44
-- need to add logic to kill all possible connections if any exists,
55
-- possible force flag to prevent from happening during transaction if user would prefer to wait
6-
DROP LOGIN [<%= @login %>]
6+
DROP LOGIN [<%= $login %>]
77
END

templates/delete/user.sql.epp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
USE [<%= $database %>];
2+
DROP USER [<%= $user %>];
3+
IF EXISTS(SELECT name FROM sys.database_principals WHERE name = '<%= $user %>')
4+
THROW 51000, 'Failed to drop user <%= $user %>', 10

templates/delete/user.sql.erb

-4
This file was deleted.

0 commit comments

Comments
 (0)