feat(neptune): support neo4j backend for openCypher via NEPTUNE_DB_TYPE (#1449)

* feat(neptune): support neo4j backend for openCypher via NEPTUNE_DB_TYPE

Neptune emulation always started a TinkerPop Gremlin Server, which only
speaks Gremlin. Add a global db-type selector (FLOCI_SERVICES_NEPTUNE_DB_TYPE,
mirroring LocalStack's NEPTUNE_DB_TYPE) so clusters can be backed by Neo4j and
served over the Bolt protocol for openCypher.

- Add NeptuneDbType enum (gremlin -> 8182, neo4j -> 7687) with alias parsing
- Add db-type and default-neo4j-image config (default neo4j:5-community)
- Resolve image/port per engine in NeptuneService and NeptuneContainerManager
- Run Neo4j with NEO4J_AUTH=none and probe readiness via the Bolt handshake
- Add openCypher/Bolt integration test and NeptuneDbType unit test
- Document the new option in README and docs/services/neptune.md

* test(neptune): assert DeleteDBCluster cleanup and clarify docs

Assert the openCypher integration test's DeleteDBCluster cleanup returns
200 so a failed delete surfaces instead of silently leaking a backend
container and proxy port into later tests.

Also drop the misleading backend ports from the db-type table (users
connect via the proxy port range, not the container-native port) and
correct the SigV4 auth-header comment to reference the management
request routing.

* fix(neptune): treat all-zero Bolt handshake reply as not-ready

The Neo4j readiness probe accepted any non-empty response, but Neo4j
answers the Bolt handshake with 0x00000000 when it rejects every
proposed version (Bolt disabled, wrong port, or an unsupported server
version). That made the probe report ready for a backend whose sessions
would then fail. Require a non-zero agreed version for the neo4j
db-type so such a backend times out with a clear error instead.

* fix(neptune): fall back to default db-type on unknown config value

A server-side typo in FLOCI_SERVICES_NEPTUNE_DB_TYPE shouldn't fail the
client's CreateDBCluster. Make NeptuneDbType.fromConfig return an
Optional and have NeptuneService fall back to GREMLIN with a WARN for an
unrecognised value, instead of throwing InvalidParameterValue. The
cluster still comes up on the default engine.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-06-20 11:24:00 -05:00
1 parent 5aee7b6312
commit 760d48b91a
10 files changed
+343 -32

No files matched your search

@@ -0,0 +1,95 @@
package io.github.hectorvent.floci.services.neptune;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Demonstrates that a Neptune cluster configured with {@code db-type=neo4j} speaks the
* openCypher query language over the Bolt protocol.
*
* <p>The default Gremlin backend ({@code tinkerpop/gremlin-server}) does NOT understand
* openCypher/Bolt, so before {@code db-type} support existed this test could not pass —
* the proxied port served a Gremlin WebSocket endpoint, not a Bolt endpoint.
*/
@QuarkusTest
@TestProfile(NeptuneOpenCypherIntegrationTest.Neo4jDbTypeProfile.class)
class NeptuneOpenCypherIntegrationTest {
private static final String FORM = "application/x-www-form-urlencoded";
private static final String CLUSTER_ID = "opencypher-cluster";
// Fake SigV4 header whose credential scope service is "neptune" routes the management
// request (CreateDBCluster/DeleteDBCluster) to the Neptune service.
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260516/us-east-1/neptune/aws4_request, " +
"SignedHeaders=content-type;host, Signature=test";
private static final Pattern PORT = Pattern.compile("<Port>(\\d+)</Port>");
@Test
void openCypherQueryWorksOverBolt() {
String createResponse = given()
.header("Authorization", AUTH)
.contentType(FORM)
.formParam("Action", "CreateDBCluster")
.formParam("DBClusterIdentifier", CLUSTER_ID)
.formParam("Engine", "neptune")
.when().post("/")
.then()
.statusCode(200)
.extract().asString();
int boltPort = parsePort(createResponse);
try (Driver driver = GraphDatabase.driver(
"bolt://localhost:" + boltPort, AuthTokens.none())) {
driver.verifyConnectivity();
try (Session session = driver.session()) {
long one = session.run("RETURN 1 AS n").single().get("n").asLong();
assertEquals(1L, one, "openCypher RETURN should evaluate on the neo4j backend");
session.run("CREATE (:Person {name: 'Alice'})").consume();
long count = session.run("MATCH (p:Person) RETURN count(p) AS c")
.single().get("c").asLong();
assertEquals(1L, count, "openCypher CREATE + MATCH should round-trip");
}
} finally {
given()
.header("Authorization", AUTH)
.contentType(FORM)
.formParam("Action", "DeleteDBCluster")
.formParam("DBClusterIdentifier", CLUSTER_ID)
.when().post("/")
.then()
.statusCode(200);
}
}
private static int parsePort(String xml) {
Matcher m = PORT.matcher(xml);
if (!m.find()) {
throw new AssertionError("No <Port> in CreateDBCluster response: " + xml);
}
return Integer.parseInt(m.group(1));
}
public static final class Neo4jDbTypeProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.neptune.db-type", "neo4j");
}
}
}
@@ -0,0 +1,43 @@
package io.github.hectorvent.floci.services.neptune.model;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
class NeptuneDbTypeTest {
@Test
void defaultsToGremlinWhenUnset() {
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig(null));
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig(""));
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig(" "));
}
@Test
void parsesGremlinAliases() {
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig("gremlin"));
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig("TinkerPop"));
assertEquals(Optional.of(NeptuneDbType.GREMLIN), NeptuneDbType.fromConfig(" GREMLIN "));
}
@Test
void parsesNeo4jAliases() {
assertEquals(Optional.of(NeptuneDbType.NEO4J), NeptuneDbType.fromConfig("neo4j"));
assertEquals(Optional.of(NeptuneDbType.NEO4J), NeptuneDbType.fromConfig("openCypher"));
assertEquals(Optional.of(NeptuneDbType.NEO4J), NeptuneDbType.fromConfig("cypher"));
assertEquals(Optional.of(NeptuneDbType.NEO4J), NeptuneDbType.fromConfig("bolt"));
}
@Test
void backendPortsMatchProtocols() {
assertEquals(8182, NeptuneDbType.GREMLIN.backendPort());
assertEquals(7687, NeptuneDbType.NEO4J.backendPort());
}
@Test
void rejectsUnknownDbType() {
assertEquals(Optional.empty(), NeptuneDbType.fromConfig("sparql"));
}
}