Skip to content

Commit

Permalink
Fix: TLS API update & unit test fix (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera authored Apr 8, 2024
1 parent 69ec445 commit ad4cb97
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
19 changes: 10 additions & 9 deletions Source/AwsCommonRuntimeKit/io/TLSContextOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.

import struct Foundation.Data
import AwsCIo
public class TLSContextOptions: CStruct {
private var rawValue: UnsafeMutablePointer<aws_tls_ctx_options>
Expand All @@ -11,14 +12,14 @@ public class TLSContextOptions: CStruct {

/// Initializes TLSContextOptions for mutual TLS (mTLS), with client certificate and private key in the PKCS#12 format.
///
/// NOTE: This only works on Apple devices.
/// NOTE: This only works on Apple devices. The library is currently only tested on macOS.
///
/// - Parameters:
/// - pkcs12Path: Path to PKCS #12 file. The file is loaded from disk and stored internally. It must remain in
/// memory for the lifetime of the returned object.
/// - password: Password to PKCS #12 file. It must remain in memory for the lifetime of the returned object.
/// - Throws: CommonRuntimeError.crtError
#if os(macOS)
#if os(tvOS) || os(iOS) || os(watchOS) || os(macOS)
public static func makeMTLS(
pkcs12Path: String,
password: String) throws -> TLSContextOptions {
Expand All @@ -37,8 +38,8 @@ public class TLSContextOptions: CStruct {
/// - Throws: CommonRuntimeError.crtError
#if !(os(tvOS) || os(iOS) || os(watchOS))
public static func makeMTLS(
certificateData: String,
privateKeyData: String) throws -> TLSContextOptions {
certificateData: Data,
privateKeyData: Data) throws -> TLSContextOptions {
try TLSContextOptions(certificateData: certificateData, privateKeyData: privateKeyData)
}
#endif
Expand Down Expand Up @@ -78,16 +79,16 @@ public class TLSContextOptions: CStruct {
}
}

init(certificateData: String,
privateKeyData: String) throws {
init(certificateData: Data,
privateKeyData: Data) throws {
self.rawValue = allocator.allocate(capacity: 1)
guard withOptionalByteCursorPointerFromStrings(
certificateData, privateKeyData, {certificateByteCursor, privatekeyByteCursor in
guard certificateData.withAWSByteCursorPointer({ certificateByteCursor in
return privateKeyData.withAWSByteCursorPointer { privatekeyByteCursor in
return aws_tls_ctx_options_init_client_mtls(self.rawValue,
allocator.rawValue,
certificateByteCursor,
privatekeyByteCursor)
}) == AWS_OP_SUCCESS else {
}}) == AWS_OP_SUCCESS else {
throw CommonRunTimeError.crtError(CRTError.makeFromLastError())
}
}
Expand Down
55 changes: 34 additions & 21 deletions Test/AwsCommonRuntimeKitTests/io/TLSContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@ import XCTest

class TLSContextTests: XCBaseTestCase {

func testCreateTlsContextWithOptions() throws {
let options = TLSContextOptions()
let context = try TLSContext(options: options, mode: .client)
_ = TLSConnectionOptions(context: context)
}

// TODO: The test is disabled as the github CI failed on access default keychain.
// TODO: Add test for testCreateTlsContextWithRawData()
// func testCreateTlsContextWithFilePath() throws{
// try skipIfiOS()
// try skipIftvOS()
// try skipIfwatchOS()
//
// let cert_path = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_CERT")
// let private_key_path = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_KEY")
//
// let options = try TLSContextOptions.makeMTLS(certificatePath: cert_path, privateKeyPath: private_key_path)
//
// let context = try TLSContext(options: options, mode: .client)
// _ = TLSConnectionOptions(context: context)
// }
func testCreateTlsContextWithOptions() throws {
let options = TLSContextOptions()
let context = try TLSContext(options: options, mode: .client)
_ = TLSConnectionOptions(context: context)
}

#if os(macOS) || os(Linux)
func testCreateTlsContextWithFilePath() throws{

let certPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_CERT")
let privateKeyPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_KEY")

let options = try TLSContextOptions.makeMTLS(certificatePath: certPath, privateKeyPath: privateKeyPath)

let context = try TLSContext(options: options, mode: .client)
_ = TLSConnectionOptions(context: context)
}
#endif

#if os(macOS) || os(Linux)
func testCreateTlsContextWithData() throws{

let certPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_CERT")
let privateKeyPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_KEY")

let certificateData = try Data(contentsOf: URL(fileURLWithPath: certPath))
let privateKeyData = try Data(contentsOf: URL(fileURLWithPath: privateKeyPath))

let options = try TLSContextOptions.makeMTLS(certificateData: certificateData, privateKeyData: privateKeyData)

let context = try TLSContext(options: options, mode: .client)
_ = TLSConnectionOptions(context: context)
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class FileBasedConfigurationTests: XCBaseTestCase {
func testResolveConfigPath() throws {
// from $HOME
let home = "/test/home"
let oldHome = getenv("HOME")
setenv("HOME", home, 1)

XCTAssertEqual(try FileBasedConfiguration.resolveConfigPath(sourceType: .config), "\(home)/.aws/config")
XCTAssertEqual(try FileBasedConfiguration.resolveConfigPath(sourceType: .credentials), "\(home)/.aws/credentials")

Expand All @@ -84,5 +86,8 @@ class FileBasedConfigurationTests: XCBaseTestCase {
// from relative path
XCTAssertEqual(try FileBasedConfiguration.resolveConfigPath(sourceType: .config, overridePath: "~/.aws/config"), "\(home)/.aws/config")
XCTAssertEqual(try FileBasedConfiguration.resolveConfigPath(sourceType: .credentials, overridePath: "~/.aws/credentials"), "\(home)/.aws/credentials")

// reset home env
setenv("HOME", oldHome!, 1)
}
}

0 comments on commit ad4cb97

Please sign in to comment.