Skip to content

Commit

Permalink
feat: wrap IMDS Client (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kneekey23 authored Oct 20, 2021
1 parent 4d46f15 commit 081b0f9
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 10 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions Source/AwsCommonRuntimeKit/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<length {
var val: UnsafeMutableRawPointer! = nil
aws_array_list_get_at(&arrayList, &val, index)
newArray[index] = val.bindMemory(to: String.self, capacity: 1).pointee
}

return newArray
}
}

public extension Int32 {
func toString() -> String? {
// Convert UnicodeScalar to a String.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,19 @@ public final class CRTAWSCredentialsProvider {
/// - credentialCallbackData: The `CredentialProviderCallbackData`options object.
public func getCredentials() -> Future<CRTCredentials> {
let future = Future<CRTCredentials>()
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<CRTCredentialsProviderCallbackData> = fromPointer(ptr: callbackData)
let pointer: UnsafeMutablePointer<CRTCredentialsCallbackData> = 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 {
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import AwsCAuth

public protocol CRTCredentialsProvider {
var allocator: Allocator {get set}
func getCredentials(credentialCallbackData: CRTCredentialsProviderCallbackData)
func getCredentials(credentialCallbackData: CRTCredentialsCallbackData)
}
17 changes: 17 additions & 0 deletions Source/AwsCommonRuntimeKit/auth/imds/CRTIAMProfile.swift
Original file line number Diff line number Diff line change
@@ -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<aws_imds_iam_profile>) {
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() ?? ""
}
}
Loading

0 comments on commit 081b0f9

Please sign in to comment.