Skip to content

Commit

Permalink
ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
lauzadis committed Apr 25, 2024
1 parent c8a1b02 commit 90e3c87
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ class SigningTest : CrtTest() {
val expectedSignature = "8b578658fa1705d62bf26aa73e764ac4b705e6d9efd223a2d9e156580f085de4" // validated using DefaultAwsSigner
assertEquals(expectedSignature, signature)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ public actual object AwsSigner {
val userData = nativeRequest to Channel<ByteArray>(1)
val userDataStableRef = StableRef.create(userData)

val signable = checkNotNull(aws_signable_new_http_request(
allocator = Allocator.Default.allocator,
request = nativeRequest.get()
)) { "aws_signable_new_http_request" }
val signable = checkNotNull(
aws_signable_new_http_request(
allocator = Allocator.Default.allocator,
request = nativeRequest.get(),
),
) { "aws_signable_new_http_request" }

val nativeSigningConfig: CPointer<aws_signing_config_base> = config.toNativeSigningConfig().reinterpret()

awsAssertOpSuccess(aws_sign_request_aws(
allocator = Allocator.Default.allocator,
signable = signable,
base_config = nativeSigningConfig,
on_complete = staticCFunction(::signCallback),
userdata = userDataStableRef.asCPointer(),
)) { "sign() aws_sign_request_aws" }
awsAssertOpSuccess(
aws_sign_request_aws(
allocator = Allocator.Default.allocator,
signable = signable,
base_config = nativeSigningConfig,
on_complete = staticCFunction(::signCallback),
userdata = userDataStableRef.asCPointer(),
),
) { "sign() aws_sign_request_aws" }

val callbackChannel = userDataStableRef.get().second
val signature = runBlocking { callbackChannel.receive() } // wait for async signing to complete....
Expand All @@ -71,12 +75,13 @@ public actual object AwsSigner {
val chunkInputStream: CValuesRef<aws_input_stream> = checkNotNull(
aws_input_stream_new_from_cursor(
Allocator.Default.allocator,
chunkBodyPinned.asAwsByteCursor()
)) { "signChunk() aws_input_stream_new_from_cursor" }
chunkBodyPinned.asAwsByteCursor(),
),
) { "signChunk() aws_input_stream_new_from_cursor" }

prevSignature.usePinned { prevSignaturePinned ->
checkNotNull(
aws_signable_new_chunk(Allocator.Default.allocator, chunkInputStream, prevSignaturePinned.asAwsByteCursor())
aws_signable_new_chunk(Allocator.Default.allocator, chunkInputStream, prevSignaturePinned.asAwsByteCursor()),
) { "aws_signable_new_chunk unexpectedly null" }
}
}
Expand All @@ -95,20 +100,24 @@ public actual object AwsSigner {
trailingHeaders.forEach { key, values ->
key.encodeToByteArray().usePinned { keyPinned ->
val keyCursor = keyPinned.asAwsByteCursor()
values.forEach { it.encodeToByteArray().usePinned { valuePinned ->
val valueCursor = valuePinned.asAwsByteCursor()
awsAssertOpSuccess(aws_http_headers_add(nativeTrailingHeaders, keyCursor, valueCursor)) {
"signChunkTrailer() aws_http_headers_add"
values.forEach {
it.encodeToByteArray().usePinned { valuePinned ->
val valueCursor = valuePinned.asAwsByteCursor()
awsAssertOpSuccess(aws_http_headers_add(nativeTrailingHeaders, keyCursor, valueCursor)) {
"signChunkTrailer() aws_http_headers_add"
}
}
}}
}
}
}

checkNotNull(aws_signable_new_trailing_headers(
Allocator.Default.allocator,
nativeTrailingHeaders,
prevSignature.usePinned { it.asAwsByteCursor() }
)) { "aws_signable_new_trailing_headers unexpectedly null" }
checkNotNull(
aws_signable_new_trailing_headers(
Allocator.Default.allocator,
nativeTrailingHeaders,
prevSignature.usePinned { it.asAwsByteCursor() },
),
) { "aws_signable_new_trailing_headers unexpectedly null" }
}

return signChunkSignable(chunkTrailerSignable, config)
Expand All @@ -121,13 +130,15 @@ private fun signChunkSignable(signable: CPointer<aws_signable>, config: AwsSigni

val nativeConfig: CPointer<aws_signing_config_base> = config.toNativeSigningConfig().reinterpret()

awsAssertOpSuccess(aws_sign_request_aws(
allocator = Allocator.Default.allocator,
signable = signable,
base_config = nativeConfig,
on_complete = staticCFunction(::signChunkCallback),
userdata = callbackChannelStableRef.asCPointer(),
)) { "aws_sign_request_aws() failed in signChunkSignable" }
awsAssertOpSuccess(
aws_sign_request_aws(
allocator = Allocator.Default.allocator,
signable = signable,
base_config = nativeConfig,
on_complete = staticCFunction(::signChunkCallback),
userdata = callbackChannelStableRef.asCPointer(),
),
) { "aws_sign_request_aws() failed in signChunkSignable" }

// wait for async signing to complete....
val signature = runBlocking { callbackChannel.receive() }.also {
Expand Down Expand Up @@ -213,7 +224,7 @@ private fun AwsSigningConfig.toNativeSigningConfig(): CPointer<aws_signing_confi
private fun signCallback(signingResult: CPointer<aws_signing_result>?, errorCode: Int, userData: COpaquePointer?) {
awsAssertOpSuccess(errorCode) { "signing failed with code $errorCode: ${CRT.errorString(errorCode)}" }
checkNotNull(signingResult) { "signing callback received null aws_signing_result" }
checkNotNull(userData) { "signing callback received null user data"}
checkNotNull(userData) { "signing callback received null user data" }

val (pinnedRequestToSign, callbackChannel) = userData
.asStableRef<Pair<Pinned<CPointer<cnames.structs.aws_http_message>>, Channel<ByteArray>>>()
Expand All @@ -234,7 +245,7 @@ private fun signCallback(signingResult: CPointer<aws_signing_result>?, errorCode
private fun signChunkCallback(signingResult: CPointer<aws_signing_result>?, errorCode: Int, userData: COpaquePointer?) {
awsAssertOpSuccess(errorCode) { "signing failed with code $errorCode: ${CRT.errorString(errorCode)}" }
checkNotNull(signingResult) { "signing callback received null aws_signing_result" }
checkNotNull(userData) { "signing callback received null user data"}
checkNotNull(userData) { "signing callback received null user data" }

val callbackChannel = userData.asStableRef<Channel<ByteArray>>().get()
runBlocking { callbackChannel.send(signingResult.getSignature()) }
Expand All @@ -245,5 +256,5 @@ private fun Credentials.toNativeCredentials(): CPointer<cnames.structs.aws_crede
access_key_id = accessKeyId.toAwsString(),
secret_access_key = secretAccessKey.toAwsString(),
session_token = sessionToken?.toAwsString(),
expiration_timepoint_seconds = UINT64_MAX // FIXME?: Our Credentials do not have an expiration field
expiration_timepoint_seconds = UINT64_MAX, // FIXME?: Our Credentials do not have an expiration field
)
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ private fun aws_socket_options.kinit(opts: SocketOptions) {
keep_alive_timeout_sec = opts.keepAliveTimeoutSecs.convert()
}

private fun SocketType.toNativeSocketType() = when(this) {
private fun SocketType.toNativeSocketType() = when (this) {
SocketType.STREAM -> aws_socket_type.AWS_SOCKET_STREAM
SocketType.DGRAM -> aws_socket_type.AWS_SOCKET_DGRAM
}

private fun SocketDomain.toNativeSocketDomain() = when(this) {
private fun SocketDomain.toNativeSocketDomain() = when (this) {
SocketDomain.IPv4 -> aws_socket_domain.AWS_SOCKET_IPV4
SocketDomain.IPv6 -> aws_socket_domain.AWS_SOCKET_IPV6
SocketDomain.LOCAL -> aws_socket_domain.AWS_SOCKET_LOCAL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import kotlinx.cinterop.*
import libcrt.aws_checksums_crc32
import libcrt.aws_checksums_crc32c
Expand All @@ -10,7 +14,7 @@ import platform.posix.uint8_tVar
internal typealias AwsChecksumsCrcFunction = (
input: CValuesRef<uint8_tVar>?,
length: Int,
previousCrc32: uint32_t
previousCrc32: uint32_t,
) -> uint32_t

internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* A function which calculates the hash of given data
*/
Expand All @@ -16,4 +20,4 @@ public interface HashFunction {
* Reset the content of the hash
*/
public fun reset()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/
import aws.sdk.kotlin.crt.Allocator
import aws.sdk.kotlin.crt.CrtRuntimeException
import aws.sdk.kotlin.crt.awsAssertOpSuccess
import kotlinx.cinterop.*
import libcrt.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private fun Project.registerCmakeBuildTask(
"--config",
buildType.toString(),
"--parallel",
System.getProperty("org.gradle.workers.max", "16")
System.getProperty("org.gradle.workers.max", "16"),
)

val osxSdk = knTarget.konanTarget.osxDeviceSdkName
Expand Down

0 comments on commit 90e3c87

Please sign in to comment.