Skip to content

Commit 35a84eb

Browse files
authored
Add functions to enable and disable TLS for Reporting Services (#2387)
1 parent b6ddc10 commit 35a84eb

16 files changed

+2050
-28
lines changed

CHANGELOG.md

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

88
### Added
99

10+
- Added public command `Get-SqlDscRSConfiguration` to retrieve the
11+
`MSReportServer_ConfigurationSetting` CIM instance for SQL Server Reporting
12+
Services or Power BI Report Server. Supports auto-detection of the Reporting
13+
Services version or explicit version specification. The returned CIM instance
14+
can be piped to `Enable-SqlDscRsSecureConnection` or `Disable-SqlDscRsSecureConnection`
15+
([issue #2022](https://github.com/dsccommunity/SqlServerDsc/issues/2022)).
16+
- Added public command `Enable-SqlDscRsSecureConnection` to enable secure
17+
connection for SQL Server Reporting Services or Power BI Report Server by
18+
setting the secure connection level to 1. Accepts the configuration CIM
19+
instance from pipeline, supports `-WhatIf`/`-Confirm`, and `-PassThru` to
20+
return the configuration object
21+
([issue #2022](https://github.com/dsccommunity/SqlServerDsc/issues/2022)).
22+
- Added public command `Disable-SqlDscRsSecureConnection` to disable secure
23+
connection for SQL Server Reporting Services or Power BI Report Server by
24+
setting the secure connection level to 0. Accepts the configuration CIM
25+
instance from pipeline, supports `-WhatIf`/`-Confirm`, and `-PassThru` to
26+
return the configuration object
27+
([issue #2023](https://github.com/dsccommunity/SqlServerDsc/issues/2023)).
28+
- Added private function `Invoke-RsCimMethod` to invoke CIM methods on Reporting
29+
Services configuration instances with consistent error handling. This function
30+
is used by `Enable-SqlDscRsSecureConnection`, `Disable-SqlDscRsSecureConnection`,
31+
and the `SqlRS` resource.
1032
- `Invoke-ReportServerSetupAction`
1133
- Now uses `Format-Path` with `-ExpandEnvironmentVariable` to expand environment
1234
variables in all path parameters (`MediaPath`, `LogPath`, `InstallFolder`)
@@ -105,6 +127,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
105127

106128
### Changed
107129

130+
- `SqlRS`
131+
- Refactored to use the public commands `Enable-SqlDscRsSecureConnection` and
132+
`Disable-SqlDscRsSecureConnection` for setting the secure connection level
133+
instead of calling the CIM method directly.
108134
- `Assert-SetupActionProperties`
109135
- Refactored to use the command `Get-FileVersion` from the DscResource.Common
110136
module instead of the private function `Get-FileVersionInformation`

azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ stages:
535535
'tests/Integration/Commands/Get-SqlDscRSPackage.Integration.Tests.ps1'
536536
'tests/Integration/Commands/Get-SqlDscRSSetupConfiguration.Integration.Tests.ps1'
537537
'tests/Integration/Commands/Test-SqlDscRSInstalled.Integration.Tests.ps1'
538+
# Group 3
539+
'tests/Integration/Commands/Get-SqlDscRSConfiguration.Integration.Tests.ps1'
540+
'tests/Integration/Commands/Enable-SqlDscRsSecureConnection.Integration.Tests.ps1'
541+
'tests/Integration/Commands/Disable-SqlDscRsSecureConnection.Integration.Tests.ps1'
538542
# Group 8
539543
'tests/Integration/Commands/Repair-SqlDscReportingService.Integration.Tests.ps1'
540544
# Group 9
@@ -601,6 +605,10 @@ stages:
601605
'tests/Integration/Commands/Get-SqlDscRSPackage.Integration.Tests.ps1'
602606
'tests/Integration/Commands/Get-SqlDscRSSetupConfiguration.Integration.Tests.ps1'
603607
'tests/Integration/Commands/Test-SqlDscRSInstalled.Integration.Tests.ps1'
608+
# Group 3
609+
'tests/Integration/Commands/Get-SqlDscRSConfiguration.Integration.Tests.ps1'
610+
'tests/Integration/Commands/Enable-SqlDscRsSecureConnection.Integration.Tests.ps1'
611+
'tests/Integration/Commands/Disable-SqlDscRsSecureConnection.Integration.Tests.ps1'
604612
# Group 8
605613
'tests/Integration/Commands/Repair-SqlDscPowerBIReportServer.Integration.Tests.ps1'
606614
# Group 9

source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,14 @@ function Set-TargetResource
604604

605605
$restartReportingService = $true
606606

607-
$invokeRsCimMethodParameters = @{
608-
CimInstance = $reportingServicesData.Configuration
609-
MethodName = 'SetSecureConnectionLevel'
610-
Arguments = @{
611-
Level = @(0, 1)[$UseSsl]
612-
}
607+
if ($UseSsl)
608+
{
609+
$reportingServicesData.Configuration | Enable-SqlDscRsSecureConnection -Force
610+
}
611+
else
612+
{
613+
$reportingServicesData.Configuration | Disable-SqlDscRsSecureConnection -Force
613614
}
614-
615-
Invoke-RsCimMethod @invokeRsCimMethodParameters
616615
}
617616
}
618617
else
@@ -825,15 +824,14 @@ function Set-TargetResource
825824

826825
$restartReportingService = $true
827826

828-
$invokeRsCimMethodParameters = @{
829-
CimInstance = $reportingServicesData.Configuration
830-
MethodName = 'SetSecureConnectionLevel'
831-
Arguments = @{
832-
Level = @(0, 1)[$UseSsl]
833-
}
827+
if ($UseSsl)
828+
{
829+
$reportingServicesData.Configuration | Enable-SqlDscRsSecureConnection -Force
830+
}
831+
else
832+
{
833+
$reportingServicesData.Configuration | Disable-SqlDscRsSecureConnection -Force
834834
}
835-
836-
Invoke-RsCimMethod @invokeRsCimMethodParameters
837835
}
838836
}
839837

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<#
2+
.SYNOPSIS
3+
Invokes a CIM method on a Reporting Services configuration instance.
4+
5+
.DESCRIPTION
6+
A helper function that wraps Invoke-CimMethod to provide consistent
7+
error handling for Reporting Services CIM method calls. This function
8+
handles both ExtendedErrors and Error properties that can be returned
9+
by the CIM method.
10+
11+
.PARAMETER CimInstance
12+
The CIM instance object that contains the method to call.
13+
14+
.PARAMETER MethodName
15+
The name of the method to invoke on the CIM instance.
16+
17+
.PARAMETER Arguments
18+
A hashtable of arguments to pass to the method.
19+
20+
.OUTPUTS
21+
Microsoft.Management.Infrastructure.CimMethodResult
22+
23+
Returns the result of the CIM method call.
24+
25+
.EXAMPLE
26+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
27+
Invoke-RsCimMethod -CimInstance $config -MethodName 'ListReservedUrls'
28+
29+
Invokes the ListReservedUrls method on the configuration CIM instance.
30+
31+
.EXAMPLE
32+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
33+
Invoke-RsCimMethod -CimInstance $config -MethodName 'SetSecureConnectionLevel' -Arguments @{ Level = 1 }
34+
35+
Invokes the SetSecureConnectionLevel method with the Level argument.
36+
#>
37+
function Invoke-RsCimMethod
38+
{
39+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification = 'Because the code throws based on an prior expression')]
40+
[CmdletBinding()]
41+
[OutputType([Microsoft.Management.Infrastructure.CimMethodResult])]
42+
param
43+
(
44+
[Parameter(Mandatory = $true)]
45+
[System.Object]
46+
$CimInstance,
47+
48+
[Parameter(Mandatory = $true)]
49+
[System.String]
50+
$MethodName,
51+
52+
[Parameter()]
53+
[System.Collections.Hashtable]
54+
$Arguments
55+
)
56+
57+
$invokeCimMethodParameters = @{
58+
MethodName = $MethodName
59+
ErrorAction = 'Stop'
60+
}
61+
62+
if ($PSBoundParameters.ContainsKey('Arguments'))
63+
{
64+
$invokeCimMethodParameters['Arguments'] = $Arguments
65+
}
66+
67+
$invokeCimMethodResult = $CimInstance | Invoke-CimMethod @invokeCimMethodParameters
68+
69+
<#
70+
Successfully calling the method returns $invokeCimMethodResult.HRESULT -eq 0.
71+
If a general error occurs in the Invoke-CimMethod, like calling a method
72+
that does not exist, returns $null in $invokeCimMethodResult.
73+
74+
cSpell: ignore HRESULT
75+
#>
76+
if ($invokeCimMethodResult -and $invokeCimMethodResult.HRESULT -ne 0)
77+
{
78+
if ($invokeCimMethodResult | Get-Member -Name 'ExtendedErrors')
79+
{
80+
<#
81+
The returned object property ExtendedErrors is an array
82+
so that needs to be concatenated.
83+
#>
84+
$errorMessage = $invokeCimMethodResult.ExtendedErrors -join ';'
85+
}
86+
else
87+
{
88+
$errorMessage = $invokeCimMethodResult.Error
89+
}
90+
91+
$errorMessage = $script:localizedData.Invoke_RsCimMethod_FailedToInvokeMethod -f $MethodName, $errorMessage, $invokeCimMethodResult.HRESULT
92+
93+
throw $errorMessage
94+
}
95+
96+
return $invokeCimMethodResult
97+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<#
2+
.SYNOPSIS
3+
Disables secure connection for SQL Server Reporting Services.
4+
5+
.DESCRIPTION
6+
Disables secure connection (TLS/SSL) for SQL Server Reporting Services
7+
or Power BI Report Server by setting the secure connection level to 0.
8+
9+
This command calls the `SetSecureConnectionLevel` method on the
10+
`MSReportServer_ConfigurationSetting` CIM instance with a level value
11+
of 0, which disables the secure connection requirement for connections
12+
to the Reporting Services web service and portal.
13+
14+
The configuration CIM instance can be obtained using the
15+
`Get-SqlDscRSConfiguration` command and passed via the pipeline.
16+
17+
.PARAMETER Configuration
18+
Specifies the `MSReportServer_ConfigurationSetting` CIM instance for
19+
the Reporting Services instance. This can be obtained using the
20+
`Get-SqlDscRSConfiguration` command. This parameter accepts pipeline
21+
input.
22+
23+
.PARAMETER PassThru
24+
If specified, returns the configuration CIM instance after disabling
25+
secure connection.
26+
27+
.PARAMETER Force
28+
If specified, suppresses the confirmation prompt.
29+
30+
.EXAMPLE
31+
Get-SqlDscRSConfiguration -InstanceName 'SSRS' | Disable-SqlDscRsSecureConnection
32+
33+
Disables secure connection for the SSRS instance by piping the configuration
34+
from `Get-SqlDscRSConfiguration`.
35+
36+
.EXAMPLE
37+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
38+
Disable-SqlDscRsSecureConnection -Configuration $config
39+
40+
Disables secure connection for the SSRS instance by passing the
41+
configuration as a parameter.
42+
43+
.EXAMPLE
44+
Get-SqlDscRSConfiguration -InstanceName 'SSRS' | Disable-SqlDscRsSecureConnection -PassThru
45+
46+
Disables secure connection for the SSRS instance and returns the
47+
configuration CIM instance.
48+
49+
.INPUTS
50+
`Microsoft.Management.Infrastructure.CimInstance`
51+
52+
Accepts MSReportServer_ConfigurationSetting CIM instance via pipeline.
53+
54+
.OUTPUTS
55+
None. By default, this command does not generate any output.
56+
57+
.OUTPUTS
58+
`Microsoft.Management.Infrastructure.CimInstance`
59+
60+
When PassThru is specified, returns the MSReportServer_ConfigurationSetting
61+
CIM instance.
62+
63+
.NOTES
64+
The Reporting Services service may need to be restarted for the change
65+
to take effect.
66+
67+
.LINK
68+
https://docs.microsoft.com/en-us/sql/reporting-services/wmi-provider-library-reference/configurationsetting-method-setsecureconnectionlevel
69+
#>
70+
function Disable-SqlDscRsSecureConnection
71+
{
72+
# cSpell: ignore PBIRS
73+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the examples use pipeline input the rule cannot validate.')]
74+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
75+
[Alias('Disable-SqlDscRSTls')]
76+
[OutputType()]
77+
[OutputType([System.Object])]
78+
param
79+
(
80+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
81+
[System.Object]
82+
$Configuration,
83+
84+
[Parameter()]
85+
[System.Management.Automation.SwitchParameter]
86+
$PassThru,
87+
88+
[Parameter()]
89+
[System.Management.Automation.SwitchParameter]
90+
$Force
91+
)
92+
93+
process
94+
{
95+
if ($Force.IsPresent -and -not $Confirm)
96+
{
97+
$ConfirmPreference = 'None'
98+
}
99+
100+
$instanceName = $Configuration.InstanceName
101+
102+
Write-Verbose -Message ($script:localizedData.Disable_SqlDscRsSecureConnection_Disabling -f $instanceName)
103+
104+
$descriptionMessage = $script:localizedData.Disable_SqlDscRsSecureConnection_ShouldProcessDescription -f $instanceName
105+
$confirmationMessage = $script:localizedData.Disable_SqlDscRsSecureConnection_ShouldProcessConfirmation -f $instanceName
106+
$captionMessage = $script:localizedData.Disable_SqlDscRsSecureConnection_ShouldProcessCaption
107+
108+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
109+
{
110+
$invokeRsCimMethodParameters = @{
111+
CimInstance = $Configuration
112+
MethodName = 'SetSecureConnectionLevel'
113+
Arguments = @{
114+
Level = 0
115+
}
116+
}
117+
118+
try
119+
{
120+
$null = Invoke-RsCimMethod @invokeRsCimMethodParameters
121+
}
122+
catch
123+
{
124+
$errorMessage = $script:localizedData.Disable_SqlDscRsSecureConnection_FailedToDisable -f $instanceName, $_.Exception.Message
125+
126+
$errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'DSRSSC0001' -ErrorCategory 'InvalidOperation' -TargetObject $Configuration
127+
128+
$PSCmdlet.ThrowTerminatingError($errorRecord)
129+
}
130+
}
131+
132+
if ($PassThru.IsPresent)
133+
{
134+
return $Configuration
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)