Skip to content

Commit 9d1110a

Browse files
shane-bordenShane Borden
andauthored
feat: updates to MSSQL user create process (#295)
* bug: 299460067 fix user ddl + add drop method * bug: fix syntax for user creation * chore: bump version to 4.3.18 --------- Co-authored-by: Shane Borden <[email protected]>
1 parent 4a42a4d commit 9d1110a

File tree

9 files changed

+168
-23
lines changed

9 files changed

+168
-23
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 4.3.17
2+
current_version = 4.3.18
33
commit = False
44
tag = False
55

scripts/collector/oracle/collect-data.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Setup directories needed for execution
1818
#############################################################################
19-
OpVersion="4.3.17"
19+
OpVersion="4.3.18"
2020
dbmajor=""
2121

2222
LOCALE=$(echo $LANG | cut -d '.' -f 1)

scripts/collector/sqlserver/createUserWithSQLAuth.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ if ([string]::IsNullorEmpty($serverName)) {
7373

7474
if ([string]::IsNullorEmpty($port)) {
7575
Write-Output "Creating Collection User in $serverName"
76-
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
76+
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -U $user -P $pass -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
7777
} else {
7878
$serverName = "$serverName,$port"
7979
Write-Output "Creating Collection User in $serverName, using PORT $port"
80-
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
80+
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -U $user -P $pass -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
8181
}
8282

8383
Exit 0

scripts/collector/sqlserver/createUserWithWindowsAuth.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ if ([string]::IsNullorEmpty($serverName)) {
6363

6464
if ([string]::IsNullorEmpty($port)) {
6565
Write-Output "Creating Collection User in $serverName"
66-
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
66+
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
6767
} else {
6868
$serverName = "$serverName,$port"
6969
Write-Output "Creating Collection User in $serverName, using PORT $port"
70-
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
70+
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
7171
}

scripts/collector/sqlserver/instanceReview.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ $current_ts = $values[4]
126126
$pkey = $values[5]
127127
$dmaSourceId = $dmaSourceId[0]
128128

129-
$op_version = "4.3.17"
129+
$op_version = "4.3.18"
130130

131131
if ($ignorePerfmon -eq "true") {
132132
$perfCounterLabel = "NoPerfCounter"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2023 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
SET NOCOUNT ON;
19+
SET LANGUAGE us_english;
20+
21+
DECLARE @dbname VARCHAR(50);
22+
DECLARE @COLLECTION_USER VARCHAR(256);
23+
DECLARE @PRODUCT_VERSION AS INTEGER
24+
25+
DECLARE db_cursor CURSOR FOR
26+
SELECT name
27+
FROM MASTER.sys.databases
28+
WHERE name NOT IN ('model','msdb','distribution','reportserver', 'reportservertempdb','resource','rdsadmin')
29+
AND state = 0;
30+
31+
SELECT @PRODUCT_VERSION = CONVERT(INTEGER, PARSENAME(CONVERT(nvarchar, SERVERPROPERTY('productversion')), 4));
32+
SELECT @COLLECTION_USER = N'$(collectionUser)'
33+
34+
BEGIN
35+
IF EXISTS
36+
(SELECT name
37+
FROM master.sys.server_principals
38+
WHERE name = @COLLECTION_USER)
39+
BEGIN
40+
exec('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
41+
exec('GRANT SELECT ALL USER SECURABLES TO [' + @COLLECTION_USER + ']');
42+
exec('GRANT VIEW ANY DATABASE TO [' + @COLLECTION_USER + ']');
43+
exec('GRANT VIEW ANY DEFINITION TO [' + @COLLECTION_USER + ']');
44+
exec('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
45+
IF @PRODUCT_VERSION > 15
46+
BEGIN
47+
exec('GRANT VIEW SERVER PERFORMANCE STATE TO [' + @COLLECTION_USER + ']');
48+
exec('GRANT VIEW SERVER SECURITY STATE TO [' + @COLLECTION_USER + ']');
49+
exec('GRANT VIEW ANY PERFORMANCE DEFINITION TO [' + @COLLECTION_USER + ']');
50+
exec('GRANT VIEW ANY SECURITY DEFINITION TO [' + @COLLECTION_USER + ']');
51+
END;
52+
END;
53+
END;
54+
55+
OPEN db_cursor
56+
FETCH NEXT FROM db_cursor INTO @dbname
57+
58+
WHILE @@FETCH_STATUS = 0
59+
BEGIN
60+
BEGIN
61+
exec ('
62+
use [' + @dbname + '];
63+
IF EXISTS (SELECT [name]
64+
FROM [sys].[database_principals]
65+
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
66+
BEGIN
67+
GRANT VIEW DATABASE STATE TO [' + @COLLECTION_USER + '];
68+
END');
69+
END;
70+
71+
FETCH NEXT FROM db_cursor INTO @dbname;
72+
END;
73+
74+
CLOSE db_cursor
75+
DEALLOCATE db_cursor

scripts/collector/sqlserver/sql/createCollectionUser.sql

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,38 @@ SET LANGUAGE us_english;
2020

2121
DECLARE @dbname VARCHAR(50);
2222
DECLARE @COLLECTION_USER VARCHAR(256);
23+
DECLARE @COLLECTION_PASS VARCHAR(256);
2324
DECLARE @PRODUCT_VERSION AS INTEGER
2425

25-
SELECT @PRODUCT_VERSION = CONVERT(INTEGER, PARSENAME(CONVERT(nvarchar, SERVERPROPERTY('productversion')), 4));
2626
DECLARE db_cursor CURSOR FOR
2727
SELECT name
2828
FROM MASTER.sys.databases
2929
WHERE name NOT IN ('model','msdb','distribution','reportserver', 'reportservertempdb','resource','rdsadmin')
3030
AND state = 0;
3131

32-
USE [master]
32+
SELECT @PRODUCT_VERSION = CONVERT(INTEGER, PARSENAME(CONVERT(nvarchar, SERVERPROPERTY('productversion')), 4));
33+
SELECT @COLLECTION_USER = N'$(collectionUser)'
34+
SELECT @COLLECTION_PASS = N'$(collectionPass)'
35+
3336
IF NOT EXISTS
3437
(SELECT name
3538
FROM master.sys.server_principals
36-
WHERE name = N'$(collectionUser)')
39+
WHERE name = @COLLECTION_USER)
3740
BEGIN
38-
CREATE LOGIN [$(collectionUser)] WITH PASSWORD=N'$(collectionPass)', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
41+
exec ('CREATE LOGIN [' + @COLLECTION_USER + '] WITH PASSWORD=N''' + @COLLECTION_PASS + ''', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF');
3942
END
4043
BEGIN
41-
GRANT VIEW SERVER STATE TO [$(collectionUser)]
42-
GRANT SELECT ALL USER SECURABLES TO [$(collectionUser)]
43-
GRANT VIEW ANY DATABASE TO [$(collectionUser)]
44-
GRANT VIEW ANY DEFINITION TO [$(collectionUser)]
45-
GRANT VIEW SERVER STATE TO [$(collectionUser)]
44+
exec ('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
45+
exec ('GRANT SELECT ALL USER SECURABLES TO [' + @COLLECTION_USER + ']');
46+
exec ('GRANT VIEW ANY DATABASE TO [' + @COLLECTION_USER + ']');
47+
exec ('GRANT VIEW ANY DEFINITION TO [' + @COLLECTION_USER + ']');
48+
exec ('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
4649
IF @PRODUCT_VERSION > 15
4750
BEGIN
48-
GRANT VIEW SERVER PERFORMANCE STATE TO [$(collectionUser)]
49-
GRANT VIEW SERVER SECURITY STATE TO [$(collectionUser)]
50-
GRANT VIEW ANY PERFORMANCE DEFINITION TO [$(collectionUser)]
51-
GRANT VIEW ANY SECURITY DEFINITION TO [$(collectionUser)]
51+
exec('GRANT VIEW SERVER PERFORMANCE STATE TO [' + @COLLECTION_USER + ']');
52+
exec('GRANT VIEW SERVER SECURITY STATE TO [' + @COLLECTION_USER + ']');
53+
exec('GRANT VIEW ANY PERFORMANCE DEFINITION TO [' + @COLLECTION_USER + ']');
54+
exec('GRANT VIEW ANY SECURITY DEFINITION TO [' + @COLLECTION_USER + ']');
5255
END;
5356
END;
5457

@@ -60,8 +63,13 @@ BEGIN
6063
BEGIN
6164
exec ('
6265
use [' + @dbname + '];
63-
CREATE USER [$(collectionUser)] FOR LOGIN [$(collectionUser)];
64-
GRANT VIEW DATABASE STATE TO [$(collectionUser)]');
66+
IF NOT EXISTS (SELECT [name]
67+
FROM [sys].[database_principals]
68+
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
69+
BEGIN
70+
CREATE USER [' + @COLLECTION_USER + '] FOR LOGIN [' + @COLLECTION_USER + '];
71+
END;
72+
GRANT VIEW DATABASE STATE TO [' + @COLLECTION_USER + ']');
6573
END;
6674

6775
FETCH NEXT FROM db_cursor INTO @dbname;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2023 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
SET NOCOUNT ON;
19+
SET LANGUAGE us_english;
20+
21+
DECLARE @dbname VARCHAR(50);
22+
DECLARE @COLLECTION_USER VARCHAR(256);
23+
24+
DECLARE db_cursor CURSOR FOR
25+
SELECT name
26+
FROM MASTER.sys.databases
27+
WHERE name NOT IN ('model','msdb','distribution','reportserver', 'reportservertempdb','resource','rdsadmin')
28+
AND state = 0;
29+
30+
SELECT @COLLECTION_USER = N'$(collectionUser)'
31+
32+
OPEN db_cursor
33+
FETCH NEXT FROM db_cursor INTO @dbname
34+
35+
WHILE @@FETCH_STATUS = 0
36+
BEGIN
37+
BEGIN
38+
exec ('
39+
use [' + @dbname + '];
40+
IF EXISTS (SELECT [name]
41+
FROM [sys].[database_principals]
42+
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
43+
BEGIN
44+
DROP USER [' + @COLLECTION_USER + '];
45+
END;
46+
');
47+
END;
48+
49+
FETCH NEXT FROM db_cursor INTO @dbname;
50+
END;
51+
52+
CLOSE db_cursor
53+
DEALLOCATE db_cursor
54+
55+
use [master];
56+
IF EXISTS
57+
(SELECT name
58+
FROM master.sys.server_principals
59+
WHERE name = @COLLECTION_USER)
60+
BEGIN
61+
exec ('DROP LOGIN [' + @COLLECTION_USER + ']');
62+
END;

scripts/masker/dma-collection-masker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ __all__ = [
3333
"run_masker",
3434
]
3535

36-
__version__ = "4.3.17"
36+
__version__ = "4.3.18"
3737

3838
logger = logging.getLogger(__name__)
3939

0 commit comments

Comments
 (0)