Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void wrongPortWithBolt() throws Exception
{
// given
CliArgs cliArgs = new CliArgs();
cliArgs.setScheme( "bolt://", "" );
cliArgs.setScheme( "bolt", "" );
cliArgs.setPort( 1234 );

ShellAndConnection sac = getShell( cliArgs );
Expand All @@ -290,7 +290,7 @@ public void wrongPortWithNeo4j() throws Exception
{
// given
CliArgs cliArgs = new CliArgs();
cliArgs.setScheme( "neo4j://", "" );
cliArgs.setScheme( "neo4j", "" );
cliArgs.setPort( 1234 );

ShellAndConnection sac = getShell( cliArgs );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ abstract class CypherShellIntegrationTest
CypherShell shell;

void connect(String password) throws CommandException {
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", password, Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", password, Encryption.DEFAULT, ABSENT_DB_NAME ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setUp() throws Exception
beginCommand = new Begin( shell );
rollbackCommand = new Rollback( shell );

shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );

// Multiple databases are only available from 4.0
assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 );
Expand Down Expand Up @@ -138,7 +138,7 @@ public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServerInter
{
shell = new CypherShell( linePrinter, new PrettyConfig( Format.PLAIN, true, 1000 ), true, new ShellParameterMap() );
useCommand = new Use( shell );
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );

useCommand.execute( SYSTEM_DB_NAME );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class CypherShellProtocolIntegrationTest{
@Test
public void shouldConnectWithBoltProtocol() throws Exception {
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
assertTrue(shell.isConnected());
}

@Test
public void shouldConnectWithNeo4jProtocol() throws Exception {
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
// This should work even on older databases without the neo4j protocol, by falling back to bolt
shell.connect( new ConnectionConfig( "neo4j://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "neo4j", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
assertTrue(shell.isConnected());
}

Expand All @@ -38,7 +38,7 @@ public void shouldConnectWithBoltSSCProtocol() throws Exception {
CypherShell shell = new CypherShell( new StringLinePrinter(), new PrettyConfig( Format.PLAIN, true, 1000), false, new ShellParameterMap());
// Given 3.X series where X > 1, where SSC are the default. Hard to test in 4.0 sadly.
onlyIn3_2to3_6( shell);
shell.connect( new ConnectionConfig( "bolt+ssc://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt+ssc", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
assertTrue(shell.isConnected());
}

Expand All @@ -48,15 +48,15 @@ public void shouldConnectWithNeo4jSSCProtocol() throws Exception {
// Given 3.X series where X > 1, where SSC are the default. Hard to test in 4.0 sadly.
onlyIn3_2to3_6( shell);
// This should work by falling back to bolt+ssc
shell.connect( new ConnectionConfig( "neo4j+ssc://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "neo4j+ssc", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
assertTrue(shell.isConnected());
}

// Here should be tests for "neo4j+s" and "bolt+s", but we don't have the infrastructure for those.

private void onlyIn3_2to3_6( CypherShell shell) throws Exception {
// Default connection settings
shell.connect( new ConnectionConfig( "bolt://", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
shell.connect( new ConnectionConfig( "bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME ) );
assumeTrue( majorVersion( shell.getServerVersion() ) == 3 );
assumeTrue( minorVersion( shell.getServerVersion() ) > 1 );
shell.disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String newPassword() {

@Nonnull
public String driverUrl() {
return String.format("%s%s:%d", scheme(), host(), port());
return String.format("%s://%s:%d", scheme(), host(), port());
}

@Nonnull
Expand Down
69 changes: 47 additions & 22 deletions cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.sourceforge.argparse4j.inf.Namespace;

import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
Expand All @@ -31,9 +33,6 @@
*/
public class CliArgHelper {

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

/**
* @param args to parse
* @return null in case of error, commandline arguments otherwise
Expand Down Expand Up @@ -70,22 +69,21 @@ public static CliArgs parseAndThrow( @Nonnull String... args ) throws ArgumentPa
private static CliArgs getCliArgs( CliArgs cliArgs, ArgumentParser parser, Namespace ns )
{
// Parse address string, returns null on error
final Matcher addressMatcher = parseAddressMatcher( parser, ns.getString( "address"));
final URI uri = parseURI( parser, ns.getString( "address"));

if (addressMatcher == null) {
if (uri == null) {
return null;
}

//---------------------
// Connection arguments
cliArgs.setScheme(addressMatcher.group("scheme"), "bolt://");
cliArgs.setHost(addressMatcher.group("host"), "localhost");
// Safe, regex only matches integers
String portString = addressMatcher.group("port");
cliArgs.setPort(portString == null ? CliArgs.DEFAULT_PORT : Integer.parseInt(portString));
cliArgs.setScheme(uri.getScheme(), "bolt");
cliArgs.setHost(uri.getHost(), "localhost");

int port = uri.getPort();
cliArgs.setPort(port == -1 ? 7687 : port);
// Also parse username and password from address if available
cliArgs.setUsername(addressMatcher.group("username"), "");
cliArgs.setPassword(addressMatcher.group("password"), "");
parseUserInfo( uri, cliArgs );

// Only overwrite user/pass from address string if the arguments were specified
String user = ns.getString("username");
Expand Down Expand Up @@ -125,19 +123,46 @@ private static CliArgs getCliArgs( CliArgs cliArgs, ArgumentParser parser, Names
return cliArgs;
}

private static void parseUserInfo(URI uri, CliArgs cliArgs)
{
String userInfo = uri.getUserInfo();
String user = null;
String password = null;
if (userInfo != null)
{
String[] split = userInfo.split( ":" );
if (split.length == 0)
{
user = userInfo;
} else if (split.length == 2)
{
user = split[0];
password = split[1];
} else {
throw new IllegalArgumentException("Cannot parse user and password from " + userInfo);
}

}
cliArgs.setUsername(user, "");
cliArgs.setPassword(password, "");
}

@Nullable
private static Matcher parseAddressMatcher(ArgumentParser parser, String address) {
Matcher matcher = ADDRESS_ARG_PATTERN.matcher(address);
if (!matcher.matches()) {
// Match behavior in built-in error handling
PrintWriter printWriter = new PrintWriter(System.err);
parser.printUsage(printWriter);
printWriter.println("cypher-shell: error: Failed to parse address: '" + address + "'");
printWriter.println("\n Address should be of the form: [scheme://][username:password@][host][:port]");
static URI parseURI( ArgumentParser parser, String address )
{
try
{
return new URI( address );
}
catch ( URISyntaxException e )
{
PrintWriter printWriter = new PrintWriter( System.err );
parser.printUsage( printWriter );
printWriter.println( "cypher-shell: error: Failed to parse address: '" + address + "'" );
printWriter.println( "\n Address should be of the form: [scheme://][username:password@][host][:port]" );
printWriter.flush();
return null;
}
return matcher;
}

private static ArgumentParser setupParser(ParameterMap parameterMap)
Expand All @@ -153,7 +178,7 @@ private static ArgumentParser setupParser(ParameterMap parameterMap)
ArgumentGroup connGroup = parser.addArgumentGroup("connection arguments");
connGroup.addArgument("-a", "--address")
.help("address and port to connect to")
.setDefault(String.format("%s%s:%d", CliArgs.DEFAULT_SCHEME, CliArgs.DEFAULT_HOST, CliArgs.DEFAULT_PORT));
.setDefault(String.format("%s://%s:%d", CliArgs.DEFAULT_SCHEME, CliArgs.DEFAULT_HOST, CliArgs.DEFAULT_PORT));
connGroup.addArgument("-u", "--username")
.setDefault("")
.help("username to connect as. Can also be specified using environment variable " + ConnectionConfig.USERNAME_ENV_VAR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.neo4j.shell.DatabaseManager.ABSENT_DB_NAME;

public class CliArgs {
static final String DEFAULT_SCHEME = "neo4j://";
static final String DEFAULT_SCHEME = "neo4j";
static final String DEFAULT_HOST = "localhost";
static final int DEFAULT_PORT = 7687;
static final int DEFAULT_NUM_SAMPLE_ROWS = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ public void connect( @Nonnull ConnectionConfig connectionConfig, ThrowingAction<
String fallbackScheme;
switch ( scheme )
{
case Scheme.NEO4J_URI_SCHEME + "://":
case Scheme.NEO4J_URI_SCHEME:
fallbackScheme = Scheme.BOLT_URI_SCHEME;
break;
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME + "://":
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
break;
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME + "://":
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
break;
default:
throw e;
}
connectionConfig = new ConnectionConfig(
fallbackScheme + "://",
fallbackScheme,
connectionConfig.host(),
connectionConfig.port(),
connectionConfig.username(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public class ConnectionConfigTest {
= new EnvironmentVariables();

private Logger logger = mock(Logger.class);
private ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 1, "bob",
private ConnectionConfig config = new ConnectionConfig("bolt", "localhost", 1, "bob",
"pass", Encryption.DEFAULT, "db");


@Test
public void scheme() {
assertEquals("bolt://", config.scheme());
assertEquals("bolt", config.scheme());
}

@Test
Expand All @@ -45,7 +45,7 @@ public void username() {
@Test
public void usernameDefaultsToEnvironmentVar() {
environmentVariables.set(ConnectionConfig.USERNAME_ENV_VAR, "alice");
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
"", Encryption.DEFAULT, ABSENT_DB_NAME);
assertEquals("alice", configWithEmptyParams.username());
}
Expand All @@ -58,7 +58,7 @@ public void password() {
@Test
public void passwordDefaultsToEnvironmentVar() {
environmentVariables.set(ConnectionConfig.PASSWORD_ENV_VAR, "ssap");
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
"", Encryption.DEFAULT, ABSENT_DB_NAME);
assertEquals("ssap", configWithEmptyParams.password());
}
Expand All @@ -71,7 +71,7 @@ public void database() {
@Test
public void databaseDefaultsToEnvironmentVar() {
environmentVariables.set(ConnectionConfig.DATABASE_ENV_VAR, "funnyDB");
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt", "localhost", 1, "",
"", Encryption.DEFAULT, ABSENT_DB_NAME);
assertEquals("funnyDB", configWithEmptyParams.database());
}
Expand All @@ -82,8 +82,8 @@ public void driverUrlDefaultScheme() {

@Test
public void encryption() {
assertEquals(Encryption.DEFAULT, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME).encryption());
assertEquals(Encryption.TRUE, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.TRUE, ABSENT_DB_NAME).encryption());
assertEquals(Encryption.FALSE, new ConnectionConfig("bolt://", "", -1, "", "", Encryption.FALSE, ABSENT_DB_NAME).encryption());
assertEquals(Encryption.DEFAULT, new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME).encryption());
assertEquals(Encryption.TRUE, new ConnectionConfig("bolt", "", -1, "", "", Encryption.TRUE, ABSENT_DB_NAME).encryption());
assertEquals(Encryption.FALSE, new ConnectionConfig("bolt", "", -1, "", "", Encryption.FALSE, ABSENT_DB_NAME).encryption());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setup() {

@Test
public void verifyDelegationOfConnectionMethods() throws CommandException {
ConnectionConfig cc = new ConnectionConfig("bolt://", "", 1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
ConnectionConfig cc = new ConnectionConfig("bolt", "", 1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter, new ShellParameterMap());

shell.connect(cc);
Expand Down
Loading