Skip to content

Commit a15a5fb

Browse files
authored
fix: profile provider seg fault on linux (#466)
1 parent 95bb916 commit a15a5fb

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

AWSClientRuntime/Sources/Regions/ProfileRegionResolver.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ import AwsCommonRuntimeKit
88
import ClientRuntime
99

1010
public struct ProfileRegionProvider: RegionProvider {
11-
let profileCollection: ProfileCollection
11+
let profileCollection: ProfileCollection?
1212
let profileName: String
13+
let path: String
1314
let logger: SwiftLogger
1415

15-
init(profileCollection: ProfileCollection, profileName: String) {
16+
init(profileCollection: ProfileCollection?, profileName: String, path: String) {
1617
self.profileCollection = profileCollection
1718
self.profileName = profileName
1819
self.logger = SwiftLogger(label: "ProfileRegionResolver")
20+
self.path = path
1921
}
2022

2123
// TODO: expose these config fields up to the sdk so customer can override path and profile name
2224
public init(path: String = "~/.aws/config", profileName: String = "default") {
23-
let profileCollection = CRTAWSProfileCollection(fromFile: path, source: .config)
24-
25-
self.init(profileCollection: profileCollection, profileName: profileName)
25+
self.init(profileCollection: nil, profileName: profileName, path: path)
2626
}
2727

2828
public func resolveRegion() -> Future<String?> {
2929
let future = Future<String?>()
30+
31+
let profileCollection = profileCollection ?? CRTAWSProfileCollection(fromFile: path, source: .config)
32+
guard let profileCollection = profileCollection else {
33+
logger.info("No default profile collection was found at the path of \(path)")
34+
future.fulfill(nil)
35+
return future
36+
}
37+
3038
guard let profile = profileCollection.profile(for: profileName) else {
3139
future.fulfill(nil)
3240
return future

AWSClientRuntime/Tests/Regions/RegionTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ class RegionTests: XCTestCase {
2727
let region = DefaultRegionResolver(providers: providers).resolveRegion()
2828
XCTAssertEqual(region, "us-east-1")
2929
}
30+
31+
func testChainWithBadProfileProviderFailsGracefully() {
32+
let providers: [RegionProvider] = [
33+
EnvironmentRegionProvider(env: MockEnvironment(region: nil)),
34+
ProfileRegionProvider(path: "~/.aws/configz", profileName: "default"),
35+
EnvironmentRegionProvider(env: MockEnvironment(region: "us-east-1")),
36+
EnvironmentRegionProvider(env: MockEnvironment(region: "us-east-2"))
37+
]
38+
let region = DefaultRegionResolver(providers: providers).resolveRegion()
39+
XCTAssertEqual(region, "us-east-1")
40+
}
41+
42+
func testChainWithBadProfileProviderFails() {
43+
let providers: [RegionProvider] = [
44+
EnvironmentRegionProvider(env: MockEnvironment(region: nil)),
45+
ProfileRegionProvider(path: "~/.aws/configz", profileName: "default")
46+
]
47+
let region = DefaultRegionResolver(providers: providers).resolveRegion()
48+
XCTAssertEqual(region, nil)
49+
}
3050
}
3151

3252
struct MockEnvironment: Environment {

0 commit comments

Comments
 (0)