aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Wielebski <piotr.wielebski@nokia.com>2018-12-14 11:26:54 +0000
committerGerrit Code Review <gerrit@onap.org>2018-12-14 11:26:54 +0000
commitbca686a94396f704ecca299d7fe2be861a46557f (patch)
tree7ee38a49a5dd7ba323e7aa8f51de8cf5e7b43ef4
parente32d91541ffa6c3ec13729032979af8160bdb8dc (diff)
parentdce766b24707cc22b4857a2705bcda8039574fbd (diff)
Merge "Use SHA-256 for detecting changes in Consul config"
-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)