X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-backend%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2Fconfig%2FWebSecurityMockConfiguration.java;h=257b4a4978ba3864284d9830cf1eddc6a5d3116d;hb=refs%2Fchanges%2F58%2F2258%2F2;hp=c17baefd215b20ae621ccf3b29a1596d7bde561f;hpb=3f812ea25d352ec33d07f5ffa4c2aa2a77e8e793;p=portal%2Fric-dashboard.git diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/WebSecurityMockConfiguration.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/WebSecurityMockConfiguration.java index c17baefd..257b4a49 100644 --- a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/WebSecurityMockConfiguration.java +++ b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/WebSecurityMockConfiguration.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 AT&T Intellectual Property and Nokia + * Copyright (C) 2019 AT&T Intellectual Property * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,33 +19,14 @@ */ package org.oransc.ric.portal.dashboard.config; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; - -import java.io.File; -import java.io.IOException; import java.lang.invoke.MethodHandles; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import javax.servlet.http.HttpServletRequest; -import org.onap.portalsdk.core.onboarding.crossapi.PortalRestAPIProxy; -import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; -import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; -import org.onap.portalsdk.core.restful.domain.EcompRole; -import org.onap.portalsdk.core.restful.domain.EcompUser; +import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService; import org.oransc.ric.portal.dashboard.DashboardConstants; -import org.oransc.ric.portal.dashboard.LoginServlet; -import org.oransc.ric.portal.dashboard.portalapi.DashboardUserManager; import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @@ -67,18 +48,20 @@ public class WebSecurityMockConfiguration extends WebSecurityConfigurerAdapter { public static final String TEST_CRED_ADMIN = "admin"; public static final String TEST_CRED_STANDARD = "standard"; - // Unfortunately EPSDK-FW does not define these as constants - public static final String PORTAL_USERNAME_HEADER_KEY = "username"; - public static final String PORTAL_PASSWORD_HEADER_KEY = "password"; - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - public WebSecurityMockConfiguration(@Value("${userfile}") final String userFilePath) { - logger.debug("ctor: user file path {}", userFilePath); - } + // Although constructor arguments are recommended over field injection, + // this results in fewer lines of code. + @Value("${portalapi.decryptor}") + private String decryptor; + @Value("${portalapi.usercookie}") + private String userCookie; + @Value("${userfile}") + private String userFilePath; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { + logger.debug("configure"); PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); auth.inMemoryAuthentication() // .passwordEncoder(encoder) // @@ -104,68 +87,15 @@ public class WebSecurityMockConfiguration extends WebSecurityConfigurerAdapter { public void configure(WebSecurity web) throws Exception { // This disables Spring security, but not the app's filter. web.ignoring().antMatchers(WebSecurityConfiguration.OPEN_PATHS); + web.ignoring().antMatchers("/", "/csrf"); // allow swagger-ui to load } @Bean - public ServletRegistrationBean loginServlet() { - LoginServlet servlet = new LoginServlet(); - final ServletRegistrationBean servletBean = new ServletRegistrationBean<>(servlet, - DashboardConstants.LOGIN_PAGE); - servletBean.setName("LoginServlet"); - return servletBean; - } - - @Bean - public ServletRegistrationBean portalApiProxyServlet() { - PortalRestAPIProxy servlet = new PortalRestAPIProxy(); - final ServletRegistrationBean servletBean = new ServletRegistrationBean<>(servlet, - PortalApiConstants.API_PREFIX + "/*"); - servletBean.setName("PortalRestApiProxyServlet"); - return servletBean; - } - - @Bean - public PortalAuthManager portalAuthManager() throws Exception { - PortalAuthManager mockManager = mock(PortalAuthManager.class); - final Map credentialsMap = new HashMap<>(); - credentialsMap.put("appName", "appName"); - credentialsMap.put(PORTAL_USERNAME_HEADER_KEY, PORTAL_USERNAME_HEADER_KEY); - credentialsMap.put(PORTAL_PASSWORD_HEADER_KEY, PORTAL_PASSWORD_HEADER_KEY); - doAnswer(inv -> { - logger.debug("getAppCredentials"); - return credentialsMap; - }).when(mockManager).getAppCredentials(); - doAnswer(inv -> { - logger.debug("getUserId"); - return "userId"; - }).when(mockManager).valdiateEcompSso(any(HttpServletRequest.class)); - doAnswer(inv -> { - logger.debug("getAppCredentials"); - return credentialsMap; - }).when(mockManager).getAppCredentials(); - return mockManager; - } - - // This implementation is so light it can be used during tests. - @Bean - public DashboardUserManager dashboardUserManager() throws IOException, PortalAPIException { - File f = new File("/tmp/users.json"); - if (f.exists()) - f.delete(); - DashboardUserManager um = new DashboardUserManager(f.getAbsolutePath()); - // Mock user for convenience in testing - EcompUser demo = new EcompUser(); - demo.setLoginId("demo"); - demo.setFirstName("Demo"); - demo.setLastName("User"); - demo.setActive(true); - EcompRole role = new EcompRole(); - role.setName("view"); - Set roles = new HashSet<>(); - roles.add(role); - demo.setRoles(roles); - um.createUser(demo); - return um; + public PortalAuthManager portalAuthManagerBean() throws Exception { + logger.debug("portalAuthManagerBean"); + return new PortalAuthManager(IPortalRestCentralService.CREDENTIALS_APP, + IPortalRestCentralService.CREDENTIALS_USER, IPortalRestCentralService.CREDENTIALS_PASS, decryptor, + userCookie); } }