Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FDP-2318: Spotless including pre-commit githook #35

Merged
merged 4 commits into from
Sep 17, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator

import org.eclipse.californium.core.CoapResource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator

import org.eclipse.californium.core.network.CoapEndpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

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 @@ -15,14 +16,11 @@ import org.junit.jupiter.api.Test
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
@Value("\${simulator.config.uri}") private lateinit var uri: URI

private val mapper = ObjectMapper()
private lateinit var coapServer: CoapServer
Expand All @@ -33,7 +31,8 @@ class SimulatorIntegrationTest {
@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
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator

import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
Expand All @@ -10,7 +9,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling


@EnableConfigurationProperties(SimulatorProperties::class)
@EnableScheduling
@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// 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 @@ -12,32 +13,37 @@ import org.eclipse.californium.scandium.dtls.pskstore.AdvancedPskStore
import org.eclipse.californium.scandium.util.SecretUtil
import org.eclipse.californium.scandium.util.ServerNames
import org.springframework.beans.factory.annotation.Value
import java.net.InetSocketAddress
import javax.crypto.SecretKey

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

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

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

var key: String = ""

override fun hasEcdhePskSupported() = true


override fun requestPskSecretResult(cid: ConnectionId?, serverName: ServerNames?, identity: PskPublicInformation, hmacAlgorithm: String?, otherSecret: SecretKey?, seed: ByteArray?, useExtendedMasterSecret: Boolean): PskSecretResult {
override fun requestPskSecretResult(
cid: ConnectionId?,
serverName: ServerNames?,
identity: PskPublicInformation,
hmacAlgorithm: String?,
otherSecret: SecretKey?,
seed: ByteArray?,
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))
}

override fun getIdentity(peerAddress: InetSocketAddress?, virtualHost: ServerNames?) = PskPublicInformation(identity)

override fun getIdentity(peerAddress: InetSocketAddress?, virtualHost: ServerNames?) =
PskPublicInformation(identity)

override fun setResultHandler(resultHandler: HandshakeResultHandler?) {
// No async handler is used, so no implementation needed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.configuration

import org.eclipse.californium.core.config.CoapConfig
Expand All @@ -25,10 +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)
.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
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.configuration

import org.gxf.crestdevicesimulator.simulator.data.entity.PreSharedKey
Expand All @@ -10,25 +9,26 @@ import org.gxf.crestdevicesimulator.simulator.data.repository.PskRepository
import org.springframework.context.annotation.Bean

@org.springframework.context.annotation.Configuration
class CoapClientConfiguration(private val simulatorProperties: SimulatorProperties,
private val pskRepository: PskRepository) {
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
)
val savedKey =
pskRepository.findFirstByIdentityAndStatusOrderByRevisionDesc(
simulatorProperties.pskIdentity, PreSharedKeyStatus.ACTIVE)

if (savedKey == null) {
val initialPreSharedKey = PreSharedKey(
simulatorProperties.pskIdentity,
0,
simulatorProperties.pskKey,
simulatorProperties.pskSecret,
PreSharedKeyStatus.ACTIVE
)
val initialPreSharedKey =
PreSharedKey(
simulatorProperties.pskIdentity,
0,
simulatorProperties.pskKey,
simulatorProperties.pskSecret,
PreSharedKeyStatus.ACTIVE)
pskRepository.save(initialPreSharedKey)
store.key = simulatorProperties.pskKey
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// 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

@ConfigurationProperties(prefix = "simulator.config")
class SimulatorProperties(
val uri: URI,
val pskIdentity: String,
val pskKey: String,
val pskSecret: String,
val sleepDuration: Duration,
val scheduledMessage: Resource,
val successMessage: Resource,
val failureMessage: Resource,
val rebootSuccessMessage: Resource,
val produceValidCbor: Boolean,
val cipherSuites: List<CipherSuite>
val uri: URI,
val pskIdentity: String,
val pskKey: String,
val pskSecret: String,
val sleepDuration: Duration,
val scheduledMessage: Resource,
val successMessage: Resource,
val failureMessage: Resource,
val rebootSuccessMessage: Resource,
val produceValidCbor: Boolean,
val cipherSuites: List<CipherSuite>
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.observability

import io.micrometer.observation.ObservationRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator

import com.fasterxml.jackson.databind.JsonNode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator

import com.fasterxml.jackson.databind.JsonNode
Expand Down Expand Up @@ -39,6 +38,5 @@ class Simulator(
}
}

fun createMessage(resource: Resource): JsonNode =
mapper.readTree(resource.inputStream)
fun createMessage(resource: Resource): JsonNode = mapper.readTree(resource.inputStream)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdevicesimulator.simulator.coap

import java.net.InetSocketAddress
import org.eclipse.californium.core.CoapClient
import org.eclipse.californium.core.coap.CoAP
import org.eclipse.californium.core.network.CoapEndpoint
Expand All @@ -14,13 +15,13 @@ 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

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

fun shutdownCoapClient(coapClient: CoapClient) {
coapClient.endpoint.stop()
Expand All @@ -32,7 +33,8 @@ class CoapClientService(
val uri = simulatorProperties.uri
val coapClient = CoapClient(uri)
if (uri.scheme == CoAP.COAP_SECURE_URI_SCHEME) {
val endpoint = CoapEndpoint.Builder()
val endpoint =
CoapEndpoint.Builder()
.setConfiguration(configuration)
.setConnector(createDtlsConnector(advancedSingleIdentityPskStore))
.build()
Expand All @@ -41,9 +43,12 @@ class CoapClientService(
return coapClient
}

private fun createDtlsConnector(advancedSingleIdentityPskStore: AdvancedSingleIdentityPskStore): DTLSConnector {
private fun createDtlsConnector(
advancedSingleIdentityPskStore: AdvancedSingleIdentityPskStore
): DTLSConnector {
val address = InetSocketAddress(0)
val dtlsBuilder = DtlsConnectorConfig.builder(configuration)
val dtlsBuilder =
DtlsConnectorConfig.builder(configuration)
.setAddress(address)
.setAdvancedPskStore(advancedSingleIdentityPskStore)
.setConnectionListener(MdcConnectionListener())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator.data.entity

import jakarta.persistence.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator.data.repository

import java.io.Serializable

class PreSharedKeyCompositeKey(
val identity: String?,
val revision: Int?
) : Serializable {
class PreSharedKeyCompositeKey(val identity: String?, val revision: Int?) : Serializable {
constructor() : this(null, null)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator.data.repository

import org.gxf.crestdevicesimulator.simulator.data.entity.PreSharedKey
Expand Down
Loading
Loading