Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 3d8b7cc

Browse files
committed
Add ability to specify URI scheme
Necessary to allow shell to interact with a causal cluster
1 parent 5b8a824 commit 3d8b7cc

File tree

10 files changed

+69
-32
lines changed

10 files changed

+69
-32
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void setUp() throws Exception {
4444
commitCommand = new Commit(shell);
4545
beginCommand = new Begin(shell);
4646

47-
shell.connect(new ConnectionConfig("localhost", 7687, "neo4j", "neo", true));
47+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true));
4848
}
4949

5050
@After
@@ -84,7 +84,7 @@ public void connectTwiceThrows() throws CommandException {
8484
thrown.expect(CommandException.class);
8585
thrown.expectMessage("Already connected");
8686

87-
ConnectionConfig config = new ConnectionConfig("localhost", 7687, "neo4j", "neo", true);
87+
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true);
8888
assertTrue("Shell should already be connected", shell.isConnected());
8989
shell.connect(config);
9090
}

cypher-shell/src/main/java/org/neo4j/shell/ConnectionConfig.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
import javax.annotation.Nonnull;
66

77
public class ConnectionConfig {
8+
private final String scheme;
89
private final String host;
910
private final int port;
1011
private final Config.EncryptionLevel encryption;
1112
private String username;
1213
private String password;
1314

14-
public ConnectionConfig(@Nonnull String host, int port, @Nonnull String username, @Nonnull String password,
15-
boolean encryption) {
15+
public ConnectionConfig(@Nonnull String scheme, @Nonnull String host, int port, @Nonnull String username,
16+
@Nonnull String password, boolean encryption) {
17+
this.scheme = scheme;
1618
this.host = host;
1719
this.port = port;
1820
this.username = fallbackToEnvVariable(username, "NEO4J_USERNAME");
@@ -32,6 +34,11 @@ static String fallbackToEnvVariable(@Nonnull String preferredValue, @Nonnull Str
3234
return result;
3335
}
3436

37+
@Nonnull
38+
public String scheme() {
39+
return scheme;
40+
}
41+
3542
@Nonnull
3643
public String host() {
3744
return host;
@@ -53,7 +60,7 @@ public String password() {
5360

5461
@Nonnull
5562
public String driverUrl() {
56-
return String.format("bolt://%s:%d", host(), port());
63+
return String.format("%s%s:%d", scheme(), host(), port());
5764
}
5865

5966
@Nonnull

cypher-shell/src/main/java/org/neo4j/shell/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ void startShell(@Nonnull CliArgs cliArgs) {
8181
return;
8282
}
8383

84-
ConnectionConfig connectionConfig = new ConnectionConfig(cliArgs.getHost(),
84+
ConnectionConfig connectionConfig = new ConnectionConfig(
85+
cliArgs.getScheme(),
86+
cliArgs.getHost(),
8587
cliArgs.getPort(),
8688
cliArgs.getUsername(),
8789
cliArgs.getPassword(),

cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class CliArgHelper {
2727

2828
static final Pattern ADDRESS_ARG_PATTERN =
29-
Pattern.compile("\\s*(?<protocol>[a-zA-Z]+://)?((?<username>\\w+):(?<password>[^\\s]+)@)?(?<host>[a-zA-Z\\d\\-\\.]+)?(:(?<port>\\d+))?\\s*");
29+
Pattern.compile("\\s*(?<scheme>[a-zA-Z0-9+\\-.]+://)?((?<username>\\w+):(?<password>[^\\s]+)@)?(?<host>[a-zA-Z\\d\\-.]+)?(:(?<port>\\d+))?\\s*");
3030

3131
/**
3232
* @param args to parse
@@ -53,6 +53,7 @@ public static CliArgs parse(@Nonnull String... args) {
5353

5454
CliArgs cliArgs = new CliArgs();
5555

56+
cliArgs.setScheme(addressMatcher.group("scheme"), "bolt://");
5657
cliArgs.setHost(addressMatcher.group("host"), "localhost");
5758
// Safe, regex only matches integers
5859
String portString = addressMatcher.group("port");
@@ -99,7 +100,7 @@ private static Matcher parseAddressMatcher(ArgumentParser parser, String address
99100
PrintWriter printWriter = new PrintWriter(System.err);
100101
parser.printUsage(printWriter);
101102
printWriter.println("cypher-shell: error: Failed to parse address: '" + address + "'");
102-
printWriter.println("\n Address should be of the form: [username:password@][host][:port]");
103+
printWriter.println("\n Address should be of the form: [scheme://][username:password@][host][:port]");
103104
printWriter.flush();
104105
return null;
105106
}
@@ -114,7 +115,7 @@ private static ArgumentParser setupParser() {
114115
ArgumentGroup connGroup = parser.addArgumentGroup("connection arguments");
115116
connGroup.addArgument("-a", "--address")
116117
.help("address and port to connect to")
117-
.setDefault("localhost:7687");
118+
.setDefault("bolt://localhost:7687");
118119
connGroup.addArgument("-u", "--username")
119120
.setDefault("")
120121
.help("username to connect as. Can also be specified using environment variable NEO4J_USERNAME");

cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgs.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Optional;
66

77
public class CliArgs {
8+
private String scheme = "bolt://";
89
private String host = "localhost";
910
private int port = 7687;
1011
private String username = "";
@@ -17,6 +18,13 @@ public class CliArgs {
1718
private boolean nonInteractive = false;
1819
private boolean version = false;
1920

21+
/**
22+
* Set the scheme to the primary value, or if null, the fallback value.
23+
*/
24+
void setScheme(@Nullable String primary, @Nonnull String fallback) {
25+
scheme = primary == null ? fallback : primary;
26+
}
27+
2028
/**
2129
* Set the host to the primary value, or if null, the fallback value.
2230
*/
@@ -87,6 +95,11 @@ void setDebugMode(boolean enabled) {
8795
this.debugMode = enabled;
8896
}
8997

98+
@Nonnull
99+
public String getScheme() {
100+
return scheme;
101+
}
102+
90103
@Nonnull
91104
public String getHost() {
92105
return host;

cypher-shell/src/test/java/org/neo4j/shell/ConnectionConfigTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import static org.junit.Assert.assertEquals;
77

88
public class ConnectionConfigTest {
9-
ConnectionConfig config = new ConnectionConfig("localhost", 1, "bob", "pass", false);
9+
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 1, "bob", "pass", false);
10+
11+
@Test
12+
public void scheme() throws Exception {
13+
assertEquals("bolt://", config.scheme());
14+
}
1015

1116
@Test
1217
public void host() throws Exception {
@@ -29,13 +34,22 @@ public void password() throws Exception {
2934
}
3035

3136
@Test
32-
public void driverUrl() throws Exception {
37+
public void driverUrlDefaultScheme() throws Exception {
3338
assertEquals("bolt://localhost:1", config.driverUrl());
3439
}
3540

41+
@Test
42+
public void driverUrlExplicitScheme() throws Exception {
43+
ConnectionConfig config = new ConnectionConfig("bolt+routing://", "localhost", 1, "bob",
44+
"pass", false);
45+
assertEquals("bolt+routing://localhost:1", config.driverUrl());
46+
}
47+
3648
@Test
3749
public void encryption() {
38-
assertEquals(Config.EncryptionLevel.REQUIRED, new ConnectionConfig("", -1, "", "", true).encryption());
39-
assertEquals(Config.EncryptionLevel.NONE, new ConnectionConfig("", -1, "", "", false).encryption());
50+
assertEquals(Config.EncryptionLevel.REQUIRED,
51+
new ConnectionConfig("bolt://", "", -1, "", "", true).encryption());
52+
assertEquals(Config.EncryptionLevel.NONE,
53+
new ConnectionConfig("bolt://", "", -1, "", "", false).encryption());
4054
}
4155
}

cypher-shell/src/test/java/org/neo4j/shell/CypherShellTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setup() {
5858

5959
@Test
6060
public void verifyDelegationOfConnectionMethods() throws CommandException {
61-
ConnectionConfig cc = new ConnectionConfig("", 1, "", "", false);
61+
ConnectionConfig cc = new ConnectionConfig("bolt://", "", 1, "", "", false);
6262
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
6363

6464
shell.connect(cc);

cypher-shell/src/test/java/org/neo4j/shell/cli/AddressArgPatternTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616
@RunWith(Parameterized.class)
1717
public class AddressArgPatternTest {
1818

19-
private final String protocol;
19+
private final String scheme;
2020
private final String host;
2121
private final String port;
2222
private final String username;
2323
private final String password;
2424
private final String connString;
2525

26-
public AddressArgPatternTest(String protocol, String host, String port,
26+
public AddressArgPatternTest(String scheme, String host, String port,
2727
String username, String password) {
2828

29-
this.protocol = protocol;
29+
this.scheme = scheme;
3030
this.host = host;
3131
this.port = port;
3232
this.username = username;
3333
this.password = password;
3434

35-
StringBuilder connString = new StringBuilder().append(protocol);
35+
StringBuilder connString = new StringBuilder().append(scheme);
3636
// Only expect username/pass in case host is present
3737
if (!host.isEmpty() && !username.isEmpty() && !password.isEmpty()) {
3838
connString.append(username).append(":").append(password).append("@");
@@ -49,12 +49,12 @@ public AddressArgPatternTest(String protocol, String host, String port,
4949
@Parameterized.Parameters
5050
public static Collection<Object[]> parameters() {
5151
Collection<Object[]> data = new ArrayList<>();
52-
for (final String protocol : getProtocols()) {
52+
for (final String scheme : getSchemes()) {
5353
for (final String username : getUsernames()) {
5454
for (final String password : getPasswords()) {
5555
for (final String host : getHosts()) {
5656
for (final String port : getPorts()) {
57-
data.add(new String[]{protocol, host, port, username, password});
57+
data.add(new String[]{scheme, host, port, username, password});
5858
}
5959
}
6060
}
@@ -79,12 +79,12 @@ private static String[] getUsernames() {
7979
return new String[]{"", "bob1"};
8080
}
8181

82-
private static String[] getProtocols() {
83-
return new String[]{"", "bolt://"};
82+
private static String[] getSchemes() {
83+
return new String[]{"", "bolt://", "bolt+routing://", "w31rd.-+://"};
8484
}
8585

8686
@Test
87-
public void testUserPassProtocolHostPort() {
87+
public void testUserPassschemeHostPort() {
8888
Matcher m = CliArgHelper.ADDRESS_ARG_PATTERN.matcher(" " + connString + " ");
8989
assertTrue("Expected a match: " + connString, m.matches());
9090
if (host.isEmpty()) {
@@ -104,10 +104,10 @@ public void testUserPassProtocolHostPort() {
104104
assertEquals("Mismatched username", username, m.group("username"));
105105
assertEquals("Mismatched password", password, m.group("password"));
106106
}
107-
if (protocol.isEmpty()) {
108-
assertNull("Protocol should have been null", m.group("protocol"));
107+
if (scheme.isEmpty()) {
108+
assertNull("scheme should have been null", m.group("scheme"));
109109
} else {
110-
assertEquals("Mismatched protocol", protocol, m.group("protocol"));
110+
assertEquals("Mismatched scheme", scheme, m.group("scheme"));
111111
}
112112
}
113113
}

cypher-shell/src/test/java/org/neo4j/shell/cli/CliArgHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void parseUserName() throws Exception {
103103

104104
@Test
105105
public void parseFullAddress() throws Exception {
106-
CliArgs cliArgs = CliArgHelper.parse("--address", "alice:foo@bar:69");
106+
CliArgs cliArgs = CliArgHelper.parse("--address", "bolt+routing://alice:foo@bar:69");
107107
assertNotNull(cliArgs);
108108
assertEquals("alice", cliArgs.getUsername());
109109
assertEquals("foo", cliArgs.getPassword());

cypher-shell/src/test/java/org/neo4j/shell/state/BoltStateHandlerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public String server() {
6464
}
6565
};
6666
BoltStateHandler handler = new BoltStateHandler(provider);
67-
ConnectionConfig config = new ConnectionConfig("", -1, "", "", false);
67+
ConnectionConfig config = new ConnectionConfig( "bolt://", "", -1, "", "", false);
6868
handler.connect(config);
6969

7070
assertEquals("", handler.getServerVersion());
@@ -90,7 +90,7 @@ public String server() {
9090
}
9191
};
9292
BoltStateHandler handler = new BoltStateHandler(provider);
93-
ConnectionConfig config = new ConnectionConfig("", -1, "", "", false);
93+
ConnectionConfig config = new ConnectionConfig("bolt://", "", -1, "", "", false);
9494
handler.connect(config);
9595

9696
assertEquals("9.4.1-ALPHA", handler.getServerVersion());
@@ -270,7 +270,7 @@ public void silentDisconnectCleansUp() throws Exception {
270270
public void turnOffEncryptionIfRequested() throws CommandException {
271271
RecordingDriverProvider provider = new RecordingDriverProvider();
272272
BoltStateHandler handler = new BoltStateHandler(provider);
273-
ConnectionConfig config = new ConnectionConfig("", -1, "", "", false);
273+
ConnectionConfig config = new ConnectionConfig("bolt://", "", -1, "", "", false);
274274
handler.connect(config);
275275
assertEquals(Config.EncryptionLevel.NONE, provider.config.encryptionLevel());
276276
}
@@ -279,7 +279,7 @@ public void turnOffEncryptionIfRequested() throws CommandException {
279279
public void turnOnEncryptionIfRequested() throws CommandException {
280280
RecordingDriverProvider provider = new RecordingDriverProvider();
281281
BoltStateHandler handler = new BoltStateHandler(provider);
282-
ConnectionConfig config = new ConnectionConfig("", -1, "", "", true);
282+
ConnectionConfig config = new ConnectionConfig("bolt://", "", -1, "", "", true);
283283
handler.connect(config);
284284
assertEquals(Config.EncryptionLevel.REQUIRED, provider.config.encryptionLevel());
285285
}
@@ -297,7 +297,7 @@ public Transaction getCurrentTransaction() {
297297
}
298298

299299
public void connect() throws CommandException {
300-
connect(new ConnectionConfig("", 1, "", "", false));
300+
connect(new ConnectionConfig("bolt://", "", 1, "", "", false));
301301
}
302302
}
303303

0 commit comments

Comments
 (0)