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.RequestMethod;
63 import org.springframework.web.bind.annotation.RestController;
66 @RequestMapping(PortalConstants.REST_AUX_API)
67 @org.springframework.context.annotation.Configuration
68 @EnableAspectJAutoProxy
70 public class EncryptAdminController implements BasicAuthenticationController {
72 DataAccessService dataAccessService;
74 protected SessionFactory sessionFactory;
76 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptAdminController.class);
78 @RequestMapping(value = { "/executeEncryptQuery" }, method = RequestMethod.POST)
79 public Map<Long, String> executeEncrypt(HttpServletRequest request, HttpServletResponse response)
80 throws CipherUtilException {
82 return fetchRecords();
85 public Map<Long, String> fetchRecords() throws CipherUtilException {
86 List appPassword = null;
87 Session localSession = null;
88 Map<Long, String> responsemap;
90 localSession = sessionFactory.openSession();
91 responsemap = new LinkedHashMap<Long, String>();
92 appPassword = dataAccessService.executeNamedQuery("getAppPassword", null, null);
93 if (appPassword != null) {
94 Iterator i = appPassword.iterator();
96 Object[] user = (Object[]) i.next();
97 Long app_id = (Long) user[0];
98 String app_password = (String) user[1];
99 if (app_id != null && StringUtils.isNotEmpty(app_password)) {
101 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(app_password));
104 localSession.getTransaction().begin();
105 query = localSession.createSQLQuery(
106 "UPDATE fn_app m SET m.app_password= :pass " + " where m.app_id = :app_id");
107 query.setParameter("pass", pass);
108 query.setParameter("app_id", app_id);
109 int result = query.executeUpdate();
110 localSession.getTransaction().commit();
111 logger.debug(EELFLoggerDelegate.debugLogger,
112 "--------------getAppPassword-------query successfull------------------"
115 } catch (Exception e) {
116 localSession.getTransaction().rollback();
117 logger.debug(EELFLoggerDelegate.debugLogger,
118 "--------------getAppPassword--------query failed-----------------" + query);
119 responsemap.put(app_id, "-------query failed-----------------" + query);
120 logger.error(EELFLoggerDelegate.errorLogger,
121 "getAppPassword error while executing the query", e);
124 } catch (Exception e) {
125 logger.error(EELFLoggerDelegate.errorLogger,
126 "getAppPassword error while executing the query", e);
131 appPassword = dataAccessService.executeNamedQuery("getBasicauthAccount", null, null);
132 if (appPassword != null) {
133 Iterator i = appPassword.iterator();
134 while (i.hasNext()) {
135 Object[] user = (Object[]) i.next();
136 Long app_id = (Long) user[0];
137 String password = (String) user[1];
138 if (app_id != null && StringUtils.isNotEmpty(password)) {
140 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
143 localSession.getTransaction().begin();
145 .createSQLQuery("UPDATE ep_basic_auth_account m SET m.password = :pass"
146 + " where m.id = :app_id");
147 query.setParameter("pass", pass);
148 query.setParameter("app_id", app_id);
149 int result = query.executeUpdate();
150 localSession.getTransaction().commit();
151 logger.debug(EELFLoggerDelegate.debugLogger,
152 "--------------getAppPassword-------query successfull------------------"
155 } catch (Exception e) {
156 localSession.getTransaction().rollback();
157 logger.debug(EELFLoggerDelegate.debugLogger,
158 "--------------getAppPassword--------query failed-----------------" + query);
159 responsemap.put(app_id, "-------query failed-----------------" + query);
160 logger.error(EELFLoggerDelegate.errorLogger,
161 "getAppPassword error while executing the query", e);
164 } catch (Exception e) {
165 logger.error(EELFLoggerDelegate.errorLogger,
166 "getAppPassword error while executing the query", e);
173 appPassword = dataAccessService.executeNamedQuery("getMicroserviceInfo", null, null);
174 if (appPassword != null) {
175 Iterator i = appPassword.iterator();
176 while (i.hasNext()) {
177 Object[] user = (Object[]) i.next();
178 Long app_id = (Long) user[0];
179 String password = (String) user[1];
180 if (app_id != null && StringUtils.isNotEmpty(password)) {
182 final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
185 localSession.getTransaction().begin();
186 query = localSession.createSQLQuery(
187 "UPDATE ep_microservice m SET m.password = :pass" + " WHERE m.id = :app_id");
188 query.setParameter("pass", pass);
189 query.setParameter("app_id", app_id);
190 int result = query.executeUpdate();
191 localSession.getTransaction().commit();
192 logger.debug(EELFLoggerDelegate.debugLogger,
193 "--------------getAppPassword-------query successfull------------------"
196 } catch (Exception e) {
197 localSession.getTransaction().rollback();
198 logger.debug(EELFLoggerDelegate.debugLogger,
199 "--------------getAppPassword--------query failed-----------------" + query);
200 responsemap.put(app_id, "-------query failed-----------------" + query);
201 logger.error(EELFLoggerDelegate.errorLogger,
202 "getAppPassword error while executing the query", e);
205 } catch (Exception e) {
206 logger.error(EELFLoggerDelegate.errorLogger,
207 "getAppPassword error while executing the query", e);
216 EcompPortalUtils.closeLocalSession(localSession, "updateRecords");