From 0ecf12f4e581fa613d771c0207ac9af06bb07eea Mon Sep 17 00:00:00 2001 From: mayur-solace <81177023+mayur-solace@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:35:42 -0400 Subject: [PATCH] DATAGO-81464: OAuth2 conditional auto configuration activation issue fix (#137) * DATAGO-81464: OAuth2 conditional auto configuration activation issue fix * DATAGO-81464: OAuth2 conditional auto configuration activation issue fix --- .../SolaceOAuthClientConfiguration.java | 5 +- .../SolaceOAuthClientConfigurationTest.java | 63 +++++++++++++++++++ .../resources/application-oauthConfigIT.yml | 24 +++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfigurationTest.java create mode 100644 solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/resources/application-oauthConfigIT.yml diff --git a/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/main/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfiguration.java b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/main/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfiguration.java index a96a62f..d8656e3 100644 --- a/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/main/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfiguration.java +++ b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/main/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfiguration.java @@ -2,7 +2,7 @@ import com.solacesystems.jcsmp.DefaultSolaceSessionOAuth2TokenProvider; import com.solacesystems.jcsmp.JCSMPProperties; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -19,8 +19,7 @@ * 'AUTHENTICATION_SCHEME_OAUTH2'. */ @Configuration -@ConditionalOnProperty(prefix = "solace.java.apiProperties", name = "AUTHENTICATION_SCHEME", - havingValue = "AUTHENTICATION_SCHEME_OAUTH2") +@ConditionalOnExpression("'${solace.java.api-properties.AUTHENTICATION_SCHEME}' == 'AUTHENTICATION_SCHEME_OAUTH2' OR '${solace.java.apiProperties.AUTHENTICATION_SCHEME}' == 'AUTHENTICATION_SCHEME_OAUTH2'") @Import(OAuth2ClientAutoConfiguration.class) public class SolaceOAuthClientConfiguration { diff --git a/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfigurationTest.java b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfigurationTest.java new file mode 100644 index 0000000..30ca98b --- /dev/null +++ b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/java/com/solace/spring/boot/autoconfigure/SolaceOAuthClientConfigurationTest.java @@ -0,0 +1,63 @@ +package com.solace.spring.boot.autoconfigure; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import com.solacesystems.jcsmp.SolaceSessionOAuth2TokenProvider; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager; + +class SolaceOAuthClientConfigurationTest { + + @SpringBootApplication + public static class TestApp { + + } + + @Test + void verifyApplicationContextContainsRequiredBeans() { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder() + .profiles("oauthConfigIT").sources(TestApp.class) + .properties(String.format("%s=%s", "solace.java.api-properties.AUTHENTICATION_SCHEME", + "AUTHENTICATION_SCHEME_OAUTH2")) + .run()) { + assertThat(context.isRunning()).isTrue(); + assertThat(context.getBean(SolaceSessionOAuth2TokenProvider.class)).isNotNull(); + assertThat( + context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class)).isNotNull(); + } + } + + @Test + void verifyApplicationContextContainsRequiredBeans2() { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder() + .profiles("oauthConfigIT").sources(TestApp.class) + .properties(String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME", + "AUTHENTICATION_SCHEME_OAUTH2")) + .run()) { + assertThat(context.isRunning()).isTrue(); + assertThat(context.getBean(SolaceSessionOAuth2TokenProvider.class)).isNotNull(); + assertThat( + context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class)).isNotNull(); + } + } + + @Test + void verifyApplicationContextDoesNotContainOAuth2BeansWhenAuthSchemeIsNotOAuth2() { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder() + .profiles("oauthConfigIT").sources(TestApp.class) + .properties(String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME", + "AUTHENTICATION_SCHEME_BASIC")) + .run()) { + assertThat(context.isRunning()).isTrue(); + + assertThatThrownBy(() -> context.getBean(SolaceSessionOAuth2TokenProvider.class)) + .isInstanceOf(NoSuchBeanDefinitionException.class); + assertThatThrownBy(() -> context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class)) + .isInstanceOf(NoSuchBeanDefinitionException.class); + } + } +} \ No newline at end of file diff --git a/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/resources/application-oauthConfigIT.yml b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/resources/application-oauthConfigIT.yml new file mode 100644 index 0000000..6c3d2a3 --- /dev/null +++ b/solace-spring-boot-autoconfigure/solace-java-spring-boot-autoconfigure/src/test/resources/application-oauthConfigIT.yml @@ -0,0 +1,24 @@ +spring: + security: + oauth2: + client: + registration: + my-oauth2-client: + provider: my-auth-server + client-id: testClientId + client-secret: testClientSecret + authorization-grant-type: client_credentials + scope: openid + provider: + my-auth-server: + token-uri: https://localhost:10443/auth/realms/solace-oauth-resource-server-role/protocol/openid-connect/token + +solace: + java: + host: tcps://localhost:55443 + msgVpn: default + connectRetries: 3 + reconnectRetries: 3 + connectRetriesPerHost: 1 + reconnectRetryWaitInMillis: 2000 + oauth2ClientRegistrationId: my-oauth2-client