Replace ecomp references
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / EncryptAdminController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.Iterator;
41 import java.util.LinkedHashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
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;
64
65 @RestController
66 @RequestMapping(PortalConstants.REST_AUX_API)
67 @org.springframework.context.annotation.Configuration
68 @EnableAspectJAutoProxy
69 @EPAuditLog
70 public class EncryptAdminController implements BasicAuthenticationController {
71         @Autowired
72         DataAccessService dataAccessService;
73         @Autowired
74         protected SessionFactory sessionFactory;
75
76         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptAdminController.class);
77
78         @RequestMapping(value = { "/executeEncryptQuery" }, method = RequestMethod.POST)
79         public Map<Long, String> executeEncrypt(HttpServletRequest request, HttpServletResponse response)
80                         throws CipherUtilException {
81
82                 return fetchRecords();
83         }
84
85         public Map<Long, String> fetchRecords() throws CipherUtilException {
86                 List appPassword = null;
87                 Session localSession = null;
88                 Map<Long, String> responsemap;
89                 try {
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();
95                                 while (i.hasNext()) {
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)) {
100                                                 try {
101                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(app_password));
102                                                         Query query = null;
103                                                         try {
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------------------"
113                                                                                                 + query);
114
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);
122
123                                                         }
124                                                 } catch (Exception e) {
125                                                         logger.error(EELFLoggerDelegate.errorLogger,
126                                                                         "getAppPassword error while executing  the query", e);
127                                                 }
128                                         }
129                                 }
130                         }
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)) {
139                                                 try {
140                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
141                                                         Query query = null;
142                                                         try {
143                                                                 localSession.getTransaction().begin();
144                                                                 query = localSession
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------------------"
153                                                                                                 + query);
154
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);
162
163                                                         }
164                                                 } catch (Exception e) {
165                                                         logger.error(EELFLoggerDelegate.errorLogger,
166                                                                         "getAppPassword error while executing  the query", e);
167                                                 }
168
169                                         }
170
171                                 }
172                         }
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)) {
181                                                 try {
182                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
183                                                         Query query = null;
184                                                         try {
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------------------"
194                                                                                                 + query);
195
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);
203
204                                                         }
205                                                 } catch (Exception e) {
206                                                         logger.error(EELFLoggerDelegate.errorLogger,
207                                                                         "getAppPassword error while executing  the query", e);
208                                                 }
209
210                                         }
211
212                                 }
213                         }
214                         
215                 } finally {
216                         EcompPortalUtils.closeLocalSession(localSession, "updateRecords");
217                 }
218                 return responsemap;
219         }
220 }