diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d955f03a2..63e897068 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,12 +22,6 @@ jobs: uses: actions/setup-java@v1 with: java-version: 11 - - name: Setup Redis - uses: supercharge/redis-github-action@1.1.0 - - - name: Show running services - run: sudo netstat -tuplen # listing all the port for debug purpose. - - name: Run Unit tests with Maven run: mvn -B clean test jacoco:report --file pom.xml --no-transfer-progress - name: Set Branch name Environment variable @@ -46,4 +40,4 @@ jobs: -D repoToken="$COVERALLS_REPO_TOKEN" \ -D serviceName=Github \ -D branch="$BRANCH_NAME" \ - -D pullRequest="$PR_NUMBER" \ \ No newline at end of file + -D pullRequest="$PR_NUMBER" \ diff --git a/pom.xml b/pom.xml index 3eede7e19..40bcda654 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ opensrp-server-web war - 2.1.70.8-SNAPSHOT + 2.1.70.9-SNAPSHOT opensrp-server-web OpenSRP Server Web Application https://github.com/OpenSRP/opensrp-server-web @@ -394,6 +394,12 @@ 3.29.2-GA + + org.testcontainers + testcontainers + 1.17.2 + test + diff --git a/src/main/java/org/opensrp/web/config/security/OAuth2SecurityConfig.java b/src/main/java/org/opensrp/web/config/security/OAuth2SecurityConfig.java index ce96a569a..477a76eef 100644 --- a/src/main/java/org/opensrp/web/config/security/OAuth2SecurityConfig.java +++ b/src/main/java/org/opensrp/web/config/security/OAuth2SecurityConfig.java @@ -1,8 +1,10 @@ /** - * + * */ package org.opensrp.web.config.security; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.opensrp.web.config.Role; import org.opensrp.web.security.OauthAuthenticationProvider; import org.springframework.beans.factory.annotation.Autowired; @@ -10,10 +12,15 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.jdbc.core.JdbcTemplate; 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.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.ClientDetailsService; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator; +import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @@ -26,15 +33,15 @@ @EnableWebSecurity @Configuration @Profile("oauth2") -public class OAuth2SecurityConfig extends BasicAuthSecurityConfig{ +public class OAuth2SecurityConfig extends BasicAuthSecurityConfig { @Autowired private OauthAuthenticationProvider opensrpAuthenticationProvider; @Autowired private ClientDetailsService clientDetailsService; - - @Qualifier( value = "openSRPDataSource") + + @Qualifier(value = "openSRPDataSource") @Autowired private DataSource dataSource; @@ -67,14 +74,13 @@ protected void configure(HttpSecurity http) throws Exception { /* @formatter:on */ } - @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(opensrpAuthenticationProvider).eraseCredentials(false); - } + } public DefaultTokenServices tokenServices() { - DefaultTokenServices tokenServices= new DefaultTokenServices(); + DefaultTokenServices tokenServices = new DefaultTokenServices(); tokenServices.setTokenStore(tokenStore()); tokenServices.setSupportRefreshToken(true); tokenServices.setClientDetailsService(clientDetailsService); @@ -83,7 +89,27 @@ public DefaultTokenServices tokenServices() { @Bean public JdbcTokenStore tokenStore() { - return new JdbcTokenStore(dataSource); + final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + final AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator(); + Logger logger = LogManager.getLogger(JdbcTokenStore.class.toString()); + return new JdbcTokenStore(dataSource) { + + @Override + public void storeAccessToken(final OAuth2AccessToken token, final OAuth2Authentication authentication) { + logger.info("Invoking store access token method"); + if (authentication != null) { + final String key = authenticationKeyGenerator.extractKey(authentication); + int rowsAffected = jdbcTemplate.update("delete from oauth_access_token where authentication_id = ?", + key); + String isSuccess = (rowsAffected > 0) ? "Success" : "Failure"; + logger.info("Attempt to delete authentication_id {} from oauth_access_token table was a {}", key, + isSuccess); + } + + super.storeAccessToken(token, authentication); + } + + }; } } diff --git a/src/test/java/org/opensrp/TestRedisConfig.java b/src/test/java/org/opensrp/TestRedisConfig.java index ff0850952..dc363f9d6 100644 --- a/src/test/java/org/opensrp/TestRedisConfig.java +++ b/src/test/java/org/opensrp/TestRedisConfig.java @@ -22,22 +22,19 @@ */ @Configuration @EnableCaching -public class TestRedisConfig { +public class TestRedisConfig extends TestRedisInstance { @Value("#{opensrp['redis.host']}") private String redisHost; - - @Value("#{opensrp['redis.port']}") - private int redisPort; - + private int redisDatabase = 0; @Value("#{opensrp['redis.pool.max.connections']}") private int redisMaxConnections = 0; - private RedisStandaloneConfiguration redisStandaloneConfiguration() { - RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort); + int port = TestRedisInstance.redisContainer.getMappedPort(TestRedisInstance.DOCKER_EXPOSE_PORT); + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, port); redisStandaloneConfiguration.setDatabase(redisDatabase); return redisStandaloneConfiguration; } diff --git a/src/test/java/org/opensrp/TestRedisInstance.java b/src/test/java/org/opensrp/TestRedisInstance.java new file mode 100644 index 000000000..7f14036e6 --- /dev/null +++ b/src/test/java/org/opensrp/TestRedisInstance.java @@ -0,0 +1,16 @@ +package org.opensrp; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; + +public abstract class TestRedisInstance { + private static final String DOCKER_IMAGE_NAME = "redis:7-alpine"; + protected static final int DOCKER_EXPOSE_PORT = 6379; + + protected static final GenericContainer redisContainer = new GenericContainer<>(DockerImageName.parse(DOCKER_IMAGE_NAME)) + .withExposedPorts(DOCKER_EXPOSE_PORT); + + static { + redisContainer.start(); + } +} diff --git a/src/test/resources/test-persistence-postgres.xml b/src/test/resources/test-persistence-postgres.xml index 6d8b8639b..179cd2e40 100644 --- a/src/test/resources/test-persistence-postgres.xml +++ b/src/test/resources/test-persistence-postgres.xml @@ -34,4 +34,4 @@ - \ No newline at end of file +