Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mockk = "1.14.7"
msal4j = "1.23.1"
sonarqube = "7.2.2.6593"
spotless = "8.1.0"
springBoot = "3.5.8"
springmockk = "4.0.2"
springBoot = "4.0.1"
springmockk = "5.0.1"

[libraries]
assertJ = { group = "org.assertj", name = "assertj-core" }
Expand All @@ -28,15 +28,15 @@ msal = { group = "com.microsoft.azure", name = "msal4j", version.ref = "msal4j"
slf4jApi = { group = "org.slf4j", name = "slf4j-api" }
kotlinReflect = { group = "org.jetbrains.kotlin", name = "kotlin-reflect", version.ref = "kotlin" }
kotlinLoggingJvm = { group = "io.github.oshai", name = "kotlin-logging-jvm", version.ref = "kotlinLogging" }
springBootAutoconfigure = { group = "org.springframework.boot", name = "spring-boot-autoconfigure" }
springBoot = { group = "org.springframework.boot", name = "spring-boot" }
springBootConfigurationProcessor = { group = "org.springframework.boot", name = "spring-boot-configuration-processor" }
springBootDependencies = { group = "org.springframework.boot", name = "spring-boot-dependencies", version.ref = "springBoot" }
springBootStarter = { group = "org.springframework.boot", name = "spring-boot-starter" }
springBootStarterTest = { group = "org.springframework.boot", name = "spring-boot-starter-test" }
springBootTest = { group = "org.springframework.boot", name = "spring-boot-test" }
springContext = { group = "org.springframework", name = "spring-context" }
springKafka = { group = "org.springframework.kafka", name = "spring-kafka" }
springKafkaTest = { group = "org.springframework.kafka", name = "spring-kafka-test" }
springBootKafka = { group = "org.springframework.boot", name = "spring-boot-starter-kafka" }
springBootKafkaTest = { group = "org.springframework.boot", name = "spring-boot-starter-kafka-test" }
springmockk = { group = "com.ninja-squad", name = "springmockk", version.ref = "springmockk" }
springTest = { group = "org.springframework", name = "spring-test" }

Expand Down
7 changes: 3 additions & 4 deletions kafka-message-signing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

dependencies {
implementation(libs.springContext)
implementation(libs.springKafka)
implementation(libs.springBootAutoconfigure)
implementation(libs.springBootKafka)
implementation(libs.kotlinLoggingJvm)

implementation(project(":kafka-avro"))
Expand Down Expand Up @@ -37,8 +36,8 @@ testing {
dependencies {
implementation(project())
implementation(libs.springBootStarterTest)
implementation(libs.springKafka)
implementation(libs.springKafkaTest)
implementation(libs.springBootKafka)
implementation(libs.springBootKafkaTest)
implementation(libs.kafkaClients)
implementation(libs.assertJ)
implementation(libs.avro)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object IntegrationTestHelper {
): Consumer<String, ByteArray> {
val consumerFactory =
DefaultKafkaConsumerFactory(
KafkaTestUtils.consumerProps(UUID.randomUUID().toString(), "true", embeddedKafkaBroker),
KafkaTestUtils.consumerProps(embeddedKafkaBroker, UUID.randomUUID().toString(), true),
StringDeserializer(),
ByteArrayDeserializer(),
)
Expand All @@ -52,7 +52,7 @@ object IntegrationTestHelper {
): Consumer<String, SpecificRecordBase> {
val consumerFactory =
DefaultKafkaConsumerFactory(
KafkaTestUtils.consumerProps(UUID.randomUUID().toString(), "true", embeddedKafkaBroker),
KafkaTestUtils.consumerProps(embeddedKafkaBroker, UUID.randomUUID().toString(), true),
StringDeserializer(),
AvroDeserializer(listOf(Message.getClassSchema())),
)
Expand Down Expand Up @@ -84,7 +84,8 @@ object IntegrationTestHelper {
}

class Message(private var message: String?) : SpecificRecordBase() {
constructor() : this(null) {}
@Suppress("unused") // Used reflectively by Spring Kafka
constructor() : this(null)

companion object {
fun getClassSchema(): Schema =
Expand All @@ -96,9 +97,7 @@ class Message(private var message: String?) : SpecificRecordBase() {

override fun getSchema() = getClassSchema()

override fun get(field: Int): Any {
return message!!
}
override fun get(field: Int): Any = message!!

override fun put(field: Int, value: Any) {
message = value.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import org.apache.kafka.clients.producer.ProducerRecord
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.kafka.KafkaProperties
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
import org.springframework.boot.kafka.autoconfigure.KafkaProperties
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.kafka.test.EmbeddedKafkaBroker
import org.springframework.kafka.test.context.EmbeddedKafka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,22 @@ class MessageSigner(properties: MessageSigningProperties) {
*/
fun <T> verifyUsingField(message: FlexibleSignableMessageWrapper<T>, key: PublicKey? = verificationKey): Boolean {
if (!canVerifyMessageSignatures(key)) {
logger.error(KEY_NOT_FOR_VERIFICATION)
logger.error { KEY_NOT_FOR_VERIFICATION }
return false
}

val messageSignature = message.getSignature()

if (messageSignature == null) {
logger.error("This message does not contain a signature")
logger.error { "This message does not contain a signature" }
return false
}

try {
message.clearSignature()
return verifySignatureBytes(messageSignature, toByteBuffer(message), key!!)
} catch (e: Exception) {
logger.error(UNABLE_TO_VERIFY_SIGNATURE, e)
logger.error(e) { UNABLE_TO_VERIFY_SIGNATURE }
return false
} finally {
message.setSignature(messageSignature)
Expand All @@ -267,15 +267,15 @@ class MessageSigner(properties: MessageSigningProperties) {
key: PublicKey? = verificationKey,
): Boolean {
if (!canVerifyMessageSignatures(key)) {
logger.error(KEY_NOT_FOR_VERIFICATION)
logger.error { KEY_NOT_FOR_VERIFICATION }
return false
}
try {
val signatureBytes = getSignatureBytes(consumerRecord)
val specificRecordBase: SpecificRecordBase = consumerRecord.value()
return verifySignatureBytes(ByteBuffer.wrap(signatureBytes), toByteBuffer(specificRecordBase), key!!)
} catch (e: Exception) {
logger.error(UNABLE_TO_VERIFY_SIGNATURE, e)
logger.error(e) { UNABLE_TO_VERIFY_SIGNATURE }
return false
}
}
Expand All @@ -300,13 +300,13 @@ class MessageSigner(properties: MessageSigningProperties) {
): Boolean {
try {
if (!canVerifyMessageSignatures(key)) {
logger.error(KEY_NOT_FOR_VERIFICATION)
logger.error { KEY_NOT_FOR_VERIFICATION }
return false
}
val signatureBytes = getSignatureBytes(consumerRecord)
return verifySignatureBytes(ByteBuffer.wrap(signatureBytes), ByteBuffer.wrap(consumerRecord.value()), key!!)
} catch (e: Exception) {
logger.error(UNABLE_TO_VERIFY_SIGNATURE, e)
logger.error(e) { UNABLE_TO_VERIFY_SIGNATURE }
return false
}
}
Expand Down Expand Up @@ -453,7 +453,7 @@ class MessageSigner(properties: MessageSigningProperties) {
}
try {
val content = privateKeyFile.getContentAsString(StandardCharsets.ISO_8859_1)
return PemContent.of(content).privateKey
return PemContent.of(content)?.privateKey
} catch (e: IOException) {
logger.error(e) { "Unable to read ${privateKeyFile.filename} as ISO-LATIN-1 PEM text" }
throw UncheckedIOException(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.apache.kafka.clients.producer.ProducerInterceptor
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.clients.producer.RecordMetadata

class MessageSigningAvroProducerInterceptor() : ProducerInterceptor<String, SpecificRecordBase> {
class MessageSigningAvroProducerInterceptor : ProducerInterceptor<String, SpecificRecordBase> {
private lateinit var messageSigner: MessageSigner

override fun onSend(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.apache.kafka.clients.producer.ProducerInterceptor
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.clients.producer.RecordMetadata

class MessageSigningByteArrayProducerInterceptor() : ProducerInterceptor<String, ByteArray> {
class MessageSigningByteArrayProducerInterceptor : ProducerInterceptor<String, ByteArray> {
private lateinit var messageSigner: MessageSigner

override fun onSend(producerRecord: ProducerRecord<String, ByteArray>): ProducerRecord<String, ByteArray> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ package com.gxf.utilities.kafka.message.signing.interceptors
import com.gxf.utilities.kafka.message.signing.MessageSigner
import org.apache.kafka.clients.producer.ProducerConfig
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.kafka.KafkaProperties
import org.springframework.boot.ssl.SslBundles
import org.springframework.boot.kafka.autoconfigure.KafkaProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@ConditionalOnProperty(value = ["message-signing.use-interceptor"], havingValue = "true", matchIfMissing = false)
@Configuration
class MessageSigningInterceptorAutoConfiguration(private val sslBundles: SslBundles) {
class MessageSigningInterceptorAutoConfiguration {
@Bean
fun producerPropertiesForByteArrayRecords(
kafkaProperties: KafkaProperties,
messageSigner: MessageSigner,
): Map<String, Any> {
val properties = kafkaProperties.buildProducerProperties(sslBundles)
val properties = kafkaProperties.buildProducerProperties()
properties[ProducerConfig.INTERCEPTOR_CLASSES_CONFIG] =
listOf(MessageSigningByteArrayProducerInterceptor::class.java)
properties["message.signer"] = messageSigner
Expand All @@ -31,7 +30,7 @@ class MessageSigningInterceptorAutoConfiguration(private val sslBundles: SslBund
kafkaProperties: KafkaProperties,
messageSigner: MessageSigner,
): Map<String, Any> {
val properties = kafkaProperties.buildProducerProperties(sslBundles)
val properties = kafkaProperties.buildProducerProperties()
properties[ProducerConfig.INTERCEPTOR_CLASSES_CONFIG] =
listOf(MessageSigningAvroProducerInterceptor::class.java)
properties["message.signer"] = messageSigner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class DeprecatedMessageSignerTest {
return signature
}

override fun setSignature(newSignature: ByteBuffer?) {
signature = newSignature
override fun setSignature(signature: ByteBuffer?) {
this.signature = signature
}
}
}
2 changes: 1 addition & 1 deletion oauth-token-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies{
implementation(libs.springBootAutoconfigure)
implementation(libs.springBoot)
implementation(libs.kotlinReflect)
implementation(libs.msal)

Expand Down
Loading