From 86c7d2cb7dc6539d46255220861a511b515c2fc4 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 10:45:39 -0700 Subject: [PATCH 01/23] Add signer protocol. --- .../ClientRuntime/Auth/HTTPAuthAPI/Signer.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift new file mode 100644 index 000000000..226d8ebc3 --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift @@ -0,0 +1,14 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public protocol Signer { + associatedtype IdentityT: Identity + + func sign(requestBuilder: SdkHttpRequestBuilder, identity: IdentityT, signingProperties: Attributes) async throws -> SdkHttpRequestBuilder +} From 5eb2c8df6ec6af68bd1f288e4b75b37f0ed70e87 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 11:11:36 -0700 Subject: [PATCH 02/23] Add http context changes. --- .../Networking/Http/HttpContext.swift | 190 +++++++++++------- 1 file changed, 121 insertions(+), 69 deletions(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 27c78c453..28e8a2927 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -9,41 +9,49 @@ public class HttpContext: MiddlewareContext { public init(attributes: Attributes) { self.attributes = attributes } - // FIXME: Move all defined keys to separate file as constants to be used elsewhere - public func getPath() -> String { - return attributes.get(key: AttributeKey(name: "Path"))! - } - public func getMethod() -> HttpMethodType { - return attributes.get(key: AttributeKey(name: "Method"))! + public func getAuthSchemes() -> Attributes { + return attributes.get(key: AttributeKeys.authSchemes)! } - - public func getEncoder() -> RequestEncoder { - return attributes.get(key: AttributeKey(name: "Encoder"))! + + public func getDecoder() -> ResponseDecoder { + return attributes.get(key: AttributeKeys.decoder)! } - public func getDecoder() -> ResponseDecoder { - return attributes.get(key: AttributeKey(name: "Decoder"))! + public func getEncoder() -> RequestEncoder { + return attributes.get(key: AttributeKeys.encoder)! } public func getHost() -> String? { - return attributes.get(key: AttributeKey(name: "Host")) + return attributes.get(key: AttributeKeys.host) } - public func getServiceName() -> String { - return attributes.get(key: AttributeKey(name: "ServiceName"))! + public func getHostPrefix() -> String? { + return attributes.get(key: AttributeKeys.hostPrefix) } public func getIdempotencyTokenGenerator() -> IdempotencyTokenGenerator { - return attributes.get(key: AttributeKey(name: "IdempotencyTokenGenerator"))! + return attributes.get(key: AttributeKeys.idempotencyTokenGenerator)! } - public func getHostPrefix() -> String? { - return attributes.get(key: AttributeKey(name: "HostPrefix")) + public func getIdentityResolvers() -> Attributes { + return attributes.get(key: AttributeKeys.identityResolvers)! } public func getLogger() -> LogAgent? { - return attributes.get(key: AttributeKey(name: "Logger")) + return attributes.get(key: AttributeKeys.logger) + } + + public func getMessageEncoder() -> MessageEncoder? { + return attributes.get(key: AttributeKeys.messageEncoder) + } + + public func getMessageSigner() -> MessageSigner? { + return attributes.get(key: AttributeKeys.messageSigner) + } + + public func getMethod() -> HttpMethodType { + return attributes.get(key: AttributeKeys.method)! } /// The partition ID to be used for this context. @@ -51,19 +59,23 @@ public class HttpContext: MiddlewareContext { /// Requests made with the same partition ID will be grouped together for retry throttling purposes. /// If no partition ID is provided, requests will be partitioned based on the hostname. public func getPartitionID() -> String? { - return attributes.get(key: AttributeKey(name: "PartitionID")) + return attributes.get(key: AttributeKeys.partitionId) } - public func getMessageEncoder() -> MessageEncoder? { - return attributes.get(key: HttpContext.messageEncoder) + public func getPath() -> String { + return attributes.get(key: AttributeKeys.path)! } - public func getMessageSigner() -> MessageSigner? { - return attributes.get(key: HttpContext.messageSigner) + public func getSelectedAuthScheme() -> SelectedAuthScheme? { + return attributes.get(key: AttributeKeys.selectedAuthScheme) + } + + public func getServiceName() -> String { + return attributes.get(key: AttributeKeys.serviceName)! } public func isBidirectionalStreamingEnabled() -> Bool { - return attributes.get(key: HttpContext.bidirectionalStreaming) ?? false + return attributes.get(key: AttributeKeys.bidirectionalStreaming) ?? false } /// Returns `true` if the request should use `http2` and only `http2` without falling back to `http1` @@ -72,29 +84,11 @@ public class HttpContext: MiddlewareContext { } } -extension HttpContext { - public static let messageEncoder = AttributeKey(name: "MessageEncoder") - public static let messageSigner = AttributeKey(name: "MessageSigner") - public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") -} - public class HttpContextBuilder { - public init() {} public var attributes: Attributes = Attributes() - let encoder = AttributeKey(name: "Encoder") - let method = AttributeKey(name: "Method") - let path = AttributeKey(name: "Path") - let operation = AttributeKey(name: "Operation") - let host = AttributeKey(name: "Host") - let serviceName = AttributeKey(name: "ServiceName") var response: HttpResponse = HttpResponse() - let decoder = AttributeKey(name: "Decoder") - let idempotencyTokenGenerator = AttributeKey(name: "IdempotencyTokenGenerator") - let hostPrefix = AttributeKey(name: "HostPrefix") - let logger = AttributeKey(name: "Logger") - let partitionID = AttributeKey(name: "PartitionID") // We follow the convention of returning the builder object // itself from any configuration methods, and by adding the @@ -106,70 +100,78 @@ public class HttpContextBuilder { self.attributes.set(key: key, value: value) return self } - + @discardableResult - public func withEncoder(value: RequestEncoder) -> HttpContextBuilder { - self.attributes.set(key: encoder, value: value) + public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { + var authSchemes: Attributes + if self.attributes.contains(key: AttributeKeys.authSchemes) { + authSchemes = self.attributes.get(key: AttributeKeys.authSchemes)! + } else { + authSchemes = Attributes() + } + authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) + self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) return self } @discardableResult - public func withMethod(value: HttpMethodType) -> HttpContextBuilder { - self.attributes.set(key: method, value: value) + public func withDecoder(value: ResponseDecoder) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.decoder, value: value) return self } @discardableResult - public func withPath(value: String) -> HttpContextBuilder { - self.attributes.set(key: path, value: value) + public func withEncoder(value: RequestEncoder) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.encoder, value: value) return self } @discardableResult public func withHost(value: String) -> HttpContextBuilder { - self.attributes.set(key: host, value: value) + self.attributes.set(key: AttributeKeys.host, value: value) return self } @discardableResult public func withHostPrefix(value: String) -> HttpContextBuilder { - self.attributes.set(key: hostPrefix, value: value) + self.attributes.set(key: AttributeKeys.hostPrefix, value: value) return self } @discardableResult - public func withOperation(value: String) -> HttpContextBuilder { - self.attributes.set(key: operation, value: value) - return self - } - - @discardableResult - public func withServiceName(value: String) -> HttpContextBuilder { - self.attributes.set(key: serviceName, value: value) + public func withIdempotencyTokenGenerator(value: IdempotencyTokenGenerator) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.idempotencyTokenGenerator, value: value) return self } - + @discardableResult - public func withDecoder(value: ResponseDecoder) -> HttpContextBuilder { - self.attributes.set(key: decoder, value: value) + public func withIdentityResolver(value: any IdentityResolver, type: IdentityType) -> HttpContextBuilder { + var identityResolvers: Attributes + if self.attributes.contains(key: AttributeKeys.identityResolvers) { + identityResolvers = self.attributes.get(key: AttributeKeys.identityResolvers)! + } else { + identityResolvers = Attributes() + } + identityResolvers.set(key: AttributeKey(name: "\(type)"), value: value) + self.attributes.set(key: AttributeKeys.identityResolvers, value: identityResolvers) return self } @discardableResult - public func withResponse(value: HttpResponse) -> HttpContextBuilder { - self.response = value + public func withLogger(value: LogAgent) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.logger, value: value) return self } @discardableResult - public func withIdempotencyTokenGenerator(value: IdempotencyTokenGenerator) -> HttpContextBuilder { - self.attributes.set(key: idempotencyTokenGenerator, value: value) + public func withMethod(value: HttpMethodType) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.method, value: value) return self } @discardableResult - public func withLogger(value: LogAgent) -> HttpContextBuilder { - self.attributes.set(key: logger, value: value) + public func withOperation(value: String) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.operation, value: value) return self } @@ -181,7 +183,31 @@ public class HttpContextBuilder { /// - Returns: `self`, after the partition ID is set as specified. @discardableResult public func withPartitionID(value: String?) -> HttpContextBuilder { - self.attributes.set(key: partitionID, value: value) + self.attributes.set(key: AttributeKeys.partitionId, value: value) + return self + } + + @discardableResult + public func withPath(value: String) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.path, value: value) + return self + } + + @discardableResult + public func withResponse(value: HttpResponse) -> HttpContextBuilder { + self.response = value + return self + } + + @discardableResult + public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) + return self + } + + @discardableResult + public func withServiceName(value: String) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.serviceName, value: value) return self } @@ -189,3 +215,29 @@ public class HttpContextBuilder { return HttpContext(attributes: attributes) } } + +public struct AttributeKeys { + // Namespace object for key values, hence private init + private init() {} + + //public static let authSchemes = AttributeKey(name: "AuthSchemes") + public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") + public static let decoder = AttributeKey(name: "Decoder") + public static let encoder = AttributeKey(name: "Encoder") + public static let host = AttributeKey(name: "Host") + public static let hostPrefix = AttributeKey(name: "HostPrefix") + public static let idempotencyTokenGenerator = AttributeKey(name: "IdempotencyTokenGenerator") + public static let identityResolvers = AttributeKey(name: "IdentityResolvers") + public static let logger = AttributeKey(name: "Logger") + public static let messageEncoder = AttributeKey(name: "MessageEncoder") + public static let messageSigner = AttributeKey(name: "MessageSigner") + public static let method = AttributeKey(name: "Method") + public static let operation = AttributeKey(name: "Operation") + public static let partitionId = AttributeKey(name: "PartitionID") + public static let path = AttributeKey(name: "Path") + //public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") + public static let serviceName = AttributeKey(name: "ServiceName") + + // Keys for different types of identity resolvers + public static let awsIdResolver = AttributeKey(name:"\(IdentityType.aws)") +} From 75646fcf4dc7df174048d013f9d31de0fec15cfd Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 11:19:10 -0700 Subject: [PATCH 03/23] Swiftlint issues. --- .../Auth/HTTPAuthAPI/Signer.swift | 8 ++- .../Networking/Http/HttpContext.swift | 64 ++++++++++--------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift index 226d8ebc3..3a59292d3 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift @@ -9,6 +9,10 @@ import Foundation public protocol Signer { associatedtype IdentityT: Identity - - func sign(requestBuilder: SdkHttpRequestBuilder, identity: IdentityT, signingProperties: Attributes) async throws -> SdkHttpRequestBuilder + + func sign( + requestBuilder: SdkHttpRequestBuilder, + identity: IdentityT, + signingProperties: Attributes + ) async throws -> SdkHttpRequestBuilder } diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 28e8a2927..bbbfd9dfb 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -10,10 +10,10 @@ public class HttpContext: MiddlewareContext { self.attributes = attributes } - public func getAuthSchemes() -> Attributes { - return attributes.get(key: AttributeKeys.authSchemes)! - } - +// public func getAuthSchemes() -> Attributes { +// return attributes.get(key: AttributeKeys.authSchemes)! +// } + public func getDecoder() -> ResponseDecoder { return attributes.get(key: AttributeKeys.decoder)! } @@ -66,9 +66,9 @@ public class HttpContext: MiddlewareContext { return attributes.get(key: AttributeKeys.path)! } - public func getSelectedAuthScheme() -> SelectedAuthScheme? { - return attributes.get(key: AttributeKeys.selectedAuthScheme) - } +// public func getSelectedAuthScheme() -> SelectedAuthScheme? { +// return attributes.get(key: AttributeKeys.selectedAuthScheme) +// } public func getServiceName() -> String { return attributes.get(key: AttributeKeys.serviceName)! @@ -100,19 +100,19 @@ public class HttpContextBuilder { self.attributes.set(key: key, value: value) return self } - - @discardableResult - public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { - var authSchemes: Attributes - if self.attributes.contains(key: AttributeKeys.authSchemes) { - authSchemes = self.attributes.get(key: AttributeKeys.authSchemes)! - } else { - authSchemes = Attributes() - } - authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) - self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) - return self - } + +// @discardableResult +// public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { +// var authSchemes: Attributes +// if self.attributes.contains(key: AttributeKeys.authSchemes) { +// authSchemes = self.attributes.get(key: AttributeKeys.authSchemes)! +// } else { +// authSchemes = Attributes() +// } +// authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) +// self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) +// return self +// } @discardableResult public func withDecoder(value: ResponseDecoder) -> HttpContextBuilder { @@ -143,7 +143,7 @@ public class HttpContextBuilder { self.attributes.set(key: AttributeKeys.idempotencyTokenGenerator, value: value) return self } - + @discardableResult public func withIdentityResolver(value: any IdentityResolver, type: IdentityType) -> HttpContextBuilder { var identityResolvers: Attributes @@ -199,12 +199,12 @@ public class HttpContextBuilder { return self } - @discardableResult - public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { - self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) - return self - } - +// @discardableResult +// public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { +// self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) +// return self +// } + @discardableResult public func withServiceName(value: String) -> HttpContextBuilder { self.attributes.set(key: AttributeKeys.serviceName, value: value) @@ -220,13 +220,15 @@ public struct AttributeKeys { // Namespace object for key values, hence private init private init() {} - //public static let authSchemes = AttributeKey(name: "AuthSchemes") + // public static let authSchemes = AttributeKey(name: "AuthSchemes") public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") public static let decoder = AttributeKey(name: "Decoder") public static let encoder = AttributeKey(name: "Encoder") public static let host = AttributeKey(name: "Host") public static let hostPrefix = AttributeKey(name: "HostPrefix") - public static let idempotencyTokenGenerator = AttributeKey(name: "IdempotencyTokenGenerator") + public static let idempotencyTokenGenerator = AttributeKey( + name: "IdempotencyTokenGenerator" + ) public static let identityResolvers = AttributeKey(name: "IdentityResolvers") public static let logger = AttributeKey(name: "Logger") public static let messageEncoder = AttributeKey(name: "MessageEncoder") @@ -235,9 +237,9 @@ public struct AttributeKeys { public static let operation = AttributeKey(name: "Operation") public static let partitionId = AttributeKey(name: "PartitionID") public static let path = AttributeKey(name: "Path") - //public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") + // public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") public static let serviceName = AttributeKey(name: "ServiceName") - + // Keys for different types of identity resolvers public static let awsIdResolver = AttributeKey(name:"\(IdentityType.aws)") } From dd339a01e0544c7ce539f389b435a0105ed89c14 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 11:26:41 -0700 Subject: [PATCH 04/23] Fix swiftlint. --- Sources/ClientRuntime/Networking/Http/HttpContext.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index bbbfd9dfb..87a20eede 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -241,5 +241,5 @@ public struct AttributeKeys { public static let serviceName = AttributeKey(name: "ServiceName") // Keys for different types of identity resolvers - public static let awsIdResolver = AttributeKey(name:"\(IdentityType.aws)") + public static let awsIdResolver = AttributeKey(name: "\(IdentityType.aws)") } From f5808fa11e33621d5577032e419c230483fea474 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 11:33:33 -0700 Subject: [PATCH 05/23] Add identity type --- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index 6a674c7d6..47156b3ea 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -11,3 +11,8 @@ import Foundation public protocol Identity { var expiration: Date? { get } } + +// Enum of identity types supported by SDK +public enum IdentityType: CaseIterable { + case aws +} From 6371b2803d459d6652f664e3c35ff272b3375b04 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 13:15:03 -0700 Subject: [PATCH 06/23] Use nil coalescing operator to check attribute existence --- .../Networking/Http/HttpContext.swift | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 87a20eede..641ac60d9 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -9,10 +9,19 @@ public class HttpContext: MiddlewareContext { public init(attributes: Attributes) { self.attributes = attributes } + + public func toBuilder() -> HttpContextBuilder { + let builder = HttpContextBuilder() + builder.attributes = self.attributes + if let response = self.response { + builder.response = response + } + return builder + } -// public func getAuthSchemes() -> Attributes { -// return attributes.get(key: AttributeKeys.authSchemes)! -// } + public func getAuthSchemes() -> Attributes { + return attributes.get(key: AttributeKeys.authSchemes)! + } public func getDecoder() -> ResponseDecoder { return attributes.get(key: AttributeKeys.decoder)! @@ -66,9 +75,9 @@ public class HttpContext: MiddlewareContext { return attributes.get(key: AttributeKeys.path)! } -// public func getSelectedAuthScheme() -> SelectedAuthScheme? { -// return attributes.get(key: AttributeKeys.selectedAuthScheme) -// } + public func getSelectedAuthScheme() -> SelectedAuthScheme? { + return attributes.get(key: AttributeKeys.selectedAuthScheme) + } public func getServiceName() -> String { return attributes.get(key: AttributeKeys.serviceName)! @@ -101,18 +110,13 @@ public class HttpContextBuilder { return self } -// @discardableResult -// public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { -// var authSchemes: Attributes -// if self.attributes.contains(key: AttributeKeys.authSchemes) { -// authSchemes = self.attributes.get(key: AttributeKeys.authSchemes)! -// } else { -// authSchemes = Attributes() -// } -// authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) -// self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) -// return self -// } + @discardableResult + public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { + var authSchemes: Attributes = self.attributes.get(key: AttributeKeys.authSchemes) ?? Attributes() + authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) + self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) + return self + } @discardableResult public func withDecoder(value: ResponseDecoder) -> HttpContextBuilder { @@ -145,13 +149,8 @@ public class HttpContextBuilder { } @discardableResult - public func withIdentityResolver(value: any IdentityResolver, type: IdentityType) -> HttpContextBuilder { - var identityResolvers: Attributes - if self.attributes.contains(key: AttributeKeys.identityResolvers) { - identityResolvers = self.attributes.get(key: AttributeKeys.identityResolvers)! - } else { - identityResolvers = Attributes() - } + public func withIdentityResolver(value: T, type: IdentityType) -> HttpContextBuilder { + var identityResolvers: Attributes = self.attributes.get(key: AttributeKeys.identityResolvers) ?? Attributes() identityResolvers.set(key: AttributeKey(name: "\(type)"), value: value) self.attributes.set(key: AttributeKeys.identityResolvers, value: identityResolvers) return self @@ -199,11 +198,11 @@ public class HttpContextBuilder { return self } -// @discardableResult -// public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { -// self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) -// return self -// } + @discardableResult + public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) + return self + } @discardableResult public func withServiceName(value: String) -> HttpContextBuilder { @@ -220,7 +219,7 @@ public struct AttributeKeys { // Namespace object for key values, hence private init private init() {} - // public static let authSchemes = AttributeKey(name: "AuthSchemes") + public static let authSchemes = AttributeKey(name: "AuthSchemes") public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") public static let decoder = AttributeKey(name: "Decoder") public static let encoder = AttributeKey(name: "Encoder") @@ -237,7 +236,7 @@ public struct AttributeKeys { public static let operation = AttributeKey(name: "Operation") public static let partitionId = AttributeKey(name: "PartitionID") public static let path = AttributeKey(name: "Path") - // public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") + public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") public static let serviceName = AttributeKey(name: "ServiceName") // Keys for different types of identity resolvers From 9327f98621351190299cac6eed4abf1cf4d6b5c2 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 13:17:26 -0700 Subject: [PATCH 07/23] Clean up changes. --- .../Networking/Http/HttpContext.swift | 45 ------------------- 1 file changed, 45 deletions(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 87a20eede..94c471ebf 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -10,10 +10,6 @@ public class HttpContext: MiddlewareContext { self.attributes = attributes } -// public func getAuthSchemes() -> Attributes { -// return attributes.get(key: AttributeKeys.authSchemes)! -// } - public func getDecoder() -> ResponseDecoder { return attributes.get(key: AttributeKeys.decoder)! } @@ -66,10 +62,6 @@ public class HttpContext: MiddlewareContext { return attributes.get(key: AttributeKeys.path)! } -// public func getSelectedAuthScheme() -> SelectedAuthScheme? { -// return attributes.get(key: AttributeKeys.selectedAuthScheme) -// } - public func getServiceName() -> String { return attributes.get(key: AttributeKeys.serviceName)! } @@ -101,19 +93,6 @@ public class HttpContextBuilder { return self } -// @discardableResult -// public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { -// var authSchemes: Attributes -// if self.attributes.contains(key: AttributeKeys.authSchemes) { -// authSchemes = self.attributes.get(key: AttributeKeys.authSchemes)! -// } else { -// authSchemes = Attributes() -// } -// authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) -// self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) -// return self -// } - @discardableResult public func withDecoder(value: ResponseDecoder) -> HttpContextBuilder { self.attributes.set(key: AttributeKeys.decoder, value: value) @@ -144,19 +123,6 @@ public class HttpContextBuilder { return self } - @discardableResult - public func withIdentityResolver(value: any IdentityResolver, type: IdentityType) -> HttpContextBuilder { - var identityResolvers: Attributes - if self.attributes.contains(key: AttributeKeys.identityResolvers) { - identityResolvers = self.attributes.get(key: AttributeKeys.identityResolvers)! - } else { - identityResolvers = Attributes() - } - identityResolvers.set(key: AttributeKey(name: "\(type)"), value: value) - self.attributes.set(key: AttributeKeys.identityResolvers, value: identityResolvers) - return self - } - @discardableResult public func withLogger(value: LogAgent) -> HttpContextBuilder { self.attributes.set(key: AttributeKeys.logger, value: value) @@ -199,12 +165,6 @@ public class HttpContextBuilder { return self } -// @discardableResult -// public func withSelectedAuthScheme(value: SelectedAuthScheme) -> HttpContextBuilder { -// self.attributes.set(key: AttributeKeys.selectedAuthScheme, value: value) -// return self -// } - @discardableResult public func withServiceName(value: String) -> HttpContextBuilder { self.attributes.set(key: AttributeKeys.serviceName, value: value) @@ -220,7 +180,6 @@ public struct AttributeKeys { // Namespace object for key values, hence private init private init() {} - // public static let authSchemes = AttributeKey(name: "AuthSchemes") public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") public static let decoder = AttributeKey(name: "Decoder") public static let encoder = AttributeKey(name: "Encoder") @@ -237,9 +196,5 @@ public struct AttributeKeys { public static let operation = AttributeKey(name: "Operation") public static let partitionId = AttributeKey(name: "PartitionID") public static let path = AttributeKey(name: "Path") - // public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") public static let serviceName = AttributeKey(name: "ServiceName") - - // Keys for different types of identity resolvers - public static let awsIdResolver = AttributeKey(name: "\(IdentityType.aws)") } From 9b7b4c5dce607ee61991483af7e1054e5ff3d515 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 13:21:48 -0700 Subject: [PATCH 08/23] New protocols and structs. --- ...DefaultIdentityResolverConfiguration.swift | 23 +++++++++++++++++++ .../Auth/HTTPAuthAPI/AuthOption.swift | 14 +++++++++++ .../Auth/HTTPAuthAPI/AuthScheme.swift | 20 ++++++++++++++++ .../Auth/HTTPAuthAPI/AuthSchemeResolver.swift | 12 ++++++++++ .../AuthSchemeResolverParameters.swift | 12 ++++++++++ .../IdentityResolverConfiguration.swift | 12 ++++++++++ .../Auth/HTTPAuthAPI/SelectedAuthScheme.swift | 15 ++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift create mode 100644 Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift diff --git a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift new file mode 100644 index 000000000..3bfd26c8d --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift @@ -0,0 +1,23 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguration { + let credentialsProvider: (any IdentityResolver)? + + public init(configuredIdResolvers: Attributes) { + self.credentialsProvider = configuredIdResolvers.get(key: AttributeKeys.awsIdResolver) ?? nil + } + + func getIdentityResolver(identityType: IdentityType) -> (any IdentityResolver)? { + switch identityType { + case .aws: + return self.credentialsProvider + } + } +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift new file mode 100644 index 000000000..9f4022f11 --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift @@ -0,0 +1,14 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public struct AuthOption { + let schemeId: String + var identityProperties: Attributes + var signerProperties: Attributes +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift new file mode 100644 index 000000000..c72ade652 --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift @@ -0,0 +1,20 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public protocol AuthScheme { + var schemeId: String { get } + var signer: any Signer { get } + var idType: IdentityType { get } +} + +extension AuthScheme { + func identityResolver(config: IdentityResolverConfiguration) -> (any IdentityResolver)? { + return config.getIdentityResolver(identityType: self.idType) + } +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift new file mode 100644 index 000000000..1733c63aa --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift @@ -0,0 +1,12 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public protocol AuthSchemeResolver { + func resolveAuthScheme(params: AuthSchemeResolverParameters) -> Array +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift new file mode 100644 index 000000000..aa2f83b1c --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift @@ -0,0 +1,12 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public protocol AuthSchemeResolverParameters { + var operation: String { get } +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift new file mode 100644 index 000000000..08cf34271 --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift @@ -0,0 +1,12 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +protocol IdentityResolverConfiguration { + func getIdentityResolver(identityType: IdentityType) -> (any IdentityResolver)? +} diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift new file mode 100644 index 000000000..3f9433913 --- /dev/null +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift @@ -0,0 +1,15 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +public struct SelectedAuthScheme { + let schemeId: String + let identity: Identity? + let signingProperties: Attributes? + let signer: (any Signer)? +} From 39b386e1346b6f100288645f4277d457049bc931 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 13:30:53 -0700 Subject: [PATCH 09/23] Delete auth scheme related change. --- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index 47156b3ea..6a674c7d6 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -11,8 +11,3 @@ import Foundation public protocol Identity { var expiration: Date? { get } } - -// Enum of identity types supported by SDK -public enum IdentityType: CaseIterable { - case aws -} From d87e82f7ac4245789b43c8bc39286ee4e5a0f949 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 14:59:12 -0700 Subject: [PATCH 10/23] Update selected auth scheme --- Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift index 3f9433913..88d493c18 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift @@ -11,5 +11,5 @@ public struct SelectedAuthScheme { let schemeId: String let identity: Identity? let signingProperties: Attributes? - let signer: (any Signer)? + let signer: Signer? } From ad8cd2cd50c19f019ec1bd5a388774c922b9d44a Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 15:00:00 -0700 Subject: [PATCH 11/23] Change signer protocol to use generic instead of associatedtype --- Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift index 3a59292d3..9079e90aa 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift @@ -8,9 +8,7 @@ import Foundation public protocol Signer { - associatedtype IdentityT: Identity - - func sign( + func sign( requestBuilder: SdkHttpRequestBuilder, identity: IdentityT, signingProperties: Attributes From cca514b7d0fde5c4e1ec9bb2978a8b410f54eeac Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 15:30:04 -0700 Subject: [PATCH 12/23] Add back identity type and aws id resolver key --- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 4 ++++ Sources/ClientRuntime/Networking/Http/HttpContext.swift | 2 ++ 2 files changed, 6 insertions(+) diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index 6a674c7d6..d62440ba9 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -11,3 +11,7 @@ import Foundation public protocol Identity { var expiration: Date? { get } } + +public enum IdentityType: CaseIterable { + case aws +} diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 45c96cf9f..7231236f9 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -239,4 +239,6 @@ public struct AttributeKeys { public static let path = AttributeKey(name: "Path") public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") public static let serviceName = AttributeKey(name: "ServiceName") + + public static let awsIdResolver = AttributeKey(name: "AWSIDResolver") } From a05e6cdd6caf8cd95615fba27c9729cd195a7651 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 4 Oct 2023 19:00:10 -0700 Subject: [PATCH 13/23] Rename field. --- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift index 9f4022f11..fdbdc8ae2 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift @@ -10,5 +10,5 @@ import Foundation public struct AuthOption { let schemeId: String var identityProperties: Attributes - var signerProperties: Attributes + var signingProperties: Attributes } From 1bd5ac582de0b29c92b157388a776bf2e046938e Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Thu, 5 Oct 2023 14:47:27 -0700 Subject: [PATCH 14/23] Change attribute keys namespace from struct to enum. --- Sources/ClientRuntime/Networking/Http/HttpContext.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 94c471ebf..4542a3eb8 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -176,10 +176,7 @@ public class HttpContextBuilder { } } -public struct AttributeKeys { - // Namespace object for key values, hence private init - private init() {} - +public enum AttributeKeys { public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") public static let decoder = AttributeKey(name: "Decoder") public static let encoder = AttributeKey(name: "Encoder") From 1f7d2acbabc4cd10a575b20446ed31289b84ce20 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Fri, 6 Oct 2023 14:31:08 -0700 Subject: [PATCH 15/23] Add auth scheme resolver to httpcontext, and change return values of added methods to optionals --- .../Networking/Http/HttpContext.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index e017f0130..ca0495707 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -18,9 +18,13 @@ public class HttpContext: MiddlewareContext { } return builder } + + public func getAuthSchemeResolver() -> AuthSchemeResolver? { + return attributes.get(key: AttributeKeys.authSchemeResolver) + } - public func getAuthSchemes() -> Attributes { - return attributes.get(key: AttributeKeys.authSchemes)! + public func getAuthSchemes() -> Attributes? { + return attributes.get(key: AttributeKeys.authSchemes) } public func getDecoder() -> ResponseDecoder { @@ -43,8 +47,8 @@ public class HttpContext: MiddlewareContext { return attributes.get(key: AttributeKeys.idempotencyTokenGenerator)! } - public func getIdentityResolvers() -> Attributes { - return attributes.get(key: AttributeKeys.identityResolvers)! + public func getIdentityResolvers() -> Attributes? { + return attributes.get(key: AttributeKeys.identityResolvers) } public func getLogger() -> LogAgent? { @@ -110,6 +114,12 @@ public class HttpContextBuilder { self.attributes.set(key: key, value: value) return self } + + @discardableResult + public func withAuthSchemeResolver(value: AuthSchemeResolver) -> HttpContextBuilder { + self.attributes.set(key: AttributeKeys.authSchemeResolver, value: value) + return self + } @discardableResult public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { @@ -218,6 +228,7 @@ public class HttpContextBuilder { public enum AttributeKeys { + public static let authSchemeResolver = AttributeKey(name: "AuthSchemeResolver") public static let authSchemes = AttributeKey(name: "AuthSchemes") public static let bidirectionalStreaming = AttributeKey(name: "BidirectionalStreaming") public static let decoder = AttributeKey(name: "Decoder") From 422721cb6d87c66c5a57d3d77772c2497f4a3c6e Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Fri, 6 Oct 2023 14:49:06 -0700 Subject: [PATCH 16/23] Add method to ASR protocol, used in AS middleware to construct service-specific ASRP. --- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift index 1733c63aa..9ea5ff36e 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift @@ -9,4 +9,5 @@ import Foundation public protocol AuthSchemeResolver { func resolveAuthScheme(params: AuthSchemeResolverParameters) -> Array + func constructParameters(context: HttpContext) throws -> AuthSchemeResolverParameters } From f7f9e0e9a2b583bef85b71b5eeba17c01af50cad Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Mon, 9 Oct 2023 14:45:23 -0700 Subject: [PATCH 17/23] Swiftlint resolved & additional comment. --- .../HTTPAuth/DefaultIdentityResolverConfiguration.swift | 4 ++-- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 6 ++++++ Sources/ClientRuntime/Networking/Http/HttpContext.swift | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift index 3bfd26c8d..b230f334f 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift @@ -9,11 +9,11 @@ import Foundation public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguration { let credentialsProvider: (any IdentityResolver)? - + public init(configuredIdResolvers: Attributes) { self.credentialsProvider = configuredIdResolvers.get(key: AttributeKeys.awsIdResolver) ?? nil } - + func getIdentityResolver(identityType: IdentityType) -> (any IdentityResolver)? { switch identityType { case .aws: diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index d62440ba9..f3137d91e 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -12,6 +12,12 @@ public protocol Identity { var expiration: Date? { get } } +// Identity v. IdentityT v. IdentityType +// - Identity is the protocol that all identity types must conform to. +// - IdentityT is the associated type / generic type name used by protocols like IdentityResolver and Signer. +// - IdentityType is the enum that's used by IdentityResolverConfiguration to return correct type of identity resolver +// for the given auth scheme. E.g., SigV4AuthScheme has idType field as .aws. And identityResolver method in SigV4AuthScheme +// returns an identity resolver that returns identity of type .aws. public enum IdentityType: CaseIterable { case aws } diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index e4980f7d7..86d3ca6f6 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -18,7 +18,7 @@ public class HttpContext: MiddlewareContext { } return builder } - + public func getAuthSchemeResolver() -> AuthSchemeResolver? { return attributes.get(key: AttributeKeys.authSchemeResolver) } @@ -113,7 +113,7 @@ public class HttpContextBuilder { self.attributes.set(key: key, value: value) return self } - + @discardableResult public func withAuthSchemeResolver(value: AuthSchemeResolver) -> HttpContextBuilder { self.attributes.set(key: AttributeKeys.authSchemeResolver, value: value) From d54fda86962a5f32fa142c38f317759e0f203088 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Mon, 9 Oct 2023 14:48:20 -0700 Subject: [PATCH 18/23] Add clarification to attribute key. --- Sources/ClientRuntime/Networking/Http/HttpContext.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 86d3ca6f6..397e43cae 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -247,5 +247,6 @@ public enum AttributeKeys { public static let selectedAuthScheme = AttributeKey(name: "SelectedAuthScheme") public static let serviceName = AttributeKey(name: "ServiceName") + // The attribute key used to store a credentials provider configured on service client config onto middleware context. public static let awsIdResolver = AttributeKey(name: "AWSIDResolver") } From b0b7d9efa2ed2ae6a1c50e2fbdb383c051cc3e1b Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 11 Oct 2023 14:17:26 -0700 Subject: [PATCH 19/23] Resolve PR comments. --- .../Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift | 2 +- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift | 2 +- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift | 4 ++-- .../ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift | 4 +--- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 4 ++-- Sources/ClientRuntime/Networking/Http/HttpContext.swift | 2 +- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift index b230f334f..4c91e7da2 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift @@ -14,7 +14,7 @@ public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguratio self.credentialsProvider = configuredIdResolvers.get(key: AttributeKeys.awsIdResolver) ?? nil } - func getIdentityResolver(identityType: IdentityType) -> (any IdentityResolver)? { + func getIdentityResolver(identityType: IdentityKind) -> (any IdentityResolver)? { switch identityType { case .aws: return self.credentialsProvider diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift index fdbdc8ae2..78de5302d 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift @@ -8,7 +8,7 @@ import Foundation public struct AuthOption { - let schemeId: String + let schemeID: String var identityProperties: Attributes var signingProperties: Attributes } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift index c72ade652..858c1045b 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift @@ -9,8 +9,8 @@ import Foundation public protocol AuthScheme { var schemeId: String { get } - var signer: any Signer { get } - var idType: IdentityType { get } + var signer: Signer { get } + var idType: IdentityKind { get } } extension AuthScheme { diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift index 9ea5ff36e..93d7d60da 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift @@ -5,9 +5,7 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public protocol AuthSchemeResolver { - func resolveAuthScheme(params: AuthSchemeResolverParameters) -> Array + func resolveAuthScheme(params: AuthSchemeResolverParameters) -> [AuthOption] func constructParameters(context: HttpContext) throws -> AuthSchemeResolverParameters } diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index f3137d91e..7017770e7 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -15,9 +15,9 @@ public protocol Identity { // Identity v. IdentityT v. IdentityType // - Identity is the protocol that all identity types must conform to. // - IdentityT is the associated type / generic type name used by protocols like IdentityResolver and Signer. -// - IdentityType is the enum that's used by IdentityResolverConfiguration to return correct type of identity resolver +// - IdentityKind is the enum that's used by IdentityResolverConfiguration to return correct kind of identity resolver // for the given auth scheme. E.g., SigV4AuthScheme has idType field as .aws. And identityResolver method in SigV4AuthScheme // returns an identity resolver that returns identity of type .aws. -public enum IdentityType: CaseIterable { +public enum IdentityKind: CaseIterable { case aws } diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 397e43cae..4a5014b63 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -159,7 +159,7 @@ public class HttpContextBuilder { } @discardableResult - public func withIdentityResolver(value: T, type: IdentityType) -> HttpContextBuilder { + public func withIdentityResolver(value: T, type: IdentityKind) -> HttpContextBuilder { var identityResolvers: Attributes = self.attributes.get(key: AttributeKeys.identityResolvers) ?? Attributes() identityResolvers.set(key: AttributeKey(name: "\(type)"), value: value) self.attributes.set(key: AttributeKeys.identityResolvers, value: identityResolvers) From 1794543dc9dbf45254e1476bb7b1350370e842d0 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 11 Oct 2023 14:20:47 -0700 Subject: [PATCH 20/23] Update enum name in one more place. --- .../Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift index 08cf34271..b91426be4 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift @@ -8,5 +8,5 @@ import Foundation protocol IdentityResolverConfiguration { - func getIdentityResolver(identityType: IdentityType) -> (any IdentityResolver)? + func getIdentityResolver(identityType: IdentityKind) -> (any IdentityResolver)? } From 07ff3cad4c2c6a8a5546e191eecc6d796897ad91 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 11 Oct 2023 14:37:41 -0700 Subject: [PATCH 21/23] More places where identityType is changed to identityKind. --- .../Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift | 4 ++-- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift | 4 ++-- .../Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift | 2 +- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift index 4c91e7da2..d8ecf9e78 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift @@ -14,8 +14,8 @@ public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguratio self.credentialsProvider = configuredIdResolvers.get(key: AttributeKeys.awsIdResolver) ?? nil } - func getIdentityResolver(identityType: IdentityKind) -> (any IdentityResolver)? { - switch identityType { + func getIdentityResolver(identityKind: IdentityKind) -> (any IdentityResolver)? { + switch identityKind { case .aws: return self.credentialsProvider } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift index 858c1045b..337032e5b 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift @@ -10,11 +10,11 @@ import Foundation public protocol AuthScheme { var schemeId: String { get } var signer: Signer { get } - var idType: IdentityKind { get } + var idKind: IdentityKind { get } } extension AuthScheme { func identityResolver(config: IdentityResolverConfiguration) -> (any IdentityResolver)? { - return config.getIdentityResolver(identityType: self.idType) + return config.getIdentityResolver(identityKind: self.idKind) } } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift index b91426be4..2364e8b3b 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift @@ -8,5 +8,5 @@ import Foundation protocol IdentityResolverConfiguration { - func getIdentityResolver(identityType: IdentityKind) -> (any IdentityResolver)? + func getIdentityResolver(identityKind: IdentityKind) -> (any IdentityResolver)? } diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index 7017770e7..0e1fac149 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -12,11 +12,11 @@ public protocol Identity { var expiration: Date? { get } } -// Identity v. IdentityT v. IdentityType +// Identity v. IdentityT v. IdentityKind // - Identity is the protocol that all identity types must conform to. // - IdentityT is the associated type / generic type name used by protocols like IdentityResolver and Signer. // - IdentityKind is the enum that's used by IdentityResolverConfiguration to return correct kind of identity resolver -// for the given auth scheme. E.g., SigV4AuthScheme has idType field as .aws. And identityResolver method in SigV4AuthScheme +// for the given auth scheme. E.g., SigV4AuthScheme has idKind field as .aws. And identityResolver method in SigV4AuthScheme // returns an identity resolver that returns identity of type .aws. public enum IdentityKind: CaseIterable { case aws From e0a6146e7918ce94ce0bae7340f364cce2850ecf Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 11 Oct 2023 14:42:23 -0700 Subject: [PATCH 22/23] Remove unnecessary imports. --- .../Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift | 2 -- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift | 2 -- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift | 2 -- .../Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift | 2 -- .../Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift | 2 -- Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift | 2 -- Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift | 2 -- Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift | 2 -- .../ClientRuntime/Identity/IdentityAPI/IdentityResolver.swift | 2 -- 9 files changed, 18 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift index d8ecf9e78..20632da77 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuth/DefaultIdentityResolverConfiguration.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguration { let credentialsProvider: (any IdentityResolver)? diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift index 78de5302d..b4b3a0d96 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public struct AuthOption { let schemeID: String var identityProperties: Attributes diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift index 337032e5b..c6bc86672 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public protocol AuthScheme { var schemeId: String { get } var signer: Signer { get } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift index aa2f83b1c..57b5e8528 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolverParameters.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public protocol AuthSchemeResolverParameters { var operation: String { get } } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift index 2364e8b3b..875574a35 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/IdentityResolverConfiguration.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - protocol IdentityResolverConfiguration { func getIdentityResolver(identityKind: IdentityKind) -> (any IdentityResolver)? } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift index 88d493c18..2f7f4d16e 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public struct SelectedAuthScheme { let schemeId: String let identity: Identity? diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift index 9079e90aa..19b679188 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - public protocol Signer { func sign( requestBuilder: SdkHttpRequestBuilder, diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift index 0e1fac149..dd04c1a13 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - // Base protocol for all identity types public protocol Identity { var expiration: Date? { get } diff --git a/Sources/ClientRuntime/Identity/IdentityAPI/IdentityResolver.swift b/Sources/ClientRuntime/Identity/IdentityAPI/IdentityResolver.swift index 34f7ed9b1..19900ffab 100644 --- a/Sources/ClientRuntime/Identity/IdentityAPI/IdentityResolver.swift +++ b/Sources/ClientRuntime/Identity/IdentityAPI/IdentityResolver.swift @@ -5,8 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 // -import Foundation - // Base protocol for all identity provider types public protocol IdentityResolver { associatedtype IdentityT: Identity From dd4324b49f8e8c31a964cdfc562035be8764e8e6 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Wed, 11 Oct 2023 15:34:47 -0700 Subject: [PATCH 23/23] Change schemeId to schemeID in auth scheme too. --- Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift | 2 +- Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift | 2 +- Sources/ClientRuntime/Networking/Http/HttpContext.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift index c6bc86672..5e9e432f2 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift @@ -6,7 +6,7 @@ // public protocol AuthScheme { - var schemeId: String { get } + var schemeID: String { get } var signer: Signer { get } var idKind: IdentityKind { get } } diff --git a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift index 2f7f4d16e..fa85ae3bf 100644 --- a/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift +++ b/Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift @@ -6,7 +6,7 @@ // public struct SelectedAuthScheme { - let schemeId: String + let schemeID: String let identity: Identity? let signingProperties: Attributes? let signer: Signer? diff --git a/Sources/ClientRuntime/Networking/Http/HttpContext.swift b/Sources/ClientRuntime/Networking/Http/HttpContext.swift index 4a5014b63..618e64996 100644 --- a/Sources/ClientRuntime/Networking/Http/HttpContext.swift +++ b/Sources/ClientRuntime/Networking/Http/HttpContext.swift @@ -123,7 +123,7 @@ public class HttpContextBuilder { @discardableResult public func withAuthScheme(value: AuthScheme) -> HttpContextBuilder { var authSchemes: Attributes = self.attributes.get(key: AttributeKeys.authSchemes) ?? Attributes() - authSchemes.set(key: AttributeKey(name: "\(value.schemeId)"), value: value) + authSchemes.set(key: AttributeKey(name: "\(value.schemeID)"), value: value) self.attributes.set(key: AttributeKeys.authSchemes, value: authSchemes) return self }