From f11e8e8f32c6406f19476a02435d0d3961f558fa Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Sun, 29 Nov 2026 13:40:42 +0700 Subject: [PATCH 1/2] fix(cassandra): Amazon Keyspaces IAM sign-in signs with the ISO 8601 timestamp Keyspaces reads --- .../KeyspacesSigV4Tests.swift | 57 +++++++++++++++++-- .../AWS/KeyspacesSigV4.swift | 28 ++++++--- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/KeyspacesSigV4Tests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/KeyspacesSigV4Tests.swift index af28f65a35..04f19222dc 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/KeyspacesSigV4Tests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/KeyspacesSigV4Tests.swift @@ -9,6 +9,8 @@ struct KeyspacesSigV4Tests { sessionToken: nil ) private let fixedDate = Date(timeIntervalSince1970: 1_440_938_160) + private let awsNonce = Data("91703fdc2ef562e19fbdab0f58e42fe5".utf8) + private let awsInstant = Date(timeIntervalSince1970: 1_591_742_511) @Test("The initial SASL response is SigV4 followed by two null bytes") func initialResponse() { @@ -41,13 +43,9 @@ struct KeyspacesSigV4Tests { let response = KeyspacesSigV4.authResponse( nonce: nonce, credentials: credentials, region: "us-east-1", now: fixedDate ) - let fields = Dictionary(uniqueKeysWithValues: response.split(separator: ",").compactMap { pair -> (String, String)? in - let parts = pair.split(separator: "=", maxSplits: 1) - guard parts.count == 2 else { return nil } - return (String(parts[0]), String(parts[1])) - }) + let fields = responseFields(response) #expect(fields["access_key"] == "AKIDEXAMPLE") - #expect(fields["amzdate"] == "20150830T123600Z") + #expect(fields["amzdate"] == "2015-08-30T12:36:00.000Z") #expect(fields["signature"]?.count == 64) #expect(fields["session_token"] == nil) } @@ -70,4 +68,51 @@ struct KeyspacesSigV4Tests { let second = KeyspacesSigV4.authResponse(nonce: nonce, credentials: credentials, region: "us-east-1", now: fixedDate) #expect(first == second) } + + @Test("Matches the response AWS's Keyspaces SigV4 plugins produce for their shared test vector") + func awsReferenceVector() { + let response = KeyspacesSigV4.authResponse( + nonce: awsNonce, credentials: awsCredentials(sessionToken: nil), region: "us-west-2", now: awsInstant + ) + #expect(response == "signature=7f3691c18a81b8ce7457699effbfae5b09b4e0714ab38c1292dbdf082c9ddd87" + + ",access_key=UserID-1,amzdate=2020-06-09T22:41:51.000Z") + } + + @Test("Appends the session token after the AWS reference signature") + func awsReferenceVectorWithSessionToken() { + let response = KeyspacesSigV4.authResponse( + nonce: awsNonce, credentials: awsCredentials(sessionToken: "sess-token-1"), region: "us-west-2", now: awsInstant + ) + #expect(response == "signature=7f3691c18a81b8ce7457699effbfae5b09b4e0714ab38c1292dbdf082c9ddd87" + + ",access_key=UserID-1,amzdate=2020-06-09T22:41:51.000Z,session_token=sess-token-1") + } + + @Test("amzdate carries milliseconds, truncated, and never rolls into the next second", arguments: zip( + [1_735_689_600.0, 1_735_689_600.0005, 1_735_689_600.001, 1_735_689_600.25, 1_735_689_600.9995, 1_767_225_599.9999], + [ + "2025-01-01T00:00:00.000Z", "2025-01-01T00:00:00.000Z", "2025-01-01T00:00:00.001Z", + "2025-01-01T00:00:00.250Z", "2025-01-01T00:00:00.999Z", "2025-12-31T23:59:59.999Z" + ] + )) + func amzDateMilliseconds(secondsSince1970: TimeInterval, expected: String) { + let response = KeyspacesSigV4.authResponse( + nonce: awsNonce, + credentials: awsCredentials(sessionToken: nil), + region: "us-west-2", + now: Date(timeIntervalSince1970: secondsSince1970) + ) + #expect(responseFields(response)["amzdate"] == expected) + } + + private func awsCredentials(sessionToken: String?) -> AWSCredentials { + AWSCredentials(accessKeyId: "UserID-1", secretAccessKey: "UserSecretKey-1", sessionToken: sessionToken) + } + + private func responseFields(_ response: String) -> [String: String] { + Dictionary(uniqueKeysWithValues: response.split(separator: ",").compactMap { pair -> (String, String)? in + let parts = pair.split(separator: "=", maxSplits: 1) + guard parts.count == 2 else { return nil } + return (String(parts[0]), String(parts[1])) + }) + } } diff --git a/apple/Plugins/TableProPluginKit/AWS/KeyspacesSigV4.swift b/apple/Plugins/TableProPluginKit/AWS/KeyspacesSigV4.swift index 7eff45afa6..f5b0a564a5 100644 --- a/apple/Plugins/TableProPluginKit/AWS/KeyspacesSigV4.swift +++ b/apple/Plugins/TableProPluginKit/AWS/KeyspacesSigV4.swift @@ -22,13 +22,7 @@ public enum KeyspacesSigV4 { region: String, now: Date = Date() ) -> String { - let formatter = DateFormatter() - formatter.locale = Locale(identifier: "en_US_POSIX") - formatter.timeZone = TimeZone(identifier: "UTC") - formatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'" - let amzDate = formatter.string(from: now) - formatter.dateFormat = "yyyyMMdd" - let dateStamp = formatter.string(from: now) + let (amzDate, dateStamp) = requestTimestamps(at: now) let scope = "\(dateStamp)/\(region)/\(service)/aws4_request" let credential = "\(credentials.accessKeyId)/\(scope)" @@ -69,4 +63,24 @@ public enum KeyspacesSigV4 { } return response } + + // Keyspaces reads amzdate as an ISO 8601 instant with milliseconds, as AWS's own plugins send it, + // and answers the compact SigV4 form with a server error. + private static func requestTimestamps(at date: Date) -> (amzDate: String, dateStamp: String) { + // Rounding to microseconds first keeps a Double just under a millisecond from losing it. + let microseconds = Int64((date.timeIntervalSince1970 * 1_000_000).rounded()) + let milliseconds = microseconds / 1_000 + let wholeSecond = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1_000)) + let fraction = String(milliseconds % 1_000) + + let formatter = DateFormatter() + formatter.locale = Locale(identifier: "en_US_POSIX") + formatter.calendar = Calendar(identifier: .gregorian) + formatter.timeZone = TimeZone(identifier: "UTC") + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" + let amzDate = formatter.string(from: wholeSecond) + + "." + String(repeating: "0", count: 3 - fraction.count) + fraction + "Z" + formatter.dateFormat = "yyyyMMdd" + return (amzDate, formatter.string(from: wholeSecond)) + } } From 9dc43bf02da27286bfab7c30079267dfb37c48de Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Sun, 29 Nov 2026 13:59:02 +0700 Subject: [PATCH 2/2] docs(changelog): note the Amazon Keyspaces IAM sign-in fix --- apple/changelog.d/4712.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 apple/changelog.d/4712.fixed.md diff --git a/apple/changelog.d/4712.fixed.md b/apple/changelog.d/4712.fixed.md new file mode 100644 index 0000000000..6be898566c --- /dev/null +++ b/apple/changelog.d/4712.fixed.md @@ -0,0 +1 @@ +Signing in to Amazon Keyspaces with AWS IAM failed with "Internal Server Error".