Skip to content

Commit 084afb9

Browse files
Copilotjohlju
andcommitted
Add validation for single permission per DSC_DatabaseObjectPermission instance
Co-authored-by: johlju <[email protected]>
1 parent 885bc1e commit 084afb9

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
177177
### Fixed
178178

179179
- `SqlDatabaseObjectPermission`
180-
- Added documentation clarifying that each `DSC_DatabaseObjectPermission`
181-
instance can only contain a single permission name. Specifying multiple
182-
permissions as a comma-separated string causes an error
180+
- Added validation to ensure each `DSC_DatabaseObjectPermission` instance
181+
only contains a single permission name. Specifying multiple permissions
182+
as a comma-separated string now throws a descriptive error
183183
([issue #2020](https://github.com/dsccommunity/SqlServerDsc/issues/2020)).
184184
- `Get-SqlDscRSSetupConfiguration`
185185
- Fixed issue where the function doesn't provide an output for SSRS 2016 instances

source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,14 @@ function Assert-PermissionEnsureProperty
908908

909909
foreach ($desiredPermission in $Permission)
910910
{
911+
# Validate that Permission only contains a single permission name.
912+
if ($desiredPermission.Permission -notmatch '^\w+$')
913+
{
914+
$errorMessage = $script:localizedData.InvalidPermissionValue -f $desiredPermission.Permission
915+
916+
New-ArgumentException -ArgumentName 'Permission' -Message $errorMessage
917+
}
918+
911919
if (-not $desiredPermission.Ensure)
912920
{
913921
$desiredPermission.Ensure = 'Present'

source/DSCResources/DSC_SqlDatabaseObjectPermission/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ block. Specifying multiple permissions as a comma-separated string (e.g.,
2929
`'DELETE,INSERT,SELECT'`) will cause an error similar to:
3030

3131
```text
32-
Cannot bind argument to parameter 'ReferenceObject' because it is null.
32+
The permission value 'DELETE,INSERT,SELECT' is invalid. Each
33+
DSC_DatabaseObjectPermission instance can only contain a single permission
34+
name. Specify each permission in a separate DSC_DatabaseObjectPermission
35+
instance.
3336
```
3437

3538
**Incorrect usage:**

source/DSCResources/DSC_SqlDatabaseObjectPermission/en-US/DSC_SqlDatabaseObjectPermission.strings.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ ConvertFrom-StringData @'
1212
PermissionStateInDesiredState = The permission state '{0}' is already in desired state for database object '{1}'. (SDOP0010)
1313
RevokePermissionWithGrant = One or more of the permissions was granted with the 'With Grant' permission for the user '{1}' on the database object '{2}' of type '{3}' in the database '{4}'. For the permissions ('{0}') the 'With Grant' permission is revoked, and the revoke is cascaded. (SDOP0011)
1414
GrantCantBeSetBecauseRevokeIsNotOptedIn = One or more of the permissions was granted with the 'With Grant' permission for the user '{1}' on the database object '{2}' of type '{3}' in the database '{4}'. For the permissions ('{0}') the 'With Grant' permission must be revoked, and the revoke must be cascaded, to enforce the desired state. If this desired state should be enforced then set the parameter Force to $true.
15+
InvalidPermissionValue = The permission value '{0}' is invalid. Each DSC_DatabaseObjectPermission instance can only contain a single permission name. Specify each permission in a separate DSC_DatabaseObjectPermission instance. (SDOP0012)
1516
'@

tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,3 +2791,66 @@ Describe 'SqlDatabaseObjectPermission\Get-DatabaseObject' -Tag 'Helper' {
27912791
}
27922792
}
27932793
}
2794+
2795+
Describe 'SqlDatabaseObjectPermission\Assert-PermissionEnsureProperty' -Tag 'Helper' {
2796+
Context 'When permission value is valid' {
2797+
It 'Should not throw an error for a single permission name' {
2798+
InModuleScope -ScriptBlock {
2799+
Set-StrictMode -Version 1.0
2800+
2801+
$mockPermission = New-CimInstance `
2802+
-ClassName 'DSC_DatabaseObjectPermission' `
2803+
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
2804+
-Property @{
2805+
State = 'Grant'
2806+
Permission = 'Select'
2807+
} `
2808+
-ClientOnly
2809+
2810+
{ Assert-PermissionEnsureProperty -Permission $mockPermission } | Should -Not -Throw
2811+
}
2812+
}
2813+
}
2814+
2815+
Context 'When permission value is invalid' {
2816+
It 'Should throw an error for comma-separated permissions' {
2817+
InModuleScope -ScriptBlock {
2818+
Set-StrictMode -Version 1.0
2819+
2820+
$mockPermission = New-CimInstance `
2821+
-ClassName 'DSC_DatabaseObjectPermission' `
2822+
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
2823+
-Property @{
2824+
State = 'Grant'
2825+
Permission = 'Delete,Insert,Select'
2826+
} `
2827+
-ClientOnly
2828+
2829+
$mockErrorMessage = InModuleScope -ScriptBlock {
2830+
$script:localizedData.InvalidPermissionValue
2831+
}
2832+
2833+
{ Assert-PermissionEnsureProperty -Permission $mockPermission } |
2834+
Should -Throw -ExpectedMessage '*Delete,Insert,Select*'
2835+
}
2836+
}
2837+
2838+
It 'Should throw an error for permissions with spaces' {
2839+
InModuleScope -ScriptBlock {
2840+
Set-StrictMode -Version 1.0
2841+
2842+
$mockPermission = New-CimInstance `
2843+
-ClassName 'DSC_DatabaseObjectPermission' `
2844+
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
2845+
-Property @{
2846+
State = 'Grant'
2847+
Permission = 'Delete Insert'
2848+
} `
2849+
-ClientOnly
2850+
2851+
{ Assert-PermissionEnsureProperty -Permission $mockPermission } |
2852+
Should -Throw -ExpectedMessage '*Delete Insert*'
2853+
}
2854+
}
2855+
}
2856+
}

0 commit comments

Comments
 (0)