1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.wa.starter.audit;
20
21 import com.fasterxml.jackson.core.JsonProcessingException;
22 import java.time.OffsetDateTime;
23 import java.util.Map;
24 import java.util.Set;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.syncope.common.lib.audit.AuditEntry;
27 import org.apache.syncope.common.lib.types.AuditElements;
28 import org.apache.syncope.common.lib.types.AuditLoggerName;
29 import org.apache.syncope.common.rest.api.service.AuditService;
30 import org.apache.syncope.wa.bootstrap.WARestClient;
31 import org.apereo.cas.audit.spi.AbstractAuditTrailManager;
32 import org.apereo.inspektr.audit.AuditActionContext;
33
34 public class WAAuditTrailManager extends AbstractAuditTrailManager {
35
36 private final WARestClient waRestClient;
37
38 public WAAuditTrailManager(final WARestClient restClient) {
39 super(true);
40 this.waRestClient = restClient;
41 }
42
43 @Override
44 protected void saveAuditRecord(final AuditActionContext audit) {
45 if (!waRestClient.isReady()) {
46 LOG.debug("Syncope client is not yet ready to store audit record");
47 return;
48 }
49
50 LOG.info("Loading application definitions");
51 try {
52 String output = MAPPER.writeValueAsString(Map.of("resource", audit.getResourceOperatedUpon(),
53 "clientIpAddress", audit.getClientIpAddress(),
54 "serverIpAddress", audit.getServerIpAddress()));
55
56 AuditEntry auditEntry = new AuditEntry();
57 auditEntry.setWho(audit.getPrincipal());
58 auditEntry.setDate(
59 audit.getWhenActionWasPerformed().toInstant().atOffset(OffsetDateTime.now().getOffset()));
60 auditEntry.setOutput(output);
61 AuditElements.Result result = StringUtils.containsIgnoreCase(audit.getActionPerformed(), "fail")
62 ? AuditElements.Result.FAILURE
63 : AuditElements.Result.SUCCESS;
64
65 AuditLoggerName auditLogger = new AuditLoggerName(
66 AuditElements.EventCategoryType.WA,
67 null,
68 AuditElements.AUTHENTICATION_CATEGORY.toUpperCase(),
69 audit.getActionPerformed(),
70 result);
71 auditEntry.setLogger(auditLogger);
72 waRestClient.getService(AuditService.class).create(auditEntry);
73 } catch (JsonProcessingException e) {
74 LOG.error("During serialization", e);
75 }
76 }
77
78 @Override
79 public Set<? extends AuditActionContext> getAuditRecords(final Map<WhereClauseFields, Object> map) {
80 throw new UnsupportedOperationException("Fetching audit events from WA is not supported");
81 }
82
83 @Override
84 public void removeAll() {
85 throw new UnsupportedOperationException("Removing audit events from WA is not supported");
86 }
87 }