Add basic security to query interface
[cps/cps-temporal.git] / src / main / java / org / onap / cps / temporal / controller / rest / config / WebSecurityConfig.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021 Bell Canada.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.temporal.controller.rest.config;
22
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
27 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
28 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
29
30 /**
31  * Configuration class to implement application security. It enforces Basic Authentication access control.
32  */
33 @Configuration
34 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
35
36     private static final String USER_ROLE = "USER";
37
38     private final String username;
39     private final String password;
40     private final String[] permitUris;
41
42     /**
43      * Constructor. Accepts parameters from configuration.
44      *
45      * @param permitUris comma-separated list of uri patterns for endpoints permitted
46      * @param username   username
47      * @param password   password
48      */
49     public WebSecurityConfig(
50         @Autowired @Value("${security.permit-uri}") final String permitUris,
51         @Autowired @Value("${security.auth.username}") final String username,
52         @Autowired @Value("${security.auth.password}") final String password
53     ) {
54         super();
55         this.permitUris =
56             permitUris.isEmpty() ? new String[]{"/swagger/openapi.yml"} : permitUris.split("\\s{0,9},\\s{0,9}");
57         this.username = username;
58         this.password = password;
59     }
60
61     @Override
62     // The team decided to disable default CSRF Spring protection and not implement CSRF tokens validation.
63     // CPS is a stateless REST API that is not as vulnerable to CSRF attacks as web applications running in
64     // web browsers are. CPS  does not manage sessions, each request requires the authentication token in the header.
65     // See https://docs.spring.io/spring-security/site/docs/5.3.8.RELEASE/reference/html5/#csrf
66     @SuppressWarnings("squid:S4502")
67     protected void configure(final HttpSecurity http) throws Exception {
68         http
69             .csrf().disable()
70             .authorizeRequests()
71             .antMatchers(permitUris).permitAll()
72             .anyRequest().authenticated()
73             .and().httpBasic();
74     }
75
76     @Override
77     protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
78         auth.inMemoryAuthentication().withUser(username).password("{noop}" + password).roles(USER_ROLE);
79     }
80 }