Skip to content

Commit

Permalink
chore: refactor endpoints resolver to extend from smithy-swift
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed May 9, 2024
1 parent 72fe4c0 commit 78a4f37
Show file tree
Hide file tree
Showing 25 changed files with 50 additions and 712 deletions.
18 changes: 0 additions & 18 deletions Sources/Core/AWSClientRuntime/Endpoints/AWSEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,3 @@ public struct AWSEndpoint: Equatable {
return try candidate.resolveEndpoint(region: region)
}
}

/// Keys used to access auth scheme container and auth scheme properties
private enum AuthSchemeKeys {
static let authSchemes = "authSchemes"
}

extension Endpoint {
/// Returns list of auth schemes
/// This is an internal API and subject to change without notice
/// - Returns: list of auth schemes if present
public func authSchemes() -> [[String: Any]]? {
guard let schemes = properties[AuthSchemeKeys.authSchemes] as? [[String: Any]] else {
return nil
}

return schemes
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
// Partition definitions are embedded as a static resource in this project, for now.
// When Trebuchet integration is performed, partitions should be obtained from Trebuchet for every
// build instead of being loaded from a static definition.
public let partitionJSON = """
public let awsPartitionJSON = """
{
"version": "1.1",
"partitions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import ClientRuntime

/// Supported authentication schemes
public enum AuthScheme: Equatable {
case sigV4(SigV4Parameters)
Expand Down
13 changes: 0 additions & 13 deletions Sources/Core/AWSClientRuntime/Endpoints/EndpointError.swift

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/Core/AWSClientRuntime/Signing/AWSSigningAlgorithm.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
//

import ClientRuntime
import AwsCommonRuntimeKit
import Foundation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package software.amazon.smithy.aws.swift.codegen

import software.amazon.smithy.aws.swift.codegen.config.AWSDefaultClientConfiguration
import software.amazon.smithy.aws.swift.codegen.config.AWSEndpointClientConfiguration
import software.amazon.smithy.aws.swift.codegen.config.AWSRegionClientConfiguration
import software.amazon.smithy.aws.swift.codegen.plugins.AuthSchemePlugin
import software.amazon.smithy.aws.swift.codegen.plugins.DefaultAWSAuthSchemePlugin
Expand All @@ -20,7 +19,7 @@ import software.amazon.smithy.swift.codegen.integration.plugins.DefaultAWSClient

class AWSClientConfigurationIntegration : SwiftIntegration {
override fun clientConfigurations(ctx: ProtocolGenerator.GenerationContext): List<ClientConfiguration> {
return listOf(AWSDefaultClientConfiguration(), AWSRegionClientConfiguration(), AWSEndpointClientConfiguration(ctx))
return listOf(AWSDefaultClientConfiguration(), AWSRegionClientConfiguration())
}

override fun plugins(serviceConfig: ServiceConfig): List<Plugin> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,18 @@ object AWSClientRuntimeTypes {
val FrameworkMetadata = runtimeSymbol("FrameworkMetadata")
val AWSCredentialIdentityResolver = runtimeSymbol("AWSCredentialIdentityResolver")
val AWSClientConfiguration = runtimeSymbol("AWSClientConfiguration")
val AWSEndpoint = runtimeSymbol("AWSEndpoint")

val Partition = runtimeSymbol("Partition")
val ServiceEndpointMetadata = runtimeSymbol("ServiceEndpointMetadata")
val CredentialScope = runtimeSymbol("CredentialScope")
val UnknownAWSHTTPServiceError = runtimeSymbol("UnknownAWSHTTPServiceError")
val AWSServiceError = runtimeSymbol("AWSServiceError")
val RegionResolver = runtimeSymbol("RegionResolver")
val Sha256TreeHashMiddleware = runtimeSymbol("Sha256TreeHashMiddleware")
val AWSEndpointsRuleEngine = runtimeSymbol("AWSEndpointsRuleEngine")
val AWSEndpointsRequestContext = runtimeSymbol("AWSEndpointsRequestContext")
val AuthSchemeResolver = runtimeSymbol("AuthSchemeResolver")
val DefaultAuthSchemeResolver = runtimeSymbol("DefaultAuthSchemeResolver")
val AWSEndpoints = runtimeSymbol("AWSEndpoints")
val AWSRetryErrorInfoProvider = runtimeSymbol("AWSRetryErrorInfoProvider")
val AWSRetryMode = runtimeSymbol("AWSRetryMode")
val AWSPartitionDefinition = runtimeSymbol("awsPartitionJSON")
}

object CRT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package software.amazon.smithy.aws.swift.codegen

import software.amazon.smithy.aws.swift.codegen.message.MessageMarshallableGenerator
import software.amazon.smithy.aws.swift.codegen.message.MessageUnmarshallableGenerator
import software.amazon.smithy.aws.swift.codegen.middleware.OperationEndpointResolverMiddleware
import software.amazon.smithy.aws.swift.codegen.middleware.UserAgentMiddleware
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.MemberShape
Expand All @@ -19,6 +18,7 @@ import software.amazon.smithy.rulesengine.language.EndpointRuleSet
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait
import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.endpoint.OperationEndpointResolverMiddleware
import software.amazon.smithy.swift.codegen.integration.HttpBindingProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolTestGenerator
import software.amazon.smithy.swift.codegen.integration.HttpProtocolUnitTestErrorGenerator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package software.amazon.smithy.aws.swift.codegen

import software.amazon.smithy.aws.swift.codegen.customization.RulesBasedAuthSchemeResolverGenerator
import software.amazon.smithy.aws.swift.codegen.middleware.AWSEndpointResolverMiddleware
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.swift.codegen.AuthSchemeResolverGenerator
Expand All @@ -21,7 +22,6 @@ import software.amazon.smithy.swift.codegen.model.isOutputEventStream
abstract class AWSHttpProtocolCustomizations : DefaultHttpProtocolCustomizations() {

override fun renderContextAttributes(ctx: ProtocolGenerator.GenerationContext, writer: SwiftWriter, serviceShape: ServiceShape, op: OperationShape) {
val endpointPrefix = ctx.service.endpointPrefix // get endpoint prefix from smithy trait

// FIXME handle indentation properly or do swift formatting after the fact
writer.write(" .withIdentityResolver(value: config.awsCredentialIdentityResolver, schemeID: \$S)", "aws.auth#sigv4")
Expand All @@ -45,12 +45,15 @@ abstract class AWSHttpProtocolCustomizations : DefaultHttpProtocolCustomizations
}

override fun renderInternals(ctx: ProtocolGenerator.GenerationContext) {
super.renderInternals(ctx)
AuthSchemeResolverGenerator().render(ctx)
// Generate rules-based auth scheme resolver for services that depend on endpoint resolver for auth scheme resolution
if (AuthSchemeResolverGenerator.usesRulesBasedAuthResolver(ctx)) {
RulesBasedAuthSchemeResolverGenerator().render(ctx)
}
EndpointResolverGenerator().render(ctx)
EndpointResolverGenerator(
{ writer, input, output, outputError -> AWSEndpointResolverMiddleware(writer, input, output, outputError) },
AWSClientRuntimeTypes.Core.AWSPartitionDefinition
).render(ctx)
}

override fun getClientProperties(): List<ClientProperty> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,9 @@

package software.amazon.smithy.aws.swift.codegen

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.ShapeType
import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait
import software.amazon.smithy.swift.codegen.SwiftTypes
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.ConfigField
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.ServiceConfig
import software.amazon.smithy.swift.codegen.model.buildSymbol
import software.amazon.smithy.swift.codegen.model.getTrait
import software.amazon.smithy.swift.codegen.utils.clientName
import software.amazon.smithy.swift.codegen.utils.toLowerCamelCase

const val ENDPOINT_RESOLVER = "endpointResolver"
const val AUTH_SCHEME_RESOLVER = "authSchemeResolver"
const val ENDPOINT_PARAMS = "endpointParams"

class AWSServiceConfig(writer: SwiftWriter, val ctx: ProtocolGenerator.GenerationContext) :
ServiceConfig(writer, ctx.symbolProvider.toSymbol(ctx.service).name, ctx.service.sdkId) {

override fun serviceSpecificConfigProperties(): List<ConfigField> {
var configs = mutableListOf<ConfigField>()

// service specific EndpointResolver
configs.add(ConfigField(ENDPOINT_RESOLVER, AWSServiceTypes.EndpointResolver, "\$N", "Endpoint resolver"))
// service specific AuthSchemeResolver
configs.add(ConfigField(AUTH_SCHEME_RESOLVER, buildSymbol { this.name = serviceName.clientName() + "AuthSchemeResolver" }, "\$N"))

val clientContextParams = ctx.service.getTrait<ClientContextParamsTrait>()
clientContextParams?.parameters?.forEach {
configs.add(ConfigField(it.key.toLowerCamelCase(), it.value.type.toSwiftType(), "\$T"))
}
return configs.sortedBy { it.memberName }
}
}

fun ShapeType.toSwiftType(): Symbol {
return when (this) {
ShapeType.STRING -> SwiftTypes.String
ShapeType.BOOLEAN -> SwiftTypes.Bool
else -> throw IllegalArgumentException("Unsupported shape type: $this")
}
}

object AWSServiceTypes {
val EndpointResolver = symbol("EndpointResolver")
val EndpointParams = symbol("EndpointParams")
val EndpointResolverMiddleware = symbol("EndpointResolverMiddleware")
val DefaultEndpointResolver = symbol("DefaultEndpointResolver")

private fun symbol(name: String): Symbol = buildSymbol {
this.name = name
}
}
ServiceConfig(writer, ctx.symbolProvider.toSymbol(ctx.service).name, ctx.settings.sdkId)
Loading

0 comments on commit 78a4f37

Please sign in to comment.