Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string array as an option for request context params for endpoint resolution #276

Merged
merged 10 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: GitHub Action for SwiftLint
uses: norio-nomura/action-swiftlint@3.2.1

Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:
runs-on: ubuntu-22.04 # latest
steps:
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
Expand Down
34 changes: 34 additions & 0 deletions Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

extension aws_byte_buf {
func toString() -> String {
return String(

Check warning on line 91 in Source/AwsCommonRuntimeKit/crt/Utilities.swift

View workflow job for this annotation

GitHub Actions / lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
data: toData(),
encoding: .utf8)!
}
Expand Down Expand Up @@ -136,7 +136,7 @@
func toOptionalString() -> String? {
if self.len == 0 { return nil }
let data = Data(bytesNoCopy: self.ptr, count: self.len, deallocator: .none)
return String(data: data, encoding: .utf8)

Check warning on line 139 in Source/AwsCommonRuntimeKit/crt/Utilities.swift

View workflow job for this annotation

GitHub Actions / lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
}
}

Expand Down Expand Up @@ -272,3 +272,37 @@
}
}
}

public func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] {
var result: [U] = []
result.reserveCapacity(seq.underestimatedCount)
var runningResult = initial
for element in seq {
runningResult = combine(runningResult, element)
result.append(runningResult)
}
DmitriyMusatkin marked this conversation as resolved.
Show resolved Hide resolved
return result
}

/// Note: this function and associated scan function above are copied verbatim from
/// swift standard lib where its private.
public func withArrayOfCStrings<R>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions should be internal to our module and not public. Also, if performance is not a concern, we should just implement the simpler version from https://oleb.net/blog/2016/10/swift-array-of-c-strings/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, i didnt really understand how swift works. updated to a much simpler version.

_ args: [String], _ body: ([UnsafeMutablePointer<CChar>?]) -> R
) -> R {
let argsCounts = Array(args.map { $0.utf8.count + 1 })
let argsOffsets = [ 0 ] + scan(argsCounts, 0, +)
let argsBufferSize = argsOffsets.last!
var argsBuffer: [UInt8] = []
argsBuffer.reserveCapacity(argsBufferSize)
for arg in args {
argsBuffer.append(contentsOf: arg.utf8)
argsBuffer.append(0)
}
return argsBuffer.withUnsafeMutableBufferPointer { (argsBuffer) in
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory(
to: CChar.self, capacity: argsBuffer.count)
var cStrings: [UnsafeMutablePointer<CChar>?] = argsOffsets.map { ptr + $0 }
cStrings[cStrings.count - 1] = nil
return body(cStrings)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ public class EndpointsRequestContext {
}
}

private func withByteCursorArrayFromStringArray<R>(
DmitriyMusatkin marked this conversation as resolved.
Show resolved Hide resolved
_ arg: [String], _ body: (UnsafeMutablePointer<aws_byte_cursor>, Int) -> R) -> R {
withArrayOfCStrings(arg) { cStrArr in
var cursors = cStrArr.map { aws_byte_cursor_from_c_str($0) }
let len = cursors.count
return cursors.withUnsafeMutableBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
}
DmitriyMusatkin marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Add a string array endpoint parameter to the request context
/// - Parameters:
/// - name: The name of the parameter
/// - value: The value of the parameter
public func add(name: String, value: [String]?) throws {
guard let value = value else {
return
}
DmitriyMusatkin marked this conversation as resolved.
Show resolved Hide resolved
if (name.withByteCursor { nameCursor in
withByteCursorArrayFromStringArray(value) { ptr, len in
aws_endpoints_request_context_add_string_array(allocator.rawValue, rawValue, nameCursor, ptr, len)
}
}) != AWS_OP_SUCCESS {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
}

deinit {
aws_endpoints_request_context_release(rawValue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,11 @@ class EndpointsRuleEngineTests: XCBaseTestCase {
let _ = try! engine.resolve(context: context)
}
}

func testRequestContext() {
let context = try! EndpointsRequestContext()
try! context.add(name: "Region", value: "us-west-2")
try! context.add(name: "Boolean", value: true)
try! context.add(name: "StringArray", value: ["a", "b"])
}
}
2 changes: 1 addition & 1 deletion aws-common-runtime/s2n
Submodule s2n updated 83 files
+3 −0 .github/workflows/proof_ci.yaml
+1 −1 CMakeLists.txt
+23 −0 api/s2n.h
+110 −0 api/unstable/fingerprint.h
+1 −5 bindings/rust/integration/Cargo.toml
+0 −26 bindings/rust/integration/benches/handshake.rs
+1 −1 bindings/rust/s2n-tls-sys/templates/Cargo.template
+2 −2 bindings/rust/s2n-tls-tokio/Cargo.toml
+2 −5 bindings/rust/s2n-tls/Cargo.toml
+14 −33 bindings/rust/s2n-tls/src/callbacks/pkey.rs
+5 −270 bindings/rust/s2n-tls/src/client_hello.rs
+15 −0 bindings/rust/s2n-tls/src/connection.rs
+720 −0 bindings/rust/s2n-tls/src/fingerprint.rs
+4 −2 bindings/rust/s2n-tls/src/lib.rs
+7 −215 bindings/rust/s2n-tls/src/testing.rs
+21 −58 bindings/rust/s2n-tls/src/testing/resumption.rs
+63 −242 bindings/rust/s2n-tls/src/testing/s2n_tls.rs
+0 −2 codebuild/bin/criterion_baseline.sh
+0 −2 codebuild/bin/criterion_delta.sh
+0 −7 codebuild/bin/s2n_codebuild.sh
+1 −6 codebuild/bin/s2n_codebuild_al2.sh
+0 −1 codebuild/bin/s2n_setup_env.sh
+1 −8 codebuild/spec/buildspec_generalbatch.yml
+1 −6 codebuild/spec/buildspec_omnibus.yml
+0 −2 codebuild/spec/buildspec_ubuntu_integv2criterion.yml
+100 −104 crypto/s2n_aead_cipher_aes_gcm.c
+38 −38 crypto/s2n_aead_cipher_chacha20_poly1305.c
+15 −15 crypto/s2n_cbc_cipher_3des.c
+25 −25 crypto/s2n_cbc_cipher_aes.c
+5 −5 crypto/s2n_cipher.h
+37 −37 crypto/s2n_composite_cipher_aes_sha.c
+8 −8 crypto/s2n_stream_cipher_null.c
+17 −17 crypto/s2n_stream_cipher_rc4.c
+2 −2 flake.nix
+2 −2 tests/cbmc/proofs/lib/summarize.py
+1 −6 tests/integrationv2/conftest.py
+0 −3 tests/integrationv2/global_flags.py
+0 −1 tests/integrationv2/tox.ini
+ tests/pcap/data/tcp_fragmentation.pcap
+29 −13 tests/pcap/src/handshake_message.rs
+1 −0 tests/pcap/tests/s2n_client_hellos.rs
+1 −0 tests/pems/permutations/generate-certs.sh
+14 −0 tests/pems/permutations/rsae_pkcs_1024_sha1/ca-cert.pem
+14 −0 tests/pems/permutations/rsae_pkcs_1024_sha1/client-cert.pem
+16 −0 tests/pems/permutations/rsae_pkcs_1024_sha1/client-key.pem
+42 −0 tests/pems/permutations/rsae_pkcs_1024_sha1/server-chain.pem
+16 −0 tests/pems/permutations/rsae_pkcs_1024_sha1/server-key.pem
+4 −4 tests/testlib/s2n_connection_test_utils.c
+7 −6 tests/unit/s2n_3des_test.c
+6 −6 tests/unit/s2n_aead_aes_test.c
+12 −12 tests/unit/s2n_aead_chacha20_poly1305_test.c
+8 −8 tests/unit/s2n_aes_sha_composite_test.c
+12 −12 tests/unit/s2n_aes_test.c
+0 −17 tests/unit/s2n_config_test.c
+25 −0 tests/unit/s2n_connection_test.c
+7 −7 tests/unit/s2n_fingerprint_ja3_test.c
+716 −0 tests/unit/s2n_fingerprint_test.c
+4 −4 tests/unit/s2n_handshake_io_early_data_test.c
+8 −8 tests/unit/s2n_rc4_test.c
+21 −21 tests/unit/s2n_record_size_test.c
+26 −0 tests/unit/s2n_resume_test.c
+56 −0 tests/unit/s2n_security_policy_cert_preferences_test.c
+6 −6 tests/unit/s2n_send_key_update_test.c
+226 −0 tests/unit/s2n_session_ticket_test.c
+10 −10 tests/unit/s2n_tls13_key_schedule_rfc8448_test.c
+12 −0 tests/unit/s2n_tls13_pq_handshake_test.c
+11 −11 tests/unit/s2n_tls13_record_aead_test.c
+4 −2 tls/extensions/s2n_client_session_ticket.c
+2 −1 tls/extensions/s2n_server_session_ticket.c
+20 −0 tls/s2n_config.c
+2 −0 tls/s2n_config.h
+9 −6 tls/s2n_connection.c
+2 −2 tls/s2n_crypto.c
+194 −243 tls/s2n_fingerprint.c
+58 −0 tls/s2n_fingerprint.h
+227 −0 tls/s2n_fingerprint_ja3.c
+6 −6 tls/s2n_prf.c
+13 −12 tls/s2n_resume.c
+2 −0 tls/s2n_security_policies.c
+2 −2 tls/s2n_tls13_handshake.c
+2 −2 tls/s2n_tls13_key_schedule.c
+3 −1 tls/s2n_x509_validator.c
+1 −1 utils/s2n_safety.h
Loading