diff --git a/gate-web/src/main/java/com/opsmx/spinnaker/gate/audit/AuthenticationAuditListener.java b/gate-web/src/main/java/com/opsmx/spinnaker/gate/audit/AuthenticationAuditListener.java index 241a9fffb7..61d8c6e6f9 100644 --- a/gate-web/src/main/java/com/opsmx/spinnaker/gate/audit/AuthenticationAuditListener.java +++ b/gate-web/src/main/java/com/opsmx/spinnaker/gate/audit/AuthenticationAuditListener.java @@ -16,12 +16,12 @@ package com.opsmx.spinnaker.gate.audit; +import com.google.gson.Gson; import com.opsmx.spinnaker.gate.constant.CamelEndpointConstant; import com.opsmx.spinnaker.gate.enums.AuditEventType; import com.opsmx.spinnaker.gate.model.AuditData; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; +import com.opsmx.spinnaker.gate.model.OesAuditModel; +import java.util.*; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.apache.camel.ProducerTemplate; @@ -41,10 +41,10 @@ public class AuthenticationAuditListener extends AbstractAuthenticationAuditListener { @Autowired private AuditHandler auditHandler; - - // @Produce(uri = "direct:startRabbitMQPoint") @Autowired @Lazy private ProducerTemplate template; + Gson gson = new Gson(); + @Async @Override public void onApplicationEvent(AbstractAuthenticationEvent event) { @@ -82,9 +82,13 @@ public void onApplicationEvent(AbstractAuthenticationEvent event) { return; } log.debug("publishEvent LogoutSuccessEvent"); + auditHandler.publishEvent(AuditEventType.SUCCESSFUL_USER_LOGOUT_AUDIT, event); + AbstractAuthenticationToken auth = (AbstractAuthenticationToken) event.getAuthentication(); + String name = auth.getName(); template.asyncSendBody( CamelEndpointConstant.directUserActivity, - auditHandler.publishEvent(AuditEventType.SUCCESSFUL_USER_LOGOUT_AUDIT, event)); + getOesAuditModel( + name, event.getTimestamp(), AuditEventType.SUCCESSFUL_USER_LOGOUT_AUDIT)); } } catch (Exception e) { @@ -101,7 +105,22 @@ private void handleAuthenticationEvent( .map(GrantedAuthority::getAuthority) .collect(Collectors.toList()); AuditData data = new AuditData(name, roles, event.getTimestamp()); + auditHandler.publishEvent(eventType, data); template.asyncSendBody( - CamelEndpointConstant.directUserActivity, auditHandler.publishEvent(eventType, data)); + CamelEndpointConstant.directUserActivity, + getOesAuditModel(name, event.getTimestamp(), eventType)); + } + + private String getOesAuditModel(String name, Long timestamp, AuditEventType eventType) { + OesAuditModel oesAuditModel = new OesAuditModel(); + Map date = new HashMap<>(); + date.put("userName", name); + date.put("timestamp", timestamp); + oesAuditModel.setEventId(UUID.randomUUID().toString()); + oesAuditModel.setAuditData(date); + oesAuditModel.setEventType(eventType); + String model = gson.toJson(oesAuditModel, OesAuditModel.class); + log.info("model: {}", model); + return model; } }