Skip to content

Commit

Permalink
Add RabbitmqEventsAutoConfigurationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
groldan committed Oct 13, 2023
1 parent 73ace03 commit 3170782
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package org.georchestra.gateway.events;

import org.georchestra.gateway.autoconfigure.security.ConditionalOnCreateLdapAccounts;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Profile;

@Profile("!test && !it")
/**
* {@link AutoConfiguration @AutoConfiguration} to enable sending events over
* rabbitmq when it is enabled to create LDAP accounts from both/either
* pre-authenticated and OIDC authentication scenarios, AND
* {@literal georchestra.gateway.security.enableRabbitmqEvents = true}
*
* @see ConditionalOnCreateLdapAccounts
*/
@AutoConfiguration
@ConditionalOnCreateLdapAccounts
@AutoConfigureAfter(GatewayAutoConfiguration.class)
@ImportResource({ "classpath:rabbit-listener-context.xml", "classpath:rabbit-sender-context.xml" })
@ConditionalOnProperty(name = "georchestra.gateway.security.enableRabbitmqEvents", havingValue = "true", matchIfMissing = false)
public class RabbitmqEventsAutoConfiguration {

@Bean
@DependsOn({ "eventTemplate" })
RabbitmqEventsSender eventsSender(AmqpTemplate eventTemplate) {
RabbitmqEventsSender eventsSender(@Qualifier("eventTemplate") AmqpTemplate eventTemplate) {
return new RabbitmqEventsSender(eventTemplate);
}

Queue OAuth2ReplyQueue() {
return new Queue("OAuth2ReplyQueue", false);
}
@Bean
org.springframework.amqp.rabbit.connection.CachingConnectionFactory connectionFactory(//
@Value("${rabbitmqHost}") String host, //
@Value("${rabbitmqPort}") int port, //
@Value("${rabbitmqUser}") String user, //
@Value("${rabbitmqPassword}") String pwd) {

com.rabbitmq.client.ConnectionFactory fac = new com.rabbitmq.client.ConnectionFactory();
fac.setHost(host);
fac.setPort(port);
fac.setUsername(user);
fac.setPassword(pwd);

MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
simpleMessageListenerContainer.setConnectionFactory(connectionFactory);
simpleMessageListenerContainer.setQueues(OAuth2ReplyQueue());
simpleMessageListenerContainer.setMessageListener(new RabbitmqEventsListener());
return simpleMessageListenerContainer;
return new CachingConnectionFactory(fac);
}
}
}
2 changes: 1 addition & 1 deletion gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ server:
ssl:
enabled: false
#TODO: configure SSL with a self-signed certificate

spring:
config:
import: optional:file:${georchestra.datadir}/default.properties,optional:file:${georchestra.datadir}/gateway/gateway.yaml,optional:file:${georchestra.datadir}/gateway/security.yaml
Expand Down
51 changes: 33 additions & 18 deletions gateway/src/main/resources/rabbit-listener-context.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="connectionFactory" host="${rabbitmqHost}" port="${rabbitmqPort}" username="${rabbitmqUser}" password="${rabbitmqPassword}" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- Create OAuth2Queue queue -->
<rabbit:queue id="OAuth2ReplyQueue" />
<!-- create OAuth2Exchange and bind OAuth2Queue with routing-gateway to the OAUTH2-EXCHANGE-->
<rabbit:topic-exchange id="OAuth2Exchange" name="OAUTH2-EXCHANGE-GATEWAY">
<rabbit:bindings>
<rabbit:binding queue="OAuth2ReplyQueue" pattern="routing-console"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- instantiate eventsListener -->
<bean id="eventsListener" class="org.georchestra.gateway.events.RabbitmqEventsListener" >
</bean>
<!-- glue the listener and OAuth2Queue to the container-->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="eventsListener" queues="OAuth2ReplyQueue" /></rabbit:listener-container>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<!-- defined in RabbitmqEventsAutoConfiguration.class -->
<!--
<rabbit:connection-factory id="connectionFactory"
host="${rabbitmqHost}"
port="${rabbitmqPort}"
username="${rabbitmqUser}"
password="${rabbitmqPassword}" />
-->
<rabbit:admin connection-factory="connectionFactory" />

<!-- Create OAuth2Queue queue -->
<rabbit:queue id="OAuth2ReplyQueue" />

<!-- create OAuth2Exchange and bind OAuth2Queue with routing-gateway to the OAUTH2-EXCHANGE -->
<rabbit:topic-exchange id="OAuth2Exchange" name="OAUTH2-EXCHANGE-GATEWAY">
<rabbit:bindings>
<rabbit:binding queue="OAuth2ReplyQueue" pattern="routing-console"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>

<!-- instantiate eventsListener -->
<bean id="eventsListener" class="org.georchestra.gateway.events.RabbitmqEventsListener">
</bean>

<!-- glue the listener and OAuth2Queue to the container -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="eventsListener" queues="OAuth2ReplyQueue" />
</rabbit:listener-container>
</beans>
17 changes: 9 additions & 8 deletions gateway/src/main/resources/rabbit-sender-context.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<!-- obtain admin rights to create the an exchange -->
<rabbit:admin connection-factory="connectionFactory" />
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<!-- create a bean which can send message to OAUTH2-EXCHANGE for the program to call -->
<rabbit:template id="eventTemplate" connection-factory="connectionFactory" exchange="OAUTH2-EXCHANGE"/>
<!-- obtain admin rights to create the an exchange -->
<rabbit:admin connection-factory="connectionFactory" />

<!-- create a bean which can send message to OAUTH2-EXCHANGE for the program to call -->
<rabbit:template id="eventTemplate" connection-factory="connectionFactory" exchange="OAUTH2-EXCHANGE" />
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.georchestra.gateway.events;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

/**
* Application context test for {@link RabbitmqEventsAutoConfiguration}
*/
class RabbitmqEventsAutoConfigurationTest {

private ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RabbitmqEventsAutoConfiguration.class));

@Test
void conditionalOnPropertyNotSet() {
runner.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class));
}

@Test
void conditionalOnPropertyDisabled() {
runner.withPropertyValues("georchestra.gateway.security.enableRabbitmqEvents=false")
.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class));
}

@Test
void conditionalOnPropertyEnabled_requires_default_ldap_and_create_users_enabled() {
runner.withPropertyValues("georchestra.gateway.security.createNonExistingUsersInLDAP=false", //
"georchestra.gateway.security.ldap.default.enabled=", //
"georchestra.gateway.security.enableRabbitmqEvents=true", //
"rabbitmqHost=test.rabbit", //
"rabbitmqPort=3333", //
"rabbitmqUser=bunny", //
"rabbitmqPassword=rabbit"//
).run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class));

runner.withPropertyValues("georchestra.gateway.security.createNonExistingUsersInLDAP=true", //
"georchestra.gateway.security.ldap.default.enabled=true", //
"georchestra.gateway.security.enableRabbitmqEvents=true", //
"rabbitmqHost=test.rabbit", //
"rabbitmqPort=3333", //
"rabbitmqUser=bunny", //
"rabbitmqPassword=rabbit"//
).run(context -> {

assertThat(context).hasNotFailed().hasSingleBean(RabbitmqEventsSender.class);

assertThat(context).hasBean("connectionFactory");
CachingConnectionFactory rabbitMQConnectionFactory = (CachingConnectionFactory) context
.getBean("connectionFactory");
assertThat(rabbitMQConnectionFactory.getHost()).isEqualTo("test.rabbit");
assertThat(rabbitMQConnectionFactory.getPort()).isEqualTo(3333);
assertThat(rabbitMQConnectionFactory.getUsername()).isEqualTo("bunny");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import com.nimbusds.oauth2.sdk.util.JSONUtils;

/**
*
*
*/
class OpenIdConnectUserMapperTest {

Expand Down

0 comments on commit 3170782

Please sign in to comment.