2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.service;
\r
22 import java.util.List;
\r
24 import javax.servlet.http.HttpServletRequest;
\r
26 import org.apache.commons.codec.binary.Base64;
\r
27 import org.openecomp.portalapp.portal.domain.EPUser;
\r
28 import org.openecomp.portalapp.portal.domain.MicroserviceData;
\r
29 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
\r
30 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
\r
31 import org.openecomp.portalapp.portal.domain.WidgetCatalogParameter;
\r
32 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
\r
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
35 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
\r
36 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
37 import org.springframework.beans.factory.annotation.Autowired;
\r
38 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r
39 import org.springframework.http.HttpEntity;
\r
40 import org.springframework.http.HttpHeaders;
\r
41 import org.springframework.http.HttpMethod;
\r
42 import org.springframework.http.MediaType;
\r
43 import org.springframework.http.ResponseEntity;
\r
44 import org.springframework.stereotype.Service;
\r
45 import org.springframework.web.client.HttpClientErrorException;
\r
46 import org.springframework.web.client.RestTemplate;
\r
48 @Service("microserviceProxyService")
\r
49 @EnableAspectJAutoProxy
\r
51 public class MicroserviceProxyServiceImpl implements MicroserviceProxyService {
\r
53 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyServiceImpl.class);
\r
54 private static final String BASIC_AUTH = "Basic Authentication";
\r
55 private static final String NO_AUTH = "No Authentication";
\r
56 private static final String COOKIE_AUTH = "Cookie based Authentication";
\r
57 private static final String QUESTION_MARK = "?";
\r
58 private static final String ADD_MARK = "&";
\r
60 String whatService = "widgets-service";
\r
63 private ConsulHealthService consulHealthService;
\r
66 MicroserviceService microserviceService;
\r
69 WidgetParameterService widgetParameterService;
\r
71 RestTemplate template = new RestTemplate();
\r
74 public String proxyToDestination(long serviceId, EPUser user, HttpServletRequest request) throws Exception {
\r
76 String response = null;
\r
78 // get the microservice object by the id
\r
79 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);
\r
81 // No such microservice available
\r
85 List<MicroserviceParameter> params = data.getParameterList();
\r
86 if (data.getSecurityType().equals(NO_AUTH)) {
\r
87 HttpHeaders headers = new HttpHeaders();
\r
88 headers.setContentType(MediaType.APPLICATION_JSON);
\r
89 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
91 String url = microserviceUrlConverter(data, params);
\r
92 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
94 } else if (data.getSecurityType().equals(BASIC_AUTH)) {
\r
95 // encoding the username and password
\r
96 String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());
\r
97 byte[] plainCredsBytes = plainCreds.getBytes();
\r
98 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
\r
99 String base64Creds = new String(base64CredsBytes);
\r
101 HttpHeaders headers = new HttpHeaders();
\r
102 headers.add("Authorization", "Basic " + base64Creds);
\r
103 headers.setContentType(MediaType.APPLICATION_JSON);
\r
104 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
106 String url = microserviceUrlConverter(data, params);
\r
107 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
108 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {
\r
109 HttpHeaders headers = new HttpHeaders();
\r
110 headers.setContentType(MediaType.APPLICATION_JSON);
\r
111 String rawCookie = request.getHeader("Cookie");
\r
112 headers.add("Cookie", rawCookie);
\r
113 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
115 String url = microserviceUrlConverter(data, params);
\r
116 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
122 public String proxyToDestinationByWidgetId(long widgetId, EPUser user, HttpServletRequest request) throws Exception {
\r
124 String response = null;
\r
126 ResponseEntity<Long> ans = (ResponseEntity<Long>) template.exchange(
\r
127 "https://" + consulHealthService.getServiceLocation(whatService)
\r
128 + "/widget/microservices/widgetCatalog/parameters/" + widgetId,
\r
129 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), Long.class);
\r
130 Long serviceId = ans.getBody();
\r
132 // get the microservice object by the id
\r
133 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);
\r
135 // No such microservice available
\r
136 if (data == null) {
\r
140 List<MicroserviceParameter> params = data.getParameterList();
\r
142 for (MicroserviceParameter p : params) {
\r
143 WidgetCatalogParameter userValue = widgetParameterService.getUserParamById(widgetId, user.getId(), p.getId());
\r
144 if (userValue != null)
\r
145 p.setPara_value(userValue.getUser_value());
\r
148 if (data.getSecurityType().equals(NO_AUTH)) {
\r
149 HttpHeaders headers = new HttpHeaders();
\r
150 headers.setContentType(MediaType.APPLICATION_JSON);
\r
151 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
153 String url = microserviceUrlConverter(data, params);
\r
155 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
156 } catch (HttpClientErrorException e) {
\r
159 } else if (data.getSecurityType().equals(BASIC_AUTH)) {
\r
160 // encoding the username and password
\r
161 String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());
\r
162 byte[] plainCredsBytes = plainCreds.getBytes();
\r
163 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
\r
164 String base64Creds = new String(base64CredsBytes);
\r
166 HttpHeaders headers = new HttpHeaders();
\r
167 headers.add("Authorization", "Basic " + base64Creds);
\r
168 headers.setContentType(MediaType.APPLICATION_JSON);
\r
169 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
171 String url = microserviceUrlConverter(data, params);
\r
173 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
174 } catch (HttpClientErrorException e) {
\r
177 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {
\r
178 HttpHeaders headers = new HttpHeaders();
\r
179 headers.setContentType(MediaType.APPLICATION_JSON);
\r
180 String rawCookie = request.getHeader("Cookie");
\r
181 headers.add("Cookie", rawCookie);
\r
182 HttpEntity<String> entity = new HttpEntity<String>(headers);
\r
184 String url = microserviceUrlConverter(data, params);
\r
186 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
\r
187 } catch (HttpClientErrorException e) {
\r
194 private String decryptedPassword(String encryptedPwd) throws Exception {
\r
195 String result = "";
\r
196 if (encryptedPwd != null & encryptedPwd.length() > 0) {
\r
198 result = CipherUtil.decrypt(encryptedPwd,
\r
199 SystemProperties.getProperty(SystemProperties.Decryption_Key));
\r
200 } catch (Exception e) {
\r
201 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
\r
208 private String microserviceUrlConverter(MicroserviceData data, List<MicroserviceParameter> params) {
\r
209 String url = data.getUrl();
\r
210 for (int i = 0; i < params.size(); i++) {
\r
212 url += QUESTION_MARK;
\r
214 url += params.get(i).getPara_key() + "=" + params.get(i).getPara_value();
\r
215 if (i != (params.size() - 1)) {
\r