Skip to content

Commit

Permalink
feat: updates to MSSQL user create process (#295)
Browse files Browse the repository at this point in the history
* 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 <shaneborden@google.com>
  • Loading branch information
shane-borden and Shane Borden authored Sep 7, 2023
1 parent 4a42a4d commit 9d1110a
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 4.3.17
current_version = 4.3.18
commit = False
tag = False

Expand Down
2 changes: 1 addition & 1 deletion scripts/collector/oracle/collect-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### Setup directories needed for execution
#############################################################################
OpVersion="4.3.17"
OpVersion="4.3.18"
dbmajor=""

LOCALE=$(echo $LANG | cut -d '.' -f 1)
Expand Down
4 changes: 2 additions & 2 deletions scripts/collector/sqlserver/createUserWithSQLAuth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ if ([string]::IsNullorEmpty($serverName)) {

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

Exit 0
4 changes: 2 additions & 2 deletions scripts/collector/sqlserver/createUserWithWindowsAuth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ if ([string]::IsNullorEmpty($serverName)) {

if ([string]::IsNullorEmpty($port)) {
Write-Output "Creating Collection User in $serverName"
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
} else {
$serverName = "$serverName,$port"
Write-Output "Creating Collection User in $serverName, using PORT $port"
sqlcmd -S $serverName -i sql\createCollectionUser.sql -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
sqlcmd -S $serverName -i sql\createCollectionUser.sql -d master -l 30 -m 1 -v collectionUser=$collectionUserName collectionPass=$collectionUserPass
}
2 changes: 1 addition & 1 deletion scripts/collector/sqlserver/instanceReview.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ $current_ts = $values[4]
$pkey = $values[5]
$dmaSourceId = $dmaSourceId[0]

$op_version = "4.3.17"
$op_version = "4.3.18"

if ($ignorePerfmon -eq "true") {
$perfCounterLabel = "NoPerfCounter"
Expand Down
75 changes: 75 additions & 0 deletions scripts/collector/sqlserver/sql/addCollectionUserPermissions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

SET NOCOUNT ON;
SET LANGUAGE us_english;

DECLARE @dbname VARCHAR(50);
DECLARE @COLLECTION_USER VARCHAR(256);
DECLARE @PRODUCT_VERSION AS INTEGER

DECLARE db_cursor CURSOR FOR
SELECT name
FROM MASTER.sys.databases
WHERE name NOT IN ('model','msdb','distribution','reportserver', 'reportservertempdb','resource','rdsadmin')
AND state = 0;

SELECT @PRODUCT_VERSION = CONVERT(INTEGER, PARSENAME(CONVERT(nvarchar, SERVERPROPERTY('productversion')), 4));
SELECT @COLLECTION_USER = N'$(collectionUser)'

BEGIN
IF EXISTS
(SELECT name
FROM master.sys.server_principals
WHERE name = @COLLECTION_USER)
BEGIN
exec('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
exec('GRANT SELECT ALL USER SECURABLES TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY DATABASE TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY DEFINITION TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
IF @PRODUCT_VERSION > 15
BEGIN
exec('GRANT VIEW SERVER PERFORMANCE STATE TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW SERVER SECURITY STATE TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY PERFORMANCE DEFINITION TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY SECURITY DEFINITION TO [' + @COLLECTION_USER + ']');
END;
END;
END;

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN
exec ('
use [' + @dbname + '];
IF EXISTS (SELECT [name]
FROM [sys].[database_principals]
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
BEGIN
GRANT VIEW DATABASE STATE TO [' + @COLLECTION_USER + '];
END');
END;

FETCH NEXT FROM db_cursor INTO @dbname;
END;

CLOSE db_cursor
DEALLOCATE db_cursor
38 changes: 23 additions & 15 deletions scripts/collector/sqlserver/sql/createCollectionUser.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,38 @@ SET LANGUAGE us_english;

DECLARE @dbname VARCHAR(50);
DECLARE @COLLECTION_USER VARCHAR(256);
DECLARE @COLLECTION_PASS VARCHAR(256);
DECLARE @PRODUCT_VERSION AS INTEGER

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

USE [master]
SELECT @PRODUCT_VERSION = CONVERT(INTEGER, PARSENAME(CONVERT(nvarchar, SERVERPROPERTY('productversion')), 4));
SELECT @COLLECTION_USER = N'$(collectionUser)'
SELECT @COLLECTION_PASS = N'$(collectionPass)'

IF NOT EXISTS
(SELECT name
FROM master.sys.server_principals
WHERE name = N'$(collectionUser)')
WHERE name = @COLLECTION_USER)
BEGIN
CREATE LOGIN [$(collectionUser)] WITH PASSWORD=N'$(collectionPass)', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
exec ('CREATE LOGIN [' + @COLLECTION_USER + '] WITH PASSWORD=N''' + @COLLECTION_PASS + ''', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF');
END
BEGIN
GRANT VIEW SERVER STATE TO [$(collectionUser)]
GRANT SELECT ALL USER SECURABLES TO [$(collectionUser)]
GRANT VIEW ANY DATABASE TO [$(collectionUser)]
GRANT VIEW ANY DEFINITION TO [$(collectionUser)]
GRANT VIEW SERVER STATE TO [$(collectionUser)]
exec ('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
exec ('GRANT SELECT ALL USER SECURABLES TO [' + @COLLECTION_USER + ']');
exec ('GRANT VIEW ANY DATABASE TO [' + @COLLECTION_USER + ']');
exec ('GRANT VIEW ANY DEFINITION TO [' + @COLLECTION_USER + ']');
exec ('GRANT VIEW SERVER STATE TO [' + @COLLECTION_USER + ']');
IF @PRODUCT_VERSION > 15
BEGIN
GRANT VIEW SERVER PERFORMANCE STATE TO [$(collectionUser)]
GRANT VIEW SERVER SECURITY STATE TO [$(collectionUser)]
GRANT VIEW ANY PERFORMANCE DEFINITION TO [$(collectionUser)]
GRANT VIEW ANY SECURITY DEFINITION TO [$(collectionUser)]
exec('GRANT VIEW SERVER PERFORMANCE STATE TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW SERVER SECURITY STATE TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY PERFORMANCE DEFINITION TO [' + @COLLECTION_USER + ']');
exec('GRANT VIEW ANY SECURITY DEFINITION TO [' + @COLLECTION_USER + ']');
END;
END;

Expand All @@ -60,8 +63,13 @@ BEGIN
BEGIN
exec ('
use [' + @dbname + '];
CREATE USER [$(collectionUser)] FOR LOGIN [$(collectionUser)];
GRANT VIEW DATABASE STATE TO [$(collectionUser)]');
IF NOT EXISTS (SELECT [name]
FROM [sys].[database_principals]
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
BEGIN
CREATE USER [' + @COLLECTION_USER + '] FOR LOGIN [' + @COLLECTION_USER + '];
END;
GRANT VIEW DATABASE STATE TO [' + @COLLECTION_USER + ']');
END;

FETCH NEXT FROM db_cursor INTO @dbname;
Expand Down
62 changes: 62 additions & 0 deletions scripts/collector/sqlserver/sql/dropCollectionUser.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

SET NOCOUNT ON;
SET LANGUAGE us_english;

DECLARE @dbname VARCHAR(50);
DECLARE @COLLECTION_USER VARCHAR(256);

DECLARE db_cursor CURSOR FOR
SELECT name
FROM MASTER.sys.databases
WHERE name NOT IN ('model','msdb','distribution','reportserver', 'reportservertempdb','resource','rdsadmin')
AND state = 0;

SELECT @COLLECTION_USER = N'$(collectionUser)'

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN
exec ('
use [' + @dbname + '];
IF EXISTS (SELECT [name]
FROM [sys].[database_principals]
WHERE [type] = N''S'' AND [name] = N''' + @COLLECTION_USER + ''')
BEGIN
DROP USER [' + @COLLECTION_USER + '];
END;
');
END;

FETCH NEXT FROM db_cursor INTO @dbname;
END;

CLOSE db_cursor
DEALLOCATE db_cursor

use [master];
IF EXISTS
(SELECT name
FROM master.sys.server_principals
WHERE name = @COLLECTION_USER)
BEGIN
exec ('DROP LOGIN [' + @COLLECTION_USER + ']');
END;
2 changes: 1 addition & 1 deletion scripts/masker/dma-collection-masker
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ __all__ = [
"run_masker",
]

__version__ = "4.3.17"
__version__ = "4.3.18"

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit 9d1110a

Please sign in to comment.