-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SecurityLog.java
237 lines (201 loc) · 6.37 KB
/
SecurityLog.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.server.security.enterprise.log;
import java.io.File;
import java.io.IOException;
import java.time.ZoneId;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.internal.kernel.api.security.AuthSubject;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.logging.FormattedLog;
import org.neo4j.logging.Log;
import org.neo4j.logging.Logger;
import org.neo4j.logging.RotatingFileOutputStreamSupplier;
import org.neo4j.scheduler.Group;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.server.security.enterprise.configuration.SecuritySettings;
import static org.neo4j.helpers.Strings.escape;
public class SecurityLog extends LifecycleAdapter implements Log
{
private RotatingFileOutputStreamSupplier rotatingSupplier;
private final Log inner;
public SecurityLog( Config config, FileSystemAbstraction fileSystem, Executor executor ) throws IOException
{
ZoneId logTimeZoneId = config.get( GraphDatabaseSettings.db_timezone ).getZoneId();
File logFile = config.get( SecuritySettings.security_log_filename );
FormattedLog.Builder builder = FormattedLog.withZoneId( logTimeZoneId );
rotatingSupplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile,
config.get( SecuritySettings.store_security_log_rotation_threshold ),
config.get( SecuritySettings.store_security_log_rotation_delay ).toMillis(),
config.get( SecuritySettings.store_security_log_max_archives ), executor );
FormattedLog formattedLog = builder.toOutputStream( rotatingSupplier );
formattedLog.setLevel( config.get( SecuritySettings.security_log_level ) );
this.inner = formattedLog;
}
/* Only used for tests */
public SecurityLog( Log log )
{
inner = log;
}
private static String withSubject( AuthSubject subject, String msg )
{
return "[" + escape( subject.username() ) + "]: " + msg;
}
@Override
public boolean isDebugEnabled()
{
return inner.isDebugEnabled();
}
@Override
public Logger debugLogger()
{
return inner.debugLogger();
}
@Override
public void debug( String message )
{
inner.debug( message );
}
@Override
public void debug( String message, Throwable throwable )
{
inner.debug( message, throwable );
}
@Override
public void debug( String format, Object... arguments )
{
inner.debug( format, arguments );
}
public void debug( AuthSubject subject, String format, Object... arguments )
{
inner.debug( withSubject( subject, format ), arguments );
}
@Override
public Logger infoLogger()
{
return inner.infoLogger();
}
@Override
public void info( String message )
{
inner.info( message );
}
@Override
public void info( String message, Throwable throwable )
{
inner.info( message, throwable );
}
@Override
public void info( String format, Object... arguments )
{
inner.info( format, arguments );
}
public void info( AuthSubject subject, String format, Object... arguments )
{
inner.info( withSubject( subject, format ), arguments );
}
public void info( AuthSubject subject, String format )
{
inner.info( withSubject( subject, format ) );
}
@Override
public Logger warnLogger()
{
return inner.warnLogger();
}
@Override
public void warn( String message )
{
inner.warn( message );
}
@Override
public void warn( String message, Throwable throwable )
{
inner.warn( message, throwable );
}
@Override
public void warn( String format, Object... arguments )
{
inner.warn( format, arguments );
}
public void warn( AuthSubject subject, String format, Object... arguments )
{
inner.warn( withSubject( subject, format ), arguments );
}
@Override
public Logger errorLogger()
{
return inner.errorLogger();
}
@Override
public void error( String message )
{
inner.error( message );
}
@Override
public void error( String message, Throwable throwable )
{
inner.error( message, throwable );
}
@Override
public void error( String format, Object... arguments )
{
inner.error( format, arguments );
}
public void error( AuthSubject subject, String format, Object... arguments )
{
inner.error( withSubject( subject, format ), arguments );
}
@Override
public void bulk( Consumer<Log> consumer )
{
inner.bulk( consumer );
}
public static SecurityLog create( Config config, Log log, FileSystemAbstraction fileSystem,
JobScheduler jobScheduler )
{
try
{
return new SecurityLog( config, fileSystem,
jobScheduler.executor( Group.LOG_ROTATION ) );
}
catch ( IOException ioe )
{
log.warn( "Unable to create log for auth-manager. Auth logging turned off." );
return null;
}
}
@Override
public void shutdown() throws Throwable
{
if ( this.rotatingSupplier != null )
{
this.rotatingSupplier.close();
this.rotatingSupplier = null;
}
}
}