Skip to content

Commit

Permalink
SqlServerDsc: Supports authenticate using both NetBIOS and FQDN (#1575)
Browse files Browse the repository at this point in the history
- SqlServerDsc.Common
  - Connect-UncPath
    - Now support to authenticate using both NetBIOS domain and Fully Qualified
      Domain Name (FQDN) (issue #1223).
  - Connect-SQL
    - Now support to authenticate using both NetBIOS domain and Fully Qualified
      Domain Name (FQDN) (issue #1223).
  - Connect-SQLAnalysis
    - Now support to authenticate using both NetBIOS domain and Fully Qualified
      Domain Name (FQDN) (issue #1223).
- SqlWindowsFirewall
  - Now support to authenticate using both NetBIOS domain and Fully Qualified
    Domain Name (FQDN) (issue #1223).
  • Loading branch information
johlju authored Jun 28, 2020
1 parent 5924adf commit 83a8e4a
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 70 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update resource parameter documentation ([issue #1568](https://github.com/dsccommunity/SqlServerDsc/issues/1568)).
- Documentation is now published to the GitHub Wiki.
- Deploy task was updated with the correct name.
- SqlServerDsc.Common
- Connect-UncPath
- Now support to authenticate using both NetBIOS domain and Fully Qualified
Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)).
- Connect-SQL
- Now support to authenticate using both NetBIOS domain and Fully Qualified
Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)).
- Connect-SQLAnalysis
- Now support to authenticate using both NetBIOS domain and Fully Qualified
Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)).
- SqlWindowsFirewall
- Now support to authenticate using both NetBIOS domain and Fully Qualified
Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)).

## [14.0.0] - 2020-06-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Get-TargetResource

if ($SourceCredential)
{
$userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)"
$userName = $SourceCredential.UserName

Write-Verbose -Message (
$script:localizedData.ConnectUsingCredential -f $SourcePath, $userName
Expand Down Expand Up @@ -375,7 +375,7 @@ function Set-TargetResource

if ($SourceCredential)
{
$userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)"
$userName = $SourceCredential.UserName

Write-Verbose -Message (
$script:localizedData.ConnectUsingCredential -f $SourcePath, $userName
Expand Down
6 changes: 3 additions & 3 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function Connect-UncPath

if ($PSBoundParameters.ContainsKey('SourceCredential'))
{
$newSmbMappingParameters['UserName'] = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)"
$newSmbMappingParameters['UserName'] = $SourceCredential.UserName
$newSmbMappingParameters['Password'] = $SourceCredential.GetNetworkCredential().Password
}

Expand Down Expand Up @@ -527,7 +527,7 @@ function Connect-SQL
}
else
{
$connectUserName = $SetupCredential.GetNetworkCredential().UserName
$connectUserName = $SetupCredential.UserName

Write-Verbose -Message (
$script:localizedData.ConnectingUsingImpersonation -f $connectUsername, $LoginType
Expand Down Expand Up @@ -633,7 +633,7 @@ function Connect-SQLAnalysis

if ($SetupCredential)
{
$userName = $SetupCredential.GetNetworkCredential().UserName
$userName = $SetupCredential.UserName
$password = $SetupCredential.GetNetworkCredential().Password

$analysisServicesDataSource = "Data Source=$analysisServiceInstance;User ID=$userName;Password=$password"
Expand Down
127 changes: 96 additions & 31 deletions tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ try
$mockNamedInstance_IntegrationServiceName = $mockSqlIntegrationName
$mockNamedInstance_AnalysisServiceName = "$($mockSqlAnalysisName)`$$($mockNamedInstance_InstanceName)"

$mockmockSourceCredentialUserName = "COMPANY\sqladmin"
$mockmockSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force
$mockSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockmockSourceCredentialUserName, $mockmockSourceCredentialPassword)
$mockSourceCredentialUserName = "COMPANY\sqladmin"
$mockSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force
$mockSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockSourceCredentialUserName, $mockSourceCredentialPassword)

$mockFqdnSourceCredentialUserName = "sqladmin@company.local"
$mockFqdnSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force
$mockFqdnSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockFqdnSourceCredentialUserName, $mockFqdnSourceCredentialPassword)

$mockDynamicSQLEngineFirewallRulePresent = $true
$mockDynamicSQLBrowserFirewallRulePresent = $true
Expand Down Expand Up @@ -525,7 +529,7 @@ try
}

Context "When SQL Server version is $mockCurrentSqlMajorVersion and there are no components installed" {
BeforeEach {
BeforeAll {
$testParameters = $mockDefaultParameters.Clone()
$testParameters += @{
InstanceName = $mockCurrentInstanceName
Expand Down Expand Up @@ -559,19 +563,50 @@ try
$result.Features | Should -BeNullOrEmpty
}

It 'Should call the correct functions exact number of times' {
$result = Get-TargetResource @testParameters
Assert-MockCalled -CommandName New-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It
Context 'When authenticating using NetBIOS domain' {
It 'Should call the correct functions exact number of times' {
$result = Get-TargetResource @testParameters

Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It

Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter {
$UserName -eq $mockSourceCredentialUserName
} -Exactly -Times 1 -Scope It
}
}

Context 'When authenticating using Fully Qualified Domain Name (FQDN)' {
BeforeAll {
$testParameters['SourceCredential'] = $mockFqdnSourceCredential
}

It 'Should call the correct functions exact number of times' {
$result = Get-TargetResource @testParameters

Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It

Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter {
$UserName -eq $mockFqdnSourceCredentialUserName
} -Exactly -Times 1 -Scope It
}
}
}

Expand Down Expand Up @@ -1054,7 +1089,7 @@ try
Mock -CommandName Test-TargetResource -MockWith { return $false }

Context "When SQL Server version is $mockCurrentSqlMajorVersion and there are no components installed" {
BeforeEach {
BeforeAll {
$testParameters = $mockDefaultParameters.Clone()
$testParameters += @{
InstanceName = $mockCurrentInstanceName
Expand All @@ -1069,20 +1104,50 @@ try
Mock -CommandName Set-NetFirewallRule -Verifiable
}

It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' {
{ Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet
Context 'When authenticating using NetBIOS domain' {
It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' {
{ Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet

Assert-MockCalled -CommandName New-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It

Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter {
$UserName -eq $mockSourceCredentialUserName
} -Exactly -Times 1 -Scope It
}
}

Context 'When authenticating using Fully Qualified Domain Name (FQDN)' {
BeforeAll {
$testParameters['SourceCredential'] = $mockFqdnSourceCredential
}

It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' {
{ Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet

Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It
Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It

Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter {
$UserName -eq $mockFqdnSourceCredentialUserName
} -Exactly -Times 1 -Scope It
}
}
}

Expand Down
Loading

0 comments on commit 83a8e4a

Please sign in to comment.