-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from reportportal/EPMRPP-85164-AUTH
EPMRPP-85164 || Send Create SAML user events via Rabbit MQ to DB and logs
- Loading branch information
Showing
7 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/epam/reportportal/auth/config/rabbit/RabbitMqConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.epam.reportportal.auth.config.rabbit; | ||
|
||
import com.epam.ta.reportportal.exception.ReportPortalException; | ||
import com.epam.ta.reportportal.ws.model.ErrorType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.rabbitmq.http.client.Client; | ||
import java.net.URI; | ||
import org.springframework.amqp.rabbit.annotation.EnableRabbit; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@EnableRabbit | ||
@Configuration | ||
public class RabbitMqConfig { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public RabbitMqConfig(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Bean | ||
public MessageConverter jsonMessageConverter() { | ||
return new Jackson2JsonMessageConverter(objectMapper); | ||
} | ||
|
||
@Bean | ||
public ConnectionFactory connectionFactory(@Value("${rp.amqp.api-address}") String apiAddress, | ||
@Value("${rp.amqp.addresses}") URI addresses, | ||
@Value("${rp.amqp.base-vhost}") String virtualHost) { | ||
try { | ||
Client client = new Client(apiAddress); | ||
client.createVhost(virtualHost); | ||
} catch (Exception e) { | ||
throw new ReportPortalException(ErrorType.UNCLASSIFIED_REPORT_PORTAL_ERROR, | ||
"Unable to create RabbitMq virtual host: " + e.getMessage() | ||
); | ||
} | ||
final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(addresses); | ||
cachingConnectionFactory.setVirtualHost(virtualHost); | ||
return cachingConnectionFactory; | ||
} | ||
|
||
@Bean | ||
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { | ||
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); | ||
rabbitTemplate.setMessageConverter(jsonMessageConverter()); | ||
return rabbitTemplate; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/epam/reportportal/auth/event/ActivityEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.epam.reportportal.auth.event; | ||
|
||
import com.epam.reportportal.auth.event.activity.ActivityEvent; | ||
import com.epam.ta.reportportal.entity.activity.Activity; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
/** | ||
* Activity Event Handler catch events after commit, transforms to Activity and send to queue. | ||
* | ||
* @author Ryhor_Kukharenka | ||
*/ | ||
@Component | ||
public class ActivityEventHandler { | ||
|
||
private static final String EXCHANGE_ACTIVITY = "activity"; | ||
private final RabbitTemplate rabbitTemplate; | ||
|
||
public ActivityEventHandler(RabbitTemplate rabbitTemplate) { | ||
this.rabbitTemplate = rabbitTemplate; | ||
} | ||
|
||
@EventListener | ||
@TransactionalEventListener | ||
public void onApplicationEvent(ActivityEvent event) { | ||
Activity activity = event.toActivity(); | ||
String key = generateKey(activity); | ||
|
||
rabbitTemplate.convertAndSend(EXCHANGE_ACTIVITY, key, activity); | ||
} | ||
|
||
private String generateKey(Activity activity) { | ||
return String.format("activity.%d.%s.%s", | ||
activity.getProjectId(), | ||
activity.getObjectType(), | ||
activity.getEventName()); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/epam/reportportal/auth/event/activity/ActivityEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.epam.reportportal.auth.event.activity; | ||
|
||
import com.epam.ta.reportportal.entity.activity.Activity; | ||
|
||
public interface ActivityEvent { | ||
|
||
/** | ||
* Method for transform Event to Activity. | ||
* | ||
* @return Activity entity | ||
*/ | ||
Activity toActivity(); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/epam/reportportal/auth/event/activity/UserCreatedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.epam.reportportal.auth.event.activity; | ||
|
||
import com.epam.ta.reportportal.builder.ActivityBuilder; | ||
import com.epam.ta.reportportal.entity.activity.Activity; | ||
import com.epam.ta.reportportal.entity.activity.ActivityAction; | ||
import com.epam.ta.reportportal.entity.activity.EventAction; | ||
import com.epam.ta.reportportal.entity.activity.EventObject; | ||
import com.epam.ta.reportportal.entity.activity.EventPriority; | ||
import com.epam.ta.reportportal.entity.activity.EventSubject; | ||
|
||
/** | ||
* Event publish when user is created. | ||
* | ||
* @author Ryhor_Kukharenka | ||
*/ | ||
public class UserCreatedEvent implements ActivityEvent { | ||
|
||
private final Long userId; | ||
private final String userLogin; | ||
|
||
public UserCreatedEvent(Long userId, String userLogin) { | ||
this.userId = userId; | ||
this.userLogin = userLogin; | ||
} | ||
|
||
@Override | ||
public Activity toActivity() { | ||
return new ActivityBuilder() | ||
.addCreatedNow() | ||
.addAction(EventAction.CREATE) | ||
.addEventName(ActivityAction.CREATE_USER.getValue()) | ||
.addPriority(EventPriority.HIGH) | ||
.addObjectId(userId) | ||
.addObjectName(userLogin) | ||
.addObjectType(EventObject.USER) | ||
.addSubjectName("Auth Service") | ||
.addSubjectType(EventSubject.APPLICATION) | ||
.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters