|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +BeforeDiscovery { |
| 4 | + if ($IsWindows) { |
| 5 | + $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() |
| 6 | + $principal = [System.Security.Principal.WindowsPrincipal]::new($identity) |
| 7 | + $isElevated = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) |
| 8 | + $sshdExists = ($null -ne (Get-Command sshd -CommandType Application -ErrorAction Ignore)) |
| 9 | + $skipTest = !$isElevated -or !$sshdExists |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +Describe 'SSHDConfig resource tests' -Skip:(!$IsWindows -or $skipTest) { |
| 14 | + BeforeAll { |
| 15 | + # set a non-default value in a temporary sshd_config file |
| 16 | + "LogLevel Debug3`nPasswordAuthentication no" | Set-Content -Path $TestDrive/test_sshd_config |
| 17 | + $filepath = Join-Path $TestDrive 'test_sshd_config' |
| 18 | + $yaml = @" |
| 19 | +`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 20 | +metadata: |
| 21 | + Microsoft.DSC: |
| 22 | + securityContext: elevated |
| 23 | +resources: |
| 24 | +- name: sshdconfig |
| 25 | + type: Microsoft.OpenSSH.SSHD/sshd_config |
| 26 | + metadata: |
| 27 | + filepath: $filepath |
| 28 | + properties: |
| 29 | +"@ |
| 30 | + } |
| 31 | + |
| 32 | + It '<command> works' -TestCases @( |
| 33 | + @{ command = 'get' } |
| 34 | + @{ command = 'export' } |
| 35 | + ) { |
| 36 | + param($command) |
| 37 | + $out = dsc config $command -i "$yaml" | ConvertFrom-Json -Depth 10 |
| 38 | + $LASTEXITCODE | Should -Be 0 |
| 39 | + if ($command -eq 'export') { |
| 40 | + $out.resources.count | Should -Be 1 |
| 41 | + $out.resources[0].properties | Should -Not -BeNullOrEmpty |
| 42 | + $out.resources[0].properties.port | Should -BeNullOrEmpty |
| 43 | + $out.resources[0].properties.passwordAuthentication | Should -Be 'no' |
| 44 | + $out.resources[0].properties._inheritedDefaults | Should -BeNullOrEmpty |
| 45 | + } else { |
| 46 | + $out.results.count | Should -Be 1 |
| 47 | + $out.results.result.actualState | Should -Not -BeNullOrEmpty |
| 48 | + $out.results.result.actualState.port[0] | Should -Be 22 |
| 49 | + $out.results.result.actualState.passwordAuthentication | Should -Be 'no' |
| 50 | + $out.results.result.actualState._inheritedDefaults | Should -Contain 'port' |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + It 'Export with filter works' { |
| 55 | + $export_yaml = @" |
| 56 | +`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 57 | +metadata: |
| 58 | + Microsoft.DSC: |
| 59 | + securityContext: elevated |
| 60 | +resources: |
| 61 | +- name: sshdconfig |
| 62 | + type: Microsoft.OpenSSH.SSHD/sshd_config |
| 63 | + metadata: |
| 64 | + filepath: $filepath |
| 65 | + properties: |
| 66 | + passwordauthentication: 'yes' |
| 67 | +"@ |
| 68 | + $out = dsc config export -i "$export_yaml" | ConvertFrom-Json -Depth 10 |
| 69 | + $LASTEXITCODE | Should -Be 0 |
| 70 | + $out.resources.count | Should -Be 1 |
| 71 | + ($out.resources[0].properties.psobject.properties | Measure-Object).count | Should -Be 1 |
| 72 | + $out.resources[0].properties.passwordAuthentication | Should -Be 'no' |
| 73 | + } |
| 74 | + |
| 75 | + It '<command> with _includeDefaults specified works' -TestCases @( |
| 76 | + @{ command = 'get'; includeDefaults = $false } |
| 77 | + @{ command = 'export'; includeDefaults = $true } |
| 78 | + ) { |
| 79 | + param($command, $includeDefaults) |
| 80 | + $filepath = Join-Path $TestDrive 'test_sshd_config' |
| 81 | + $input = @" |
| 82 | +`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 83 | +metadata: |
| 84 | + Microsoft.DSC: |
| 85 | + securityContext: elevated |
| 86 | +resources: |
| 87 | +- name: sshdconfig |
| 88 | + type: Microsoft.OpenSSH.SSHD/sshd_config |
| 89 | + metadata: |
| 90 | + filepath: $filepath |
| 91 | + properties: |
| 92 | + _includeDefaults: $includeDefaults |
| 93 | +"@ |
| 94 | + $out = dsc config $command -i "$input" | ConvertFrom-Json -Depth 10 |
| 95 | + $LASTEXITCODE | Should -Be 0 |
| 96 | + if ($command -eq 'export') { |
| 97 | + $out.resources.count | Should -Be 1 |
| 98 | + $out.resources[0].properties.loglevel | Should -Be 'debug3' |
| 99 | + $out.resources[0].properties.port | Should -Be 22 |
| 100 | + $out.resources[0].properties._inheritedDefaults | Should -BeNullOrEmpty |
| 101 | + } else { |
| 102 | + $out.results.count | Should -Be 1 |
| 103 | + ($out.results.result.actualState.psobject.properties | Measure-Object).count | Should -Be 2 |
| 104 | + $out.results.result.actualState.loglevel | Should -Be 'debug3' |
| 105 | + $out.results.result.actualState._inheritedDefaults | Should -BeNullOrEmpty |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + Context 'Surface a default value that has been set in file' { |
| 110 | + BeforeAll { |
| 111 | + "Port 22" | Set-Content -Path $TestDrive/test_sshd_config |
| 112 | + } |
| 113 | + |
| 114 | + It '<command> works' -TestCases @( |
| 115 | + @{ command = 'get' } |
| 116 | + @{ command = 'export' } |
| 117 | + ) { |
| 118 | + param($command) |
| 119 | + $out = dsc config $command -i "$yaml" | ConvertFrom-Json -Depth 10 |
| 120 | + $LASTEXITCODE | Should -Be 0 |
| 121 | + if ($command -eq 'export') { |
| 122 | + $out.resources.count | Should -Be 1 |
| 123 | + $out.resources[0].properties | Should -Not -BeNullOrEmpty |
| 124 | + $out.resources[0].properties.port[0] | Should -Be 22 |
| 125 | + $out.resources[0].properties.passwordauthentication | Should -BeNullOrEmpty |
| 126 | + $out.resources[0].properties._inheritedDefaults | Should -BeNullOrEmpty |
| 127 | + } else { |
| 128 | + $out.results.count | Should -Be 1 |
| 129 | + $out.results.result.actualState | Should -Not -BeNullOrEmpty |
| 130 | + $out.results.result.actualState.port | Should -Be 22 |
| 131 | + $out.results.result.actualState.passwordAuthentication | Should -Be 'yes' |
| 132 | + $out.results.result.actualState._inheritedDefaults | Should -Not -Contain 'port' |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments