github.com/cloudfoundry-incubator/windows-utilities-tests@v0.11.1-0.20230315194243-a2ce46b74d8a/assets/wuts-release/jobs/check_ssh/templates/pre-start.ps1.erb (about) 1 $SSHDir = "C:\Program Files\OpenSSH" 2 $Expected = [bool]$<%= p("check_ssh.expected") %> 3 Write-Host "Running Test..." 4 5 $CheckSshState = [PSCustomObject]@{ 6 Enabled = "Enabled" 7 Disabled = "Disabled" 8 Pending = "Pending" 9 Error = "Error" 10 } 11 12 function New-CheckSshState { 13 param($Expected) 14 return $(if ($Expected) {$CheckSshState.Enabled} Else {$CheckSshState.Disabled}) 15 } 16 17 function SSH-Enabled { 18 # Check services 19 20 $agent = Get-Service | Where { $_.Name -eq 'ssh-agent' } 21 if ($agent -eq $null) { 22 Write-Error "ssh-agent service not installed" 23 return $CheckSshState.Error 24 } 25 26 $daemon = Get-Service | Where { $_.Name -eq 'sshd' } 27 if ($daemon -eq $null) { 28 Write-Error "sshd service not installed" 29 return $CheckSshState.Error 30 } 31 # Can't check LGPO - only verify that script ran without error 32 33 # Check firewall 34 $firewallSet = ((Get-NetFirewallRule | where { $_.DisplayName -eq 'SSH' }) -Ne $null) 35 36 Write-Host "Firewall Set: ${firewallSet}" 37 Write-Host "Agent Status: $($agent.Status.ToString())" 38 Write-Host "Daemon Status: $($daemon.Status.ToString())" 39 Write-Host "Agent StartType: $($agent.StartType.ToString())" 40 Write-Host "Daemon StartType: $($daemon.StartType.ToString())" 41 42 If ( 43 $firewallSet -And 44 ($agent.Status -Eq "Running") -And 45 ($daemon.Status -Eq "Running") -And 46 ($agent.StartType -Eq "Automatic") -And 47 ($daemon.StartType -Eq "Automatic") 48 ) { 49 return $CheckSshState.Enabled 50 } 51 52 If ( 53 !$firewallSet -And 54 ($agent.Status -Eq "Stopped") -And 55 ($daemon.Status -Eq "Stopped") -And 56 ($agent.StartType -Eq "Disabled") -And 57 ($daemon.StartType -Eq "Disabled") 58 ) { 59 return $CheckSshState.Disabled 60 } 61 62 # This can happen if we catch 'enable_ssh' in the middle of enabling SSH. 63 $msg = @" 64 Invalid configuration: 65 firewallSet: $($firewallSet) 66 agent.Status: $($agent.Status) 67 daemon.Status: $($daemon.Status) 68 agent.StartType: $($agent.StartType) 69 daemon.StartType: $($daemon.StartType) 70 "@ 71 Write-Host $msg 72 return $CheckSshState.Pending 73 } 74 75 Write-Host "Starting: $(Get-Date -Format g)" 76 Write-Host "Expected: ${Expected}" 77 78 if ($(SSH-Enabled) -eq $CheckSshState.Error) { 79 Write-Host "Error, exiting." 80 Exit 1 81 } 82 83 $ExpectedSshEnabledState = New-CheckSshState $Expected 84 for ($i=0; $i -lt 20 -and ($(SSH-Enabled) -eq $CheckSshState.Pending -or $(SSH-Enabled) -ne $ExpectedSshEnabledState); $i++) { 85 Write-Host "Sleeping..." 86 Start-Sleep 5 87 } 88 89 Write-Host "Waking up" 90 91 $Actual = SSH-Enabled 92 Write-Host "Actual: ${Actual}" 93 Write-Host "Expected: ${ExpectedSshEnabledState}" 94 95 If ($Actual -Ne $ExpectedSshEnabledState) { 96 Write-Host "Expected SSH enabled to be $($Expected.ToString())" 97 Exit 1 98 } 99 100 function Test-Key { 101 param ( 102 $name=$1 103 ) 104 $key=Join-Path $SSHDir $name 105 echo $key 106 if (Test-Path $key) { 107 Write-Error "Private Key: '${key}' Has Not Been Removed" 108 Exit 1 109 } 110 } 111 112 # Ensure ssh private keys have been deleted 113 Test-Key 'ssh_host_dsa_key' 114 Test-Key 'ssh_host_rsa_key' 115 Test-Key 'ssh_host_ecdsa_key' 116 Test-Key 'ssh_host_ed25519_key' 117 118 Write-Host "SUCCESS" 119 Exit 0