Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion Sources/xcodeinstall/API/Authentication+Hashcash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 14 additions & 5 deletions Sources/xcodeinstall/API/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
55 changes: 52 additions & 3 deletions Sources/xcodeinstall/API/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Int> = [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)
}
}
}
23 changes: 23 additions & 0 deletions Tests/xcodeinstallTests/API/AuthenticationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions Tests/xcodeinstallTests/API/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test really sleeps 300 ms plus 600 ms. Could the delay be injectable, so the test runs instantly?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 97e7799: retryDelay is injectable and tests set it to zero.

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)
}
}
Loading