3477ab0ad9da1986903093da20cbc2f64717903d
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / WebSecurityConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.config;
21
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.lang.reflect.InvocationTargetException;
25
26 import org.onap.portalsdk.core.onboarding.crossapi.PortalRestAPIProxy;
27 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.LoginServlet;
30 import org.oransc.ric.portal.dashboard.controller.AcXappController;
31 import org.oransc.ric.portal.dashboard.controller.AdminController;
32 import org.oransc.ric.portal.dashboard.controller.AnrXappController;
33 import org.oransc.ric.portal.dashboard.controller.AppManagerController;
34 import org.oransc.ric.portal.dashboard.controller.E2ManagerController;
35 import org.oransc.ric.portal.dashboard.portalapi.DashboardUserManager;
36 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
37 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.web.servlet.ServletRegistrationBean;
42 import org.springframework.context.annotation.Bean;
43 import org.springframework.context.annotation.Configuration;
44 import org.springframework.context.annotation.Profile;
45 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
46 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
47 import org.springframework.security.config.annotation.web.builders.WebSecurity;
48 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
49 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
50 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
51 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
52
53 @Configuration
54 @EnableWebSecurity
55 @EnableGlobalMethodSecurity(securedEnabled = true)
56 @Profile("!test")
57 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
58
59         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60
61         // Although constructor arguments are recommended over field injection,
62         // this results in fewer lines of code.
63         @Value("${userfile}")
64         private String userFilePath;
65         @Value("${portalapi.appname}")
66         private String appName;
67         @Value("${portalapi.username}")
68         private String userName;
69         @Value("${portalapi.password}")
70         private String password;
71         @Value("${portalapi.decryptor}")
72         private String decryptor;
73         @Value("${portalapi.usercookie}")
74         private String userCookie;
75
76         protected void configure(HttpSecurity http) throws Exception {
77                 logger.debug("configure: portalapi.username {}", userName);
78                 // A chain of ".and()" always baffles me
79                 http.authorizeRequests().anyRequest().authenticated();
80                 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
81                 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
82         }
83
84         /**
85          * Resource paths that do not require authentication, especially including
86          * Swagger-generated documentation.
87          */
88         public static final String[] OPEN_PATHS = { //
89                         "/v2/api-docs", //
90                         "/swagger-resources/**", //
91                         "/swagger-ui.html", //
92                         "/webjars/**", //
93                         PortalApiConstants.API_PREFIX + "/**", //
94                         AcXappController.CONTROLLER_PATH + "/" + AcXappController.VERSION_METHOD, //
95                         AdminController.CONTROLLER_PATH + "/" + AdminController.HEALTH_METHOD, //
96                         AdminController.CONTROLLER_PATH + "/" + AdminController.VERSION_METHOD, //
97                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_ALIVE_METHOD, //
98                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_READY_METHOD, //
99                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.VERSION_METHOD, //
100                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_ALIVE_METHOD, //
101                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_READY_METHOD, //
102                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.VERSION_METHOD, //
103                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.HEALTH_METHOD, //
104                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.VERSION_METHOD, //
105                         DashboardConstants.LOGIN_PAGE //
106         };
107
108         @Override
109         public void configure(WebSecurity web) throws Exception {
110                 // This disables Spring security, but not the app's filter.
111                 web.ignoring().antMatchers(OPEN_PATHS);
112         }
113
114         @Bean
115         public PortalAuthManager portalAuthManagerBean()
116                         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
117                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
118                 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
119         }
120
121         @Bean
122         public DashboardUserManager dashboardUserManagerBean() throws IOException {
123                 return new DashboardUserManager(userFilePath);
124         }
125
126         /*
127          * If this is annotated with @Bean, it is created automatically AND REGISTERED,
128          * and Spring processes annotations in the source of the class. However, the
129          * filter is added in the chain apparently in the wrong order. Alternately, with
130          * no @Bean and added to the chain up in the configure() method in the desired
131          * order, the ignoring() matcher pattern configured above causes Spring to
132          * bypass this filter, which seems to me means the filter participates
133          * correctly.
134          */
135         public PortalAuthenticationFilter portalAuthenticationFilterBean()
136                         throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
137                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
138                 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalAuthManagerBean(),
139                                 dashboardUserManagerBean());
140                 return portalAuthenticationFilter;
141         }
142
143         /**
144          * Instantiates the EPSDK-FW servlet. Needed because this app is not configured
145          * to scan the EPSDK-FW packages; there's also a chance that Spring-Boot does
146          * not automatically process @WebServlet annotations.
147          * 
148          * @return Servlet registration bean for the Portal Rest API proxy servlet.
149          */
150         @Bean
151         public ServletRegistrationBean<PortalRestAPIProxy> portalApiProxyServletBean() {
152                 PortalRestAPIProxy servlet = new PortalRestAPIProxy();
153                 final ServletRegistrationBean<PortalRestAPIProxy> servletBean = new ServletRegistrationBean<>(servlet,
154                                 PortalApiConstants.API_PREFIX + "/*");
155                 servletBean.setName("PortalRestApiProxyServlet");
156                 return servletBean;
157         }
158
159         /**
160          * Instantiates a trivial login servlet that serves a basic page with a link to
161          * authenticate at Portal. The login filter redirects to this page instead of
162          * Portal.
163          * 
164          * @return Servlet registration bean for the Dashboard login servlet.
165          */
166         @Bean
167         public ServletRegistrationBean<LoginServlet> loginServletBean() {
168                 LoginServlet servlet = new LoginServlet();
169                 final ServletRegistrationBean<LoginServlet> servletBean = new ServletRegistrationBean<>(servlet,
170                                 DashboardConstants.LOGIN_PAGE);
171                 servletBean.setName("LoginServlet");
172                 return servletBean;
173         }
174
175 }