Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAGO-81464: OAuth2 conditional auto configuration activation issue fix #137

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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