Skip to content

Commit 9472ff9

Browse files
authored
feat: imds client (#454)
1 parent a8b2c53 commit 9472ff9

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

AWSClientRuntime/Sources/Auth/CredentialsProviderCRTAdapter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct CredentialsProviderCRTAdapter: CRTCredentialsProvider {
1818
self.allocator = defaultAllocator
1919
}
2020

21-
func getCredentials(credentialCallbackData: CRTCredentialsProviderCallbackData) {
21+
func getCredentials(credentialCallbackData: CRTCredentialsCallbackData) {
2222
do {
2323
let credentialsResult = try credentialsProvider.getCredentials()
2424
let credentials = try credentialsResult.get()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import AwsCommonRuntimeKit
9+
import ClientRuntime
10+
11+
/// IMDSv2 Client
12+
///
13+
/// This client supports fetching tokens, retrying failures, and token caching according to the specified TTL.
14+
/// NOTE: This client ONLY supports IMDSv2. It will not fallback to IMDSv1.
15+
/// See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html#instance-metadata-transition-to-version-2 for more information.
16+
public class IMDSClient {
17+
let crtIMDSClient: CRTIMDSClient
18+
19+
public init(config: IMDSConfig = IMDSConfig()) {
20+
let crtConfig = config.toCRTConfig()
21+
self.crtIMDSClient = CRTIMDSClient(options: crtConfig)
22+
}
23+
24+
public func get(path: String, completion: @escaping (Result<String, ClientError>) -> Void) {
25+
let callbackData = CRTIMDSClientResourceCallbackData { resource, error in
26+
guard let resource = resource else {
27+
completion(.failure(.crtError(error)))
28+
return
29+
}
30+
completion(.success(resource))
31+
}
32+
crtIMDSClient.getResource(resourcePath: path, callbackData: callbackData)
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
import AwsCommonRuntimeKit
8+
import ClientRuntime
9+
10+
//TODO: support more config options that are required to be supported when the CRT adds the ability to pass them down.
11+
public struct IMDSConfig {
12+
let retries: Int
13+
public init(retries: Int = 3) {
14+
self.retries = retries
15+
}
16+
}
17+
18+
extension IMDSConfig {
19+
// swiftlint:disable force_try
20+
func toCRTConfig() -> CRTIMDSClientOptions {
21+
let options = RetryOptions(backOffRetryOptions: ExponentialBackOffRetryOptions(maxRetries: retries))
22+
let crtRetryer = try! CRTAWSRetryStrategy(options: options.toCRTType())
23+
return CRTIMDSClientOptions(bootstrap: SDKDefaultIO.shared.clientBootstrap, retryStrategy: crtRetryer)
24+
}
25+
}

AWSClientRuntime/Sources/Regions/ProfileRegionResolver.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ public struct ProfileRegionProvider: RegionProvider {
2929
return profile.getProperty(name: "region")
3030
}
3131
}
32-

0 commit comments

Comments
 (0)