From b844ce62da5ca1e5c527cd275d4e2fd04c9bfce8 Mon Sep 17 00:00:00 2001 From: Sachin Argade Date: Sat, 25 May 2024 01:19:16 +0530 Subject: [PATCH] Added utitlty functionality --- .../manager/ConnectorRegistrationManager.java | 20 +++---- .../manager/PortalIntegrationManager.java | 10 ++-- .../utility/JsonObjectProcessingUtility.java | 50 +++++++++++++++++ .../utility/KeyCloakTokenProxyUtitlity.java | 55 +++++++++++++++++++ .../ConnectorRegistrationManagerTest.java | 6 +- .../AutoSetupOrchitestratorServiceTest.java | 6 +- 6 files changed, 126 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/eclipse/tractusx/autosetup/utility/JsonObjectProcessingUtility.java create mode 100644 src/main/java/org/eclipse/tractusx/autosetup/utility/KeyCloakTokenProxyUtitlity.java diff --git a/src/main/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManager.java b/src/main/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManager.java index 38b85d9f..c63e1ca7 100644 --- a/src/main/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManager.java +++ b/src/main/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManager.java @@ -36,8 +36,8 @@ import org.eclipse.tractusx.autosetup.model.Customer; import org.eclipse.tractusx.autosetup.model.SelectedTools; import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy; -//import org.eclipse.tractusx.autosetup.utility.JsonObjectProcessingUtility; -//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; +import org.eclipse.tractusx.autosetup.utility.JsonObjectProcessingUtility; +import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; import org.eclipse.tractusx.autosetup.utility.LogUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.retry.annotation.Backoff; @@ -77,7 +77,7 @@ public class ConnectorRegistrationManager { private final AutoSetupTriggerManager autoSetupTriggerManager; private final PortalIntegrationProxy portalIntegrationProxy; - //private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; + private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; @Retryable(retryFor = { ServiceException.class }, maxAttemptsExpression = "${retry.maxAttempts}", backoff = @Backoff(delayExpression = "#{${retry.backOffDelay}}")) @@ -106,7 +106,7 @@ public Map registerConnector(Customer customerDetails, SelectedT body.add(SUBSCRIPTION_ID, subscriptionIdVal); Map header = new HashMap<>(); header.put("Authorization", - "Bearer " + ""); + "Bearer " + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI)); String connectorId = checkSubcriptionHaveConnectorRegister(header, subscriptionIdVal); @@ -175,13 +175,13 @@ private String checkSubcriptionHaveConnectorRegister(Map header, if (subcriptionWithConnectors != null && subcriptionWithConnectors.isArray()) { for (JsonNode jsonNode : subcriptionWithConnectors) { - String remoteSubscriptionId = "";//JsonObjectProcessingUtility.getValueFromJsonNode(jsonNode, - //SUBSCRIPTION_ID); + String remoteSubscriptionId = JsonObjectProcessingUtility.getValueFromJsonNode(jsonNode, + SUBSCRIPTION_ID); - if (subscriptionId.equalsIgnoreCase(remoteSubscriptionId) && jsonNode!=null) { + if (subscriptionId.equalsIgnoreCase(remoteSubscriptionId)) { - JsonNode connectorIds = null;//JsonObjectProcessingUtility.getArrayNodeFromJsonNode(jsonNode, - //"connectorIds"); + JsonNode connectorIds = JsonObjectProcessingUtility.getArrayNodeFromJsonNode(jsonNode, + "connectorIds"); if (connectorIds != null && connectorIds.isArray() && connectorIds.size() > 0) return connectorIds.get(0).asText(); @@ -214,7 +214,7 @@ public Map deleteConnector(SelectedTools tool, Map header = new HashMap<>(); header.put("Authorization", - "Bearer " + ""); + "Bearer " + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI)); autoSetupTriggerDetails.setStatus(TriggerStatusEnum.SUCCESS.name()); portalIntegrationProxy.deleteConnector(connectorRegistrationUrl, header, connectorId); diff --git a/src/main/java/org/eclipse/tractusx/autosetup/manager/PortalIntegrationManager.java b/src/main/java/org/eclipse/tractusx/autosetup/manager/PortalIntegrationManager.java index 34ca7769..19ee39e1 100644 --- a/src/main/java/org/eclipse/tractusx/autosetup/manager/PortalIntegrationManager.java +++ b/src/main/java/org/eclipse/tractusx/autosetup/manager/PortalIntegrationManager.java @@ -40,7 +40,7 @@ import org.eclipse.tractusx.autosetup.portal.model.TechnicalUserDetails; import org.eclipse.tractusx.autosetup.portal.model.TechnicalUsers; import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy; -//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; +import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; import org.eclipse.tractusx.autosetup.utility.LogUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.retry.annotation.Backoff; @@ -68,7 +68,7 @@ public class PortalIntegrationManager { private final AutoSetupTriggerManager autoSetupTriggerManager; - //private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; + private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; @Value("${portal.url}") private URI portalUrl; @@ -115,7 +115,7 @@ public Map postServiceInstanceResultAndGetTenantSpecs(Customer c Map header = new HashMap<>(); header.put(AUTHORIZATION, - BEARER + ""); + BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI)); ServiceInstanceResultRequest serviceInstanceResultRequest = ServiceInstanceResultRequest.builder() .requestId(subscriptionId).offerUrl(applicationURL).build(); @@ -248,7 +248,7 @@ private ServiceInstanceResultResponse verifyIsAlreadySubcribedActivatedAndGetDet try { header.put(AUTHORIZATION, - BEARER + ""); + BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI)); serviceInstanceResultResponse = portalIntegrationProxy.getAppServiceInstanceSubcriptionDetails( portalUrl, header, appServiceURIPath, offerId, subscriptionId); @@ -292,7 +292,7 @@ private void readTechnicalUserDetails(Map header, if (serviceInstanceResultResponse.getTechnicalUserData() != null) { header.put(AUTHORIZATION, - BEARER + ""); + BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI)); serviceInstanceResultResponse.getTechnicalUserData().forEach(elel -> { try { diff --git a/src/main/java/org/eclipse/tractusx/autosetup/utility/JsonObjectProcessingUtility.java b/src/main/java/org/eclipse/tractusx/autosetup/utility/JsonObjectProcessingUtility.java new file mode 100644 index 00000000..ec5ce8f1 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/autosetup/utility/JsonObjectProcessingUtility.java @@ -0,0 +1,50 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.autosetup.utility; + +import com.fasterxml.jackson.databind.JsonNode; + +import lombok.SneakyThrows; + + +public class JsonObjectProcessingUtility { + + private JsonObjectProcessingUtility() {} + + @SneakyThrows + public static String getValueFromJsonNode(JsonNode jsonNode, String propertyId) { + if (jsonNode != null && jsonNode.get(propertyId) != null) + return jsonNode.get(propertyId).asText(); + else + return ""; + } + + + @SneakyThrows + public static JsonNode getArrayNodeFromJsonNode(JsonNode jsonnode, String propertyId) { + if (jsonnode != null && jsonnode.get(propertyId) != null) + return jsonnode.get(propertyId); + else + return null; + } + + +} diff --git a/src/main/java/org/eclipse/tractusx/autosetup/utility/KeyCloakTokenProxyUtitlity.java b/src/main/java/org/eclipse/tractusx/autosetup/utility/KeyCloakTokenProxyUtitlity.java new file mode 100644 index 00000000..65b5453a --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/autosetup/utility/KeyCloakTokenProxyUtitlity.java @@ -0,0 +1,55 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.autosetup.utility; + +import java.net.URI; + +import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; + +@Service +@RequiredArgsConstructor +public class KeyCloakTokenProxyUtitlity { + + private final PortalIntegrationProxy portalIntegrationProxy; + + @SneakyThrows + public String getKeycloakToken(String clientId, String clientSecret, URI tokenURI) { + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("grant_type", "client_credentials"); + body.add("client_id", clientId); + body.add("client_secret", clientSecret); + var resultBody = portalIntegrationProxy.readAuthToken(tokenURI, body); + + if (resultBody != null) { + return resultBody.getAccessToken(); + } + return null; + + } + +} diff --git a/src/test/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManagerTest.java b/src/test/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManagerTest.java index 68839139..1c7a9562 100644 --- a/src/test/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManagerTest.java +++ b/src/test/java/org/eclipse/tractusx/autosetup/manager/ConnectorRegistrationManagerTest.java @@ -37,7 +37,7 @@ import org.eclipse.tractusx.autosetup.model.SelectedTools; import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy; import org.eclipse.tractusx.autosetup.utility.Certutil; -//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; +import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.mockito.InjectMocks; @@ -67,8 +67,8 @@ class ConnectorRegistrationManagerTest { @InjectMocks private AutoSetupTriggerEntry triger; -// @Mock -// private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; + @Mock + private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; @Test void createClient() throws IOException { diff --git a/src/test/java/org/eclipse/tractusx/autosetup/service/AutoSetupOrchitestratorServiceTest.java b/src/test/java/org/eclipse/tractusx/autosetup/service/AutoSetupOrchitestratorServiceTest.java index 07f5ec37..7af7b98a 100644 --- a/src/test/java/org/eclipse/tractusx/autosetup/service/AutoSetupOrchitestratorServiceTest.java +++ b/src/test/java/org/eclipse/tractusx/autosetup/service/AutoSetupOrchitestratorServiceTest.java @@ -25,7 +25,7 @@ import org.eclipse.tractusx.autosetup.mapper.AutoSetupRequestMapper; import org.eclipse.tractusx.autosetup.model.AutoSetupRequest; import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy; -//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; +import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; @@ -56,8 +56,8 @@ class AutoSetupOrchitestratorServiceTest { @MockBean private KubeAppManageProxy kubeAppManageProxy; -// @MockBean -// private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; + @MockBean + private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity; @Test void createPackage() {