diff --git a/CHANGELOG.md b/CHANGELOG.md index ef354809c..38ab6014a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Removed + +- SqlServerDsc + - Removed `Assert-ElevatedUser` from private functions - [Issue #1797](https://github.com/dsccommunity/SqlServerDsc/issues/1797) + - `Assert-ElevatedUser` added to _DscResource.Common_ public functions - [Issue #82](https://github.com/dsccommunity/DscResource.Common/issues/82) + ### Added - SqlServerDsc diff --git a/source/Private/Assert-ElevatedUser.ps1 b/source/Private/Assert-ElevatedUser.ps1 deleted file mode 100644 index 396cc74b6..000000000 --- a/source/Private/Assert-ElevatedUser.ps1 +++ /dev/null @@ -1,45 +0,0 @@ -<# - .SYNOPSIS - Assert that the user has elevated the PowerShell session. - - .DESCRIPTION - Assert that the user has elevated the PowerShell session. - - .EXAMPLE - Assert-ElevatedUser - - Throws an exception if the user has not elevated the PowerShell session. - - .OUTPUTS - None. -#> -function Assert-ElevatedUser -{ - [CmdletBinding()] - param () - - $isElevated = $false - - if ($IsMacOS -or $IsLinux) - { - $isElevated = (id -u) -eq 0 - } - else - { - [Security.Principal.WindowsPrincipal] $user = [Security.Principal.WindowsIdentity]::GetCurrent() - - $isElevated = $user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) - } - - if (-not $isElevated) - { - $PSCmdlet.ThrowTerminatingError( - [System.Management.Automation.ErrorRecord]::new( - $script:localizedData.IsElevated_UserNotElevated, - 'AEU0001', - [System.Management.Automation.ErrorCategory]::InvalidOperation, - 'Command parameters' - ) - ) - } -} diff --git a/source/en-US/SqlServerDsc.strings.psd1 b/source/en-US/SqlServerDsc.strings.psd1 index 95702bf56..d5f11b31e 100644 --- a/source/en-US/SqlServerDsc.strings.psd1 +++ b/source/en-US/SqlServerDsc.strings.psd1 @@ -95,9 +95,6 @@ ConvertFrom-StringData @' RequiredCommandParameter_SpecificParametersMustAllBeSet = The parameters '{0}' must all be specified. RequiredCommandParameter_SpecificParametersMustAllBeSetWhenParameterExist = The parameters '{0}' must all be specified if either parameter '{1}' is specified. - ## Assert-IsElevated - IsElevated_UserNotElevated = This command must run in an elevated PowerShell session. - ## Assert-SetupActionProperties InstallSqlServerProperties_ASServerModeInvalidValue = The value for ASServerMode is not valid for the setup action {0}. InstallSqlServerProperties_RsInstallModeInvalidValue = The only valid value for RsInstallMode is 'FilesOnlyMode' when using setup action {0}. diff --git a/tests/Unit/Private/Assert-ElevatedUser.Tests.ps1 b/tests/Unit/Private/Assert-ElevatedUser.Tests.ps1 deleted file mode 100644 index 41ac2b0d5..000000000 --- a/tests/Unit/Private/Assert-ElevatedUser.Tests.ps1 +++ /dev/null @@ -1,114 +0,0 @@ -[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] -param () - -BeforeDiscovery { - try - { - if (-not (Get-Module -Name 'DscResource.Test')) - { - # Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. - if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) - { - # Redirect all streams to $null, except the error stream (stream 2) - & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null - } - - # If the dependencies has not been resolved, this will throw an error. - Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' - } - } - catch [System.IO.FileNotFoundException] - { - throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' - } -} - -BeforeAll { - $script:dscModuleName = 'SqlServerDsc' - - Import-Module -Name $script:dscModuleName - - $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName - $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName - $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName -} - -AfterAll { - $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') - $PSDefaultParameterValues.Remove('Mock:ModuleName') - $PSDefaultParameterValues.Remove('Should:ModuleName') - - # Unload the module being tested so that it doesn't impact any other tests. - Get-Module -Name $script:dscModuleName -All | Remove-Module -Force -} - -Describe 'Assert-ElevatedUser' -Tag 'Private' { - BeforeDiscovery { - <# - Since it is not possible to elevated (or un-elevate) a user during testing - the test that cannot run need to be skipped. - #> - if ($IsMacOS -or $IsLinux) - { - $mockIsElevated = (id -u) -eq 0 - } - else - { - [Security.Principal.WindowsPrincipal] $mockCurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent() - - $mockIsElevated = $mockCurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) - } - } - - It 'Should throw the correct error' -Skip:$mockIsElevated { - InModuleScope -ScriptBlock { - $mockErrorMessage = $script:localizedData.IsElevated_UserNotElevated - - { Assert-ElevatedUser } | Should -Throw -ExpectedMessage $mockErrorMessage - } - } - - It 'Should not throw an exception' -Skip:(-not $mockIsElevated) { - InModuleScope -ScriptBlock { - { Assert-ElevatedUser } | Should -Not -Throw - } - } - - Context 'When on Linux or macOS' { - BeforeAll { - $previousIsMacOS = InModuleScope -ScriptBlock { - $IsMacOS - } - - InModuleScope -ScriptBlock { - $script:IsMacOS = $true - - # Stub for command 'id'. - function id - { - throw '{0}: StubNotImplemented' -f $MyInvocation.MyCommand - } - - Mock -CommandName id -MockWith { - return 0 - } - } - } - - AfterAll { - $inModuleScopeParameters = @{ - PreviousIsMacOS = $previousIsMacOS - } - - InModuleScope -Parameters $inModuleScopeParameters -ScriptBlock { - $script:IsMacOS = $PreviousIsMacOS - } - } - - It 'Should not throw an exception' { - InModuleScope -ScriptBlock { - { Assert-ElevatedUser } | Should -Not -Throw - } - } - } -}