From 9bbcb1be8d83cca5ba97dc4a2e86c525e51991e5 Mon Sep 17 00:00:00 2001 From: tragait Date: Mon, 24 Feb 2020 16:33:50 +0000 Subject: pnf sw upgrade csit Change-Id: I776d61030efc3d51e04f98698ceb8c445db2dafa Signed-off-by: tragait Issue-ID: INT-1308 Signed-off-by: tragait --- .../pnf-sw-upgrade/so/simulator/common/pom.xml | 38 ++++++++ .../provider/AbstractCacheServiceProvider.java | 54 +++++++++++ .../configuration/SimulatorSecurityConfigurer.java | 65 +++++++++++++ .../java/org/onap/so/simulator/model/User.java | 101 +++++++++++++++++++++ .../onap/so/simulator/model/UserCredentials.java | 66 ++++++++++++++ .../onap/so/simulator/model/PojoClassesTest.java | 60 ++++++++++++ 6 files changed, 384 insertions(+) create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/pom.xml create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/cache/provider/AbstractCacheServiceProvider.java create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/configuration/SimulatorSecurityConfigurer.java create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/User.java create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/UserCredentials.java create mode 100755 plans/usecases/pnf-sw-upgrade/so/simulator/common/src/test/java/org/onap/so/simulator/model/PojoClassesTest.java (limited to 'plans/usecases/pnf-sw-upgrade/so/simulator/common') diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/pom.xml b/plans/usecases/pnf-sw-upgrade/so/simulator/common/pom.xml new file mode 100755 index 00000000..340894e5 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + org.onap.so.simulators + simulator + 1.0-SNAPSHOT + + common + + 2.5.1 + 0.8.6 + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-tomcat + + + compile + + + nl.jqno.equalsverifier + equalsverifier + ${version.equalsverifier} + test + + + com.openpojo + openpojo + ${version.openpojo} + + + \ No newline at end of file diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/cache/provider/AbstractCacheServiceProvider.java b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/cache/provider/AbstractCacheServiceProvider.java new file mode 100755 index 00000000..6a101979 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/cache/provider/AbstractCacheServiceProvider.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.simulator.cache.provider; + +import java.util.concurrent.ConcurrentHashMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; + +/** + * @author Waqas Ikram (waqas.ikram@ericsson.com) + */ +public abstract class AbstractCacheServiceProvider { + + private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); + + private final CacheManager cacheManager; + + public AbstractCacheServiceProvider(final CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + protected void clearCache(final String name) { + final Cache cache = cacheManager.getCache(name); + if (cache != null) { + final ConcurrentHashMap nativeCache = (ConcurrentHashMap) cache.getNativeCache(); + LOGGER.info("Clear all entries from cahce: {}", cache.getName()); + nativeCache.clear(); + } + } + + protected Cache getCache(final String name) { + return cacheManager.getCache(name); + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/configuration/SimulatorSecurityConfigurer.java b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/configuration/SimulatorSecurityConfigurer.java new file mode 100755 index 00000000..5d59cbbd --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/configuration/SimulatorSecurityConfigurer.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.simulator.configuration; + +import java.util.List; +import org.onap.so.simulator.model.User; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public abstract class SimulatorSecurityConfigurer extends WebSecurityConfigurerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorSecurityConfigurer.class); + + + private final List users; + + public SimulatorSecurityConfigurer(final List users) { + this.users = users; + } + + @Bean + public BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Autowired + public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { + final InMemoryUserDetailsManagerConfigurer inMemoryAuthentication = + auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()); + for (int index = 0; index < users.size(); index++) { + final User user = users.get(index); + LOGGER.info("Adding {} to InMemoryUserDetailsManager ...", user); + inMemoryAuthentication.withUser(user.getUsername()).password(user.getPassword()).roles(user.getRole()); + if (index < users.size()) { + inMemoryAuthentication.and(); + } + } + } +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/User.java b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/User.java new file mode 100755 index 00000000..48d5622a --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/User.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.simulator.model; + +import static org.springframework.util.ObjectUtils.nullSafeEquals; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class User { + private String username; + private String password; + private String role; + + /** + * @return the username + */ + public String getUsername() { + return username; + } + + /** + * @param username the username to set + */ + public void setUsername(final String username) { + this.username = username; + } + + /** + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * @param password the password to set + */ + public void setPassword(final String password) { + this.password = password; + } + + /** + * @return the role + */ + public String getRole() { + return role; + } + + /** + * @param role the role to set + */ + public void setRole(final String role) { + this.role = role; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((role == null) ? 0 : role.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (obj instanceof User) { + final User other = (User) obj; + return nullSafeEquals(this.username, other.username) && nullSafeEquals(this.password, other.password) + && nullSafeEquals(this.role, other.role); + } + return false; + } + + @Override + public String toString() { + return "UserCredential [username=" + username + ", password=" + password + ", role=" + role + "]"; + } + + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/UserCredentials.java b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/UserCredentials.java new file mode 100755 index 00000000..f12c2475 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/main/java/org/onap/so/simulator/model/UserCredentials.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.simulator.model; + +import java.util.ArrayList; +import java.util.List; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; +import org.springframework.util.ObjectUtils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Component +@ConfigurationProperties(prefix = "spring.security") +public class UserCredentials { + + private final List users = new ArrayList<>(); + + public List getUsers() { + return users; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((users == null) ? 0 : users.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + + if (obj instanceof UserCredentials) { + final UserCredentials other = (UserCredentials) obj; + return ObjectUtils.nullSafeEquals(users, other.users); + } + + return false; + } + + @Override + public String toString() { + return "UserCredentials [userCredentials=" + users + "]"; + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/test/java/org/onap/so/simulator/model/PojoClassesTest.java b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/test/java/org/onap/so/simulator/model/PojoClassesTest.java new file mode 100755 index 00000000..8ae9b8b7 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/common/src/test/java/org/onap/so/simulator/model/PojoClassesTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.simulator.model; + +import org.junit.Test; +import org.onap.so.simulator.model.UserCredentials; +import com.openpojo.reflection.impl.PojoClassFactory; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; + + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class PojoClassesTest { + + @Test + public void test_UserCredentials_class() throws ClassNotFoundException { + verify(UserCredentials.class); + validate(UserCredentials.class); + } + + @Test + public void test_User_class() throws ClassNotFoundException { + verify(User.class); + validate(User.class); + } + + private void validate(final Class clazz) { + final Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(PojoClassFactory.getPojoClass(clazz)); + } + + private void verify(final Class clazz) { + EqualsVerifier.forClass(clazz).suppress(Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS).verify(); + } + +} -- cgit 1.2.3-korg