diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d70db019..b050b7319 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,8 +88,6 @@ jobs: docker run --mount type=bind,source=$(pwd),target=/root/${{ env.PACKAGE_NAME }} --env GITHUB_REF $DOCKER_IMAGE build -p ${{ env.PACKAGE_NAME }} --build-dir=/root/${{ env.PACKAGE_NAME }} --spec=downstream osx: runs-on: macos-11 - env: - DEVELOPER_DIR: /Applications/Xcode_12.5.app steps: - name: Checkout Sources uses: actions/checkout@v2 @@ -102,8 +100,6 @@ jobs: ./builder build -p ${{ env.PACKAGE_NAME }} --spec=downstream ios: runs-on: macos-11 - env: - DEVELOPER_DIR: /Applications/Xcode_12.5.app steps: - name: Checkout Sources uses: actions/checkout@v2 diff --git a/Source/AwsCommonRuntimeKit/Utilities.swift b/Source/AwsCommonRuntimeKit/Utilities.swift index 571ef4784..d1f1308cd 100644 --- a/Source/AwsCommonRuntimeKit/Utilities.swift +++ b/Source/AwsCommonRuntimeKit/Utilities.swift @@ -49,6 +49,22 @@ extension aws_byte_buf { } } +extension aws_array_list { + func toStringArray() -> [String] { + let length = self.length + var arrayList = self + var newArray: [String] = Array(repeating: "", count: length) + + for index in 0.. String? { // Convert UnicodeScalar to a String. diff --git a/Source/AwsCommonRuntimeKit/auth/credentials/CRTAWSCredentialsProvider.swift b/Source/AwsCommonRuntimeKit/auth/credentials/CRTAWSCredentialsProvider.swift index 6a6f9c21e..eb7526533 100644 --- a/Source/AwsCommonRuntimeKit/auth/credentials/CRTAWSCredentialsProvider.swift +++ b/Source/AwsCommonRuntimeKit/auth/credentials/CRTAWSCredentialsProvider.swift @@ -283,19 +283,19 @@ public final class CRTAWSCredentialsProvider { /// - credentialCallbackData: The `CredentialProviderCallbackData`options object. public func getCredentials() -> Future { let future = Future() - let callbackData = CRTCredentialsProviderCallbackData(allocator: allocator) { (crtCredentials, crtError) in + let callbackData = CRTCredentialsCallbackData(allocator: allocator) { (crtCredentials, crtError) in if let crtCredentials = crtCredentials { future.fulfill(crtCredentials) } else { future.fail(crtError) } } - let pointer: UnsafeMutablePointer = fromPointer(ptr: callbackData) + let pointer: UnsafeMutablePointer = fromPointer(ptr: callbackData) aws_credentials_provider_get_credentials(rawValue, { (credentials, errorCode, userdata) -> Void in guard let userdata = userdata else { return } - let pointer = userdata.assumingMemoryBound(to: CRTCredentialsProviderCallbackData.self) + let pointer = userdata.assumingMemoryBound(to: CRTCredentialsCallbackData.self) defer { pointer.deinitializeAndDeallocate() } let error = AWSError(errorCode: errorCode) if let onCredentialsResolved = pointer.pointee.onCredentialsResolved { @@ -332,7 +332,7 @@ private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer?, guard let credentialsProvider = delegatePtr?.assumingMemoryBound(to: CRTCredentialsProvider.self) else { return 1 } - guard let credentialCallbackData = userData?.assumingMemoryBound(to: CRTCredentialsProviderCallbackData.self) else { + guard let credentialCallbackData = userData?.assumingMemoryBound(to: CRTCredentialsCallbackData.self) else { return 1 } diff --git a/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProviderCallbackData.swift b/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsCallbackData.swift similarity index 91% rename from Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProviderCallbackData.swift rename to Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsCallbackData.swift index 1a5988661..08e0d3c6b 100644 --- a/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProviderCallbackData.swift +++ b/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsCallbackData.swift @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0. -public struct CRTCredentialsProviderCallbackData { +public struct CRTCredentialsCallbackData { public typealias OnCredentialsResolved = (CRTCredentials?, CRTError) -> Void public var onCredentialsResolved: OnCredentialsResolved? public let allocator: Allocator diff --git a/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProvider.swift b/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProvider.swift index 177db5f4f..c564e6c50 100644 --- a/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProvider.swift +++ b/Source/AwsCommonRuntimeKit/auth/credentials/CRTCredentialsProvider.swift @@ -5,5 +5,5 @@ import AwsCAuth public protocol CRTCredentialsProvider { var allocator: Allocator {get set} - func getCredentials(credentialCallbackData: CRTCredentialsProviderCallbackData) + func getCredentials(credentialCallbackData: CRTCredentialsCallbackData) } diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIAMProfile.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIAMProfile.swift new file mode 100644 index 000000000..b414dc524 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIAMProfile.swift @@ -0,0 +1,17 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +import AwsCAuth + +public struct CRTIAMProfile { + public let lastUpdated: AWSDate + public let profileArn: String + public let profileId: String + + init(pointer: UnsafePointer) { + let profile = pointer.pointee + self.lastUpdated = AWSDate(rawValue: profile.last_updated) + self.profileArn = profile.instance_profile_arn.toString() ?? "" + self.profileId = profile.instance_profile_id.toString() ?? "" + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClient.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClient.swift new file mode 100644 index 000000000..105dfbbf7 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClient.swift @@ -0,0 +1,315 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +import AwsCAuth + +public class CRTIMDSClient { + let rawValue: OpaquePointer + + public init(options: CRTIMDSClientOptions, allocator: Allocator = defaultAllocator) { + let shutDownOptions = CRTIMDSClient.setUpShutDownOptions(shutDownOptions: options.shutDownOptions) + var imdsOptions = aws_imds_client_options(shutdown_options: shutDownOptions, + bootstrap: options.bootstrap.rawValue, + retry_strategy: options.retryStrategy.rawValue, + imds_version: options.protocolVersion.rawValue, + function_table: nil) + self.rawValue = aws_imds_client_new(allocator.rawValue, &imdsOptions) + } + /// Queries a generic resource (string) from the ec2 instance metadata document + /// + /// - Parameters: + /// - resourcePath: `String` path of the resource to query + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getResource(resourcePath: String, callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_resource_async(rawValue, resourcePath.awsByteCursor, resourceCallback, pointer) + } + + /// Gets the ami id of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getAmiId(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_ami_id(rawValue, resourceCallback, pointer) + } + + /// Gets the ami launch index of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getAmiLaunchIndex(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_ami_launch_index(rawValue, resourceCallback, pointer) + } + + /// Gets the ami manifest path of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getAmiManifestPath(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_ami_manifest_path(rawValue, resourceCallback, pointer) + } + + /// Gets the list of ancestor ami ids of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientArrayCallbackData` object with an async callback + public func getAncestorAmiIDs(callbackData: CRTIMDSClientArrayCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_ancestor_ami_ids(rawValue, arrayCallback, pointer) + } + + /// Gets the instance-action of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getInstanceAction(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_instance_action(rawValue, resourceCallback, pointer) + } + + /// Gets the instance id of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getInstanceId(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_instance_id(rawValue, resourceCallback, pointer) + } + + /// Gets the instance type of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getInstanceType(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_instance_type(rawValue, resourceCallback, pointer) + } + + /// Gets the mac address of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getMacAddress(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_mac_address(rawValue, resourceCallback, pointer) + } + + /// Gets the private ip address of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getPrivateIpAddress(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_private_ip_address(rawValue, resourceCallback, pointer) + } + + /// Gets the availability zone of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getAvailabilityZone(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_availability_zone(rawValue, resourceCallback, pointer) + } + + /// Gets the product codes of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getProductCodes(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_product_codes(rawValue, resourceCallback, pointer) + } + + /// Gets the public key of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getPublicKey(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_public_key(rawValue, resourceCallback, pointer) + } + + /// Gets the ramdisk id of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getRamDiskId(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_ramdisk_id(rawValue, resourceCallback, pointer) + } + + /// Gets the reservation id of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getReservationId(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_reservation_id(rawValue, resourceCallback, pointer) + } + + /// Gets the list of the security groups of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientArrayCallbackData` object with an async callback + public func getSecurityGroups(callbackData: CRTIMDSClientArrayCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_security_groups(rawValue, arrayCallback, pointer) + } + + /// Gets the list of block device mappings of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientArrayCallbackData` object with an async callback + public func getBlockDeviceMapping(callbackData: CRTIMDSClientArrayCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_block_device_mapping(rawValue, arrayCallback, pointer) + } + + /// Gets the attached iam role of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getAttachedIAMRole(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_attached_iam_role(rawValue, resourceCallback, pointer) + } + + /// Gets temporary credentials based on the attached iam role of the ec2 instance + /// + /// - Parameters: + /// - callbackData: The `CRTCredentialsCallbackData` object with an async callback + public func getCredentials(iamRoleName: String, callbackData: CRTCredentialsCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_credentials(rawValue, iamRoleName.awsByteCursor, { credentialsPointer, errorCode, userData in + guard let userData = userData else { + return + } + let pointer = userData.assumingMemoryBound(to: CRTCredentialsCallbackData.self) + let credentials = CRTCredentials(rawValue: credentialsPointer) + let error = AWSError(errorCode: errorCode) + if let onCredentialsResolved = pointer.pointee.onCredentialsResolved { + onCredentialsResolved(credentials, CRTError.crtError(error)) + } + pointer.deinitializeAndDeallocate() + }, pointer) + } + + /// Gets the iam profile information of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientIAMProfileCallbackData` object with an async callback + public func getIAMProfile(callbackData: CRTIMDSClientIAMProfileCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_iam_profile(rawValue, { profilePointer, errorCode, userData in + guard let userData = userData else { + return + } + let pointer = userData.assumingMemoryBound(to: CRTIMDSClientIAMProfileCallbackData.self) + let error = AWSError(errorCode: errorCode) + guard let profilePointer = profilePointer else { + pointer.pointee.onIAMProfileResolved(nil, CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() + return + } + let profile = CRTIAMProfile(pointer: profilePointer) + pointer.pointee.onIAMProfileResolved(profile, CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() + }, pointer) + } + + /// Gets the user data of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getUserData(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_user_data(rawValue, resourceCallback, pointer) + } + + /// Gets the signature of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientResourceCallbackData` object with an async callback + public func getInstanceSignature(callbackData: CRTIMDSClientResourceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_instance_signature(rawValue, resourceCallback, pointer) + } + + /// Gets the instance information data block of the ec2 instance from the instance metadata document + /// + /// - Parameters: + /// - callbackData: The `CRTIMDSClientInstanceCallbackData` object with an async callback + public func getInstanceInfo(callbackData: CRTIMDSClientInstanceCallbackData) { + let pointer: UnsafeMutableRawPointer = fromPointer(ptr: callbackData) + aws_imds_client_get_instance_info(rawValue, { instancePointer, errorCode, userData in + guard let userData = userData else { + return + } + let pointer = userData.assumingMemoryBound(to: CRTIMDSClientInstanceCallbackData.self) + let error = AWSError(errorCode: errorCode) + guard let instancePointer = instancePointer else { + pointer.pointee.onInstanceInfoResolved(nil, + CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() + return + } + pointer.pointee.onInstanceInfoResolved(CRTIMDSInstanceInfo(pointer: instancePointer), + CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() + }, pointer) + } + + static func setUpShutDownOptions(shutDownOptions: CRTIDMSClientShutdownOptions?) + -> aws_imds_client_shutdown_options { + + let pointer: UnsafeMutablePointer? = fromOptionalPointer(ptr: shutDownOptions) + let shutDownOptionsC = aws_imds_client_shutdown_options(shutdown_callback: { userData in + guard let userData = userData else { + return + } + let pointer = userData.assumingMemoryBound(to: CRTIDMSClientShutdownOptions.self) + pointer.pointee.shutDownCallback() + pointer.deinitializeAndDeallocate() + }, shutdown_user_data: pointer) + + return shutDownOptionsC + } + + deinit { + aws_imds_client_release(rawValue) + } +} + +private func resourceCallback(_ byteBuf: UnsafePointer?, + _ errorCode: Int32, + _ userData: UnsafeMutableRawPointer?) { + guard let userData = userData else { + return + } + + let pointer = userData.assumingMemoryBound(to: CRTIMDSClientResourceCallbackData.self) + let error = AWSError(errorCode: errorCode) + let byteCursor = aws_byte_cursor_from_buf(byteBuf) + + pointer.pointee.onResourceResolved(byteCursor.toString(), CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() +} + +private func arrayCallback(_ arrayListPointer: UnsafePointer?, + _ errorCode: Int32, + _ userData: UnsafeMutableRawPointer?) { + guard let userData = userData else { + return + } + let pointer = userData.assumingMemoryBound(to: CRTIMDSClientArrayCallbackData.self) + let error = AWSError(errorCode: errorCode) + + let amiIds = arrayListPointer?.pointee.toStringArray() + + pointer.pointee.onArrayResolved(amiIds, CRTError.crtError(error)) + pointer.deinitializeAndDeallocate() +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientArrayCallbackData.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientArrayCallbackData.swift new file mode 100644 index 000000000..c7d749d01 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientArrayCallbackData.swift @@ -0,0 +1,14 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIMDSClientArrayCallbackData { + public typealias OnArrayResolved = ([String]?, CRTError) -> Void + public var onArrayResolved: OnArrayResolved + public let allocator: Allocator + + public init(allocator: Allocator = defaultAllocator, + onArrayResolved: @escaping OnArrayResolved) { + self.onArrayResolved = onArrayResolved + self.allocator = allocator + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientIAMProfileCallbackData.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientIAMProfileCallbackData.swift new file mode 100644 index 000000000..21c178aff --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientIAMProfileCallbackData.swift @@ -0,0 +1,14 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIMDSClientIAMProfileCallbackData { + public typealias OnIAMProfileResolved = (CRTIAMProfile?, CRTError) -> Void + public var onIAMProfileResolved: OnIAMProfileResolved + public let allocator: Allocator + + public init(allocator: Allocator = defaultAllocator, + onIAMProfileResolved: @escaping OnIAMProfileResolved) { + self.onIAMProfileResolved = onIAMProfileResolved + self.allocator = allocator + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientInstanceCallbackData.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientInstanceCallbackData.swift new file mode 100644 index 000000000..3a90dd621 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientInstanceCallbackData.swift @@ -0,0 +1,14 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIMDSClientInstanceCallbackData { + public typealias OnInstanceInfoResolved = (CRTIMDSInstanceInfo?, CRTError) -> Void + public var onInstanceInfoResolved: OnInstanceInfoResolved + public let allocator: Allocator + + public init(allocator: Allocator = defaultAllocator, + onInstanceInfoResolved: @escaping OnInstanceInfoResolved) { + self.onInstanceInfoResolved = onInstanceInfoResolved + self.allocator = allocator + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientOptions.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientOptions.swift new file mode 100644 index 000000000..76432d650 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientOptions.swift @@ -0,0 +1,19 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIMDSClientOptions { + public let bootstrap: ClientBootstrap + public let retryStrategy: CRTAWSRetryStrategy + public let protocolVersion: CRTIMDSProtocolVersion + public let shutDownOptions: CRTIDMSClientShutdownOptions? + + public init(bootstrap: ClientBootstrap, + retryStrategy: CRTAWSRetryStrategy, + protocolVersion: CRTIMDSProtocolVersion = .version2, + shutDownOptions: CRTIDMSClientShutdownOptions? = nil) { + self.bootstrap = bootstrap + self.retryStrategy = retryStrategy + self.protocolVersion = protocolVersion + self.shutDownOptions = shutDownOptions + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientResourceCallbackData.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientResourceCallbackData.swift new file mode 100644 index 000000000..9c557dade --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientResourceCallbackData.swift @@ -0,0 +1,14 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIMDSClientResourceCallbackData { + public typealias OnResourceResolved = (String?, CRTError) -> Void + public var onResourceResolved: OnResourceResolved + public let allocator: Allocator + + public init(allocator: Allocator = defaultAllocator, + onResourceResolved: @escaping OnResourceResolved) { + self.onResourceResolved = onResourceResolved + self.allocator = allocator + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientShutdownOptions.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientShutdownOptions.swift new file mode 100644 index 000000000..e424cc791 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSClientShutdownOptions.swift @@ -0,0 +1,13 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +public struct CRTIDMSClientShutdownOptions { + public typealias ShutDownCallback = () -> Void + + public let shutDownCallback: ShutDownCallback + + public init(shutDownCallback: @escaping ShutDownCallback) { + self.shutDownCallback = shutDownCallback + + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSInstanceInfo.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSInstanceInfo.swift new file mode 100644 index 000000000..33050e4c3 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSInstanceInfo.swift @@ -0,0 +1,39 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +import AwsCAuth + +public struct CRTIMDSInstanceInfo { + public let marketPlaceProductCodes: [String] + public let availabilityZone: String + public let privateIp: String + public let version: String + public let instanceId: String + public let billingProducts: [String] + public let instanceType: String + public let accountId: String + public let imageId: String + public let pendingTime: AWSDate + public let architecture: String + public let kernelId: String + public let ramDiskId: String + public let region: String + + init(pointer: UnsafePointer) { + let instanceInfo = pointer.pointee + self.marketPlaceProductCodes = instanceInfo.marketplace_product_codes.toStringArray() + self.availabilityZone = instanceInfo.availability_zone.toString() ?? "" + self.privateIp = instanceInfo.private_ip.toString() ?? "" + self.version = instanceInfo.version.toString() ?? "" + self.instanceId = instanceInfo.instance_id.toString() ?? "" + self.billingProducts = instanceInfo.billing_products.toStringArray() + self.instanceType = instanceInfo.instance_type.toString() ?? "" + self.accountId = instanceInfo.account_id.toString() ?? "" + self.imageId = instanceInfo.image_id.toString() ?? "" + self.pendingTime = AWSDate(rawValue: instanceInfo.pending_time) + self.architecture = instanceInfo.architecture.toString() ?? "" + self.kernelId = instanceInfo.kernel_id.toString() ?? "" + self.ramDiskId = instanceInfo.ramdisk_id.toString() ?? "" + self.region = instanceInfo.region.toString() ?? "" + } +} diff --git a/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSProtocolVersion.swift b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSProtocolVersion.swift new file mode 100644 index 000000000..b3ba3b302 --- /dev/null +++ b/Source/AwsCommonRuntimeKit/auth/imds/CRTIMDSProtocolVersion.swift @@ -0,0 +1,24 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0. + +import AwsCAuth + +public enum CRTIMDSProtocolVersion { + case version2 + case version1 +} + +extension CRTIMDSProtocolVersion: RawRepresentable, CaseIterable { + + public init(rawValue: aws_imds_protocol_version) { + let value = Self.allCases.first(where: {$0.rawValue == rawValue}) + self = value ?? .version2 + } + + public var rawValue: aws_imds_protocol_version { + switch self { + case .version2: return IMDS_PROTOCOL_V2 + case .version1: return IMDS_PROTOCOL_V1 + } + } +} diff --git a/Source/AwsCommonRuntimeKit/crt/AWSDate.swift b/Source/AwsCommonRuntimeKit/crt/AWSDate.swift index 3abcc7cbe..3fc44ccfc 100644 --- a/Source/AwsCommonRuntimeKit/crt/AWSDate.swift +++ b/Source/AwsCommonRuntimeKit/crt/AWSDate.swift @@ -41,6 +41,11 @@ public class AWSDate: Comparable { self.rawValue = fromPointer(ptr: aws_date_time()) aws_date_time_init_now(rawValue) } + + init(rawValue: aws_date_time) { + self.rawValue = fromPointer(ptr: rawValue) + } + public init(epochMs: UInt64) { self.rawValue = allocatePointer() aws_date_time_init_epoch_millis(rawValue, epochMs)