-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoltKernelExtension.java
250 lines (223 loc) · 11.5 KB
/
BoltKernelExtension.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt;
import io.netty.handler.ssl.SslContext;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.time.Clock;
import java.util.Map;
import org.neo4j.bolt.logging.BoltMessageLogging;
import org.neo4j.bolt.runtime.BoltConnectionFactory;
import org.neo4j.bolt.runtime.BoltConnectionReadLimiter;
import org.neo4j.bolt.runtime.BoltSchedulerProvider;
import org.neo4j.bolt.runtime.CachedThreadPoolExecutorFactory;
import org.neo4j.bolt.runtime.DefaultBoltConnectionFactory;
import org.neo4j.bolt.runtime.ExecutorBoltSchedulerProvider;
import org.neo4j.bolt.security.auth.Authentication;
import org.neo4j.bolt.security.auth.BasicAuthentication;
import org.neo4j.bolt.transport.BoltProtocolPipelineInstallerFactory;
import org.neo4j.bolt.transport.DefaultBoltProtocolPipelineInstallerFactory;
import org.neo4j.bolt.transport.Netty4LoggerFactory;
import org.neo4j.bolt.transport.NettyServer;
import org.neo4j.bolt.transport.NettyServer.ProtocolInitializer;
import org.neo4j.bolt.transport.SocketTransport;
import org.neo4j.bolt.transport.TransportThrottleGroup;
import org.neo4j.bolt.v1.runtime.BoltFactory;
import org.neo4j.bolt.v1.runtime.BoltFactoryImpl;
import org.neo4j.configuration.Description;
import org.neo4j.configuration.LoadableConfig;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.ListenSocketAddress;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.api.bolt.BoltConnectionTracker;
import org.neo4j.kernel.api.security.AuthManager;
import org.neo4j.kernel.api.security.UserManagerSupplier;
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.ConnectorPortRegister;
import org.neo4j.kernel.configuration.ssl.SslPolicyLoader;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.Log;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.udc.UsageData;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.neo4j.kernel.configuration.Settings.STRING;
import static org.neo4j.kernel.configuration.Settings.setting;
import static org.neo4j.kernel.configuration.ssl.LegacySslPolicyConfig.LEGACY_POLICY_NAME;
import static org.neo4j.scheduler.JobScheduler.Groups.boltNetworkIO;
public class BoltKernelExtension extends LifecycleAdapter
{
// platform dependencies
private final GraphDatabaseAPI db;
private final FileSystemAbstraction fs;
private final JobScheduler jobScheduler;
private final AvailabilityGuard availabilityGuard;
private final ConnectorPortRegister connectorPortRegister;
private final UsageData usageData;
private final Config config;
private final Clock clock;
private final Monitors monitors;
private final LogService logService;
// edition specific dependencies are resolved dynamically
private final DependencyResolver dependencyResolver;
private final LifeSupport life = new LifeSupport();
public static class Settings implements LoadableConfig
{
@Description( "Specify the SSL policy to use" )
public static final Setting<String> ssl_policy = setting( "bolt.ssl_policy", STRING, LEGACY_POLICY_NAME );
}
public BoltKernelExtension( GraphDatabaseAPI db, FileSystemAbstraction fs, JobScheduler jobScheduler, AvailabilityGuard availabilityGuard,
ConnectorPortRegister connectorPortRegister, UsageData usageData, Config config, Clock clock, Monitors monitors,
LogService logService, DependencyResolver dependencyResolver )
{
this.db = db;
this.fs = fs;
this.jobScheduler = jobScheduler;
this.availabilityGuard = availabilityGuard;
this.connectorPortRegister = connectorPortRegister;
this.usageData = usageData;
this.config = config;
this.clock = clock;
this.monitors = monitors;
this.logService = logService;
this.dependencyResolver = dependencyResolver;
}
@Override
public void start() throws Throwable
{
Log log = logService.getInternalLog( BoltKernelExtension.class );
Log userLog = logService.getUserLog( BoltKernelExtension.class );
InternalLoggerFactory.setDefaultFactory( new Netty4LoggerFactory( logService.getInternalLogProvider() ) );
BoltMessageLogging boltLogging = BoltMessageLogging.create( fs, jobScheduler, config, log );
life.add( boltLogging );
Authentication authentication = createAuthentication();
TransportThrottleGroup throttleGroup = new TransportThrottleGroup( config, clock );
BoltFactory boltFactory = createBoltFactory( authentication );
BoltSchedulerProvider boltSchedulerProvider =
life.add( new ExecutorBoltSchedulerProvider( config, new CachedThreadPoolExecutorFactory( log ), jobScheduler, logService ) );
BoltConnectionFactory boltConnectionFactory =
createConnectionFactory( config, boltFactory, boltSchedulerProvider, throttleGroup, logService, clock );
BoltProtocolPipelineInstallerFactory handlerFactory = createHandlerFactory( boltConnectionFactory, throttleGroup );
if ( !config.enabledBoltConnectors().isEmpty() && !config.get( GraphDatabaseSettings.disconnected ) )
{
NettyServer server = new NettyServer( jobScheduler.threadFactory( boltNetworkIO ),
createConnectors( handlerFactory, throttleGroup, boltLogging, log ), connectorPortRegister, userLog );
life.add( server );
log.info( "Bolt server loaded" );
}
life.start(); // init and start the nested lifecycle
}
@Override
public void stop() throws Throwable
{
life.shutdown(); // stop and shutdown the nested lifecycle
}
private BoltConnectionFactory createConnectionFactory( Config config, BoltFactory boltFactory, BoltSchedulerProvider schedulerProvider,
TransportThrottleGroup throttleGroup, LogService logService, Clock clock )
{
return new DefaultBoltConnectionFactory( boltFactory, schedulerProvider, throttleGroup, logService, clock,
new BoltConnectionReadLimiter( logService.getInternalLog( BoltConnectionReadLimiter.class ),
config.get( GraphDatabaseSettings.bolt_inbound_message_throttle_low_water_mark ),
config.get( GraphDatabaseSettings.bolt_inbound_message_throttle_high_water_mark ) ), monitors );
}
private Map<BoltConnector,ProtocolInitializer> createConnectors( BoltProtocolPipelineInstallerFactory handlerFactory,
TransportThrottleGroup throttleGroup, BoltMessageLogging boltLogging, Log log )
{
return config.enabledBoltConnectors()
.stream()
.collect( toMap( identity(), connector -> createProtocolInitializer( connector, handlerFactory, throttleGroup, boltLogging, log ) ) );
}
private ProtocolInitializer createProtocolInitializer( BoltConnector connector, BoltProtocolPipelineInstallerFactory handlerFactory,
TransportThrottleGroup throttleGroup, BoltMessageLogging boltLogging, Log log )
{
SslContext sslCtx;
boolean requireEncryption;
BoltConnector.EncryptionLevel encryptionLevel = config.get( connector.encryption_level );
SslPolicyLoader sslPolicyLoader = dependencyResolver.resolveDependency( SslPolicyLoader.class );
switch ( encryptionLevel )
{
case REQUIRED:
// Encrypted connections are mandatory, a self-signed certificate may be generated.
requireEncryption = true;
sslCtx = createSslContext( sslPolicyLoader, config );
break;
case OPTIONAL:
// Encrypted connections are optional, a self-signed certificate may be generated.
requireEncryption = false;
sslCtx = createSslContext( sslPolicyLoader, config );
break;
case DISABLED:
// Encryption is turned off, no self-signed certificate will be generated.
requireEncryption = false;
sslCtx = null;
break;
default:
// In the unlikely event that we happen to fall through to the default option here,
// there is a mismatch between the BoltConnector.EncryptionLevel enum and the options
// handled in this switch statement. In this case, we'll log a warning and default to
// disabling encryption, since this mirrors the functionality introduced in 3.0.
log.warn( "Unhandled encryption level %s - assuming DISABLED.", encryptionLevel.name() );
requireEncryption = false;
sslCtx = null;
break;
}
ListenSocketAddress listenAddress = config.get( connector.listen_address );
return new SocketTransport( connector.key(), listenAddress, sslCtx, requireEncryption, logService.getInternalLogProvider(), boltLogging,
throttleGroup, handlerFactory );
}
private static SslContext createSslContext( SslPolicyLoader sslPolicyFactory, Config config )
{
try
{
String policyName = config.get( Settings.ssl_policy );
if ( policyName == null )
{
throw new IllegalArgumentException( "No SSL policy has been configured for Bolt server" );
}
return sslPolicyFactory.getPolicy( policyName ).nettyServerContext();
}
catch ( Exception e )
{
throw new RuntimeException( "Failed to initialize SSL encryption support, which is required to start this connector. " +
"Error was: " + e.getMessage(), e );
}
}
private Authentication createAuthentication()
{
return new BasicAuthentication( dependencyResolver.resolveDependency( AuthManager.class ),
dependencyResolver.resolveDependency( UserManagerSupplier.class ) );
}
private BoltProtocolPipelineInstallerFactory createHandlerFactory( BoltConnectionFactory connectionFactory, TransportThrottleGroup throttleGroup )
{
return new DefaultBoltProtocolPipelineInstallerFactory( connectionFactory, throttleGroup, logService );
}
private BoltFactory createBoltFactory( Authentication authentication )
{
BoltConnectionTracker connectionTracker = dependencyResolver.resolveDependency( BoltConnectionTracker.class );
return new BoltFactoryImpl( db, usageData, availabilityGuard, authentication, connectionTracker, config, logService );
}
}