View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }