Skip to content
Merged
Show file tree
Hide file tree
Changes from 37 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
22 changes: 18 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed environment variable persistence by using $GITHUB_ENV instead of
job-level env declaration.
- `Grant-SqlDscServerPermission`
- Added new public command to grant server permissions to a principal (Login or ServerRole) on a SQL Server Database Engine instance.
- Added new public command to grant server permissions to a principal
(Login or ServerRole) on a SQL Server Database Engine instance.
- `Deny-SqlDscServerPermission`
- Added new public command to deny server permissions to a principal (Login or ServerRole).
- Added new public command to deny server permissions to a principal
(Login or ServerRole).
- `Revoke-SqlDscServerPermission`
- Added new public command to revoke server permissions from a principal (Login or ServerRole).
- Added new public command to revoke server permissions from a principal
(Login or ServerRole).
- `Test-SqlDscServerPermission`
- Added new public command with Grant/Deny parameter sets (and `-WithGrant`) to test server permissions for a principal.
- Added new public command with Grant/Deny parameter sets (and `-WithGrant`)
to test server permissions for a principal.
- `Assert-SqlDscLogin`
- Added new public command to validate that a specified SQL Server principal
is a login.
- `Enable-SqlDscLogin`
- Added new public command to enable a SQL Server login.
- `Get-SqlDscServerPermission`
- Enhanced command to support pipeline input for Login and ServerRole
objects while maintaining backward compatibility with the original
parameter set.
- `Disable-SqlDscLogin`
- Added new public command to disable a SQL Server login.
- `Test-SqlDscIsLoginEnabled`
Expand Down Expand Up @@ -88,6 +96,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
2 changes: 1 addition & 1 deletion RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Sampler = 'latest'
'Sampler.GitHubTasks' = 'latest'
MarkdownLinkCheck = 'latest'
'DscResource.Test' = 'latest'
'DscResource.Test' = '0.17.2'
xDscResourceDesigner = 'latest'

# Build dependencies needed for using the module
Expand Down
3 changes: 1 addition & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ stages:
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Deny-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Revoke-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
Expand All @@ -315,9 +314,9 @@ stages:
# Group 8
'tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscDatabase.Integration.Tests.ps1'
'tests/Integration/Commands/Revoke-SqlDscServerPermission.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
14 changes: 4 additions & 10 deletions source/Public/Deny-SqlDscServerPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,19 @@ function Deny-SqlDscServerPermission
$permissionSet.$permissionName = $true
}

# Get the permissions names that are set to $true in the ServerPermissionSet.
$permissionName = $permissionSet |
Get-Member -MemberType 'Property' |
Select-Object -ExpandProperty 'Name' |
Where-Object -FilterScript {
$permissionSet.$_
}

try
{
$serverObject.Deny($permissionSet, $principalName)
}
catch
{
$errorMessage = $script:localizedData.ServerPermission_Deny_FailedToDenyPermission -f $principalName, $serverObject.InstanceName
$errorMessage = $script:localizedData.ServerPermission_Deny_FailedToDenyPermission -f $principalName, $serverObject.InstanceName, ($Permission -join ',')

$exception = [System.InvalidOperationException]::new($errorMessage, $_.Exception)

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$errorMessage,
$exception,
'DSDSP0001', # cSpell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$principalName
Expand Down
162 changes: 145 additions & 17 deletions source/Public/Get-SqlDscServerPermission.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
<#
.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.

The command supports two modes of operation:
1. By name: Specify ServerObject, Name, and optionally PrincipalType
2. By object: Pass Login or ServerRole objects via pipeline

.PARAMETER ServerObject
Specifies current server connection object.
Specifies current server connection object. This parameter is used in the
default parameter set for backward compatibility.

.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. This parameter is used in the default
parameter set for backward compatibility.

.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. This parameter
is used in the default parameter set for backward compatibility.

.PARAMETER Login
Specifies the Login object for which the permissions are returned.
This parameter accepts pipeline input.

.PARAMETER ServerRole
Specifies the ServerRole object for which the permissions are returned.
This parameter accepts pipeline input.

.OUTPUTS
[Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]]
Expand All @@ -21,51 +43,157 @@

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.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
$login = $serverInstance | Get-SqlDscLogin -Name 'MyLogin'

Get-SqlDscServerPermission -Login $login

Get the permissions for the login 'MyLogin' using a Login object.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
$role = $serverInstance | Get-SqlDscRole -Name 'MyRole'

$role | Get-SqlDscServerPermission

Get the permissions for the server role 'MyRole' using a ServerRole object from the pipeline.

.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine

$serverInstance | Get-SqlDscLogin | Get-SqlDscServerPermission

Get the permissions for all logins from the pipeline.

.NOTES
If specifying `-ErrorAction 'SilentlyContinue'` then the command will silently
ignore if the principal (parameter **Name**) is not present. In such case the
command will return `$null`. If specifying `-ErrorAction 'Stop'` the command
will throw an error if the principal is missing.

The Login or ServerRole object must come from the same SQL Server instance
where the permissions will be retrieved.
#>
function Get-SqlDscServerPermission
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Because the rule does not understands that the command returns [System.String[]] when using , (comma) in the return statement')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification = 'Because the code throws based on an prior expression')]
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'ByName')]
[OutputType([Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByName')]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,

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

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

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Login')]
[Microsoft.SqlServer.Management.Smo.Login]
$Login,

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ServerRole')]
[Microsoft.SqlServer.Management.Smo.ServerRole]
$ServerRole
)

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

$testSqlDscIsLoginParameters = @{
ServerObject = $ServerObject
Name = $Name
# Determine which parameter set we're using and set up variables accordingly
if ($PSCmdlet.ParameterSetName -eq 'Login')
{
$principalName = $Login.Name
$serverObject = $Login.Parent
$isLogin = $true
$isRole = $false
}
elseif ($PSCmdlet.ParameterSetName -eq 'ServerRole')
{
$principalName = $ServerRole.Name
$serverObject = $ServerRole.Parent
$isLogin = $false
$isRole = $true
}
else
{
# ByName parameter set (default for backward compatibility)
$principalName = $Name
$serverObject = $ServerObject

$isLogin = Test-SqlDscIsLogin @testSqlDscIsLoginParameters
$testSqlDscIsPrincipalParameters = @{
ServerObject = $serverObject
Name = $principalName
}

# 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)
$getSqlDscServerPermissionResult = $serverObject.EnumServerPermissions($principalName)
}
else
{
$missingPrincipalMessage = $script:localizedData.ServerPermission_MissingPrincipal -f $Name, $ServerObject.InstanceName
$missingPrincipalMessage = $script:localizedData.ServerPermission_MissingPrincipal -f $principalName, $serverObject.InstanceName

Write-Error -Message $missingPrincipalMessage -Category 'InvalidOperation' -ErrorId 'GSDSP0001' -TargetObject $Name
Write-Error -Message $missingPrincipalMessage -Category 'InvalidOperation' -ErrorId 'GSDSP0001' -TargetObject $principalName
}

return , [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]] $getSqlDscServerPermissionResult
Expand Down
14 changes: 4 additions & 10 deletions source/Public/Grant-SqlDscServerPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,6 @@ function Grant-SqlDscServerPermission
$permissionSet.$permissionName = $true
}

# Get the permissions names that are set to $true in the ServerPermissionSet.
$permissionName = $permissionSet |
Get-Member -MemberType 'Property' |
Select-Object -ExpandProperty 'Name' |
Where-Object -FilterScript {
$permissionSet.$_
}

try
{
if ($WithGrant.IsPresent)
Expand All @@ -140,11 +132,13 @@ function Grant-SqlDscServerPermission
}
catch
{
$errorMessage = $script:localizedData.ServerPermission_Grant_FailedToGrantPermission -f $principalName, $serverObject.InstanceName
$errorMessage = $script:localizedData.ServerPermission_Grant_FailedToGrantPermission -f $principalName, $serverObject.InstanceName, ($Permission -join ', ')

$exception = [System.InvalidOperationException]::new($errorMessage, $_.Exception)

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$errorMessage,
$exception,
'GSDSP0001', # cSpell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$principalName
Expand Down
14 changes: 4 additions & 10 deletions source/Public/Revoke-SqlDscServerPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ function Revoke-SqlDscServerPermission
$permissionSet.$permissionName = $true
}

# Get the permissions names that are set to $true in the ServerPermissionSet.
$permissionName = $permissionSet |
Get-Member -MemberType 'Property' |
Select-Object -ExpandProperty 'Name' |
Where-Object -FilterScript {
$permissionSet.$_
}

try
{
if ($WithGrant.IsPresent)
Expand All @@ -138,11 +130,13 @@ function Revoke-SqlDscServerPermission
}
catch
{
$errorMessage = $script:localizedData.ServerPermission_Revoke_FailedToRevokePermission -f $principalName, $serverObject.InstanceName
$errorMessage = $script:localizedData.ServerPermission_Revoke_FailedToRevokePermission -f $principalName, $serverObject.InstanceName, ($Permission -join ',')

$exception = [System.InvalidOperationException]::new($errorMessage, $_.Exception)

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$errorMessage,
$exception,
'RSDSP0001', # cSpell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$principalName
Expand Down
Loading
Loading