Add configuration option for Portal security
[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.A1MediatorController;
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.controller.SimpleErrorController;
36 import org.oransc.ric.portal.dashboard.portalapi.DashboardUserManager;
37 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
38 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.boot.web.servlet.ServletRegistrationBean;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.context.annotation.Configuration;
45 import org.springframework.context.annotation.Profile;
46 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
47 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
48 import org.springframework.security.config.annotation.web.builders.WebSecurity;
49 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
50 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
51 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
52 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
53
54 @Configuration
55 @EnableWebSecurity
56 @EnableGlobalMethodSecurity(securedEnabled = true)
57 @Profile("!test")
58 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
59
60         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62         // Although constructor arguments are recommended over field injection,
63         // this results in fewer lines of code.
64         @Value("${portalapi.security}")
65         private Boolean portalapiSecurity;
66         @Value("${portalapi.appname}")
67         private String appName;
68         @Value("${portalapi.username}")
69         private String userName;
70         @Value("${portalapi.password}")
71         private String password;
72         @Value("${portalapi.decryptor}")
73         private String decryptor;
74         @Value("${portalapi.usercookie}")
75         private String userCookie;
76         @Value("${userfile}")
77         private String userFilePath;
78
79         protected void configure(HttpSecurity http) throws Exception {
80                 logger.debug("configure: portalapi.username {}", userName);
81                 // A chain of ".and()" always baffles me
82                 http.authorizeRequests().anyRequest().authenticated();
83                 http.headers().frameOptions().disable();
84                 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
85                 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
86         }
87
88         /**
89          * Resource paths that do not require authentication, especially including
90          * Swagger-generated documentation.
91          */
92         public static final String[] OPEN_PATHS = { //
93                         "/v2/api-docs", //
94                         "/swagger-resources/**", //
95                         "/swagger-ui.html", //
96                         "/webjars/**", //
97                         PortalApiConstants.API_PREFIX + "/**", //
98                         A1MediatorController.CONTROLLER_PATH + "/" + A1MediatorController.VERSION_METHOD, //
99                         AdminController.CONTROLLER_PATH + "/" + AdminController.HEALTH_METHOD, //
100                         AdminController.CONTROLLER_PATH + "/" + AdminController.VERSION_METHOD, //
101                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_ALIVE_METHOD, //
102                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_READY_METHOD, //
103                         AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.VERSION_METHOD, //
104                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_ALIVE_METHOD, //
105                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_READY_METHOD, //
106                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.VERSION_METHOD, //
107                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.HEALTH_METHOD, //
108                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.VERSION_METHOD, //
109                         SimpleErrorController.ERROR_PATH, //
110                         DashboardConstants.LOGIN_PAGE //
111         };
112
113         @Override
114         public void configure(WebSecurity web) throws Exception {
115                 // This disables Spring security, but not the app's filter.
116                 web.ignoring().antMatchers(OPEN_PATHS);
117         }
118
119         @Bean
120         public PortalAuthManager portalAuthManagerBean()
121                         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
122                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
123                 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
124         }
125
126         @Bean
127         public DashboardUserManager dashboardUserManagerBean() throws IOException {
128                 return new DashboardUserManager(userFilePath);
129         }
130
131         /*
132          * If this is annotated with @Bean, it is created automatically AND REGISTERED,
133          * and Spring processes annotations in the source of the class. However, the
134          * filter is added in the chain apparently in the wrong order. Alternately, with
135          * no @Bean and added to the chain up in the configure() method in the desired
136          * order, the ignoring() matcher pattern configured above causes Spring to
137          * bypass this filter, which seems to me means the filter participates
138          * correctly.
139          */
140         public PortalAuthenticationFilter portalAuthenticationFilterBean()
141                         throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
142                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
143                 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalapiSecurity,
144                                 portalAuthManagerBean(), dashboardUserManagerBean());
145                 return portalAuthenticationFilter;
146         }
147
148         /**
149          * Instantiates the EPSDK-FW servlet. Needed because this app is not configured
150          * to scan the EPSDK-FW packages; there's also a chance that Spring-Boot does
151          * not automatically process @WebServlet annotations.
152          * 
153          * @return Servlet registration bean for the Portal Rest API proxy servlet.
154          */
155         @Bean
156         public ServletRegistrationBean<PortalRestAPIProxy> portalApiProxyServletBean() {
157                 PortalRestAPIProxy servlet = new PortalRestAPIProxy();
158                 final ServletRegistrationBean<PortalRestAPIProxy> servletBean = new ServletRegistrationBean<>(servlet,
159                                 PortalApiConstants.API_PREFIX + "/*");
160                 servletBean.setName("PortalRestApiProxyServlet");
161                 return servletBean;
162         }
163
164         /**
165          * Instantiates a trivial login servlet that serves a basic page with a link to
166          * authenticate at Portal. The login filter redirects to this page instead of
167          * Portal.
168          * 
169          * @return Servlet registration bean for the Dashboard login servlet.
170          */
171         @Bean
172         public ServletRegistrationBean<LoginServlet> loginServletBean() {
173                 LoginServlet servlet = new LoginServlet();
174                 final ServletRegistrationBean<LoginServlet> servletBean = new ServletRegistrationBean<>(servlet,
175                                 DashboardConstants.LOGIN_PAGE);
176                 servletBean.setName("LoginServlet");
177                 return servletBean;
178         }
179
180 }