From 32563249ea8a984244dc4059438db275169cad39 Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Fri, 15 Jul 2022 11:35:37 +0200 Subject: Fix environment variable substitution for complex cases Issue-ID: DCAEGEN2-3223 Signed-off-by: Tomasz Wrobel Change-Id: I9cfb612f0cabf6a2c96ccc4c80493885c02549fe --- rest-services/cbs-client/pom.xml | 24 +++++----- .../client/impl/CbsClientEnvironmentParsing.java | 39 ++++++++++++++-- .../impl/CbsClientEnvironmentParsingTest.java | 4 +- .../sample_expected_parsed_service_config.json | 54 ++++++++++++++++++++++ .../src/test/resources/sample_service_config.json | 1 + 5 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 rest-services/cbs-client/src/test/resources/sample_expected_parsed_service_config.json (limited to 'rest-services/cbs-client') diff --git a/rest-services/cbs-client/pom.xml b/rest-services/cbs-client/pom.xml index f78d97bf..e99fc44a 100644 --- a/rest-services/cbs-client/pom.xml +++ b/rest-services/cbs-client/pom.xml @@ -1,16 +1,16 @@ org.onap.dcaegen2.services.sdk dcaegen2-services-sdk-rest-services - 1.8.8-SNAPSHOT + 1.8.9-SNAPSHOT org.onap.dcaegen2.services.sdk.rest.services diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsing.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsing.java index 17144f1a..21dfb5ed 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsing.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsing.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * DCAEGEN2-SERVICES-SDK * ================================================================================ - * Copyright (C) 2021 Nokia. All rights reserved. + * Copyright (C) 2021-2022 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,9 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.util.HashMap; +import java.util.Map.Entry; +import org.jetbrains.annotations.NotNull; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.EnvironmentParsingException; import java.util.Map; import java.util.regex.Matcher; @@ -37,7 +40,8 @@ public class CbsClientEnvironmentParsing { private static final Pattern shellEnvPattern = Pattern.compile("\\$\\{(.+?)}"); - private CbsClientEnvironmentParsing() {} + private CbsClientEnvironmentParsing() { + } /** *

@@ -56,22 +60,47 @@ public class CbsClientEnvironmentParsing { processJsonObject(jsonObjectCopy); return jsonObjectCopy; } + private static void processJsonObject(JsonObject jsonObject) { for (Map.Entry entry : jsonObject.entrySet()) { processJsonObjectEntry(jsonObject, entry); } } + private static void processJsonObjectEntry(JsonObject jsonObject, Map.Entry entry) { if (entry.getValue().isJsonArray()) { processJsonArray(entry.getValue().getAsJsonArray()); } else if (entry.getValue().isJsonObject()) { processJsonObject(entry.getValue().getAsJsonObject()); } else { - Matcher matcher = getMatcher(entry.getValue().getAsString()); - if (matcher.find()) { - jsonObject.addProperty(entry.getKey(), getValueFromSystemEnv(matcher.group(1))); + Map systemEnvMatches = getSystemEnvMatches(entry); + if (systemEnvMatches.isEmpty()) { + return; } + String result = getReplacedValue(entry.getValue().getAsString(), systemEnvMatches); + + jsonObject.addProperty(entry.getKey(), result); + } + } + + @NotNull + private static Map getSystemEnvMatches(Entry entry) { + Matcher matcher = getMatcher(entry.getValue().getAsString()); + Map systemEnvMatches = new HashMap<>(); + while (matcher.find()) { + String stringTobeReplaced = matcher.group(0); + String systemEnv = matcher.group(1); + systemEnvMatches.put(stringTobeReplaced, systemEnv); + } + return systemEnvMatches; + } + + private static String getReplacedValue(String inputValue, Map systemEnvMatches) { + String result = inputValue; + for (Entry valueToReplace : systemEnvMatches.entrySet()) { + result = result.replace(valueToReplace.getKey(), getValueFromSystemEnv(valueToReplace.getValue())); } + return result; } private static void processJsonArray(JsonArray jsonArray) { diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java index 41d757fd..91e3a044 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START==================================== * DCAEGEN2-SERVICES-SDK * ========================================================= - * Copyright (C) 2021 Nokia. All rights reserved. + * Copyright (C) 2021-2022 Nokia. All rights reserved. * ========================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class CbsClientEnvironmentParsingTest { private static final String SAMPLE_CONFIG = "src/test/resources/sample_service_config.json"; - private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_service_config.json"; + private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_parsed_service_config.json"; @Rule public final EnvironmentVariables envs = new EnvironmentVariables(); diff --git a/rest-services/cbs-client/src/test/resources/sample_expected_parsed_service_config.json b/rest-services/cbs-client/src/test/resources/sample_expected_parsed_service_config.json new file mode 100644 index 00000000..e14d9609 --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/sample_expected_parsed_service_config.json @@ -0,0 +1,54 @@ +{ + "keystore.path": "/var/run/security/keystore.p12", + "testMultiReplacement": "admin/admin/admin_secret/admin_secret", + "streams_publishes": { + "perf3gpp": { + "testArray": [{ + "testPrimitiveArray": ["admin", "admin_secret", { + "nestedArray": ["admin"] + } + ], + "testPrimitive": "admin", + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + } + } + ], + "type": "kafka", + "kafka_info": { + "bootstrap_servers": "dmaap-mr-kafka:6060", + "topic_name": "HVVES_PERF3GPP" + } + }, + "pnf_ready": { + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + }, + "type": "message_router", + "dmaap_info": { + "topic_url": "http://message-router:3904/events/VES_PNF_READY" + } + }, + "call_trace": { + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + }, + "type": "kafka", + "kafka_info": { + "bootstrap_servers": "dmaap-mr-kafka:6060", + "topic_name": "HVVES_TRACE" + } + } + }, + "streams_subscribes": { + "measurements": { + "type": "message_router", + "dmaap_info": { + "topic_url": "http://message-router:3904/events/VES_MEASUREMENT" + } + } + } +} diff --git a/rest-services/cbs-client/src/test/resources/sample_service_config.json b/rest-services/cbs-client/src/test/resources/sample_service_config.json index e5798597..b0c2dd68 100644 --- a/rest-services/cbs-client/src/test/resources/sample_service_config.json +++ b/rest-services/cbs-client/src/test/resources/sample_service_config.json @@ -1,5 +1,6 @@ { "keystore.path": "/var/run/security/keystore.p12", + "testMultiReplacement": "${AAF_USER}/${AAF_USER}/${AAF_PASSWORD}/${AAF_PASSWORD}", "streams_publishes": { "perf3gpp": { "testArray": [{ -- cgit 1.2.3-korg