From 1c887f8ea35f836ec57a9ba6ea5d6d04093ed051 Mon Sep 17 00:00:00 2001 From: Lukasz Rajewski Date: Tue, 11 Oct 2022 09:59:57 +0200 Subject: [PATCH] Add Rest client that do not add any default headers In consequence we can specify client without default headers. Still, we can specify additional headers in the client properties. We can use data disctionary definition to add extra headers. Sice Kohn we can also template the headers section in the data dictionary so ssuch headers can be created dynamically. Issue-ID: CCSDK-3787 Signed-off-by: Lukasz Rajewski Change-Id: I14c219251e11733c7cdfe059c87717f6b0fded0d --- .../rest/BluePrintRestLibConfiguration.kt | 2 + .../rest/BluePrintRestLibData.kt | 1 + .../service/BluePrintRestLibPropertyService.kt | 48 ++++++++++++--- .../service/NoHeadersBlueprintWebClientService.kt | 33 +++++++++++ .../rest/service/SSLRestClientService.kt | 12 ++-- .../service/BluePrintRestLibPropertyServiceTest.kt | 69 ++++++++++++++++++++++ 6 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/NoHeadersBlueprintWebClientService.kt diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt index 6e9e4b554..526f208c6 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt @@ -51,6 +51,8 @@ class RestLibConstants { const val SERVICE_BLUEPRINT_REST_LIB_PROPERTY = "blueprint-rest-lib-property-service" const val PROPERTY_REST_CLIENT_PREFIX = "blueprintsprocessor.restclient." const val PROPERTY_TYPE = "type" + const val TYPE_NO_DEF_HEADERS = "no-def-headers" + const val TYPE_SSL_NO_DEF_HEADERS = "ssl-no-def-headers" const val TYPE_TOKEN_AUTH = "token-auth" const val TYPE_BASIC_AUTH = "basic-auth" const val TYPE_SSL_BASIC_AUTH = "ssl-basic-auth" diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt index b0282c40f..a12680e07 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt @@ -22,6 +22,7 @@ open class RestClientProperties { lateinit var type: String lateinit var url: String + lateinit var values: Map var connectTimeout: Int = 0 var socketTimeout: Int = 0 var connectionRequestTimeout: Int = 0 diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt index ac6cac2b7..d412d0dbc 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt @@ -75,7 +75,16 @@ open class BluePrintRestLibPropertyService(private var bluePrintPropertiesServic val type = bluePrintPropertiesService.propertyBeanType( "$prefix.type", String::class.java ) - return when (type) { + val allValues = bluePrintPropertiesService.propertyBeanType( + prefix, HashMap()::class.java + ) + val properties = when (type) { + RestLibConstants.TYPE_NO_DEF_HEADERS -> { + noDefHeadersRestClientProperties(prefix, false) + } + RestLibConstants.TYPE_SSL_NO_DEF_HEADERS -> { + noDefHeadersRestClientProperties(prefix, true) + } RestLibConstants.TYPE_BASIC_AUTH -> { basicAuthRestClientProperties(prefix) } @@ -102,12 +111,18 @@ open class BluePrintRestLibPropertyService(private var bluePrintPropertiesServic ) } } + properties.values = allValues + return properties } fun restClientProperties(jsonNode: JsonNode): RestClientProperties { val type = jsonNode.get("type").textValue() - return when (type) { + val allValues = JacksonUtils.readValue(jsonNode, HashMap()::class.java)!! + val properties = when (type) { + RestLibConstants.TYPE_NO_DEF_HEADERS -> { + JacksonUtils.readValue(jsonNode, RestClientProperties::class.java)!! + } RestLibConstants.TYPE_TOKEN_AUTH -> { JacksonUtils.readValue(jsonNode, TokenAuthRestClientProperties::class.java)!! } @@ -115,6 +130,9 @@ open class BluePrintRestLibPropertyService(private var bluePrintPropertiesServic JacksonUtils.readValue(jsonNode, BasicAuthRestClientProperties::class.java)!! } + RestLibConstants.TYPE_SSL_NO_DEF_HEADERS -> { + JacksonUtils.readValue(jsonNode, SSLRestClientProperties::class.java)!! + } RestLibConstants.TYPE_POLICY_MANAGER -> { JacksonUtils.readValue(jsonNode, PolicyManagerRestClientProperties::class.java)!! } @@ -133,27 +151,41 @@ open class BluePrintRestLibPropertyService(private var bluePrintPropertiesServic ) } } + properties.values = allValues + return properties } private fun blueprintWebClientService(restClientProperties: RestClientProperties): BlueprintWebClientService { - - when (restClientProperties) { + return when (restClientProperties) { is SSLRestClientProperties -> { - return SSLRestClientService(restClientProperties) + SSLRestClientService(restClientProperties) } is TokenAuthRestClientProperties -> { - return TokenAuthRestClientService(restClientProperties) + TokenAuthRestClientService(restClientProperties) } is BasicAuthRestClientProperties -> { - return BasicAuthRestClientService(restClientProperties) + BasicAuthRestClientService(restClientProperties) } else -> { - throw BluePrintProcessorException("couldn't get rest service for type:${restClientProperties.type} uri: ${restClientProperties.url}") + NoHeadersBlueprintWebClientService(restClientProperties) } } } + private fun noDefHeadersRestClientProperties(prefix: String, ssl: Boolean): + RestClientProperties { + return if (ssl) { + bluePrintPropertiesService.propertyBeanType( + prefix, SSLRestClientProperties::class.java + ) + } else { + bluePrintPropertiesService.propertyBeanType( + prefix, RestClientProperties::class.java + ) + } + } + private fun tokenRestClientProperties(prefix: String): TokenAuthRestClientProperties { return bluePrintPropertiesService.propertyBeanType( diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/NoHeadersBlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/NoHeadersBlueprintWebClientService.kt new file mode 100644 index 000000000..761b0137e --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/NoHeadersBlueprintWebClientService.kt @@ -0,0 +1,33 @@ +/* + * Copyright © 2022 Deutsche Telekom AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.rest.service + +import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties + +open class NoHeadersBlueprintWebClientService( + private val restClientProperties: RestClientProperties +) : + BaseBlueprintWebClientService() { + + override fun getRestClientProperties(): RestClientProperties { + return restClientProperties + } + + override fun defaultHeaders(): Map { + return mapOf() + } +} diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt index 602609b6a..8c8c70ff6 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt @@ -25,6 +25,7 @@ import org.apache.http.message.BasicHeader import org.apache.http.ssl.SSLContextBuilder import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties +import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties @@ -80,10 +81,13 @@ open class SSLRestClientService(private val restClientProperties: SSLRestClientP if (auth != null) { return auth!!.defaultHeaders() } - return mapOf( - HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, - HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE - ) + return if (restClientProperties.type == RestLibConstants.TYPE_SSL_NO_DEF_HEADERS) + mapOf() + else + mapOf( + HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE, + HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE + ) } override fun httpClient(): CloseableHttpClient { diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyServiceTest.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyServiceTest.kt index 0d187fbc2..64f3f10ac 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyServiceTest.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyServiceTest.kt @@ -39,6 +39,7 @@ import org.springframework.test.context.junit4.SpringRunner import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertNotNull +import kotlin.test.assertTrue @RunWith(SpringRunner::class) @ContextConfiguration( @@ -197,6 +198,40 @@ class BluePrintRestLibPropertyServiceTest { assertEquals("https://localhost:8443", p.url) } + @Test + fun testSSLNoDefHeadersPropertiesAsJson() { + val actualObj: JsonNode = defaultMapper.readTree(sslNoDefHeadersField()) + val properties = bluePrintRestLibPropertyService.restClientProperties( + actualObj + ) + assertNotNull(properties, "failed to create property bean") + + val p: SSLRestClientProperties = + properties as SSLRestClientProperties + + assertEquals("src/test/resources/keystore.p12", p.sslTrust) + assertEquals("changeit", p.sslTrustPassword) + assertEquals("PKCS12", p.keyStoreInstance) + assertEquals("src/test/resources/keystore.p12", p.sslKey) + assertEquals("changeit", p.sslKeyPassword) + assertEquals("ssl-no-def-headers", p.type) + assertEquals("https://localhost:8443", p.url) + assertTrue(p.values.containsKey("type")) + } + + @Test + fun testNoDefHeadersPropertiesAsJson() { + val actualObj: JsonNode = defaultMapper.readTree(noDefaultHeadersField()) + val p = bluePrintRestLibPropertyService.restClientProperties( + actualObj + ) + assertNotNull(p, "failed to create property bean") + + assertEquals("no-def-headers", p.type) + assertEquals("http://127.0.0.1:8000", p.url) + assertTrue(p.values.containsKey("type")) + } + @Test fun testBlueprintWebClientService() { val blueprintWebClientService = bluePrintRestLibPropertyService @@ -215,6 +250,22 @@ class BluePrintRestLibPropertyServiceTest { assertNotNull(blueprintWebClientService, "failed to create blueprintWebClientService") } + @Test + fun testNoHeadersForNoDefaultHeaderService() { + val actualObj: JsonNode = defaultMapper.readTree(noDefaultHeadersField()) + val blueprintWebClientService = bluePrintRestLibPropertyService + .blueprintWebClientService(actualObj) + assertEquals(0, blueprintWebClientService.defaultHeaders().size) + } + + @Test + fun testNoHeadersForSSLNoDefaultHeaderService() { + val actualObj: JsonNode = defaultMapper.readTree(sslNoDefHeadersField()) + val blueprintWebClientService = bluePrintRestLibPropertyService + .blueprintWebClientService(actualObj) + assertEquals(0, blueprintWebClientService.defaultHeaders().size) + } + // pass the result of $typeEndpointWithHeadersField() output with and without headers to compare. private fun validateHeadersDidNotChangeWithEmptyAdditionalHeaders(noHeaders: String, withHeaders: String) { val parsedObj: JsonNode = defaultMapper.readTree(noHeaders) @@ -480,6 +531,17 @@ class BluePrintRestLibPropertyServiceTest { } """.trimIndent() + private fun sslNoDefHeadersField(): String = """{ + "type" : "ssl-no-def-headers", + "url" : "https://localhost:8443", + "keyStoreInstance" : "PKCS12", + "sslTrust" : "src/test/resources/keystore.p12", + "sslTrustPassword" : "changeit", + "sslKey" : "src/test/resources/keystore.p12", + "sslKeyPassword" : "changeit" + } + """.trimIndent() + // Don't forget to supply "," as the first char to make valid JSON private fun basicAuthEndpointWithHeadersField(headers: String = ""): String = """{ @@ -490,6 +552,13 @@ class BluePrintRestLibPropertyServiceTest { } """.trimIndent() + private fun noDefaultHeadersField(): String = + """{ + "type": "no-def-headers", + "url": "http://127.0.0.1:8000" + } + """.trimIndent() + private val emptyAdditionalHeaders = """, "additionalHeaders" : { } -- 2.16.6