From 1b816444db4bd53a88eab43d6ed522503efc6f53 Mon Sep 17 00:00:00 2001 From: Andrew Charneski Date: Sat, 25 Nov 2023 11:25:00 -0500 Subject: [PATCH] AWSSDK2 --- core/build.gradle.kts | 14 ++++++-- .../simiacryptus/skyenet/core/util/AwsUtil.kt | 36 +++++++++++-------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 9747e9c9..31c2bbe7 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -28,6 +28,7 @@ kotlin { val junit_version = "5.10.1" val logback_version = "1.4.11" +val jackson_version = "2.15.3" dependencies { @@ -36,6 +37,10 @@ dependencies { implementation(group = "org.slf4j", name = "slf4j-api", version = "2.0.9") implementation(group = "commons-io", name = "commons-io", version = "2.15.0") + implementation(group = "com.fasterxml.jackson.core", name = "jackson-databind", version = jackson_version) + implementation(group = "com.fasterxml.jackson.core", name = "jackson-annotations", version = jackson_version) + implementation(group = "com.fasterxml.jackson.module", name = "jackson-module-kotlin", version = jackson_version) + compileOnlyApi(kotlin("stdlib")) implementation(group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version = "1.7.3") testImplementation(kotlin("stdlib")) @@ -46,13 +51,16 @@ dependencies { compileOnlyApi(group = "org.junit.jupiter", name = "junit-jupiter-api", version = junit_version) compileOnlyApi(group = "org.junit.jupiter", name = "junit-jupiter-engine", version = junit_version) - compileOnlyApi(group = "com.amazonaws", name = "aws-java-sdk", version = "1.12.587") + compileOnlyApi(platform("software.amazon.awssdk:bom:2.21.29")) + compileOnlyApi(group = "software.amazon.awssdk", name = "aws-sdk-java", version = "2.21.9") + testImplementation(platform("software.amazon.awssdk:bom:2.21.29")) + testImplementation(group = "software.amazon.awssdk", name = "aws-sdk-java", version = "2.21.9") + compileOnlyApi(group = "ch.qos.logback", name = "logback-classic", version = logback_version) compileOnlyApi(group = "ch.qos.logback", name = "logback-core", version = logback_version) - - testImplementation(group = "com.amazonaws", name = "aws-java-sdk", version = "1.12.587") testImplementation(group = "ch.qos.logback", name = "logback-classic", version = logback_version) testImplementation(group = "ch.qos.logback", name = "logback-core", version = logback_version) + testImplementation(group = "org.mockito", name = "mockito-core", version = "5.7.0") } diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/core/util/AwsUtil.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/core/util/AwsUtil.kt index b802212e..bb06cf5c 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/core/util/AwsUtil.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/core/util/AwsUtil.kt @@ -1,9 +1,10 @@ package com.simiacryptus.skyenet.core.util -import com.amazonaws.services.kms.AWSKMSClientBuilder -import com.amazonaws.services.kms.model.DecryptRequest -import com.amazonaws.services.kms.model.EncryptRequest -import java.nio.ByteBuffer +import software.amazon.awssdk.core.SdkBytes +import software.amazon.awssdk.regions.Region +import software.amazon.awssdk.services.kms.KmsClient +import software.amazon.awssdk.services.kms.model.DecryptRequest +import software.amazon.awssdk.services.kms.model.EncryptRequest import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Paths @@ -11,6 +12,12 @@ import java.util.* object AwsUtil { + private val kmsClient: KmsClient by lazy { + KmsClient.builder() + .region(Region.US_EAST_1) // Specify the region or use the default region provider chain + .build() + } + fun encryptFile(inputFilePath: String, outputFilePath: String) { val filePath = Paths.get(inputFilePath) val fileBytes = Files.readAllBytes(filePath) @@ -18,13 +25,13 @@ object AwsUtil { } fun encryptData(fileBytes: ByteArray, outputFilePath: String) { - val kmsClient = AWSKMSClientBuilder.standard().build() - val encryptRequest = - EncryptRequest().withKeyId("arn:aws:kms:us-east-1:470240306861:key/a1340b89-64e6-480c-a44c-e7bc0c70dcb1") - .withPlaintext(ByteBuffer.wrap(fileBytes)) + val encryptRequest = EncryptRequest.builder() + .keyId("arn:aws:kms:us-east-1:470240306861:key/a1340b89-64e6-480c-a44c-e7bc0c70dcb1") + .plaintext(SdkBytes.fromByteArray(fileBytes)) + .build() val result = kmsClient.encrypt(encryptRequest) - val cipherTextBlob = result.ciphertextBlob - val encryptedData = Base64.getEncoder().encodeToString(cipherTextBlob.array()) + val cipherTextBlob = result.ciphertextBlob() + val encryptedData = Base64.getEncoder().encodeToString(cipherTextBlob.asByteArray()) val outputPath = Paths.get(outputFilePath) Files.write(outputPath, encryptedData.toByteArray()) } @@ -35,10 +42,11 @@ object AwsUtil { throw RuntimeException("Unable to load resource: $resourceFile") } val decodedData = Base64.getDecoder().decode(encryptedData) - val kmsClient = AWSKMSClientBuilder.defaultClient() - val decryptRequest = DecryptRequest().withCiphertextBlob(ByteBuffer.wrap(decodedData)) + val decryptRequest = DecryptRequest.builder() + .ciphertextBlob(SdkBytes.fromByteArray(decodedData)) + .build() val decryptResult = kmsClient.decrypt(decryptRequest) - val decryptedData = decryptResult.plaintext.array() + val decryptedData = decryptResult.plaintext().asByteArray() return String(decryptedData, StandardCharsets.UTF_8) } -} \ No newline at end of file +}