From bf598be30e229f0f4a59dae017ceb034811039f2 Mon Sep 17 00:00:00 2001 From: lha Date: Fri, 22 Nov 2024 17:43:25 +0100 Subject: [PATCH] fix more tests --- pom.xml | 44 ++++++++-- .../activeviam/mac/app/MacSpringBootApp.java | 2 +- .../impl/ActiveUiResourceServerConfig.java | 6 +- .../mac/cfg/impl/ContentServiceConfig.java | 9 +- .../mac/cfg/impl/MacServerConfig.java | 54 ------------ .../mac/cfg/security/impl/SecurityConfig.java | 83 ++++++++++++------- .../activeviam/mac/app/TestAppLoading.java | 8 +- .../memory/ATestMemoryStatistic.java | 10 +-- 8 files changed, 102 insertions(+), 114 deletions(-) diff --git a/pom.xml b/pom.xml index 4258547c..c49288c7 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.18 + 3.2.10 @@ -26,9 +26,9 @@ 11 6.1.2-SNAPSHOT - 5.1.13 + 5.2.0-beta - 9.0.31 + 10.1.30 com.activeviam.mac.app.MacSpringBootApp @@ -51,6 +51,11 @@ jakarta.servlet-api 6.0.0 + + org.springframework.boot + spring-boot-starter-json + 3.2.10 + @@ -94,6 +99,7 @@ org.hibernate hibernate-core + 6.4.10.Final com.h2database @@ -102,6 +108,7 @@ org.hibernate hibernate-c3p0 + 6.4.10.Final @@ -169,11 +176,6 @@ guava 32.1.3-jre test - - - io.atoti - directquery-bigquery - ${atoti-server.version} com.activeviam.source @@ -188,7 +190,31 @@ jakarta.persistence jakarta.persistence-api - 3.2.0 + ${jakarta-persistence.version} + + + org.yaml + snakeyaml + 2.2 + + + jakarta.ws.rs + jakarta.ws.rs-api + ${jakarta-ws-rs.version} + + + org.springframework + spring-web + ${spring-framework.version} + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + + + org.springframework.boot + spring-boot-autoconfigure diff --git a/src/main/java/com/activeviam/mac/app/MacSpringBootApp.java b/src/main/java/com/activeviam/mac/app/MacSpringBootApp.java index 23041e81..dfd3d01e 100644 --- a/src/main/java/com/activeviam/mac/app/MacSpringBootApp.java +++ b/src/main/java/com/activeviam/mac/app/MacSpringBootApp.java @@ -9,7 +9,7 @@ import com.activeviam.mac.cfg.impl.MacServerConfig; import com.activeviam.tech.core.api.registry.Registry; -import javax.servlet.MultipartConfigElement; +import jakarta.servlet.MultipartConfigElement; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; diff --git a/src/main/java/com/activeviam/mac/cfg/impl/ActiveUiResourceServerConfig.java b/src/main/java/com/activeviam/mac/cfg/impl/ActiveUiResourceServerConfig.java index 1a375375..8215b7ff 100644 --- a/src/main/java/com/activeviam/mac/cfg/impl/ActiveUiResourceServerConfig.java +++ b/src/main/java/com/activeviam/mac/cfg/impl/ActiveUiResourceServerConfig.java @@ -41,9 +41,9 @@ protected void registerRedirections(final ResourceRegistry registry) { protected void registerExtensions(final ResourceRegistry registry) { registry.serve("/ui/extensions*.json").addResourceLocations("classpath:/static/activeui/"); - registry - .serve("/ui/extensions/text-editor-extension/**/*.js") - .addResourceLocations("classpath:/static/activeui/extensions/text-editor-extension/"); + // registry + // .serve("/ui/extensions/text-editor-extension/**/*.js") + // .addResourceLocations("classpath:/static/activeui/extensions/text-editor-extension/"); } /** diff --git a/src/main/java/com/activeviam/mac/cfg/impl/ContentServiceConfig.java b/src/main/java/com/activeviam/mac/cfg/impl/ContentServiceConfig.java index 456667ba..b94d5fc8 100644 --- a/src/main/java/com/activeviam/mac/cfg/impl/ContentServiceConfig.java +++ b/src/main/java/com/activeviam/mac/cfg/impl/ContentServiceConfig.java @@ -23,6 +23,7 @@ import com.activeviam.tools.bookmark.constant.impl.ContentServerConstants.Paths; import com.activeviam.tools.bookmark.constant.impl.ContentServerConstants.Role; import com.activeviam.tools.bookmark.impl.BookmarkTool; +import java.io.IOException; import java.lang.reflect.Method; import java.util.List; import java.util.Map; @@ -37,6 +38,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; /** * Spring configuration of the Content Service. @@ -82,11 +85,13 @@ public class ContentServiceConfig implements IActivePivotContentServiceConfig { * @return the Hibernate's configuration */ private static SessionFactory loadConfiguration(final Properties hibernateProperties) - throws HibernateException { + throws HibernateException, IOException { hibernateProperties.put( AvailableSettings.DATASOURCE, createTomcatJdbcDataSource(hibernateProperties)); + final Resource entityMappingFile = new ClassPathResource("content-service-hibernate.xml"); return new org.hibernate.cfg.Configuration() .addProperties(hibernateProperties) + .addInputStream(entityMappingFile.getInputStream()) .buildSessionFactory(); } @@ -141,7 +146,7 @@ public IContentService contentService() { try { sessionFactory = loadConfiguration(contentServiceHibernateProperties()); return new HibernateContentService(sessionFactory); - } catch (HibernateException e) { + } catch (HibernateException | IOException e) { throw new BeanInitializationException("Failed to initialize the Content Service", e); } } diff --git a/src/main/java/com/activeviam/mac/cfg/impl/MacServerConfig.java b/src/main/java/com/activeviam/mac/cfg/impl/MacServerConfig.java index b20c37fd..b29d2d16 100644 --- a/src/main/java/com/activeviam/mac/cfg/impl/MacServerConfig.java +++ b/src/main/java/com/activeviam/mac/cfg/impl/MacServerConfig.java @@ -7,22 +7,18 @@ package com.activeviam.mac.cfg.impl; -import com.activeviam.activepivot.server.impl.private_.observability.DynamicActivePivotManagerMBean; -import com.activeviam.activepivot.server.impl.private_.observability.memory.MemoryAnalysisService; import com.activeviam.activepivot.server.spring.api.config.IActivePivotConfig; import com.activeviam.activepivot.server.spring.api.config.IActivePivotContentServiceConfig; import com.activeviam.activepivot.server.spring.api.config.IDatastoreConfig; import com.activeviam.activepivot.server.spring.private_.config.impl.ActivePivotServicesConfig; import com.activeviam.activepivot.server.spring.private_.config.impl.ActiveViamRestServicesConfig; import com.activeviam.activepivot.server.spring.private_.config.impl.ActiveViamWebSocketServicesConfig; -import com.activeviam.activepivot.server.spring.private_.pivot.content.impl.DynamicActivePivotContentServiceMBean; import com.activeviam.mac.cfg.security.impl.SecurityConfig; import com.activeviam.mac.cfg.security.impl.UserConfig; import com.activeviam.tech.core.api.agent.AgentException; import com.activeviam.web.spring.internal.JMXEnabler; import com.activeviam.web.spring.internal.config.JwtConfig; import com.activeviam.web.spring.internal.config.JwtRestServiceConfig; -import java.nio.file.Paths; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.annotation.Bean; @@ -134,28 +130,6 @@ public JMXEnabler jmxMonitoringConnectorEnabler() { return new JMXEnabler("StatisticSource", this.sourceConfig); } - /** - * Enable JMX Monitoring for the Datastore. - * - * @return the {@link JMXEnabler} attached to the datastore - */ - @Bean - public JMXEnabler jmxDatastoreEnabler() { - return new JMXEnabler(this.datastoreConfig.database()); - } - - /** - * Enable JMX Monitoring for ActivePivot Components. - * - * @return the {@link JMXEnabler} attached to the activePivotManager - */ - @Bean - public JMXEnabler jmxActivePivotEnabler() { - startManager(); - - return new JMXEnabler(new DynamicActivePivotManagerMBean(apConfig.activePivotManager())); - } - /** * [Bean] JMX Bean to export bookmarks. * @@ -165,32 +139,4 @@ public JMXEnabler jmxActivePivotEnabler() { public JMXEnabler jmxBookmarkEnabler() { return new JMXEnabler("Bookmark", this.contentServiceConfig); } - - /** - * Enable JMX Monitoring for the ContentService. - * - * @return the {@link JMXEnabler} attached to the Content Service - */ - @Bean - public JMXEnabler jmxActivePivotContentServiceEnabler() { - // to allow operations from the JMX bean - return new JMXEnabler( - new DynamicActivePivotContentServiceMBean( - this.apContentServiceConfig.activePivotContentService(), - this.apConfig.activePivotManager())); - } - - /** - * Enable Memory JMX Monitoring. - * - * @return the {@link JMXEnabler} attached to the memory analysis service. - */ - @Bean - public JMXEnabler jmxMemoryMonitoringServiceEnabler() { - return new JMXEnabler( - new MemoryAnalysisService( - this.datastoreConfig.database(), - this.apConfig.activePivotManager(), - Paths.get(System.getProperty("java.io.tmpdir")))); - } } diff --git a/src/main/java/com/activeviam/mac/cfg/security/impl/SecurityConfig.java b/src/main/java/com/activeviam/mac/cfg/security/impl/SecurityConfig.java index 02c8d29a..58049ed6 100644 --- a/src/main/java/com/activeviam/mac/cfg/security/impl/SecurityConfig.java +++ b/src/main/java/com/activeviam/mac/cfg/security/impl/SecurityConfig.java @@ -6,7 +6,6 @@ import com.activeviam.activepivot.server.spring.private_.pivot.security.impl.UserDetailsServiceWrapper; import com.activeviam.tech.contentserver.storage.api.IContentService; import com.activeviam.tech.core.api.security.IUserDetailsService; -import com.activeviam.web.spring.api.config.ICorsConfig; import com.activeviam.web.spring.api.config.IJwtConfig; import com.activeviam.web.spring.api.jwt.JwtAuthenticationProvider; import com.activeviam.web.spring.internal.config.JwtRestServiceConfig; @@ -21,11 +20,17 @@ import org.springframework.core.env.Environment; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.ProviderManager; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.Customizer; 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.configurers.AbstractHttpConfigurer; import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; +import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.DelegatingPasswordEncoder; import org.springframework.security.crypto.password.NoOpPasswordEncoder; @@ -36,11 +41,9 @@ import org.springframework.security.web.authentication.switchuser.SwitchUserFilter; import org.springframework.security.web.context.SecurityContextPersistenceFilter; import org.springframework.security.web.firewall.StrictHttpFirewall; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; @Configuration +@EnableWebSecurity public class SecurityConfig { /** Authentication Bean Name. */ @@ -81,8 +84,8 @@ public class SecurityConfig { /** {@code true} to enable the logout URL. */ protected final WebConfiguration webConfiguration; - public SecurityConfig(final String cookieName) { - this.webConfiguration = new WebConfiguration(cookieName); + public SecurityConfig() { + this.webConfiguration = new WebConfiguration(COOKIE_NAME); } /** @@ -118,6 +121,38 @@ public AuthenticationEntryPoint basicAuthenticationEntryPoint() { return new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED); } + /** + * Configures the authentication of the whole application. + * + *

This binds the defined user service to the authentication and sets the source for JWT + * tokens. + * + * @param inMemoryAuthenticationProvider the in-memory authentication provider + * @param jwtAuthenticationProvider is a provider which can perform authentication from the + * jwtService's tokens. Implementation from the {@link IJwtConfig} . + * @return the authentication manager + */ + @Bean + public AuthenticationManager authenticationManager( + final JwtAuthenticationProvider jwtAuthenticationProvider, + final AuthenticationProvider inMemoryAuthenticationProvider) { + final ProviderManager providerManager = + new ProviderManager(inMemoryAuthenticationProvider, jwtAuthenticationProvider); + providerManager.setEraseCredentialsAfterAuthentication(false); + + return providerManager; + } + + @Bean + public AuthenticationProvider inMemoryAuthenticationProvider( + final UserDetailsService userDetailsService, final PasswordEncoder passwordEncoder) { + final var authenticationProvider = new DaoAuthenticationProvider(); + authenticationProvider.setPasswordEncoder(passwordEncoder); + authenticationProvider.setUserDetailsService(userDetailsService); + + return authenticationProvider; + } + /** * Configures the authentication of the whole application. * @@ -136,28 +171,6 @@ public void configureGlobal( .authenticationProvider(jwtAuthenticationProvider); } - /** - * [Bean] Spring standard way of configuring CORS. - * - *

This simply forwards the configuration of {@link ICorsConfig} to Spring security system. - * - * @return the configuration for the application. - */ - @Bean - public CorsConfigurationSource corsConfigurationSource(final ICorsConfig corsConfig) { - final CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(corsConfig.getAllowedOrigins()); - configuration.setAllowedHeaders(corsConfig.getAllowedHeaders()); - configuration.setExposedHeaders(corsConfig.getExposedHeaders()); - configuration.setAllowedMethods(corsConfig.getAllowedMethods()); - configuration.setAllowCredentials(true); - - final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", configuration); - - return source; - } - /** * Returns the spring security bean user details service wrapper. * @@ -280,15 +293,21 @@ public SecurityFilterChain activePivotSecurity( .permitAll() .anyRequest() .hasAnyAuthority(ROLE_USER)) - // One has to be a user for all the other URLs - .securityMatcher("/**") - .authorizeHttpRequests(auth -> auth.anyRequest().hasAnyAuthority(ROLE_USER)) - .httpBasic(Customizer.withDefaults()) // SwitchUserFilter is the last filter in the chain. See FilterComparator class. .addFilterAfter(contextValueFilter, SwitchUserFilter.class); return http.build(); } + // One has to be a user for all the other URLs + @Bean + @Order(5) + public SecurityFilterChain otherSecurity(final HttpSecurity http) throws Exception { + http.securityMatcher("/**") + .authorizeHttpRequests(auth -> auth.anyRequest().permitAll()) + .httpBasic(Customizer.withDefaults()); + return http.build(); + } + public void configureWebSecurity( HttpSecurity http, JwtFilter jwtFilter, WebConfiguration webConfig) throws Exception { http diff --git a/src/test/java/com/activeviam/mac/app/TestAppLoading.java b/src/test/java/com/activeviam/mac/app/TestAppLoading.java index a6db2010..14f90173 100644 --- a/src/test/java/com/activeviam/mac/app/TestAppLoading.java +++ b/src/test/java/com/activeviam/mac/app/TestAppLoading.java @@ -12,14 +12,12 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanCreationException; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.PropertySource; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; /** Test class for tests related to loading cases of the SpringBoot application */ -@WebAppConfiguration -@Import({MacServerConfig.class}) -@PropertySource(value = "classpath:application.yml") +@WebAppConfiguration("classpath:application.yml") +@ContextConfiguration(classes = MacServerConfig.class) public class TestAppLoading { /** Ensures that the default application setup works */ diff --git a/src/test/java/com/activeviam/mac/statistic/memory/ATestMemoryStatistic.java b/src/test/java/com/activeviam/mac/statistic/memory/ATestMemoryStatistic.java index e73d147a..68564659 100644 --- a/src/test/java/com/activeviam/mac/statistic/memory/ATestMemoryStatistic.java +++ b/src/test/java/com/activeviam/mac/statistic/memory/ATestMemoryStatistic.java @@ -1247,16 +1247,10 @@ IDatastore assertLoadsCorrectly( protected static MemoryStatisticsTestUtils.StatisticsSummary computeStatisticsSummary( final Collection statistics, final Class creatorClass) { - // create a new list of statistics with no parent - final List statisticsWithoutParent = - statistics.stream() - .peek( - stat -> stat.setParent(null)) - .collect(Collectors.toList()); return MemoryStatisticsTestUtils.getStatisticsSummary( new TestMemoryStatisticBuilder(creatorClass.getName()) .withCreatorClasses(creatorClass) - .withChildren(statisticsWithoutParent) + .withChildren(statistics) .build()); } @@ -1457,7 +1451,7 @@ static AMemoryStatistic loadMemoryStatFromFolder( throw new RuntimeException("Cannot read " + file, e); } }) - .peek(stat -> stat.setParent(null)) + .peek(stat -> stat.setParent(null)) .collect(Collectors.toList()); return new DefaultMemoryStatistic.Builder()