Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
04be789
Update Get-SqlDscServerPermission to check for both login and role pe…
johlju Aug 12, 2025
b26982d
Update CHANGELOG.md
johlju Aug 12, 2025
caa95ff
Update Get-SqlDscServerPermission documentation for clarity on SQL Se…
johlju Aug 12, 2025
c1200e1
Fix test to properly handle output of Get-SqlDscServerPermission command
johlju Aug 12, 2025
233c087
Add cleanup step to disconnect from SQL Server after tests in Assert-…
johlju Aug 12, 2025
b824bad
Add integration tests for Get-SqlDscServerPermission command
johlju Aug 12, 2025
fd81b7d
Add integration test for Get-SqlDscServerPermission command
johlju Aug 12, 2025
ff657fa
Enhance Get-SqlDscServerPermission to support server roles alongside …
johlju Aug 12, 2025
7dcc114
Add PrincipalType parameter to Get-SqlDscServerPermission for selecti…
johlju Aug 12, 2025
139f03f
Update CHANGELOG.md
johlju Aug 12, 2025
125203e
Refactor Get-SqlDscServerPermission to simplify role check logic
johlju Aug 13, 2025
a71b591
Fix documentation in Test-SqlDscIsRole to clarify server principal ro…
johlju Aug 13, 2025
a135d6b
Enhance Get-SqlDscServerPermission to support Login and ServerRole pa…
johlju Sep 1, 2025
a6e1913
Add integration tests for Login and ServerRole parameter sets in Get-…
johlju Sep 1, 2025
3b35502
Update integration tests for Get-SqlDscServerPermission to use SqlDsc…
johlju Sep 1, 2025
0ada941
Enhance Grant-SqlDscServerPermission integration tests to verify perm…
johlju Sep 1, 2025
bc665ac
Refactor permission revocation tests to use string literals for permi…
johlju Sep 1, 2025
e0bae36
Enhance Get-SqlDscServerPermission integration tests to verify Create…
johlju Sep 1, 2025
2630720
Refactor Revoke-SqlDscServerPermission tests to remove InModuleScope …
johlju Sep 1, 2025
5131d04
Refactor Get-SqlDscServerPermission integration tests to improve vari…
johlju Sep 1, 2025
87506dc
Refactor integration tests to use a consistent module name variable
johlju Sep 1, 2025
c7f4204
Refactor Grant-SqlDscServerPermission integration tests to use 'Error…
johlju Sep 1, 2025
9b21b16
Refactor integration tests to start and stop SQL Server service, impr…
johlju Sep 1, 2025
c9ab88b
Pin 'DscResource.Test' module version to 0.17.2 to prevent integratio…
johlju Sep 1, 2025
40c13ba
Refactor integration tests to use string literals for SQL permissions…
johlju Sep 2, 2025
8705f03
Refactor Grant-SqlDscServerPermission integration tests to improve pe…
johlju Sep 3, 2025
b6e6e3d
Add 'ErrorAction' parameter to Connect-SqlDscDatabaseEngine calls in …
johlju Sep 3, 2025
e97c1fd
Enhance error handling in Revoke-SqlDscServerPermission by including …
johlju Sep 3, 2025
b0c00a6
Enhance error messages in Grant and Deny server permission functions …
johlju Sep 3, 2025
b03149d
Remove redundant permission name retrieval in Deny, Grant, and Revoke…
johlju Sep 3, 2025
db2681f
Refactor New-SqlDscLogin call to use -SqlLogin and -SecurePassword pa…
johlju Sep 3, 2025
cb0d2c7
Update New-SqlDscLogin call in integration test to include -PassThru …
johlju Sep 3, 2025
8287508
Refactor permission test to use built-in securityadmin role instead o…
johlju Sep 3, 2025
1a3f34f
Add verbose output for permission testing and update localized string…
johlju Sep 4, 2025
1f9aa40
Update permission tests to include additional permissions for exact m…
johlju Sep 4, 2025
047b327
Refactor verbose messages to debug messages in Test-SqlDscServerPermi…
johlju Sep 4, 2025
36d895f
Remove verbose flag from Test-SqlDscServerPermission calls for cleane…
johlju Sep 4, 2025
fad7990
Refactor permission tests to use string literals for permissions inst…
johlju Sep 5, 2025
aecc2a1
Add tests for denying server permissions to logins and roles, includi…
johlju Sep 5, 2025
785a500
Refactor permission tests to use string literals for permissions and …
johlju Sep 5, 2025
b56bca4
Add ErrorAction parameter to Connect-SqlDscDatabaseEngine for improve…
johlju Sep 5, 2025
05d965e
Refactor Revoke-SqlDscServerPermission tests to use script-scoped var…
johlju Sep 5, 2025
59a8983
Remove Revoke-SqlDscServerPermission test from integration test group
johlju Sep 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added documentation for `SqlIntegrationTest` user and
`IntegrationTestSqlLogin` login.
- Added run order information for `New-SqlDscLogin` integration test.
- `Get-SqlDscServerPermission`
- Enhanced the command to support server roles in addition to logins by
utilizing `Test-SqlDscIsRole` alongside the existing `Test-SqlDscIsLogin`
check.
- The command now accepts both login principals and server role principals
as the `Name` parameter (issue [#2063](https://github.com/dsccommunity/SqlServerDsc/issues/2063)).
- `azure-pipelines.yml`
- Remove `windows-2019` images fixes [#2106](https://github.com/dsccommunity/SqlServerDsc/issues/2106).
- Move individual tasks to `windows-latest`.
Expand Down
1 change: 0 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ stages:
'tests/Integration/Commands/Remove-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscRole.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscLogin.Integration.Tests.ps1'

# Group 9
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
)
Expand Down
73 changes: 65 additions & 8 deletions source/Public/Get-SqlDscServerPermission.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<#
.SYNOPSIS
Returns the current permissions for the principal.
Returns the current permissions for a SQL Server login or server role.

.DESCRIPTION
Returns the current permissions for the principal.
Returns the current permissions for a SQL Server login or server role.
The command can retrieve permissions for both user-defined and built-in
server principals including SQL Server logins and server roles.

.PARAMETER ServerObject
Specifies current server connection object.

.PARAMETER Name
Specifies the name of the principal for which the permissions are
returned.
Specifies the name of the SQL Server login or server role for which
the permissions are returned.

.PARAMETER PrincipalType
Specifies the type(s) of principal to check. Valid values are 'Login'
and 'Role'. If not specified, both login and role checks will be performed.
If specified, only the specified type(s) will be checked.

.OUTPUTS
[Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]]
Expand All @@ -21,6 +28,24 @@

Get the permissions for the principal 'MyPrincipal'.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
Get-SqlDscServerPermission -ServerObject $serverInstance -Name 'sysadmin'

Get the permissions for the server role 'sysadmin'.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
Get-SqlDscServerPermission -ServerObject $serverInstance -Name 'MyLogin' -PrincipalType 'Login'

Get the permissions for the login 'MyLogin', only checking if it exists as a login.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
Get-SqlDscServerPermission -ServerObject $serverInstance -Name 'MyRole' -PrincipalType 'Role'

Get the permissions for the server role 'MyRole', only checking if it exists as a role.

.NOTES
If specifying `-ErrorAction 'SilentlyContinue'` then the command will silently
ignore if the principal (parameter **Name**) is not present. In such case the
Expand All @@ -42,22 +67,54 @@ function Get-SqlDscServerPermission

[Parameter(Mandatory = $true)]
[System.String]
$Name
$Name,

[Parameter()]
[ValidateSet('Login', 'Role')]
[System.String[]]
$PrincipalType
)

# cSpell: ignore GSDSP
process
{
$getSqlDscServerPermissionResult = $null

$testSqlDscIsLoginParameters = @{
$testSqlDscIsPrincipalParameters = @{
ServerObject = $ServerObject
Name = $Name
}

$isLogin = Test-SqlDscIsLogin @testSqlDscIsLoginParameters
# Determine which checks to perform based on PrincipalType parameter
$checkLogin = $true
$checkRole = $true

if ($PSBoundParameters.ContainsKey('PrincipalType'))
{
$checkLogin = $PrincipalType -contains 'Login'
$checkRole = $PrincipalType -contains 'Role'
}

# Perform the appropriate checks
$isLogin = if ($checkLogin)
{
Test-SqlDscIsLogin @testSqlDscIsPrincipalParameters
}
else
{
$false
}

$isRole = if ($checkRole)
{
Test-SqlDscIsRole @testSqlDscIsPrincipalParameters
}
else
{
$false
}

if ($isLogin)
if ($isLogin -or $isRole)
{
$getSqlDscServerPermissionResult = $ServerObject.EnumServerPermissions($Name)
}
Expand Down
8 changes: 4 additions & 4 deletions source/Public/Test-SqlDscIsRole.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<#
.SYNOPSIS
Returns whether the database principal exists and is a database role.
Returns whether the server principal exists and is a server role.
.DESCRIPTION
Returns whether the database principal exist and is a database role.
Returns whether the server principal exist and is a server role.
.PARAMETER ServerObject
Specifies current server connection object.
.PARAMETER Name
Specifies the name of the database principal.
Specifies the name of the server principal.
.OUTPUTS
[System.Boolean]
Expand All @@ -18,7 +18,7 @@
$serverInstance = Connect-SqlDscDatabaseEngine
Test-SqlDscIsRole -ServerObject $serverInstance -Name 'MyPrincipal'
Returns $true if the principal exist as role, if not $false is returned.
Returns $true if the principal exist as a server role, if not $false is returned.
#>
function Test-SqlDscIsRole
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:instanceName -Credential $script:sqlAdminCredential
}

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
}

Context 'When a login exists' {
It 'Should not throw an error for sa login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' } | Should -Not -Throw
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()

BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies has not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}
}

BeforeAll {
$script:dscModuleName = 'SqlServerDsc'

Import-Module -Name $script:dscModuleName
}

Describe 'Get-SqlDscServerPermission' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:instanceName = 'DSCSQLTEST'
$script:computerName = Get-ComputerName
}

AfterAll {
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When connecting to SQL Server instance' {
BeforeAll {
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$script:sqlAdminCredential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)

$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:instanceName -Credential $script:sqlAdminCredential
}

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
}

Context 'When getting permissions for valid SQL logins' {
It 'Should return permissions for sa login' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for sa login using pipeline' {
$result = $script:serverObject | Get-SqlDscServerPermission -Name 'sa'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}
}

Context 'When getting permissions for valid Windows logins' {
It 'Should return permissions for SqlAdmin Windows login' {
$windowsLogin = '{0}\SqlAdmin' -f $script:computerName
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name $windowsLogin

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for NT AUTHORITY\SYSTEM login' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}
}

Context 'When getting permissions for valid server roles' {
It 'Should return permissions for sysadmin server role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for public server role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'public'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for serveradmin server role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'serveradmin'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for securityadmin server role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'securityadmin'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}
}

Context 'When getting permissions for invalid principals' {
It 'Should throw error for non-existent login with ErrorAction Stop' {
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentLogin123' -ErrorAction 'Stop' } |
Should -Throw -ExpectedMessage "*is not a login nor role*"
}

It 'Should return null for non-existent login with ErrorAction SilentlyContinue' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentLogin123' -ErrorAction 'SilentlyContinue'

$result | Should -BeNullOrEmpty
}

It 'Should throw error for non-existent server role with ErrorAction Stop' {
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentRole123' -ErrorAction 'Stop' } |
Should -Throw -ExpectedMessage "*is not a login nor role*"
}

It 'Should return null for non-existent server role with ErrorAction SilentlyContinue' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentRole123' -ErrorAction 'SilentlyContinue'

$result | Should -BeNullOrEmpty
}
}

Context 'When verifying permission properties' {
BeforeAll {
# Get permissions for a known principal that should have permissions
$script:testPermissions = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin'
}

It 'Should return ServerPermissionInfo objects with PermissionState property' {
$script:testPermissions | Should -Not -BeNullOrEmpty

foreach ($permission in $script:testPermissions) {
$permission.PermissionState | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
}
}

It 'Should return ServerPermissionInfo objects with PermissionType property' {
$script:testPermissions | Should -Not -BeNullOrEmpty

foreach ($permission in $script:testPermissions) {
$permission.PermissionType | Should -Not -BeNullOrEmpty
$permission.PermissionType | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
}
}
}

Context 'When using PrincipalType parameter' {
It 'Should return permissions for sa login when PrincipalType is Login' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -PrincipalType 'Login'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for sysadmin role when PrincipalType is Role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin' -PrincipalType 'Role'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should return permissions for sa login when PrincipalType is both Login and Role' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -PrincipalType 'Login', 'Role'

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo]
}

It 'Should throw error when looking for login as role' {
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -PrincipalType 'Role' -ErrorAction 'Stop' } |
Should -Throw -ExpectedMessage "*is not a login nor role*"
}

It 'Should throw error when looking for role as login' {
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin' -PrincipalType 'Login' -ErrorAction 'Stop' } |
Should -Throw -ExpectedMessage "*is not a login nor role*"
}

It 'Should return null when looking for login as role with SilentlyContinue' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -PrincipalType 'Role' -ErrorAction 'SilentlyContinue'

$result | Should -BeNullOrEmpty
}

It 'Should return null when looking for role as login with SilentlyContinue' {
$result = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin' -PrincipalType 'Login' -ErrorAction 'SilentlyContinue'

$result | Should -BeNullOrEmpty
}
}
}
}
Loading
Loading