removed code smells
[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.PostMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.bind.annotation.RestController;
65
66 @RestController
67 @RequestMapping(PortalConstants.REST_AUX_API)
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 @EPAuditLog
71 public class EncryptAdminController implements BasicAuthenticationController {
72         @Autowired
73         DataAccessService dataAccessService;
74         @Autowired
75         protected SessionFactory sessionFactory;
76
77         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptAdminController.class);
78
79         @PostMapping(value = { "/executeEncryptQuery" })
80         public Map<Long, String> executeEncrypt(HttpServletRequest request, HttpServletResponse response)
81                         throws CipherUtilException {
82
83                 return fetchRecords();
84         }
85
86         public Map<Long, String> fetchRecords() throws CipherUtilException {
87                 List appPassword = null;
88                 Session localSession = null;
89                 Map<Long, String> responsemap;
90                 try {
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();
96                                 while (i.hasNext()) {
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)) {
101                                                 try {
102                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(app_password));
103                                                         Query query = null;
104                                                         try {
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------------------"
114                                                                                                 + query);
115
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);
123
124                                                         }
125                                                 } catch (Exception e) {
126                                                         logger.error(EELFLoggerDelegate.errorLogger,
127                                                                         "getAppPassword error while executing  the query", e);
128                                                 }
129                                         }
130                                 }
131                         }
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)) {
140                                                 try {
141                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
142                                                         Query query = null;
143                                                         try {
144                                                                 localSession.getTransaction().begin();
145                                                                 query = localSession
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------------------"
154                                                                                                 + query);
155
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);
163
164                                                         }
165                                                 } catch (Exception e) {
166                                                         logger.error(EELFLoggerDelegate.errorLogger,
167                                                                         "getAppPassword error while executing  the query", e);
168                                                 }
169
170                                         }
171
172                                 }
173                         }
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)) {
182                                                 try {
183                                                         final String pass = CipherUtil.encryptPKC(CipherUtil.decrypt(password));
184                                                         Query query = null;
185                                                         try {
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------------------"
195                                                                                                 + query);
196
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);
204
205                                                         }
206                                                 } catch (Exception e) {
207                                                         logger.error(EELFLoggerDelegate.errorLogger,
208                                                                         "getAppPassword error while executing  the query", e);
209                                                 }
210
211                                         }
212
213                                 }
214                         }
215                         
216                 } finally {
217                         EcompPortalUtils.closeLocalSession(localSession, "updateRecords");
218                 }
219                 return responsemap;
220         }
221 }