Skip to content

Commit

Permalink
AWSSDK2
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 25, 2023
1 parent 0a30f7c commit 1b81644
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
14 changes: 11 additions & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kotlin {

val junit_version = "5.10.1"
val logback_version = "1.4.11"
val jackson_version = "2.15.3"

dependencies {

Expand All @@ -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"))
Expand All @@ -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")

}
Expand Down
36 changes: 22 additions & 14 deletions core/src/main/kotlin/com/simiacryptus/skyenet/core/util/AwsUtil.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
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
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)
encryptData(fileBytes, outputFilePath)
}

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())
}
Expand All @@ -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)
}
}
}

0 comments on commit 1b81644

Please sign in to comment.