From dbf2464d4afd351b7a4fd336f98118aad0ffbd5a Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Tue, 18 Dec 2018 16:11:09 -0500 Subject: Implement Basic Auth rest client service. Change-Id: Ic8113f718f542aa6864943dc627b2171fccccca2 Issue-ID: CCSDK-699 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../ResourceAssignmentProcessor.kt | 3 - .../rest/BluePrintRestLibData.kt | 3 +- .../rest/service/BasicAuthRestClientService.kt | 52 ++++++++++++++-- .../rest/service/BlueprintWebClientService.kt | 4 ++ .../rest/service/DME2ProxyRestClientService.kt | 8 +++ .../rest/service/SSLBasicAuthRestClientService.kt | 69 ++++++++++++++++++++-- .../rest/utils/WebClientUtils.kt | 44 ++++++++++++++ .../rest/service/RestClientServiceTest.kt | 69 ++++++++++++++++++++++ .../execution/AbstractComponentFunction.kt | 4 ++ 9 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt create mode 100644 ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/RestClientServiceTest.kt (limited to 'ms') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolutionprocessor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolutionprocessor/ResourceAssignmentProcessor.kt index d13df9bca..a55e615ea 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolutionprocessor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolutionprocessor/ResourceAssignmentProcessor.kt @@ -55,7 +55,4 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode getResource(path: String, responseType: Class): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return getResource(path, null, responseType) + } + + override fun getResource(path: String, headers: Map?, responseType: Class): T { + return webClient().get() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .retrieve() + .bodyToMono(responseType).block()!! } override fun postResource(path: String, request: Any, responseType: Class): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return postResource(path, null, request, responseType) + } + + override fun postResource(path: String, headers: Map?, request: Any, responseType: Class): T { + return webClient().post() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .body(BodyInserters.fromObject(request)) + .retrieve().bodyToMono(responseType).block()!! } override fun exchangeResource(methodType: String, path: String, request: Any, responseType: Class): T { diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt index 232f4bb00..8106c077e 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/BlueprintWebClientService.kt @@ -24,8 +24,12 @@ interface BlueprintWebClientService { fun getResource(path: String, responseType: Class): T + fun getResource(path: String, headers: Map?, responseType: Class): T + fun postResource(path: String, request: Any, responseType: Class): T + fun postResource(path: String, headers: Map?, request: Any, responseType: Class): T + fun exchangeResource(methodType: String, path: String, request: Any, responseType: Class): T } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt index bd46ced1b..27dbe6f81 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/DME2ProxyRestClientService.kt @@ -28,10 +28,18 @@ class DME2ProxyRestClientService(restClientProperties: RestClientProperties) : B TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun getResource(path: String, headers: Map?, responseType: Class): T { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun postResource(path: String, request: Any, responseType: Class): T { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun postResource(path: String, headers: Map?, request: Any, responseType: Class): T { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun exchangeResource(methodType: String, path: String, request: Any, responseType: Class): T { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt index 8b4add100..71727b935 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/SSLBasicAuthRestClientService.kt @@ -16,20 +16,79 @@ package org.onap.ccsdk.apps.blueprintsprocessor.rest.service -import org.onap.ccsdk.apps.blueprintsprocessor.rest.RestClientProperties +import io.netty.handler.ssl.SslContextBuilder +import org.onap.ccsdk.apps.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties +import org.onap.ccsdk.apps.blueprintsprocessor.rest.utils.WebClientUtils +import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.BodyInserters import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.http.client.HttpClient +import java.io.File +import java.security.KeyStore +import java.security.cert.X509Certificate + + +class SSLBasicAuthRestClientService(private val restClientProperties: SSLBasicAuthRestClientProperties) : BlueprintWebClientService { -class SSLBasicAuthRestClientService(restClientProperties: RestClientProperties) : BlueprintWebClientService { override fun webClient(): WebClient { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + + // Load the Keystore Information + val ketInputStream = File(restClientProperties.sslKey).inputStream() + val ks = KeyStore.getInstance(restClientProperties.keyStoreInstance) + ks.load(ketInputStream, restClientProperties.sslKeyPasswd.toCharArray()) + + // Manage Trust Store + val trustCertCollection = ks.aliases().toList().map { alias -> + ks.getCertificate(alias) as X509Certificate + }.toTypedArray() + val sslContext = SslContextBuilder + .forClient() + .trustManager(*trustCertCollection) + .build() + + // Create Http Client + val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) } + + return WebClient.builder() + .baseUrl(restClientProperties.url) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .filter(WebClientUtils.logRequest()) + .clientConnector(ReactorClientHttpConnector(httpClient)).build() } override fun getResource(path: String, responseType: Class): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return getResource(path, null, responseType) + } + + override fun getResource(path: String, headers: Map?, responseType: Class): T { + return webClient().get() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .retrieve() + .bodyToMono(responseType).block()!! } override fun postResource(path: String, request: Any, responseType: Class): T { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return postResource(path, null, request, responseType) + } + + override fun postResource(path: String, headers: Map?, request: Any, responseType: Class): T { + return webClient().post() + .uri(path) + .headers { httpHeaders -> + headers?.forEach { + httpHeaders.set(it.key, it.value) + } + } + .body(BodyInserters.fromObject(request)) + .retrieve().bodyToMono(responseType).block()!! } override fun exchangeResource(methodType: String, path: String, request: Any, responseType: Class): T { diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt new file mode 100644 index 000000000..40d6ba631 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/utils/WebClientUtils.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.apps.blueprintsprocessor.rest.utils + +import org.slf4j.LoggerFactory +import org.springframework.web.reactive.function.client.ExchangeFilterFunction +import reactor.core.publisher.Mono + + +class WebClientUtils { + companion object { + + val log = LoggerFactory.getLogger(WebClientUtils::class.java)!! + + fun logRequest(): ExchangeFilterFunction { + + return ExchangeFilterFunction.ofRequestProcessor { clientRequest -> + log.info("Rest request method(${clientRequest.method()}), url(${clientRequest.url()})") + Mono.just(clientRequest) + } + } + + fun logResponse(): ExchangeFilterFunction { + return ExchangeFilterFunction.ofResponseProcessor { clientResponse -> + log.info("Response status(${clientResponse.statusCode()})") + Mono.just(clientResponse) + } + } + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/RestClientServiceTest.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/RestClientServiceTest.kt new file mode 100644 index 000000000..502758a4f --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/rest/service/RestClientServiceTest.kt @@ -0,0 +1,69 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.apps.blueprintsprocessor.rest.service + +import org.junit.runner.RunWith +import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties +import org.onap.ccsdk.apps.blueprintsprocessor.core.BlueprintPropertyConfiguration +import org.onap.ccsdk.apps.blueprintsprocessor.rest.BluePrintRestLibConfiguration +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import kotlin.test.Test +import kotlin.test.assertNotNull + +@EnableAutoConfiguration +@RunWith(SpringRunner::class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, BlueprintPropertyConfiguration::class, + SampleController::class, BluePrintProperties::class]) +@TestPropertySource(properties = +["server.port=9111", + "blueprintsprocessor.restclient.sample.type=basic-auth", + "blueprintsprocessor.restclient.sample.url=http://127.0.0.1:9111", + "blueprintsprocessor.restclient.sample.userId=sampleuser", + "blueprintsprocessor.restclient.sample.token=sampletoken"]) +class RestClientServiceTest { + + @Autowired + lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService + + @Test + fun testBaseAuth() { + + val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("sample") + val headers = mutableMapOf() + headers["X-Transaction-Id"] = "1234" + val response = restClientService.getResource("/sample/name", headers, String::class.java) + assertNotNull(response, "failed to get response") + } + +} + +@RestController +@RequestMapping("/sample") +open class SampleController { + @GetMapping("/name") + fun getName(): String = "Sample Controller" +} + diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/services/execution/AbstractComponentFunction.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/services/execution/AbstractComponentFunction.kt index 363899ea9..c9e147ba5 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/services/execution/AbstractComponentFunction.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/services/execution/AbstractComponentFunction.kt @@ -120,4 +120,8 @@ abstract class AbstractComponentFunction : BlueprintFunctionNode