Skip to content

Commit c0d94c0

Browse files
committed
CI: retry and then fail the Windows probe's endpoint check on network errors
A timeout means the probe did not see the certificate rejected, so it cannot claim the runner is safe. Retry once, then fail instead of reporting INCONCLUSIVE.
1 parent cc2dc81 commit c0d94c0

1 file changed

Lines changed: 42 additions & 31 deletions

File tree

‎ci/tools/runner_trust_probe.ps1‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,48 +72,59 @@ if ($usable.Count -eq 0) {
7272
$GatingFailures++
7373
}
7474

75-
# 3. OS-store client (Invoke-WebRequest) rejects each test endpoint (only a successful connection fails the job).
75+
# 3. OS-store client (Invoke-WebRequest) rejects each test endpoint (a successful connection, or no verdict after a retry, fails the job).
7676
# -SkipHttpErrorCheck is PowerShell 7+ only, so HTTP error statuses are caught below instead:
7777
# any response at all means the TLS handshake (and thus the certificate chain) succeeded.
7878
$progTmp = $ProgressPreference
7979
$ProgressPreference = 'SilentlyContinue'
8080
try {
8181
foreach ($endpoint in $TestEndpoints) {
8282
$checkName = "Invoke-WebRequest rejects ``$endpoint``"
83-
try {
84-
$res = Invoke-WebRequest -Uri $endpoint -UseBasicParsing -TimeoutSec 15
85-
Record-Check $checkName 'FAIL' "Connection succeeded with HTTP $($res.StatusCode) (certificate was trusted)"
86-
$GatingFailures++
87-
} catch {
88-
if ($null -ne $_.Exception.Response) {
89-
Record-Check $checkName 'FAIL' "Connection succeeded with HTTP $([int]$_.Exception.Response.StatusCode) (certificate was trusted)"
83+
# Retry non-certificate/network errors once; if the second attempt also fails,
84+
# the probe could not show that the certificate is rejected, so the check fails.
85+
for ($attempt = 1; $attempt -le 2; $attempt++) {
86+
try {
87+
$res = Invoke-WebRequest -Uri $endpoint -UseBasicParsing -TimeoutSec 15
88+
Record-Check $checkName 'FAIL' "Connection succeeded with HTTP $($res.StatusCode) (certificate was trusted)"
9089
$GatingFailures++
91-
continue
92-
}
93-
$certRejected = $false
94-
$hostnameMismatchOnly = $false
95-
$ex = $_.Exception
96-
while ($null -ne $ex) {
97-
if (
98-
($ex -is [System.Security.Authentication.AuthenticationException] -and $ex.Message -match 'certificate chain|UntrustedRoot|RemoteCertificateChainErrors') -or
99-
($ex -is [System.Net.WebException] -and $ex.Status -eq [System.Net.WebExceptionStatus]::TrustFailure)
100-
) {
101-
$certRejected = $true
90+
break
91+
} catch {
92+
if ($null -ne $_.Exception.Response) {
93+
Record-Check $checkName 'FAIL' "Connection succeeded with HTTP $([int]$_.Exception.Response.StatusCode) (certificate was trusted)"
94+
$GatingFailures++
10295
break
10396
}
104-
if ($ex -is [System.Security.Authentication.AuthenticationException] -and $ex.Message -match 'RemoteCertificateNameMismatch') {
105-
$hostnameMismatchOnly = $true
97+
$certRejected = $false
98+
$hostnameMismatchOnly = $false
99+
$ex = $_.Exception
100+
while ($null -ne $ex) {
101+
if (
102+
($ex -is [System.Security.Authentication.AuthenticationException] -and $ex.Message -match 'certificate chain|UntrustedRoot|RemoteCertificateChainErrors') -or
103+
($ex -is [System.Net.WebException] -and $ex.Status -eq [System.Net.WebExceptionStatus]::TrustFailure)
104+
) {
105+
$certRejected = $true
106+
break
107+
}
108+
if ($ex -is [System.Security.Authentication.AuthenticationException] -and $ex.Message -match 'RemoteCertificateNameMismatch') {
109+
$hostnameMismatchOnly = $true
110+
}
111+
$ex = $ex.InnerException
112+
}
113+
$cleanMsg = ($_.Exception.Message -replace '[\r\n]+', ' ' -replace '\|', '\|')
114+
if ($certRejected) {
115+
Record-Check $checkName 'PASS' "Rejected with certificate validation error ($cleanMsg)"
116+
break
117+
} elseif ($hostnameMismatchOnly) {
118+
Record-Check $checkName 'FAIL' "Certificate chain was trusted; failed only hostname check ($cleanMsg)"
119+
$GatingFailures++
120+
break
121+
} elseif ($attempt -lt 2) {
122+
Write-Output "[INFO] ${checkName}: attempt $attempt failed with a non-certificate or network error ($cleanMsg); retrying"
123+
Start-Sleep -Seconds 5
124+
} else {
125+
Record-Check $checkName 'FAIL' "Could not determine whether the certificate is rejected after $attempt attempts: non-certificate or network error ($cleanMsg)"
126+
$GatingFailures++
106127
}
107-
$ex = $ex.InnerException
108-
}
109-
$cleanMsg = ($_.Exception.Message -replace '[\r\n]+', ' ' -replace '\|', '\|')
110-
if ($certRejected) {
111-
Record-Check $checkName 'PASS' "Rejected with certificate validation error ($cleanMsg)"
112-
} elseif ($hostnameMismatchOnly) {
113-
Record-Check $checkName 'FAIL' "Certificate chain was trusted; failed only hostname check ($cleanMsg)"
114-
$GatingFailures++
115-
} else {
116-
Record-Check $checkName 'INCONCLUSIVE' "Non-certificate or network error ($cleanMsg)"
117128
}
118129
}
119130
}

0 commit comments

Comments
 (0)