Fixed health check issue
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / BasicAuthAccountController.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.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.onap.portalapp.controller.EPRestrictedBaseController;
47 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
48 import org.onap.portalapp.portal.domain.EPEndpoint;
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
51 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
52 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
53 import org.onap.portalapp.portal.service.AdminRolesService;
54 import org.onap.portalapp.portal.service.BasicAuthAccountService;
55 import org.onap.portalapp.util.EPUserUtils;
56 import org.onap.portalapp.validation.DataValidator;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.context.annotation.EnableAspectJAutoProxy;
60 import org.springframework.web.bind.annotation.PathVariable;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.GetMapping;
63 import org.springframework.web.bind.annotation.PostMapping;
64 import org.springframework.web.bind.annotation.PutMapping;
65 import org.springframework.web.bind.annotation.DeleteMapping;
66 import org.springframework.web.bind.annotation.RestController;
67
68 @RestController
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 @EPAuditLog
72 public class BasicAuthAccountController extends EPRestrictedBaseController {
73
74     private static final String FAILURE = "FAILURE";
75     private static final String SUCCESS = "SUCCESS";
76     private static final String AUTHORIZATION_REQUIRED = "Authorization Required";
77     private static final String ADMIN_ONLY_OPERATIONS = "Admin Only Operation! ";
78
79     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthAccountController.class);
80     private final DataValidator dataValidator = new DataValidator();
81
82         @Autowired
83         private BasicAuthAccountService basicAuthAccountService;
84
85         @Autowired
86         private AdminRolesService adminRolesService;
87
88         /**
89          * Saves Basic Authentication account for external systems
90          *
91          * @param request
92          *            HttpServletRequest
93          * @param response
94          *            HttpServletResponse
95          * @param newBasicAuthAccount
96          *            BasicAuthCredentials
97          * @return Id of the newly created account
98          * @throws Exception
99          *             on failure
100          */
101         @PostMapping(value = { "/portalApi/basicAuthAccount" })
102         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
103                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
104
105
106
107                 EPUser user = EPUserUtils.getUserSession(request);
108                 if (!adminRolesService.isSuperAdmin(user)) {
109             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
110                     ADMIN_ONLY_OPERATIONS);
111                 }
112
113                 if (newBasicAuthAccount == null) {
114             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
115                                         "newBasicAuthAccount cannot be null or empty");
116                 }
117
118                 if(!dataValidator.isValid(newBasicAuthAccount)){
119                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "createBasicAuthAccount() failed, new credential are not safe",
120                                 "");
121                 }
122
123                 long accountId;
124                 try {
125                         accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
126                 } catch (Exception e){
127                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
128                 }
129
130                 List<Long> endpointIdList = new ArrayList<>();
131                 try {
132                         for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
133                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
134                         }
135                         for (Long endpointId : endpointIdList) {
136                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
137                         }
138                 } catch (Exception e) {
139             logger.error(EELFLoggerDelegate.errorLogger, "createBasicAuthAccount failed", e);
140             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
141                 }
142
143         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
144         }
145
146         /**
147          * Returns list of all BasicAuthCredentials in the system
148          *
149          * @param request
150          *            HttpServletRequest
151          * @param response
152          *            HttpServletResponse
153          * @return List<BasicAuthCredentials>
154          * @throws Exception
155          *             on failure
156          */
157
158         @GetMapping(value = { "/portalApi/basicAuthAccount" })
159         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
160                         HttpServletResponse response) throws Exception {
161
162                 EPUser user = EPUserUtils.getUserSession(request);
163                 if (!adminRolesService.isSuperAdmin(user)) {
164             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
165                                         "UnAuthorized! Admin Only Operation", new ArrayList<>());
166                 }
167
168         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "Success",
169                                 basicAuthAccountService.getAccountData());
170         }
171
172         /**
173          * Updates an existing BasicAuthCredentials account
174          *
175          * @param request
176          *            HttpServletRequest
177          * @param response
178          *            HttpServletResponse
179          * @param accountId
180          *            account ID
181          * @param newBasicAuthAccount
182          *            BasicAuthCredentials
183          * @return PortalRestResponse<String>
184          * @throws Exception
185          *             on failure
186          */
187         @PutMapping(value = { "/portalApi/basicAuthAccount/{accountId}" })
188         public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
189                         @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
190                         throws Exception {
191
192                 EPUser user = EPUserUtils.getUserSession(request);
193                 if (!adminRolesService.isSuperAdmin(user)) {
194             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
195                     ADMIN_ONLY_OPERATIONS);
196                 }
197
198                 if (newBasicAuthAccount == null) {
199             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
200                                         "BasicAuthCredentials cannot be null or empty");
201                 }
202                 try {
203                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
204                 } catch (Exception e) {
205             logger.error(EELFLoggerDelegate.errorLogger, "updateAccount failed", e);
206             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
207                 }
208         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
209         }
210
211         /**
212          * deletes an existing BasicAuthCredentials account
213          *
214          * @param request
215          *            HttpServletRequest
216          * @param response
217          *            HttpServletResponse
218          * @param accountId
219          *            account ID
220          * @return PortalRestResponse<String>
221          * @throws Exception
222          *             on failure
223          */
224         @DeleteMapping(value = { "/portalApi/basicAuthAccount/{accountId}" })
225         public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
226                         @PathVariable("accountId") long accountId) throws Exception {
227
228                 EPUser user = EPUserUtils.getUserSession(request);
229                 if (!adminRolesService.isSuperAdmin(user)) {
230             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
231                     ADMIN_ONLY_OPERATIONS);
232                 }
233
234                 try {
235                         basicAuthAccountService.deleteEndpointAccout(accountId);
236                 } catch (Exception e) {
237             logger.error(EELFLoggerDelegate.errorLogger, "deleteAccount failed", e);
238             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
239                 }
240         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
241         }
242
243 }