Skip to content

Commit

Permalink
OP-21175: userName and timestamp only needed for logout
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-chekuri committed Sep 26, 2023
1 parent 45a0a8c commit c46ebe8
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String, Object> 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;
}
}

0 comments on commit c46ebe8

Please sign in to comment.