2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import java.util.Iterator;
41 import java.util.LinkedHashMap;
42 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.apache.commons.lang.StringUtils;
49 import org.hibernate.Query;
50 import org.hibernate.Session;
51 import org.hibernate.SessionFactory;
52 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
53 import org.onap.portalapp.portal.utils.EcompPortalUtils;
54 import org.onap.portalapp.portal.utils.PortalConstants;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
57 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
58 import org.onap.portalsdk.core.service.DataAccessService;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.PostMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.bind.annotation.RestController;
67 @RequestMapping(PortalConstants.REST_AUX_API)
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
71 public class EncryptAdminController implements BasicAuthenticationController {
73 DataAccessService dataAccessService;
75 protected SessionFactory sessionFactory;
77 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptAdminController.class);
79 @PostMapping(value = { "/executeEncryptQuery" })
80 public Map<Long, String> executeEncrypt(HttpServletRequest request, HttpServletResponse response)
81 throws CipherUtilException {
83 return fetchRecords();
86 public Map<Long, String> fetchRecords() throws CipherUtilException {
87 List appPassword = null;
88 Session localSession = null;
89 Map<Long, String> responsemap;
91 localSession = sessionFactory.openSession();
92 responsemap = new LinkedHashMap<Long, String>();
93 appPassword = dataAccessService.executeNamedQuery("getAppPassword", null, null);
94 if (appPassword != null) {
95 Iterator i = appPassword.iterator();
97 Object[] user = (Object[]) i.next();
98 Long app_id = (Long) user[0];
99 String app_password = (String) user[1];
100 if (app_id != null && StringUtils.isNotEmpty(app_password)) {
102 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(app_password));
105 localSession.getTransaction().begin();
106 query = localSession.createSQLQuery(
107 "UPDATE fn_app m SET m.app_password= :pass " + " where m.app_id = :app_id");
108 query.setParameter("pass", pass);
109 query.setParameter("app_id", app_id);
110 int result = query.executeUpdate();
111 localSession.getTransaction().commit();
112 logger.debug(EELFLoggerDelegate.debugLogger,
113 "--------------getAppPassword-------query successfull------------------"
116 } catch (Exception e) {
117 localSession.getTransaction().rollback();
118 logger.debug(EELFLoggerDelegate.debugLogger,
119 "--------------getAppPassword--------query failed-----------------" + query);
120 responsemap.put(app_id, "-------query failed-----------------" + query);
121 logger.error(EELFLoggerDelegate.errorLogger,
122 "getAppPassword error while executing the query", e);
125 } catch (Exception e) {
126 logger.error(EELFLoggerDelegate.errorLogger,
127 "getAppPassword error while executing the query", e);
132 appPassword = dataAccessService.executeNamedQuery("getBasicauthAccount", null, null);
133 if (appPassword != null) {
134 Iterator i = appPassword.iterator();
135 while (i.hasNext()) {
136 Object[] user = (Object[]) i.next();
137 Long app_id = (Long) user[0];
138 String password = (String) user[1];
139 if (app_id != null && StringUtils.isNotEmpty(password)) {
141 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
144 localSession.getTransaction().begin();
146 .createSQLQuery("UPDATE ep_basic_auth_account m SET m.password = :pass"
147 + " where m.id = :app_id");
148 query.setParameter("pass", pass);
149 query.setParameter("app_id", app_id);
150 int result = query.executeUpdate();
151 localSession.getTransaction().commit();
152 logger.debug(EELFLoggerDelegate.debugLogger,
153 "--------------getAppPassword-------query successfull------------------"
156 } catch (Exception e) {
157 localSession.getTransaction().rollback();
158 logger.debug(EELFLoggerDelegate.debugLogger,
159 "--------------getAppPassword--------query failed-----------------" + query);
160 responsemap.put(app_id, "-------query failed-----------------" + query);
161 logger.error(EELFLoggerDelegate.errorLogger,
162 "getAppPassword error while executing the query", e);
165 } catch (Exception e) {
166 logger.error(EELFLoggerDelegate.errorLogger,
167 "getAppPassword error while executing the query", e);
174 appPassword = dataAccessService.executeNamedQuery("getMicroserviceInfo", null, null);
175 if (appPassword != null) {
176 Iterator i = appPassword.iterator();
177 while (i.hasNext()) {
178 Object[] user = (Object[]) i.next();
179 Long app_id = (Long) user[0];
180 String password = (String) user[1];
181 if (app_id != null && StringUtils.isNotEmpty(password)) {
183 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
186 localSession.getTransaction().begin();
187 query = localSession.createSQLQuery(
188 "UPDATE ep_microservice m SET m.password = :pass" + " WHERE m.id = :app_id");
189 query.setParameter("pass", pass);
190 query.setParameter("app_id", app_id);
191 int result = query.executeUpdate();
192 localSession.getTransaction().commit();
193 logger.debug(EELFLoggerDelegate.debugLogger,
194 "--------------getAppPassword-------query successfull------------------"
197 } catch (Exception e) {
198 localSession.getTransaction().rollback();
199 logger.debug(EELFLoggerDelegate.debugLogger,
200 "--------------getAppPassword--------query failed-----------------" + query);
201 responsemap.put(app_id, "-------query failed-----------------" + query);
202 logger.error(EELFLoggerDelegate.errorLogger,
203 "getAppPassword error while executing the query", e);
206 } catch (Exception e) {
207 logger.error(EELFLoggerDelegate.errorLogger,
208 "getAppPassword error while executing the query", e);
217 EcompPortalUtils.closeLocalSession(localSession, "updateRecords");