forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Neo4jAutoConfiguration.java
222 lines (200 loc) · 8.48 KB
/
Neo4jAutoConfiguration.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
/*
* Copyright 2012-2023 the original author or authors.
*
* 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
*
* https://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.springframework.boot.autoconfigure.neo4j;
import java.io.File;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokenManager;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Config.TrustStrategy;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.internal.Scheme;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Authentication;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Pool;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Security;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Neo4j.
*
* @author Michael J. Simons
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @since 2.4.0
*/
@AutoConfiguration
@ConditionalOnClass(Driver.class)
@EnableConfigurationProperties(Neo4jProperties.class)
public class Neo4jAutoConfiguration {
@Bean
@ConditionalOnMissingBean(Neo4jConnectionDetails.class)
PropertiesNeo4jConnectionDetails neo4jConnectionDetails(Neo4jProperties properties) {
return new PropertiesNeo4jConnectionDetails(properties);
}
@Bean
@ConditionalOnMissingBean
public Driver neo4jDriver(Neo4jProperties properties, Environment environment,
Neo4jConnectionDetails connectionDetails, ObjectProvider<ConfigBuilderCustomizer> configBuilderCustomizers,
ObjectProvider<AuthTokenManager> authTokenManagers) {
Config config = mapDriverConfig(properties, connectionDetails,
configBuilderCustomizers.orderedStream().toList());
AuthTokenManager authTokenManager = authTokenManagers.getIfUnique();
if (authTokenManager != null) {
return GraphDatabase.driver(connectionDetails.getUri(), authTokenManager, config);
}
AuthToken authToken = connectionDetails.getAuthToken();
return GraphDatabase.driver(connectionDetails.getUri(), authToken, config);
}
Config mapDriverConfig(Neo4jProperties properties, Neo4jConnectionDetails connectionDetails,
List<ConfigBuilderCustomizer> customizers) {
Config.ConfigBuilder builder = Config.builder();
configurePoolSettings(builder, properties.getPool());
URI uri = connectionDetails.getUri();
String scheme = (uri != null) ? uri.getScheme() : "bolt";
configureDriverSettings(builder, properties, isSimpleScheme(scheme));
builder.withLogging(new Neo4jSpringJclLogging());
customizers.forEach((customizer) -> customizer.customize(builder));
return builder.build();
}
private boolean isSimpleScheme(String scheme) {
String lowerCaseScheme = scheme.toLowerCase(Locale.ENGLISH);
try {
Scheme.validateScheme(lowerCaseScheme);
}
catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(String.format("'%s' is not a supported scheme.", scheme));
}
return lowerCaseScheme.equals("bolt") || lowerCaseScheme.equals("neo4j");
}
private void configurePoolSettings(Config.ConfigBuilder builder, Pool pool) {
if (pool.isLogLeakedSessions()) {
builder.withLeakedSessionsLogging();
}
builder.withMaxConnectionPoolSize(pool.getMaxConnectionPoolSize());
Duration idleTimeBeforeConnectionTest = pool.getIdleTimeBeforeConnectionTest();
if (idleTimeBeforeConnectionTest != null) {
builder.withConnectionLivenessCheckTimeout(idleTimeBeforeConnectionTest.toMillis(), TimeUnit.MILLISECONDS);
}
builder.withMaxConnectionLifetime(pool.getMaxConnectionLifetime().toMillis(), TimeUnit.MILLISECONDS);
builder.withConnectionAcquisitionTimeout(pool.getConnectionAcquisitionTimeout().toMillis(),
TimeUnit.MILLISECONDS);
if (pool.isMetricsEnabled()) {
builder.withDriverMetrics();
}
else {
builder.withoutDriverMetrics();
}
}
private void configureDriverSettings(Config.ConfigBuilder builder, Neo4jProperties properties,
boolean withEncryptionAndTrustSettings) {
if (withEncryptionAndTrustSettings) {
applyEncryptionAndTrustSettings(builder, properties.getSecurity());
}
builder.withConnectionTimeout(properties.getConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS);
builder.withMaxTransactionRetryTime(properties.getMaxTransactionRetryTime().toMillis(), TimeUnit.MILLISECONDS);
}
private void applyEncryptionAndTrustSettings(Config.ConfigBuilder builder,
Neo4jProperties.Security securityProperties) {
if (securityProperties.isEncrypted()) {
builder.withEncryption();
}
else {
builder.withoutEncryption();
}
builder.withTrustStrategy(mapTrustStrategy(securityProperties));
}
private Config.TrustStrategy mapTrustStrategy(Neo4jProperties.Security securityProperties) {
String propertyName = "spring.neo4j.security.trust-strategy";
Security.TrustStrategy strategy = securityProperties.getTrustStrategy();
TrustStrategy trustStrategy = createTrustStrategy(securityProperties, propertyName, strategy);
if (securityProperties.isHostnameVerificationEnabled()) {
trustStrategy.withHostnameVerification();
}
else {
trustStrategy.withoutHostnameVerification();
}
return trustStrategy;
}
private TrustStrategy createTrustStrategy(Neo4jProperties.Security securityProperties, String propertyName,
Security.TrustStrategy strategy) {
switch (strategy) {
case TRUST_ALL_CERTIFICATES:
return TrustStrategy.trustAllCertificates();
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
return TrustStrategy.trustSystemCertificates();
case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
File certFile = securityProperties.getCertFile();
if (certFile == null || !certFile.isFile()) {
throw new InvalidConfigurationPropertyValueException(propertyName, strategy.name(),
"Configured trust strategy requires a certificate file.");
}
return TrustStrategy.trustCustomCertificateSignedBy(certFile);
default:
throw new InvalidConfigurationPropertyValueException(propertyName, strategy.name(),
"Unknown strategy.");
}
}
/**
* Adapts {@link Neo4jProperties} to {@link Neo4jConnectionDetails}.
*/
static class PropertiesNeo4jConnectionDetails implements Neo4jConnectionDetails {
private final Neo4jProperties properties;
PropertiesNeo4jConnectionDetails(Neo4jProperties properties) {
this.properties = properties;
}
@Override
public URI getUri() {
URI uri = this.properties.getUri();
return (uri != null) ? uri : Neo4jConnectionDetails.super.getUri();
}
@Override
public AuthToken getAuthToken() {
Authentication authentication = this.properties.getAuthentication();
String username = authentication.getUsername();
String kerberosTicket = authentication.getKerberosTicket();
boolean hasUsername = StringUtils.hasText(username);
boolean hasKerberosTicket = StringUtils.hasText(kerberosTicket);
Assert.state(!(hasUsername && hasKerberosTicket),
() -> "Cannot specify both username ('%s') and kerberos ticket ('%s')".formatted(username,
kerberosTicket));
String password = authentication.getPassword();
if (hasUsername && StringUtils.hasText(password)) {
return AuthTokens.basic(username, password, authentication.getRealm());
}
if (hasKerberosTicket) {
return AuthTokens.kerberos(kerberosTicket);
}
return AuthTokens.none();
}
}
}