Skip to content

Commit 2a5dd46

Browse files
committed
allow user to provide a region. Check profile region first, then provided region, then default to us-east-1
1 parent 62e04b7 commit 2a5dd46

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

Sources/SotoCore/Credential/Login/LoginCredentialProvider.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public struct LoginCredentialProvider: CredentialProvider {
2929
private let configuration: LoginConfiguration?
3030
private let profileName: String?
3131
private let cacheDirectoryOverride: String?
32+
private let region: Region?
3233
private let dpopGenerator = DPoPTokenGenerator()
3334
private let httpClient: AWSHTTPClient
3435
private let threadPool: NIOThreadPool
@@ -39,6 +40,7 @@ public struct LoginCredentialProvider: CredentialProvider {
3940
self.configuration = configuration
4041
self.profileName = nil
4142
self.cacheDirectoryOverride = nil
43+
self.region = nil
4244
self.httpClient = httpClient
4345
self.threadPool = threadPool
4446
}
@@ -47,15 +49,18 @@ public struct LoginCredentialProvider: CredentialProvider {
4749
/// - Parameters:
4850
/// - profileName: Name of the profile in ~/.aws/config (defaults to "default")
4951
/// - cacheDirectoryOverride: Optional override for token cache directory
52+
/// - region: Region to use if not specified in profile (defaults to us-east-1)
5053
/// - httpClient: HTTP client for making requests
5154
public init(
5255
profileName: String? = nil,
5356
cacheDirectoryOverride: String? = nil,
57+
region: Region? = nil,
5458
httpClient: AWSHTTPClient
5559
) {
5660
self.configuration = nil
5761
self.profileName = profileName
5862
self.cacheDirectoryOverride = cacheDirectoryOverride
63+
self.region = region
5964
self.httpClient = httpClient
6065
self.threadPool = .singleton
6166
}
@@ -221,19 +226,22 @@ public struct LoginCredentialProvider: CredentialProvider {
221226
}
222227
return try await self.loadConfiguration(
223228
profileName: profileName,
224-
cacheDirectoryOverride: cacheDirectoryOverride
229+
cacheDirectoryOverride: cacheDirectoryOverride,
230+
region: region
225231
)
226232
}
227233

228234
/// Load configuration from profile
229235
/// - Parameters:
230236
/// - profileName: Name of the profile (defaults to "default")
231237
/// - cacheDirectoryOverride: Optional override for cache directory
238+
/// - region: Region to use if not specified in profile (defaults to us-east-1)
232239
/// - configPath: Optional path to config file (defaults to ~/.aws/config)
233240
/// - Returns: LoginConfiguration
234241
private func loadConfiguration(
235242
profileName: String? = nil,
236243
cacheDirectoryOverride: String? = nil,
244+
region: Region? = nil,
237245
configPath: String? = nil
238246
) async throws -> LoginConfiguration {
239247
let profile = profileName ?? "default"
@@ -261,25 +269,23 @@ public struct LoginCredentialProvider: CredentialProvider {
261269
throw AWSLoginCredentialError.loginSessionMissing
262270
}
263271

264-
// region is optional - check profile, then AWS_REGION env var, then default to us-east-1
265-
let region: Region
272+
// region is optional - check profile, then use provided region, then default to us-east-1
273+
let resolvedRegion: Region
266274
if let regionString = profileSection["region"] {
267-
region = Region(rawValue: regionString)
268-
} else if let envRegion = ProcessInfo.processInfo.environment["AWS_REGION"] {
269-
region = Region(rawValue: envRegion)
275+
resolvedRegion = Region(rawValue: regionString)
270276
} else {
271-
region = .useast1
277+
resolvedRegion = region ?? .useast1
272278
}
273279

274280
// Check environment for cache directory override
275281
let cacheDir = cacheDirectoryOverride ?? ProcessInfo.processInfo.environment[LoginConfiguration.cacheEnvVar]
276282

277-
let endpoint = "\(region.rawValue).\(LoginConfiguration.loginServiceHostPrefix).aws.amazon.com"
283+
let endpoint = "\(resolvedRegion.rawValue).\(LoginConfiguration.loginServiceHostPrefix).aws.amazon.com"
278284

279285
return LoginConfiguration(
280286
endpoint: endpoint,
281287
loginSession: loginSession,
282-
region: region,
288+
region: resolvedRegion,
283289
cacheDirectory: cacheDir
284290
)
285291
}
@@ -296,16 +302,19 @@ extension CredentialProviderFactory {
296302
/// - Parameters:
297303
/// - profileName: Name of the profile in ~/.aws/config (defaults to "default")
298304
/// - cacheDirectoryOverride: Optional override for token cache directory
305+
/// - region: Region to use if not specified in profile (defaults to us-east-1)
299306
/// - Returns: A credential provider factory
300307
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
301308
public static func login(
302309
profileName: String? = nil,
303-
cacheDirectoryOverride: String? = nil
310+
cacheDirectoryOverride: String? = nil,
311+
region: Region? = nil
304312
) -> CredentialProviderFactory {
305313
.custom { context in
306314
let provider = LoginCredentialProvider(
307315
profileName: profileName,
308316
cacheDirectoryOverride: cacheDirectoryOverride,
317+
region: region,
309318
httpClient: context.httpClient
310319
)
311320
// Wrap in RotatingCredentialProvider for automatic credential rotation

0 commit comments

Comments
 (0)