/
FileAuditEndpoint.java
298 lines (258 loc) · 9.75 KB
/
FileAuditEndpoint.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.wildfly.security.audit;
import static org.wildfly.common.Assert.checkNotNullParam;
import static org.wildfly.security.audit.ElytronMessages.audit;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.function.Supplier;
/**
* An audit endpoint to record all audit events to a local file.
*
* @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
*/
public class FileAuditEndpoint implements AuditEndpoint {
private static final String LINE_TERMINATOR = System.lineSeparator();
private volatile boolean accepting = true;
private final Supplier<DateTimeFormatter> dateTimeFormatterSupplier;
private final boolean syncOnAccept;
private final boolean flushOnAccept;
private File file;
private FileDescriptor fileDescriptor;
private Writer writer;
private Charset charset;
/** Clock providing access to current time. */
protected final Clock clock;
FileAuditEndpoint(Builder builder) throws IOException {
this.dateTimeFormatterSupplier = builder.dateTimeFormatterSupplier;
this.syncOnAccept = builder.syncOnAccept;
this.flushOnAccept = builder.flushOnAccept;
this.clock = builder.clock;
this.charset = builder.charset != null ? builder.charset : StandardCharsets.UTF_8;
setFile(builder.location.toFile());
}
void setFile(final File file) throws IOException {
boolean isFileSet = false;
final FileOutputStream fos = new FileOutputStream(file, true);
try {
final Writer writer = new OutputStreamWriter(new BufferedOutputStream(fos), this.charset);
try {
this.fileDescriptor = fos.getFD();
this.writer = writer;
this.file = file;
isFileSet = true;
} finally {
if (! isFileSet) {
safeClose(writer);
}
}
} finally {
if (! isFileSet) {
safeClose(fos);
}
}
}
File getFile() {
return file;
}
private void safeClose(Closeable c) {
try {
if (c != null) c.close();
} catch (Exception e) {
audit.trace("Unable to close", e);
}
}
/**
* Method called to write given String to the target local file.
* This method can be overridden by subclasses to modify data written into file (to encrypt them for example),
* or just for counting amount of written bytes for needs of log rotation and similar.
*
* This method can be invoked only in synchronization block surrounding one log message processing.
*
* @param toWrite the String to be written into the target local file
*/
void write(String toWrite) throws IOException {
writer.write(toWrite);
}
/**
* Method called before writing into local file.
* This method is NO-OP by default. It is intended to be overridden by subclasses
* which need to perform some operation before every writing into the target local file.
*
* This method can be invoked only in synchronization block surrounding one log message processing.
*
* @param instant time of the message acceptance
*/
void preWrite(Instant instant) {
// NO-OP by default
}
/**
* Accept formatted security event message to be processed written into target local file.
*
* @param priority priority of the logged message
* @param message the logged message
* @throws IOException when writing into the target local file fails
*/
@Override
public void accept(EventPriority priority, String message) throws IOException {
if (!accepting) return;
Instant instant = clock.instant();
StringBuffer buffer = new StringBuffer();
buffer.append(dateTimeFormatterSupplier.get().format(instant));
buffer.append(',');
buffer.append(priority.toString());
buffer.append(',');
buffer.append(message);
buffer.append(LINE_TERMINATOR);
String toWrite = buffer.toString();
synchronized(this) {
if (!accepting) return; // We may have been waiting to get in here.
preWrite(instant);
write(toWrite);
if (flushOnAccept) writer.flush();
if (syncOnAccept) fileDescriptor.sync();
}
}
@Override
public void close() throws IOException {
accepting = false;
synchronized (this) {
closeStreams();
}
}
/**
* Close opened file streams. Can be called by subclasses for needs of target file changing.
* Must be called in synchronized block together with reopening using {@code setFile()}.
*/
void closeStreams() throws IOException {
writer.flush();
fileDescriptor.sync();
writer.close();
}
/**
* Obtain a new {@link Builder} capable of building a {@link FileAuditEndpoint}.
*
* @return a new {@link Builder} capable of building a {@link FileAuditEndpoint}.
*/
public static Builder builder() {
return new Builder();
}
/**
* A builder for file audit endpoints.
*/
public static class Builder {
private Clock clock = Clock.systemUTC();
private Supplier<DateTimeFormatter> dateTimeFormatterSupplier = () -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
private Path location = new File("audit.log").toPath();
private boolean syncOnAccept = true;
private boolean flushOnAccept = true;
private boolean flushSet = false;
private Charset charset;
Builder() {
}
/**
* Set the supplier to obtain the {@link DateTimeFormatter} for dates.
* The supplied formatter has to have a time zone configured.
*
* @param dateTimeFormatterSupplier the supplier to obtain the {@link DateTimeFormatter}
* @return this builder.
*/
public Builder setDateTimeFormatterSupplier(Supplier<DateTimeFormatter> dateTimeFormatterSupplier) {
this.dateTimeFormatterSupplier = checkNotNullParam("dateTimeFormatterSupplier", dateTimeFormatterSupplier);
return this;
}
/**
* Set the location to write the audit events to.
*
* @param location the location to write the audit events to.
* @return this builder.
*/
public Builder setLocation(Path location) {
this.location = checkNotNullParam("location", location);
return this;
}
/**
* Sets if the output should be flushed on each event accepted.
* If not set, flushing is done when output buffers synchronization is set.
*
* @param flushOnAccept should the output be flushed on each event accepted.
* @return this builder.
* @since 1.3.0
*/
public Builder setFlushOnAccept(boolean flushOnAccept) {
this.flushOnAccept = flushOnAccept;
this.flushSet = true;
return this;
}
/**
* Sets if the system output buffers should be forced to be synchronized on each event accepted. Enabled by default.
* Output flushing can be set independently using {@link #setFlushOnAccept(boolean)} but defaults to this value.
*
* @param syncOnAccept should the system output buffers be forced to be synchronized on each event accepted.
* @return this builder.
*/
public Builder setSyncOnAccept(boolean syncOnAccept) {
this.syncOnAccept = syncOnAccept;
if (! flushSet) this.flushOnAccept = syncOnAccept;
return this;
}
/**
* Sets the {@link Clock} instance the resulting {@link FileAuditEndpoint} should use to query the current time.
* For testing purposes only, therefore package visible.
*
* @param clock the clock to query the current time
* @return this builder
*/
Builder setClock(Clock clock) {
this.clock = clock;
return this;
}
/**
* Set the file's character set.
*
* @param charset the character set
* @return this builder.
*/
public Builder setCharset(Charset charset) {
this.charset = charset;
return this;
}
/**
* Construct a new file audit endpoint.
*
* @return the built file audit endpoint.
*/
public AuditEndpoint build() throws IOException {
return new FileAuditEndpoint(this);
}
}
}