Add Rest client that do not add any default headers 21/131421/3
authorLukasz Rajewski <lukasz.rajewski@t-mobile.pl>
Tue, 11 Oct 2022 07:59:57 +0000 (09:59 +0200)
committerLukasz Rajewski <lukasz.rajewski@t-mobile.pl>
Tue, 25 Oct 2022 07:01:33 +0000 (07:01 +0000)
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 <lukasz.rajewski@t-mobile.pl>
Change-Id: I14c219251e11733c7cdfe059c87717f6b0fded0d

ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt
ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibData.kt
ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt
ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/NoHeadersBlueprintWebClientService.kt [new file with mode: 0644]
ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/SSLRestClientService.kt
ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyServiceTest.kt

index 6e9e4b5..526f208 100644 (file)
@@ -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"
index ac6cac2..d412d0d 100644 (file)
@@ -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<String, Any>()::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<String, Any>()::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 (file)
index 0000000..761b013
--- /dev/null
@@ -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<RestClientProperties>() {
+
+    override fun getRestClientProperties(): RestClientProperties {
+        return restClientProperties
+    }
+
+    override fun defaultHeaders(): Map<String, String> {
+        return mapOf()
+    }
+}
index 602609b..8c8c70f 100644 (file)
@@ -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 {
index 0d187fb..64f3f10 100644 (file)
@@ -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" : {
           }