Skip to content

Commit

Permalink
Explicitly set service config instead of using .with
Browse files Browse the repository at this point in the history
as endpoints in original service are wrong point to `s3`.
  • Loading branch information
adam-fowler committed Dec 11, 2024
1 parent 563d8d7 commit cb755fd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
14 changes: 2 additions & 12 deletions Sources/SotoCore/AWSServiceConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ public final class AWSServiceConfig {
/// Service config parameters you can patch
public struct Patch {
let region: Region?
let serviceIdentifier: String?
let signingName: String?
let endpoint: String?
let middleware: AWSMiddlewareProtocol?
let timeout: TimeAmount?
Expand All @@ -192,17 +190,13 @@ public final class AWSServiceConfig {

init(
region: Region? = nil,
serviceIdentifier: String? = nil,
signingName: String? = nil,
endpoint: String? = nil,
middleware: AWSMiddlewareProtocol? = nil,
timeout: TimeAmount? = nil,
byteBufferAllocator: ByteBufferAllocator? = nil,
options: AWSServiceConfig.Options? = nil
) {
self.region = region
self.serviceIdentifier = serviceIdentifier
self.signingName = signingName
self.endpoint = endpoint
self.middleware = middleware
self.timeout = timeout
Expand All @@ -221,8 +215,6 @@ public final class AWSServiceConfig {
/// - Returns: New version of the service
public func with(
region: Region? = nil,
serviceIdentifier: String? = nil,
signingName: String? = nil,
middleware: AWSMiddlewareProtocol? = nil,
timeout: TimeAmount? = nil,
byteBufferAllocator: ByteBufferAllocator? = nil,
Expand All @@ -231,8 +223,6 @@ public final class AWSServiceConfig {
self.with(
patch: .init(
region: region,
serviceIdentifier: serviceIdentifier,
signingName: signingName,
middleware: middleware,
timeout: timeout,
byteBufferAllocator: byteBufferAllocator,
Expand Down Expand Up @@ -310,8 +300,8 @@ public final class AWSServiceConfig {
) {
self.region = patch.region ?? service.region
self.options = patch.options ?? service.options
self.serviceIdentifier = patch.serviceIdentifier ?? service.serviceIdentifier
self.signingName = patch.signingName ?? patch.serviceIdentifier ?? service.signingName
self.serviceIdentifier = service.serviceIdentifier
self.signingName = service.signingName

if let endpoint = patch.endpoint {
self.endpoint = endpoint
Expand Down
26 changes: 19 additions & 7 deletions Sources/SotoCore/Middleware/Middleware/S3Middleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,27 @@ public struct S3Middleware: AWSMiddlewareProtocol {
let resourceIDSplit = arn.resourceId.split(separator: "/", maxSplits: 1, omittingEmptySubsequences: false)
guard let bucket = resourceIDSplit.first else { throw AWSClient.ClientError.invalidARN }
let path = String(resourceIDSplit.dropFirst().first ?? "")
let urlHost = "\(bucket)-\(accountId).\(arn.service).\(region).amazonaws.com"
let service = String(arn.service)
let serviceIdentifier = service != "s3" ? service : "s3-accesspoint"
let urlHost = "\(bucket)-\(accountId).\(serviceIdentifier).\(region).amazonaws.com"
let request = Self.updateRequestURL(request, host: urlHost, path: path)

// if service isn't S3 or arn region is different from the current config then build a new AWSServiceConfig
if arn.service != "s3" || arn.region != context.serviceConfig.region {
var context = context
context.serviceConfig = context.serviceConfig.with(region: region, serviceIdentifier: String(arn.service))
return try await next(request, context)
}
var context = context
context.serviceConfig = AWSServiceConfig(
region: region,
partition: region.partition,
serviceName: "S3",
serviceIdentifier: serviceIdentifier,
signingName: service,
serviceProtocol: context.serviceConfig.serviceProtocol,
apiVersion: context.serviceConfig.apiVersion,
errorType: context.serviceConfig.errorType,
xmlNamespace: context.serviceConfig.xmlNamespace,
middleware: context.serviceConfig.middleware,
timeout: context.serviceConfig.timeout,
byteBufferAllocator: context.serviceConfig.byteBufferAllocator,
options: context.serviceConfig.options
)
return try await next(request, context)
}

Expand Down
19 changes: 18 additions & 1 deletion Tests/SotoCoreTests/MiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,24 @@ class MiddlewareTests: XCTestCase {
try await client.shutdown()
}

func testS3MiddlewareArn() async throws {
func testS3MiddlewareAccessPointArn() async throws {
// Test virual address
try await self.testMiddleware(
S3Middleware(),
serviceName: "s3",
serviceOptions: .s3UseTransferAcceleratedEndpoint,
uri: "/arn:aws:s3:us-west-2:111122223333:accesspoint/test-accesspoint"
) { request, context in
XCTAssertEqual(
request.url.absoluteString,
"https://test-accesspoint-111122223333.s3-accesspoint.us-west-2.amazonaws.com/"
)
XCTAssertEqual(context.serviceConfig.serviceIdentifier, "s3-accesspoint")
XCTAssertEqual(context.serviceConfig.region, .uswest2)
}
}

func testS3MiddlewareObjectLambdaArn() async throws {
// Test virual address
try await self.testMiddleware(
S3Middleware(),
Expand Down

0 comments on commit cb755fd

Please sign in to comment.