Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPIServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.security.portal;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.LinkedHashSet;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.onap.aai.sparky.security.EcompSso;
34 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
35 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
36 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
37 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
38 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
39 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Responds to ECOMP Portal's REST queries for user and role information and management.
45  */
46 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
47
48   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
49   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
50
51   private UserManager userManager;
52
53   /**
54    * Initialise user manager.
55    */
56   public PortalRestAPIServiceImpl() {
57     userManager = new UserManager(new File(TierSupportUiConstants.USERS_FILE_LOCATION));
58   }
59
60   /////////////////////////////////////////////////////////////////////////////
61   // User interface
62   /////////////////////////////////////////////////////////////////////////////
63
64   /*
65    * (non-Javadoc)
66    *
67    * @see
68    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
69    * restful.domain.EcompUser)
70    */
71   @Override
72   public void pushUser(EcompUser user) throws PortalAPIException {
73     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
74
75     if (userManager.getUser(user.getLoginId()).isPresent()) {
76       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
77           + ", user is already stored";
78       LOG.error(message);
79       throw new PortalAPIException(message);
80     }
81
82     try {
83       userManager.pushUser(user);
84     } catch (IOException e) {
85       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
86       LOG.error(message, e);
87       throw new PortalAPIException(message, e);
88     }
89   }
90
91   /*
92    * (non-Javadoc)
93    *
94    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
95    * com.att.fusion.core.restful.domain.EcompUser)
96    */
97   @Override
98   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
99     LOG.debug("Edit user [loginId:" + loginId + "]");
100
101     userManager.getUser(loginId).orElseThrow(() -> {
102       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
103       LOG.error(message);
104       return new PortalAPIException(message);
105     });
106
107     try {
108       userManager.editUser(loginId, user);
109     } catch (IOException e) {
110       String message = getMessage(ERROR_MESSAGE, "edit", loginId);
111       LOG.error(message, e);
112       throw new PortalAPIException(message, e);
113     }
114   }
115
116   /*
117    * (non-Javadoc)
118    *
119    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
120    */
121   @Override
122   public EcompUser getUser(String loginId) throws PortalAPIException {
123     LOG.debug("Get user [loginId:" + loginId + "]");
124     return userManager.getUser(loginId).orElseThrow(() -> {
125       String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
126       LOG.error(message);
127       return new PortalAPIException(message);
128     });
129   }
130
131   /*
132    * (non-Javadoc)
133    *
134    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
135    */
136   @Override
137   public List<EcompUser> getUsers() throws PortalAPIException {
138     LOG.debug("Get users");
139     return userManager.getUsers();
140   }
141
142   @Override
143   public String getUserId(HttpServletRequest request) throws PortalAPIException {
144     return EcompSso.validateEcompSso(request);
145   }
146
147   /////////////////////////////////////////////////////////////////////////////
148   // Role interface
149   /////////////////////////////////////////////////////////////////////////////
150
151   /*
152    * (non-Javadoc)
153    *
154    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getAvailableRoles()
155    */
156   @Override
157   public List<EcompRole> getAvailableRoles(String role) throws PortalAPIException {
158     LOG.debug("Get available roles");
159     return UserManager.getRoles();
160   }
161
162   /*
163    * (non-Javadoc)
164    *
165    * @see
166    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
167    */
168   @Override
169   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
170     LOG.debug("Get user roles");
171     return userManager.getUserRoles(loginId);
172   }
173
174   /*
175    * (non-Javadoc)
176    *
177    * @see
178    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
179    * java.util.List)
180    */
181   @Override
182   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
183     LOG.debug("Push user role [loginId:" + loginId + "]");
184     try {
185       EcompUser user = getUser(loginId);
186       if (roles != null) {
187         user.setRoles(new LinkedHashSet<EcompRole>(roles));
188       } else {
189         user.setRoles(new LinkedHashSet<EcompRole>());
190       }
191       editUser(loginId, user);
192     } catch (PortalAPIException e) {
193       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
194       LOG.error(message);
195       throw new PortalAPIException(message, e);
196     }
197   }
198
199   /////////////////////////////////////////////////////////////////////////////
200   // Security interface
201   /////////////////////////////////////////////////////////////////////////////
202
203   /*
204    * (non-Javadoc)
205    *
206    * @see
207    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
208    * http.HttpServletRequest)
209    */
210   @Override
211   public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
212     LOG.debug("Authentication request");
213     PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
214     String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
215     String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
216     return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
217         && restPassword.equals(config.getPassword());
218   }
219
220   private String getMessage(String message, Object... args) {
221     MessageFormat formatter = new MessageFormat("");
222     formatter.applyPattern(message);
223     return formatter.format(args);
224   }
225
226 }