Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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]))
})
}
}
28 changes: 21 additions & 7 deletions apple/Plugins/TableProPluginKit/AWS/KeyspacesSigV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -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))
}
}
1 change: 1 addition & 0 deletions apple/changelog.d/4712.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Signing in to Amazon Keyspaces with AWS IAM failed with "Internal Server Error".
Loading