Skip to content

Commit

Permalink
FDP-2606 ~ Replaces ktfmt by diktat and fixes diktat issues
Browse files Browse the repository at this point in the history
Signed-off-by: Sander van der Heijden <[email protected]>
  • Loading branch information
smvdheijden committed Sep 19, 2024
1 parent 0bb22a7 commit 7c0d194
Show file tree
Hide file tree
Showing 24 changed files with 767 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.eclipse.californium.core.server.resources.CoapExchange
class CoapResourceStub : CoapResource("coap-path") {
var lastRequestPayload = byteArrayOf()

@Suppress("FUNCTION_NAME_INCORRECT_CASE") // Prevent diktat from renaming to lower camel case
override fun handlePOST(coapExchange: CoapExchange) {
lastRequestPayload = coapExchange.requestPayload
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import org.eclipse.californium.core.network.Endpoint
import org.eclipse.californium.elements.config.Configuration

object CoapServerHelpers {
fun createEndpoint(config: Configuration, port: Int): Endpoint {
return CoapEndpoint.builder().setConfiguration(config).setPort(port).build()
}
fun createEndpoint(
config: Configuration,
port: Int): Endpoint = CoapEndpoint.builder()
.setConfiguration(config)
.setPort(port)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package org.gxf.crestdevicesimulator

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper
import java.net.URI
import java.time.Duration
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility
import org.eclipse.californium.core.CoapServer
Expand All @@ -17,22 +15,23 @@ import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.io.ClassPathResource

import java.net.URI
import java.time.Duration

@SpringBootTest
class SimulatorIntegrationTest {

@Value("\${simulator.config.uri}") private lateinit var uri: URI

private val mapper = ObjectMapper()
private lateinit var coapServer: CoapServer
private val coapResourceStub = CoapResourceStub()
private val expectedJsonNode =
mapper.readTree(ClassPathResource("messages/kod-message.json").file)
private val expectedJsonNode = mapper.readTree(ClassPathResource("messages/kod-message.json").file)

@Value("\${simulator.config.uri}")
private lateinit var uri: URI
private lateinit var coapServer: CoapServer

@BeforeEach
fun setup() {
coapServer = CoapServer(Configuration.getStandard())
coapServer.addEndpoint(
CoapServerHelpers.createEndpoint(Configuration.getStandard(), uri.port))
coapServer.addEndpoint(CoapServerHelpers.createEndpoint(Configuration.getStandard(), uri.port))
coapServer.add(coapResourceStub)
coapServer.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.configuration

import java.net.InetSocketAddress
import javax.crypto.SecretKey
import org.eclipse.californium.scandium.dtls.ConnectionId
import org.eclipse.californium.scandium.dtls.HandshakeResultHandler
import org.eclipse.californium.scandium.dtls.PskPublicInformation
Expand All @@ -14,16 +12,18 @@ import org.eclipse.californium.scandium.util.SecretUtil
import org.eclipse.californium.scandium.util.ServerNames
import org.springframework.beans.factory.annotation.Value

class AdvancedSingleIdentityPskStore(private val identity: String) : AdvancedPskStore {

companion object {
private const val ALGORITHM = "PSK"
}

@Value("\${simulator.config.psk-key}") lateinit var defaultKey: String
import java.net.InetSocketAddress
import javax.crypto.SecretKey

/**
* @param identity
*/
class AdvancedSingleIdentityPskStore(private val identity: String) : AdvancedPskStore {
var key: String = ""

@Value("\${simulator.config.psk-key}")
lateinit var defaultKey: String

override fun hasEcdhePskSupported() = true

override fun requestPskSecretResult(
Expand All @@ -36,8 +36,7 @@ class AdvancedSingleIdentityPskStore(private val identity: String) : AdvancedPsk
useExtendedMasterSecret: Boolean
): PskSecretResult {
if (key.isEmpty()) {
return PskSecretResult(
cid, identity, SecretUtil.create(defaultKey.toByteArray(), ALGORITHM))
return PskSecretResult(cid, identity, SecretUtil.create(defaultKey.toByteArray(), ALGORITHM))
}
return PskSecretResult(cid, identity, SecretUtil.create(key.toByteArray(), ALGORITHM))
}
Expand All @@ -48,4 +47,8 @@ class AdvancedSingleIdentityPskStore(private val identity: String) : AdvancedPsk
override fun setResultHandler(resultHandler: HandshakeResultHandler?) {
// No async handler is used, so no implementation needed
}

companion object {
private const val ALGORITHM = "PSK"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import org.eclipse.californium.scandium.config.DtlsConfig
import org.eclipse.californium.scandium.config.DtlsConfig.DtlsRole
import org.springframework.context.annotation.Bean

/**
* @param simulatorProperties
*/
@org.springframework.context.annotation.Configuration
class CaliforniumConfiguration(private val simulatorProperties: SimulatorProperties) {

init {
DtlsConfig.register()
CoapConfig.register()
Expand All @@ -22,12 +24,10 @@ class CaliforniumConfiguration(private val simulatorProperties: SimulatorPropert
}

@Bean
fun configure(): Configuration {
return Configuration.getStandard()
.set(CoapConfig.COAP_PORT, simulatorProperties.uri.port)
.set(CoapConfig.COAP_SECURE_PORT, simulatorProperties.uri.port)
.set(DtlsConfig.DTLS_ROLE, DtlsRole.CLIENT_ONLY)
.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, false)
.set(DtlsConfig.DTLS_CIPHER_SUITES, simulatorProperties.cipherSuites)
}
fun configure(): Configuration = Configuration.getStandard()
.set(CoapConfig.COAP_PORT, simulatorProperties.uri.port)
.set(CoapConfig.COAP_SECURE_PORT, simulatorProperties.uri.port)
.set(DtlsConfig.DTLS_ROLE, DtlsRole.CLIENT_ONLY)
.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, false)
.set(DtlsConfig.DTLS_CIPHER_SUITES, simulatorProperties.cipherSuites)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import org.gxf.crestdevicesimulator.simulator.data.entity.PreSharedKeyStatus
import org.gxf.crestdevicesimulator.simulator.data.repository.PskRepository
import org.springframework.context.annotation.Bean

/**
* @param simulatorProperties
* @param pskRepository
*/
@org.springframework.context.annotation.Configuration
class CoapClientConfiguration(
private val simulatorProperties: SimulatorProperties,
private val pskRepository: PskRepository
) {

@Bean
fun pskStore(): AdvancedSingleIdentityPskStore {
val store = AdvancedSingleIdentityPskStore(simulatorProperties.pskIdentity)
val savedKey =
pskRepository.findFirstByIdentityAndStatusOrderByRevisionDesc(
simulatorProperties.pskIdentity, PreSharedKeyStatus.ACTIVE)

if (savedKey == null) {
savedKey?.let {
store.key = savedKey.preSharedKey
} ?: run {
val initialPreSharedKey =
PreSharedKey(
simulatorProperties.pskIdentity,
Expand All @@ -31,8 +36,6 @@ class CoapClientConfiguration(
PreSharedKeyStatus.ACTIVE)
pskRepository.save(initialPreSharedKey)
store.key = simulatorProperties.pskKey
} else {
store.key = savedKey.preSharedKey
}

return store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.configuration

import java.net.URI
import java.time.Duration
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.core.io.Resource

import java.net.URI
import java.time.Duration

/**
* @property uri
* @property pskIdentity
* @property pskKey
* @property pskSecret
* @property sleepDuration
* @property scheduledMessage
* @property successMessage
* @property failureMessage
* @property rebootSuccessMessage
* @property produceValidCbor
* @property cipherSuites
*/
@ConfigurationProperties(prefix = "simulator.config")
class SimulatorProperties(
val uri: URI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
internal class ObservabilityConfiguration {

@Bean
fun observedAspect(observationRegistry: ObservationRegistry): ObservedAspect {
return ObservedAspect(observationRegistry)
}
fun observedAspect(observationRegistry: ObservationRegistry) = ObservedAspect(observationRegistry)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.simulator

import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.message.MessageHandler

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.github.oshai.kotlinlogging.KotlinLogging
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.message.MessageHandler
import org.springframework.boot.CommandLineRunner
import org.springframework.core.io.Resource
import org.springframework.stereotype.Component

/**
* @param simulatorProperties
* @param messageHandler
* @param mapper
*/
@Component
class Simulator(
private val simulatorProperties: SimulatorProperties,
private val messageHandler: MessageHandler,
private val mapper: ObjectMapper
) : CommandLineRunner {

private val logger = KotlinLogging.logger {}

override fun run(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.simulator.coap

import java.net.InetSocketAddress
import org.gxf.crestdevicesimulator.configuration.AdvancedSingleIdentityPskStore
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties

import org.eclipse.californium.core.CoapClient
import org.eclipse.californium.core.coap.CoAP
import org.eclipse.californium.core.network.CoapEndpoint
Expand All @@ -12,17 +14,21 @@ import org.eclipse.californium.scandium.DTLSConnector
import org.eclipse.californium.scandium.MdcConnectionListener
import org.eclipse.californium.scandium.config.DtlsConnectorConfig
import org.eclipse.californium.scandium.dtls.ProtocolVersion
import org.gxf.crestdevicesimulator.configuration.AdvancedSingleIdentityPskStore
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.springframework.stereotype.Service

import java.net.InetSocketAddress

/**
* @param simulatorProperties
* @param advancedSingleIdentityPskStore
* @param configuration
*/
@Service
class CoapClientService(
private val simulatorProperties: SimulatorProperties,
private val advancedSingleIdentityPskStore: AdvancedSingleIdentityPskStore,
private val configuration: Configuration
) {

fun shutdownCoapClient(coapClient: CoapClient) {
coapClient.endpoint.stop()
coapClient.endpoint.destroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.simulator.data.entity

import org.gxf.crestdevicesimulator.simulator.data.repository.PreSharedKeyCompositeKey

import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.Id
import jakarta.persistence.IdClass
import org.gxf.crestdevicesimulator.simulator.data.repository.PreSharedKeyCompositeKey

/**
* @property identity
* @property revision
* @property preSharedKey
* @property secret
* @property status
*/
@Entity
@IdClass(PreSharedKeyCompositeKey::class)
class PreSharedKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.gxf.crestdevicesimulator.simulator.data.entity
enum class PreSharedKeyStatus {
ACTIVE,
INACTIVE,
INVALID,
PENDING,
INVALID
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package org.gxf.crestdevicesimulator.simulator.data.repository

import java.io.Serializable

/**
* @property identity
* @property revision
*/
class PreSharedKeyCompositeKey(val identity: String?, val revision: Int?) : Serializable {
constructor() : this(null, null)
}
Loading

0 comments on commit 7c0d194

Please sign in to comment.