Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `SqlDatabaseObjectPermission`
- Added validation to ensure each `DSC_DatabaseObjectPermission` instance
only contains a single permission name. Specifying multiple permissions
as a comma-separated string now throws a descriptive error
([issue #2345](https://github.com/dsccommunity/SqlServerDsc/issues/2345)).
- `Get-SqlDscRSSetupConfiguration`
- Fixed issue where the function doesn't provide an output for SSRS 2016 instances
because registry paths were using `InstanceName` instead of `InstanceId`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,14 @@ function Assert-PermissionEnsureProperty

foreach ($desiredPermission in $Permission)
{
# Validate that Permission only contains a single permission name.
if ($desiredPermission.Permission -notmatch '^\w+$')
{
$errorMessage = $script:localizedData.InvalidPermissionValue -f $desiredPermission.Permission

New-ArgumentException -ArgumentName 'Permission' -Message $errorMessage
}

if (-not $desiredPermission.Ensure)
{
$desiredPermission.Ensure = 'Present'
Expand Down
47 changes: 47 additions & 0 deletions source/DSCResources/DSC_SqlDatabaseObjectPermission/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,51 @@ property names of the [ObjectPermissionSet](https://docs.microsoft.com/en-us/dot

## Known issues

### Only one permission per `DSC_DatabaseObjectPermission` instance

Each `DSC_DatabaseObjectPermission` instance can only contain a single permission
name. When multiple permissions need to be configured for the same state (e.g.,
`Grant`), each permission must be specified in a separate `DSC_DatabaseObjectPermission`
block. Specifying multiple permissions as a comma-separated string (e.g.,
`'DELETE,INSERT,SELECT'`) will cause an error similar to:

```text
The permission value 'DELETE,INSERT,SELECT' is invalid. Each
DSC_DatabaseObjectPermission instance can only contain a single permission
name. Specify each permission in a separate DSC_DatabaseObjectPermission
instance.
```

**Incorrect usage:**

<!-- markdownlint-disable MD013 - Line length -->
```powershell
Permission = @(
DSC_DatabaseObjectPermission {
State = 'Grant'
Permission = 'DELETE,INSERT,SELECT' # This will fail - multiple permissions in single string
}
)
```
<!-- markdownlint-enable MD013 - Line length -->

**Correct usage:**

```powershell
Permission = @(
DSC_DatabaseObjectPermission {
State = 'Grant'
Permission = 'DELETE'
}
DSC_DatabaseObjectPermission {
State = 'Grant'
Permission = 'INSERT'
}
DSC_DatabaseObjectPermission {
State = 'Grant'
Permission = 'SELECT'
}
)
```

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).
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ ConvertFrom-StringData @'
PermissionStateInDesiredState = The permission state '{0}' is already in desired state for database object '{1}'. (SDOP0010)
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)
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.
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)
'@
64 changes: 64 additions & 0 deletions tests/Unit/DSC_SqlDatabaseObjectPermission.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2791,3 +2791,67 @@ Describe 'SqlDatabaseObjectPermission\Get-DatabaseObject' -Tag 'Helper' {
}
}
}

Describe 'SqlDatabaseObjectPermission\Assert-PermissionEnsureProperty' -Tag 'Helper' {
Context 'When permission value is valid' {
It 'Should not throw an error for a single permission name' {
InModuleScope -ScriptBlock {
Set-StrictMode -Version 1.0

$mockPermission = New-CimInstance `
-ClassName 'DSC_DatabaseObjectPermission' `
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
-Property @{
State = 'Grant'
Permission = 'Select'
Ensure = ''
} `
-ClientOnly

{ Assert-PermissionEnsureProperty -Permission $mockPermission } | Should -Not -Throw
}
}
}

Context 'When permission value is invalid' {
It 'Should throw an error for comma-separated permissions' {
InModuleScope -ScriptBlock {
Set-StrictMode -Version 1.0

$mockPermission = New-CimInstance `
-ClassName 'DSC_DatabaseObjectPermission' `
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
-Property @{
State = 'Grant'
Permission = 'Delete,Insert,Select'
Ensure = ''
} `
-ClientOnly

$mockErrorMessage = $script:localizedData.InvalidPermissionValue

{ Assert-PermissionEnsureProperty -Permission $mockPermission } |
Should -Throw -ExpectedMessage '*Delete,Insert,Select*'
}
}

It 'Should throw an error for permissions with spaces' {
InModuleScope -ScriptBlock {
Set-StrictMode -Version 1.0

$mockPermission = New-CimInstance `
-ClassName 'DSC_DatabaseObjectPermission' `
-Namespace 'root/microsoft/Windows/DesiredStateConfiguration' `
-Property @{
State = 'Grant'
Permission = 'Delete Insert'
Ensure = ''
} `
-ClientOnly

{ Assert-PermissionEnsureProperty -Permission $mockPermission } |
Should -Throw -ExpectedMessage '*Delete Insert*'
}
}
}
}