5fde5c28f598bf9a446ea052d4e64751557553e8
[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.util.PortalApiConstants;
27 import org.oransc.ric.portal.dashboard.controller.A1MediatorController;
28 import org.oransc.ric.portal.dashboard.controller.AdminController;
29 import org.oransc.ric.portal.dashboard.controller.AnrXappController;
30 import org.oransc.ric.portal.dashboard.controller.AppManagerController;
31 import org.oransc.ric.portal.dashboard.controller.E2ManagerController;
32 import org.oransc.ric.portal.dashboard.controller.SimpleErrorController;
33 import org.oransc.ric.portal.dashboard.portalapi.DashboardUserManager;
34 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
35 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Profile;
42 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
43 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
44 import org.springframework.security.config.annotation.web.builders.WebSecurity;
45 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
46 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
47 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
48 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
49
50 @Configuration
51 @EnableWebSecurity
52 @EnableGlobalMethodSecurity(securedEnabled = true)
53 @Profile("!test")
54 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
55
56         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58         // Although constructor arguments are recommended over field injection,
59         // this results in fewer lines of code.
60         @Value("${portalapi.security}")
61         private Boolean portalapiSecurity;
62         @Value("${portalapi.appname}")
63         private String appName;
64         @Value("${portalapi.username}")
65         private String userName;
66         @Value("${portalapi.password}")
67         private String password;
68         @Value("${portalapi.decryptor}")
69         private String decryptor;
70         @Value("${portalapi.usercookie}")
71         private String userCookie;
72         @Value("${userfile}")
73         private String userFilePath;
74
75         protected void configure(HttpSecurity http) throws Exception {
76                 logger.debug("configure: portalapi.username {}", userName);
77                 // A chain of ".and()" always baffles me
78                 http.authorizeRequests().anyRequest().authenticated();
79                 http.headers().frameOptions().disable();
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                         A1MediatorController.CONTROLLER_PATH + "/" + A1MediatorController.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                         SimpleErrorController.ERROR_PATH };
106
107         @Override
108         public void configure(WebSecurity web) throws Exception {
109                 // This disables Spring security, but not the app's filter.
110                 web.ignoring().antMatchers(OPEN_PATHS);
111         }
112
113         @Bean
114         public PortalAuthManager portalAuthManagerBean()
115                         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
116                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
117                 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
118         }
119
120         @Bean
121         public DashboardUserManager dashboardUserManagerBean() throws IOException {
122                 return new DashboardUserManager(userFilePath);
123         }
124
125         /*
126          * If this is annotated with @Bean, it is created automatically AND REGISTERED,
127          * and Spring processes annotations in the source of the class. However, the
128          * filter is added in the chain apparently in the wrong order. Alternately, with
129          * no @Bean and added to the chain up in the configure() method in the desired
130          * order, the ignoring() matcher pattern configured above causes Spring to
131          * bypass this filter, which seems to me means the filter participates
132          * correctly.
133          */
134         public PortalAuthenticationFilter portalAuthenticationFilterBean()
135                         throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
136                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
137                 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalapiSecurity,
138                                 portalAuthManagerBean(), dashboardUserManagerBean());
139                 return portalAuthenticationFilter;
140         }
141
142 }