-
Notifications
You must be signed in to change notification settings - Fork 3
/
AbstractBankIdAuditEventRepository.java
151 lines (130 loc) · 4.63 KB
/
AbstractBankIdAuditEventRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2023-2024 Sweden Connect
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.swedenconnect.bankid.idp.audit;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.util.StringUtils;
import se.swedenconnect.spring.saml.idp.audit.Saml2AuditEvents;
/**
* The base {@link AuditEventRepository} for the BankID IdP.
*
* @deprecated Use the Audit support from the SAML IdP project instead
*
* @author Martin Lindström
* @author Felix Hellman
*/
@Deprecated(forRemoval = true, since = "1.1.3")
public abstract class AbstractBankIdAuditEventRepository implements AuditEventRepository, DisposableBean {
/** Logger. */
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(AbstractBankIdAuditEventRepository.class);
/**
* The events that we support by default.
*/
public static List<String> DEFAULT_SUPPORTED_EVENTS = Stream.concat(
Arrays.stream(Saml2AuditEvents.values())
.map(Saml2AuditEvents::getTypeName)
.toList().stream(),
Arrays.stream(BankIdAuditEventTypes.values())
.map(BankIdAuditEventTypes::getTypeName)
.toList().stream())
.toList();
/** The supported events. */
private final List<String> supportedEvents;
/** The underlying JUL handler. */
private final DateRollingFileHandler handler;
/** The JUL logger. */
private final Logger auditLogger;
/** The mapper that writes JSON. */
private final AuditEventMapper mapper;
/**
* Constructor.
*
* @param logFile the log file including its path (if {@code null}, no file logging will be performed)
* @param mapper mapper for creating JSON
* @param supportedEvents the supported events (if {@code null}, {@link #DEFAULT_SUPPORTED_EVENTS} will be used)
* @throws IOException if file logging can not be initialized
*/
public AbstractBankIdAuditEventRepository(
final String logFile, final AuditEventMapper mapper, final List<String> supportedEvents)
throws IOException {
this.supportedEvents = Optional.ofNullable(supportedEvents).orElseGet(() -> DEFAULT_SUPPORTED_EVENTS);
this.mapper = Objects.requireNonNull(mapper, "mapper must not be null");
this.handler = StringUtils.hasText(logFile) ? new DateRollingFileHandler(logFile) : null;
if (this.handler != null) {
// Build the logger name based on the log file name ...
final String loggerName = Path.of(logFile).toAbsolutePath().toString();
this.auditLogger = Logger.getLogger(loggerName);
this.auditLogger.setLevel(Level.INFO);
this.auditLogger.addHandler(this.handler);
this.auditLogger.setUseParentHandlers(false);
}
else {
this.auditLogger = null;
}
}
/** {@inheritDoc} */
@Override
public final void add(final AuditEvent event) {
if (event == null) {
return;
}
if (this.supportedEvents.contains(event.getType())) {
log.info("Audit logging event '{}' for principal '{}' ...", event.getType(), event.getPrincipal());
if (this.auditLogger != null) {
try {
this.auditLogger.log(Level.INFO, this.mapper.write(event));
}
catch (final Throwable e) {
log.error("Failed to audit log to file - {}", e.getMessage(), e);
}
}
this.addEvent(event);
}
}
/**
* Logs an event.
*
* @param event the audit event to log
*/
protected abstract void addEvent(final AuditEvent event);
/** {@inheritDoc} */
@Override
public void destroy() throws Exception {
if (this.handler != null) {
this.handler.flush();
this.handler.close();
}
}
/**
* Returns the audit event mapper.
*
* @return the {@link AuditEventMapper}
*/
protected AuditEventMapper getAuditEventMapper() {
return this.mapper;
}
}