Skip to content

Commit 752efb6

Browse files
authored
SqlDatabaseObjectPermission: Add validation for single permission (#2353)
1 parent ed34b01 commit 752efb6

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
176176

177177
### Fixed
178178

179+
- `SqlDatabaseObjectPermission`
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
183+
([issue #2345](https://github.com/dsccommunity/SqlServerDsc/issues/2345)).
179184
- `Get-SqlDscRSSetupConfiguration`
180185
- Fixed issue where the function doesn't provide an output for SSRS 2016 instances
181186
because registry paths were using `InstanceName` instead of `InstanceId`.

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,51 @@ property names of the [ObjectPermissionSet](https://docs.microsoft.com/en-us/dot
2020

2121
## Known issues
2222

23+
### Only one permission per `DSC_DatabaseObjectPermission` instance
24+
25+
Each `DSC_DatabaseObjectPermission` instance can only contain a single permission
26+
name. When multiple permissions need to be configured for the same state (e.g.,
27+
`Grant`), each permission must be specified in a separate `DSC_DatabaseObjectPermission`
28+
block. Specifying multiple permissions as a comma-separated string (e.g.,
29+
`'DELETE,INSERT,SELECT'`) will cause an error similar to:
30+
31+
```text
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.
36+
```
37+
38+
**Incorrect usage:**
39+
40+
<!-- markdownlint-disable MD013 - Line length -->
41+
```powershell
42+
Permission = @(
43+
DSC_DatabaseObjectPermission {
44+
State = 'Grant'
45+
Permission = 'DELETE,INSERT,SELECT' # This will fail - multiple permissions in single string
46+
}
47+
)
48+
```
49+
<!-- markdownlint-enable MD013 - Line length -->
50+
51+
**Correct usage:**
52+
53+
```powershell
54+
Permission = @(
55+
DSC_DatabaseObjectPermission {
56+
State = 'Grant'
57+
Permission = 'DELETE'
58+
}
59+
DSC_DatabaseObjectPermission {
60+
State = 'Grant'
61+
Permission = 'INSERT'
62+
}
63+
DSC_DatabaseObjectPermission {
64+
State = 'Grant'
65+
Permission = 'SELECT'
66+
}
67+
)
68+
```
69+
2370
All issues are not listed here, see [here for all open issues](https://github.com/dsccommunity/SqlServerDsc/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+SqlDatabaseObjectPermission).

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,3 +2791,67 @@ 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+
Ensure = ''
2808+
} `
2809+
-ClientOnly
2810+
2811+
{ Assert-PermissionEnsureProperty -Permission $mockPermission } | Should -Not -Throw
2812+
}
2813+
}
2814+
}
2815+
2816+
Context 'When permission value is invalid' {
2817+
It 'Should throw an error for comma-separated permissions' {
2818+
InModuleScope -ScriptBlock {
2819+
Set-StrictMode -Version 1.0
2820+
2821+
$mockPermission = New-CimInstance `
2822+
-ClassName 'DSC_DatabaseObjectPermission' `
2823+
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
2824+
-Property @{
2825+
State = 'Grant'
2826+
Permission = 'Delete,Insert,Select'
2827+
Ensure = ''
2828+
} `
2829+
-ClientOnly
2830+
2831+
$mockErrorMessage = $script:localizedData.InvalidPermissionValue
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+
Ensure = ''
2849+
} `
2850+
-ClientOnly
2851+
2852+
{ Assert-PermissionEnsureProperty -Permission $mockPermission } |
2853+
Should -Throw -ExpectedMessage '*Delete Insert*'
2854+
}
2855+
}
2856+
}
2857+
}

0 commit comments

Comments
 (0)