From 55500c3685dbc032c5cb79a727cc588d542e7d15 Mon Sep 17 00:00:00 2001 From: shivasubedi Date: Wed, 23 Jun 2021 16:31:02 +0100 Subject: create quality params in dmi-plugin Issue-ID: CPS-432 Issue-ID: CPS-403 Signed-off-by: tragait Change-Id: I4ada1e4927a3726500396da64c454d9937f95bcf Signed-off-by: shivasubedi --- .../org/onap/cps/ncmp/config/DmiPluginConfig.java | 45 ++++++++++++ .../onap/cps/ncmp/config/WebSecurityConfig.java | 81 ++++++++++++++++++++++ .../ncmp/rest/controller/DmiRestController.java | 7 +- .../org/onap/cps/ncmp/service/DmiServiceImpl.java | 3 + 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/onap/cps/ncmp/config/DmiPluginConfig.java create mode 100644 src/main/java/org/onap/cps/ncmp/config/WebSecurityConfig.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/cps/ncmp/config/DmiPluginConfig.java b/src/main/java/org/onap/cps/ncmp/config/DmiPluginConfig.java new file mode 100644 index 00000000..427ffe19 --- /dev/null +++ b/src/main/java/org/onap/cps/ncmp/config/DmiPluginConfig.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + + +@Configuration +public class DmiPluginConfig { + /** + * Swagger-ui configuration. + */ + @Bean("dmi-plugin-docket") + public Docket api() { + return new Docket(DocumentationType.OAS_30) + .groupName("dmi-plugin-docket") + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } +} + diff --git a/src/main/java/org/onap/cps/ncmp/config/WebSecurityConfig.java b/src/main/java/org/onap/cps/ncmp/config/WebSecurityConfig.java new file mode 100644 index 00000000..34bfae9d --- /dev/null +++ b/src/main/java/org/onap/cps/ncmp/config/WebSecurityConfig.java @@ -0,0 +1,81 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * Configuration class to implement application security. + * It enforces Basic Authentication access control. + */ +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + private static final String USER_ROLE = "USER"; + + private final String username; + private final String password; + private final String[] permitUris; + + /** + * Constructor. Accepts parameters from configuration. + * + * @param permitUris comma-separated list of uri patterns for endpoints permitted + * @param username username + * @param password password + */ + public WebSecurityConfig( + @Autowired @Value("${security.permit-uri}") final String permitUris, + @Autowired @Value("${security.auth.username}") final String username, + @Autowired @Value("${security.auth.password}") final String password + ) { + super(); + this.permitUris = permitUris.isEmpty() ? new String[] {"/v3/api-docs"} : permitUris.split("\\s{0,9},\\s{0,9}"); + this.username = username; + this.password = password; + } + + @Override + // The team decided to disable default CSRF Spring protection and not implement CSRF tokens validation. + // ncmp is a stateless REST API that is not as vulnerable to CSRF attacks as web applications running in + // web browsers are. ncmp does not manage sessions, each request requires the authentication token in the header. + // See https://docs.spring.io/spring-security/site/docs/5.3.8.RELEASE/reference/html5/#csrf + @SuppressWarnings("squid:S4502") + protected void configure(final HttpSecurity http) throws Exception { + http + .csrf().disable() + .authorizeRequests() + .antMatchers(permitUris).permitAll() + .anyRequest().authenticated() + .and().httpBasic(); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser(username).password("{noop}" + password).roles(USER_ROLE); + } +} diff --git a/src/main/java/org/onap/cps/ncmp/rest/controller/DmiRestController.java b/src/main/java/org/onap/cps/ncmp/rest/controller/DmiRestController.java index 700ca82c..9507c695 100644 --- a/src/main/java/org/onap/cps/ncmp/rest/controller/DmiRestController.java +++ b/src/main/java/org/onap/cps/ncmp/rest/controller/DmiRestController.java @@ -20,6 +20,8 @@ package org.onap.cps.ncmp.rest.controller; import org.onap.cps.ncmp.rest.api.DmiPluginApi; +import org.onap.cps.ncmp.service.DmiService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -29,9 +31,12 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class DmiRestController implements DmiPluginApi { + @Autowired + private DmiService dmiService; + @Override public ResponseEntity helloWorld() { - final var helloWorld = "Hello World"; + final var helloWorld = dmiService.getHelloWorld() ; return new ResponseEntity<>(helloWorld, HttpStatus.OK); } diff --git a/src/main/java/org/onap/cps/ncmp/service/DmiServiceImpl.java b/src/main/java/org/onap/cps/ncmp/service/DmiServiceImpl.java index a1cecc3e..b0c140cd 100644 --- a/src/main/java/org/onap/cps/ncmp/service/DmiServiceImpl.java +++ b/src/main/java/org/onap/cps/ncmp/service/DmiServiceImpl.java @@ -19,6 +19,9 @@ package org.onap.cps.ncmp.service; +import org.springframework.stereotype.Service; + +@Service public class DmiServiceImpl implements DmiService { @Override -- cgit 1.2.3-korg