summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-12-14 09:10:36 +0100
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-12-14 09:43:36 +0100
commitdce766b24707cc22b4857a2705bcda8039574fbd (patch)
treed4f986c808c587aeebf7f86dd4ccefe411914a2b
parentd178c6dc0bcaf739d48f47191a20d8335a291883 (diff)
Use SHA-256 for detecting changes in Consul config
String.hashCode is probably not strong enough and was not designed to be used in that way. Change-Id: Iecb75dcdc742d1673b81cbaa213cff69297ff6f4 Issue-ID: DCAEGEN2-1034 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
-rw-r--r--sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProvider.kt18
1 files changed, 11 insertions, 7 deletions
diff --git a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProvider.kt b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProvider.kt
index 14d511be..e4453c90 100644
--- a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProvider.kt
+++ b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProvider.kt
@@ -32,6 +32,7 @@ import reactor.core.publisher.Mono
import reactor.retry.Jitter
import reactor.retry.Retry
import java.io.StringReader
+import java.security.MessageDigest
import java.time.Duration
import java.util.*
import java.util.concurrent.atomic.AtomicReference
@@ -51,8 +52,7 @@ internal class ConsulConfigurationProvider(private val http: HttpAdapter,
retrySpec: Retry<Any>
) : ConfigurationProvider {
-
- private val lastConfigurationHash: AtomicReference<Int> = AtomicReference(0)
+ private val lastConfigurationHash: AtomicReference<ByteArray> = AtomicReference(byteArrayOf())
private val retry = retrySpec
.doOnRetry {
logger.withWarn(ServiceContext::mdc) { log("Could not get fresh configuration", it.exception()) }
@@ -87,8 +87,8 @@ internal class ConsulConfigurationProvider(private val http: HttpAdapter,
private fun filterDifferentValues(configuration: BodyWithInvocationId) =
configuration.body.let { configurationString ->
- hashOf(configurationString).let {
- if (it == lastConfigurationHash.get()) {
+ configurationString.sha256().let { newHash ->
+ if (newHash contentEquals lastConfigurationHash.get()) {
logger.trace(ServiceContext::mdc, Marker.Invoke(configuration.invocationId)) {
"No change detected in consul configuration"
}
@@ -97,14 +97,12 @@ internal class ConsulConfigurationProvider(private val http: HttpAdapter,
logger.info(ServiceContext::mdc, Marker.Invoke(configuration.invocationId)) {
"Obtained new configuration from consul:\n${configurationString}"
}
- lastConfigurationHash.set(it)
+ lastConfigurationHash.set(newHash)
Mono.just(configurationString)
}
}
}
- private fun hashOf(str: String) = str.hashCode()
-
private fun parseJsonResponse(responseString: String): JsonObject =
Json.createReader(StringReader(responseString)).readObject()
@@ -130,6 +128,12 @@ internal class ConsulConfigurationProvider(private val http: HttpAdapter,
private const val MAX_RETRIES = 5L
private const val BACKOFF_INTERVAL_FACTOR = 30L
private val logger = Logger(ConsulConfigurationProvider::class)
+
+ private fun String.sha256() =
+ MessageDigest
+ .getInstance("SHA-256")
+ .digest(toByteArray())
+
}
private data class BodyWithInvocationId(val body: String, val invocationId: UUID)