diff --git a/Package.resolved b/Package.resolved index c308c35..69686db 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "18299c90f6f7c8e213a41f48c82275419484a05ec8b82bf27f1870c5b2f765bb", + "originHash" : "039cc2d9ea63f69b36ac7d088f67ebee209f15db47157fcceb430ae49c0e332d", "pins" : [ { "identity" : "async-http-client", @@ -51,8 +51,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/onevcat/Rainbow", "state" : { - "revision" : "cdf146ae671b2624917648b61c908d1244b98ca1", - "version" : "4.2.1" + "revision" : "711ca908ca19d16e577d575c8d0a48151a16bb1b", + "version" : "4.2.2" } }, { @@ -69,8 +69,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/soto-project/soto-core.git", "state" : { - "revision" : "47454bf79649691da406978ae803b51b54246d6e", - "version" : "7.14.0" + "revision" : "4309f0c0f608b1f4db1d84e5928a925a3894ee91", + "version" : "7.17.0" } }, { @@ -186,8 +186,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-log.git", "state" : { - "revision" : "c7413d967b97aae67c46452bf7645bdb7b200e8e", - "version" : "1.17.2" + "revision" : "34b131f794c303b62c38d19263bcf146e538da75", + "version" : "1.17.1" } }, { diff --git a/Package.swift b/Package.swift index 4b83463..64976fb 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( .executable(name: "xcodeinstall", targets: ["xcodeinstall"]) ], dependencies: [ - .package(url: "https://github.com/apple/swift-log.git", from: "1.17.2"), + .package(url: "https://github.com/apple/swift-log.git", from: "1.17.1"), .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.8.2"), .package(url: "https://github.com/apple/swift-crypto", from: "4.5.2"), .package(url: "https://github.com/apple/swift-system", from: "1.9.0"), diff --git a/Sources/xcodeinstall/API/Authentication+Hashcash.swift b/Sources/xcodeinstall/API/Authentication+Hashcash.swift index 99b5d00..0d2fa7a 100644 --- a/Sources/xcodeinstall/API/Authentication+Hashcash.swift +++ b/Sources/xcodeinstall/API/Authentication+Hashcash.swift @@ -55,7 +55,8 @@ extension AppleAuthenticator { "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=\(itServiceKey)" let (_, response) = try await apiCall( url: url, - validResponse: .value(200) + validResponse: .value(200), + retryTransientErrors: true ) guard let hcString = response.allHeaderFields["X-Apple-HC-Bits"] as? String, diff --git a/Sources/xcodeinstall/API/Authentication.swift b/Sources/xcodeinstall/API/Authentication.swift index 55716b3..c00b520 100644 --- a/Sources/xcodeinstall/API/Authentication.swift +++ b/Sources/xcodeinstall/API/Authentication.swift @@ -345,14 +345,22 @@ class AppleAuthenticator: HTTPClient, AppleAuthenticatorProtocol { let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) defer { session.finishTasksAndInvalidate() } + return try await signoutLocation(using: session) + } + + // Send the GET /logout request on `session` and return the Location header. + // The request is read-only, so it goes through the same retry policy as the + // other authentication calls. Split from `signoutRedirectLocation` so tests + // can drive it with a mocked session. + internal func signoutLocation(using session: URLSessionProtocol) async throws -> String? { + var request = URLRequest(url: URL(string: "https://appstoreconnect.apple.com/logout")!) request.httpMethod = HTTPVerb.GET.rawValue - let (_, response) = try await session.data(for: request) - guard let httpResponse = response as? HTTPURLResponse else { - return nil + let (_, response) = try await retryPolicy.send(log: log) { + try await session.data(for: request, delegate: nil) } - return httpResponse.value(forHTTPHeaderField: "Location") + return (response as? HTTPURLResponse)?.value(forHTTPHeaderField: "Location") } internal func fetchServiceKeyFromOlympus() async throws -> AppleServiceKey { @@ -369,7 +377,8 @@ class AppleAuthenticator: HTTPClient, AppleAuthenticatorProtocol { "https://appstoreconnect.apple.com/olympus/v1/app/config?hostname=itunesconnect.apple.com" let (data, _) = try await apiCall( url: url, - validResponse: .range(200..<400) //FIXME: should this be .value(200) ? + validResponse: .range(200..<400), //FIXME: should this be .value(200) ? + retryTransientErrors: true ) return try JSONDecoder().decode(AppleServiceKey.self, from: data) diff --git a/Sources/xcodeinstall/API/HTTPClient.swift b/Sources/xcodeinstall/API/HTTPClient.swift index 28e0baa..552979b 100644 --- a/Sources/xcodeinstall/API/HTTPClient.swift +++ b/Sources/xcodeinstall/API/HTTPClient.swift @@ -69,6 +69,9 @@ class HTTPClient { // some ID returned by Apple API to authenticate us var session = AppleSession() + // how read-only requests are repeated when Apple's gateway fails + var retryPolicy = TransientRetryPolicy() + // to be shared between apiCall and download methods // prepare headers with correct cookies and X- value for Apple authentication func prepareAuthenticationHeaders() async -> [String: String] { @@ -124,7 +127,8 @@ class HTTPClient { method: HTTPVerb = .GET, body: Data? = nil, headers: [String: String] = [:], - validResponse: ExpectedResponseCode = .value(0) + validResponse: ExpectedResponseCode = .value(0), + retryTransientErrors: Bool = false ) async throws -> (Data, HTTPURLResponse) { let request: URLRequest @@ -145,8 +149,16 @@ class HTTPClient { _log(request: request, to: log) - // send request with that session - let (data, response) = try await self.urlSession.data(for: request, delegate: nil) + // send request with that session. + // Apple's sign in servers answer 502 / 503 / 504 for a share of otherwise + // valid requests. Callers whose request is safe to repeat (read-only GETs) + // opt in to a few retries; sign in POSTs never do. + let (data, response) = try await retryPolicy.send( + maxAttempts: retryTransientErrors ? retryPolicy.maxAttempts : 1, + log: log + ) { + try await self.urlSession.data(for: request, delegate: nil) + } guard let httpResponse = response as? HTTPURLResponse, validResponse.isValid(response: httpResponse.statusCode) else { @@ -205,3 +217,40 @@ internal func buildURLRequest( return request } + +// Apple's sign in servers answer 502, 503 or 504 for a share of otherwise valid +// requests. This policy repeats a request while one of those comes back, waiting +// a little longer before each new attempt. Only requests that are safe to +// repeat (read-only GETs) should use it. +struct TransientRetryPolicy: Sendable { + // total number of attempts, the first one included + var maxAttempts = 5 + // pause before the second attempt; each later pause adds one more of these + var retryDelay: Duration = .milliseconds(300) + // gateway status codes worth another attempt + var transientStatusCodes: Set = [502, 503, 504] + + // Run `request` up to `maxAttempts` times (this policy's own limit when the + // caller passes none) and return the first answer that is not transient. + func send( + maxAttempts: Int? = nil, + log: Logger, + _ request: () async throws -> (Data, URLResponse) + ) async throws -> (Data, URLResponse) { + let limit = maxAttempts ?? self.maxAttempts + var attempt = 1 + while true { + let (data, response) = try await request() + if let http = response as? HTTPURLResponse, + transientStatusCodes.contains(http.statusCode), + attempt < limit + { + log.debug("Transient HTTP \(http.statusCode), retrying (attempt \(attempt) of \(limit))") + try await Task.sleep(for: retryDelay * attempt) + attempt += 1 + continue + } + return (data, response) + } + } +} diff --git a/Tests/xcodeinstallTests/API/AuthenticationTests.swift b/Tests/xcodeinstallTests/API/AuthenticationTests.swift index f577c60..25ed00f 100644 --- a/Tests/xcodeinstallTests/API/AuthenticationTests.swift +++ b/Tests/xcodeinstallTests/API/AuthenticationTests.swift @@ -76,6 +76,29 @@ extension AuthenticationTests { #expect(serviceKey.authServiceUrl == "https://idmsa.apple.com/appleauth") } + @Test("Test sign out redirect request retries transient gateway errors") + func testSignoutLocationRetriesTransientErrors() async throws { + let authenticator = getAppleAuthenticator() + authenticator.retryPolicy.retryDelay = .zero + let url = URL(string: "https://appstoreconnect.apple.com/logout")! + let location = "https://idmsa.apple.com/appleauth/signout?widgetKey=key&asop=destroy-session" + + self.sessionData.enqueueResponse( + data: Data(), + response: HTTPURLResponse(url: url, statusCode: 504, httpVersion: nil, headerFields: nil)! + ) + self.sessionData.enqueueResponse( + data: Data(), + response: HTTPURLResponse( + url: url, statusCode: 302, httpVersion: nil, headerFields: ["Location": location])! + ) + + let result = try await authenticator.signoutLocation(using: self.sessionData) + + #expect(result == location) + #expect(self.sessionData.allRequests.count == 2) + } + @Test("Test Apple Service Key Retrieval (olympus fallback)") func testAppleServiceKey() async throws { let url = "https://dummy" diff --git a/Tests/xcodeinstallTests/API/HTTPClientTests.swift b/Tests/xcodeinstallTests/API/HTTPClientTests.swift index 2ea8770..24e48ef 100644 --- a/Tests/xcodeinstallTests/API/HTTPClientTests.swift +++ b/Tests/xcodeinstallTests/API/HTTPClientTests.swift @@ -29,6 +29,8 @@ struct HTTPClientTests { self.sessionData = env.urlSessionData as? MockedURLSession self.sessionDownload = env.urlSessionDownload as? MockedURLSession self.client = HTTPClient(secrets: env.secrets!, urlSession: env.urlSessionData, log: log) + // no real pause between attempts in tests + self.client.retryPolicy.retryDelay = .zero try await env.secrets!.clearSecrets() } @@ -121,4 +123,41 @@ extension HTTPClientTests { // Then #expect(self.sessionData.lastURL?.debugDescription == url) } + + // MARK: - Transient gateway errors + + private func response(_ code: Int) -> HTTPURLResponse { + HTTPURLResponse( + url: URL(string: "https://idmsa.apple.com")!, statusCode: code, httpVersion: nil, headerFields: nil)! + } + + @Test("apiCall retries 504 answers when asked to") + func apiCallRetriesTransientErrors() async throws { + sessionData.enqueueResponse(data: Data(), response: response(504)) + sessionData.enqueueResponse(data: Data(), response: response(502)) + sessionData.enqueueResponse(data: Data(), response: response(200)) + + let (_, http) = try await client.apiCall( + url: "https://idmsa.apple.com/appleauth/auth/signin", + validResponse: .value(200), + retryTransientErrors: true + ) + + #expect(http.statusCode == 200) + #expect(sessionData.allRequests.count == 3) + } + + @Test("apiCall does not retry unless asked to") + func apiCallDoesNotRetryByDefault() async throws { + sessionData.enqueueResponse(data: Data(), response: response(504)) + sessionData.enqueueResponse(data: Data(), response: response(200)) + + await #expect(throws: URLError.self) { + _ = try await client.apiCall( + url: "https://idmsa.apple.com/appleauth/auth/signin", + validResponse: .value(200) + ) + } + #expect(sessionData.allRequests.count == 1) + } }