Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Auth scheme changes #601

Merged
merged 26 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
@@ -0,0 +1,21 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public struct DefaultIdentityResolverConfiguration: IdentityResolverConfiguration {
let credentialsProvider: (any IdentityResolver)?

public init(configuredIdResolvers: Attributes) {
self.credentialsProvider = configuredIdResolvers.get(key: AttributeKeys.awsIdResolver) ?? nil
}

func getIdentityResolver(identityKind: IdentityKind) -> (any IdentityResolver)? {
switch identityKind {
case .aws:
jbelkins marked this conversation as resolved.
Show resolved Hide resolved
return self.credentialsProvider
}
}
}
12 changes: 12 additions & 0 deletions Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthOption.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public struct AuthOption {
let schemeID: String
var identityProperties: Attributes
var signingProperties: Attributes
}
18 changes: 18 additions & 0 deletions Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthScheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public protocol AuthScheme {
var schemeID: String { get }
var signer: Signer { get }
var idKind: IdentityKind { get }
}

extension AuthScheme {
func identityResolver(config: IdentityResolverConfiguration) -> (any IdentityResolver)? {
return config.getIdentityResolver(identityKind: self.idKind)
}
}
11 changes: 11 additions & 0 deletions Sources/ClientRuntime/Auth/HTTPAuthAPI/AuthSchemeResolver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public protocol AuthSchemeResolver {
func resolveAuthScheme(params: AuthSchemeResolverParameters) -> [AuthOption]
func constructParameters(context: HttpContext) throws -> AuthSchemeResolverParameters
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public protocol AuthSchemeResolverParameters {
var operation: String { get }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

protocol IdentityResolverConfiguration {
func getIdentityResolver(identityKind: IdentityKind) -> (any IdentityResolver)?
Copy link
Collaborator

Choose a reason for hiding this comment

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

is any or some better here? Do we expect this function to always return a single concrete type?

Copy link
Contributor Author

@sichanyoo sichanyoo Oct 17, 2023

Choose a reason for hiding this comment

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

This function always returns a single concrete type that conforms to IdentityResolver (or nil if none is configured for the given identity kind), but it's dependent on which kind of identity is passed in as the argument. Also, having some keyword in protocols is not allowed in Swift. With these two factors combined, 'any' is used here.

}
13 changes: 13 additions & 0 deletions Sources/ClientRuntime/Auth/HTTPAuthAPI/SelectedAuthScheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public struct SelectedAuthScheme {
let schemeID: String
let identity: Identity?
let signingProperties: Attributes?
let signer: Signer?
}
2 changes: 0 additions & 2 deletions Sources/ClientRuntime/Auth/HTTPAuthAPI/Signer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

public protocol Signer {
func sign<IdentityT: Identity>(
requestBuilder: SdkHttpRequestBuilder,
Expand Down
12 changes: 10 additions & 2 deletions Sources/ClientRuntime/Identity/IdentityAPI/Identity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

// Base protocol for all identity types
public protocol Identity {
var expiration: Date? { get }
}

// 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 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 57 additions & 2 deletions Sources/ClientRuntime/Networking/Http/HttpContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ public class HttpContext: MiddlewareContext {
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 getAuthSchemeResolver() -> AuthSchemeResolver? {
return attributes.get(key: AttributeKeys.authSchemeResolver)
}

public func getAuthSchemes() -> Attributes? {
return attributes.get(key: AttributeKeys.authSchemes)
}

public func getDecoder() -> ResponseDecoder {
return attributes.get(key: AttributeKeys.decoder)!
}
Expand All @@ -30,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? {
Expand Down Expand Up @@ -62,6 +79,10 @@ 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)!
}
Expand Down Expand Up @@ -93,6 +114,20 @@ public class HttpContextBuilder {
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 {
var authSchemes: Attributes = self.attributes.get(key: AttributeKeys.authSchemes) ?? Attributes()
authSchemes.set(key: AttributeKey<AuthScheme>(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)
Expand Down Expand Up @@ -123,6 +158,14 @@ public class HttpContextBuilder {
return self
}

@discardableResult
public func withIdentityResolver<T: IdentityResolver>(value: T, type: IdentityKind) -> HttpContextBuilder {
var identityResolvers: Attributes = self.attributes.get(key: AttributeKeys.identityResolvers) ?? Attributes()
identityResolvers.set(key: AttributeKey<any IdentityResolver>(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)
Expand Down Expand Up @@ -165,6 +208,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 withServiceName(value: String) -> HttpContextBuilder {
self.attributes.set(key: AttributeKeys.serviceName, value: value)
Expand All @@ -177,6 +226,8 @@ public class HttpContextBuilder {
}

public enum AttributeKeys {
public static let authSchemeResolver = AttributeKey<AuthSchemeResolver>(name: "AuthSchemeResolver")
public static let authSchemes = AttributeKey<Attributes>(name: "AuthSchemes")
public static let bidirectionalStreaming = AttributeKey<Bool>(name: "BidirectionalStreaming")
public static let decoder = AttributeKey<ResponseDecoder>(name: "Decoder")
public static let encoder = AttributeKey<RequestEncoder>(name: "Encoder")
Expand All @@ -193,5 +244,9 @@ public enum AttributeKeys {
public static let operation = AttributeKey<String>(name: "Operation")
public static let partitionId = AttributeKey<String>(name: "PartitionID")
public static let path = AttributeKey<String>(name: "Path")
public static let selectedAuthScheme = AttributeKey<SelectedAuthScheme>(name: "SelectedAuthScheme")
public static let serviceName = AttributeKey<String>(name: "ServiceName")

// The attribute key used to store a credentials provider configured on service client config onto middleware context.
public static let awsIdResolver = AttributeKey<any IdentityResolver>(name: "AWSIDResolver")
}
Loading