From c5222a72ef11bd440f5c2bd017e820922ff6735a Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Wed, 28 Feb 2024 16:05:07 +0000 Subject: Add bearer token to NCMP passthrough operations (CPS-2126 #2) For NCMP resource data passthrough operations, accept an authorization header and propagate it to outgoing DMI request if it has a bearer token, otherwise use same behaviour as before Issue-ID: CPS-2128 Change-Id: Ib3bf401abce4221a8b706989fb6f07618aa33fe2 Signed-off-by: danielhanrahan --- .../org/onap/cps/ncmp/api/NetworkCmProxyDataService.java | 8 ++++++-- .../cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java | 11 +++++++---- .../org/onap/cps/ncmp/api/impl/client/DmiRestClient.java | 14 ++++++++++---- .../cps/ncmp/api/impl/operations/DmiDataOperations.java | 16 ++++++++++------ .../cps/ncmp/api/impl/operations/DmiModelOperations.java | 2 +- 5 files changed, 34 insertions(+), 17 deletions(-) (limited to 'cps-ncmp-service/src/main/java/org') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index 0c8474839b..dbfeca1804 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -59,6 +59,7 @@ public interface NetworkCmProxyDataService { * @param optionsParamInQuery options query * @param topicParamInQuery topic name for (triggering) async responses * @param requestId unique requestId for async request + * @param authorization contents of Authorization header, or null if not present * @return {@code Object} resource data */ Object getResourceDataForCmHandle(String datastoreName, @@ -66,7 +67,8 @@ public interface NetworkCmProxyDataService { String resourceIdentifier, String optionsParamInQuery, String topicParamInQuery, - String requestId); + String requestId, + String authorization); /** * Get resource data for operational. @@ -101,13 +103,15 @@ public interface NetworkCmProxyDataService { * @param operationType required operation type * @param requestBody request body to create resource * @param contentType content type in body + * @param authorization contents of Authorization header, or null if not present * @return {@code Object} return data */ Object writeResourceDataPassThroughRunningForCmHandle(String cmHandleId, String resourceIdentifier, OperationType operationType, String requestBody, - String contentType); + String contentType, + String authorization); /** * Retrieve module references for the given cm handle. diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 08afde49a1..3e304a4fd0 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -132,12 +132,14 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, - final String requestId) { + final String requestId, + final String authorization) { final ResponseEntity responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, - requestId); + requestId, + authorization); return responseEntity.getBody(); } @@ -163,9 +165,10 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String resourceIdentifier, final OperationType operationType, final String requestData, - final String dataType) { + final String dataType, + final String authorization) { return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, - operationType, requestData, dataType); + operationType, requestData, dataType, authorization); } @Override diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java index 5b93eb4853..798a280c8a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java @@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.impl.client; import com.fasterxml.jackson.databind.JsonNode; +import java.util.Locale; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties; @@ -51,12 +52,15 @@ public class DmiRestClient { * @param dmiResourceUrl dmi resource url * @param requestBodyAsJsonString json data body * @param operationType the type of operation being executed (for error reporting only) + * @param authorization contents of Authorization header, or null if not present * @return response entity of type String */ public ResponseEntity postOperationWithJsonData(final String dmiResourceUrl, final String requestBodyAsJsonString, - final OperationType operationType) { - final var httpEntity = new HttpEntity<>(requestBodyAsJsonString, configureHttpHeaders(new HttpHeaders())); + final OperationType operationType, + final String authorization) { + final var httpEntity = new HttpEntity<>(requestBodyAsJsonString, configureHttpHeaders(new HttpHeaders(), + authorization)); try { return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class); } catch (final HttpStatusCodeException httpStatusCodeException) { @@ -73,7 +77,7 @@ public class DmiRestClient { * @return plugin health status ("UP" is all OK, "" (not-specified) in case of any exception) */ public String getDmiHealthStatus(final String dmiPluginBaseUrl) { - final HttpEntity httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders())); + final HttpEntity httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders(), null)); try { final JsonNode responseHealthStatus = restTemplate.getForObject(dmiPluginBaseUrl + HEALTH_CHECK_URL_EXTENSION, @@ -86,9 +90,11 @@ public class DmiRestClient { } } - private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) { + private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders, final String authorization) { if (dmiProperties.isDmiBasicAuthEnabled()) { httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword()); + } else if (authorization != null && authorization.toLowerCase(Locale.getDefault()).startsWith("bearer ")) { + httpHeaders.add(HttpHeaders.AUTHORIZATION, authorization); } httpHeaders.setContentType(MediaType.APPLICATION_JSON); return httpHeaders; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java index fa18767dbe..05b6ec3001 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java @@ -79,6 +79,7 @@ public class DmiDataOperations extends DmiOperations { * @param optionsParamInQuery options query * @param topicParamInQuery topic name for (triggering) async responses * @param requestId requestId for async responses + * @param authorization contents of Authorization header, or null if not present * @return {@code ResponseEntity} response entity */ @Timed(value = "cps.ncmp.dmi.get", @@ -89,7 +90,8 @@ public class DmiDataOperations extends DmiOperations { final String resourceId, final String optionsParamInQuery, final String topicParamInQuery, - final String requestId) { + final String requestId, + final String authorization) { final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId); final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState(); validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState); @@ -97,7 +99,7 @@ public class DmiDataOperations extends DmiOperations { yangModelCmHandle); final String dmiResourceDataUrl = getDmiRequestUrl(dataStoreName, cmHandleId, resourceId, optionsParamInQuery, topicParamInQuery, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA)); - return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ); + return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, authorization); } /** @@ -120,7 +122,7 @@ public class DmiDataOperations extends DmiOperations { yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA)); final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState(); validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState); - return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ); + return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, null); } /** @@ -157,13 +159,15 @@ public class DmiDataOperations extends DmiOperations { * @param operationType operation enum * @param requestData the request data * @param dataType data type + * @param authorization contents of Authorization header, or null if not present * @return {@code ResponseEntity} response entity */ public ResponseEntity writeResourceDataPassThroughRunningFromDmi(final String cmHandleId, final String resourceId, final OperationType operationType, final String requestData, - final String dataType) { + final String dataType, + final String authorization) { final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId); final String jsonRequestBody = getDmiRequestBody(operationType, null, requestData, dataType, yangModelCmHandle); @@ -172,7 +176,7 @@ public class DmiDataOperations extends DmiOperations { yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA)); final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState(); validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState); - return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonRequestBody, operationType); + return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonRequestBody, operationType, authorization); } private YangModelCmHandle getYangModelCmHandle(final String cmHandleId) { @@ -250,7 +254,7 @@ public class DmiDataOperations extends DmiOperations { final String dmiDataOperationRequestAsJsonString = jsonObjectMapper.asJsonString(dmiDataOperationRequest); TaskExecutor.executeTask(() -> dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl, - dmiDataOperationRequestAsJsonString, READ), + dmiDataOperationRequestAsJsonString, READ, null), DEFAULT_ASYNC_TASK_EXECUTOR_TIMEOUT_IN_MILLISECONDS) .whenCompleteAsync((response, throwable) -> handleTaskCompletionException(throwable, dataOperationResourceUrl, dmiDataOperationRequestBodies)); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java index dbe386d7ca..f99fe86f66 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java @@ -112,7 +112,7 @@ public class DmiModelOperations extends DmiOperations { final String resourceName) { final String dmiResourceDataUrl = getDmiResourceUrl(dmiServiceName, cmHandle, resourceName); return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, - OperationType.READ); + OperationType.READ, null); } private static String getRequestBodyToFetchYangResources(final Collection newModuleReferences, -- cgit 1.2.3-korg