Add Docker Compose support for Neo4j

Closes gh-37379
This commit is contained in:
Andy Wilkinson 2023-09-14 13:23:59 +01:00
parent 4f6e50b55a
commit e3d884803e
9 changed files with 276 additions and 0 deletions

View File

@ -18,6 +18,7 @@ dependencies {
optional(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
optional("io.r2dbc:r2dbc-spi")
optional("org.mongodb:mongodb-driver-core")
optional("org.neo4j.driver:neo4j-java-driver")
optional("org.springframework.data:spring-data-r2dbc")
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))

View File

@ -0,0 +1,76 @@
/*
* 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.docker.compose.service.connection.neo4j;
import java.net.URI;
import org.neo4j.driver.AuthToken;
import org.springframework.boot.autoconfigure.neo4j.Neo4jConnectionDetails;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link Neo4jConnectionDetails}
* for a {@code Neo4j} service.
*
* @author Andy Wilkinson
*/
class Neo4jDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<Neo4jConnectionDetails> {
Neo4jDockerComposeConnectionDetailsFactory() {
super("neo4j");
}
@Override
protected Neo4jConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new Neo4jDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link Neo4jConnectionDetails} backed by a {@code Neo4j} {@link RunningService}.
*/
static class Neo4jDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements Neo4jConnectionDetails {
private static final int BOLT_PORT = 7687;
private final AuthToken authToken;
private final URI uri;
Neo4jDockerComposeConnectionDetails(RunningService service) {
super(service);
Neo4jEnvironment neo4jEnvironment = new Neo4jEnvironment(service.env());
this.authToken = neo4jEnvironment.getAuthToken();
this.uri = URI.create("neo4j://%s:%d".formatted(service.host(), service.ports().get(BOLT_PORT)));
}
@Override
public URI getUri() {
return this.uri;
}
@Override
public AuthToken getAuthToken() {
return this.authToken;
}
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.docker.compose.service.connection.neo4j;
import java.util.Map;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
/**
* Neo4j environment details.
*
* @author Andy Wilkinson
*/
class Neo4jEnvironment {
private final AuthToken authToken;
Neo4jEnvironment(Map<String, String> env) {
this.authToken = parse(env.get("NEO4J_AUTH"));
}
private AuthToken parse(String neo4jAuth) {
if (neo4jAuth == null) {
return null;
}
if ("none".equals(neo4jAuth)) {
return AuthTokens.none();
}
if (neo4jAuth.startsWith("neo4j/")) {
return AuthTokens.basic("neo4j", neo4jAuth.substring(6));
}
throw new IllegalStateException(
"Cannot extract auth token from NEO4J_AUTH environment variable with value '" + neo4jAuth + "'."
+ " Value should be 'none' to disable authentication or start with 'neo4j/' to specify"
+ " the neo4j user's password");
}
AuthToken getAuthToken() {
return this.authToken;
}
}

View File

@ -0,0 +1,20 @@
/*
* 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.
*/
/**
* Auto-configuration for docker compose Neo4j service connections.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;

View File

@ -15,6 +15,7 @@ org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcD
org.springframework.boot.docker.compose.service.connection.mongo.MongoDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mysql.MySqlJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mysql.MySqlR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.neo4j.Neo4jDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryMetricsDockerComposeConnectionDetailsFactory,\

View File

@ -0,0 +1,51 @@
/*
* 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.docker.compose.service.connection.neo4j;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.springframework.boot.autoconfigure.neo4j.Neo4jConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Integration tests for {@link Neo4jDockerComposeConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
class Neo4jDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests {
Neo4jDockerComposeConnectionDetailsFactoryIntegrationTests() {
super("neo4j-compose.yaml", DockerImageNames.neo4j());
}
@Test
void runCreatesConnectionDetailsThatCanAccessNeo4j() {
Neo4jConnectionDetails connectionDetails = run(Neo4jConnectionDetails.class);
assertThat(connectionDetails.getAuthToken()).isEqualTo(AuthTokens.basic("neo4j", "secret"));
try (Driver driver = GraphDatabase.driver(connectionDetails.getUri(), connectionDetails.getAuthToken())) {
assertThatNoException().isThrownBy(driver::verifyConnectivity);
}
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.docker.compose.service.connection.neo4j;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.AuthTokens;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link Neo4jEnvironment}.
*
* @author Andy Wilkinson
*/
class Neo4jEnvironmentTests {
@Test
void whenNeo4jAuthIsNullThenAuthTokenIsNull() {
Neo4jEnvironment environment = new Neo4jEnvironment(Collections.emptyMap());
assertThat(environment.getAuthToken()).isNull();
}
@Test
void whenNeo4jAuthIsNoneThenAuthTokenIsNone() {
Neo4jEnvironment environment = new Neo4jEnvironment(Map.of("NEO4J_AUTH", "none"));
assertThat(environment.getAuthToken()).isEqualTo(AuthTokens.none());
}
@Test
void whenNeo4jAuthIsNeo4jSlashPasswordThenAuthTokenIsBasic() {
Neo4jEnvironment environment = new Neo4jEnvironment(Map.of("NEO4J_AUTH", "neo4j/custom-password"));
assertThat(environment.getAuthToken()).isEqualTo(AuthTokens.basic("neo4j", "custom-password"));
}
@Test
void whenNeo4jAuthIsNeitherNoneNorNeo4jSlashPasswordEnvironmentCreationThrows() {
assertThatIllegalStateException()
.isThrownBy(() -> new Neo4jEnvironment(Map.of("NEO4J_AUTH", "graphdb/custom-password")));
}
}

View File

@ -0,0 +1,8 @@
services:
neo4j:
image: '{imageName}'
ports:
- '7687'
environment:
- 'NEO4J_AUTH=neo4j/secret'

View File

@ -76,6 +76,9 @@ The following service connections are currently supported:
| `MongoConnectionDetails`
| Containers named "mongo"
| `Neo4jConnectionDetails`
| Containers named "neo4j"
| `OtlpMetricsConnectionDetails`
| Containers named "otel/opentelemetry-collector-contrib"