SQL: Check connection on CLI startup (elastic/x-pack-elasticsearch#3278)
* SQL: Check connection on CLI startup Adds a connection check at the CLI startup. If connection cannot be established or elasticsearch has incompatible version, the CLI doesn't start. relates elastic/x-pack-elasticsearch#2984 Original commit: elastic/x-pack-elasticsearch@c9a58d2cd6
This commit is contained in:
parent
018d4d7722
commit
fab3712e3d
|
@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.containsString;
|
|||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
public class CliSecurityIT extends SqlSecurityTestCase {
|
||||
static final String NO_INIT_CONNECTION_CHECK_PREFIX = "-c false ";
|
||||
static String adminEsUrlPrefix() {
|
||||
return "test_admin:x-pack-test-password@";
|
||||
}
|
||||
|
@ -117,7 +118,8 @@ public class CliSecurityIT extends SqlSecurityTestCase {
|
|||
|
||||
@Override
|
||||
public void expectForbidden(String user, String sql) throws Exception {
|
||||
try (RemoteCli cli = new RemoteCli(userPrefix(user) + elasticsearchAddress())) {
|
||||
// Skip initial check to make sure it doesn't trip
|
||||
try (RemoteCli cli = new RemoteCli(NO_INIT_CONNECTION_CHECK_PREFIX + userPrefix(user) + elasticsearchAddress())) {
|
||||
assertThat(cli.command(sql), containsString("is unauthorized for user [" + user + "]"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,3 +95,12 @@ task run {
|
|||
.waitFor()
|
||||
}
|
||||
}
|
||||
|
||||
// Use the jar for testing so we can get the proper version information
|
||||
test {
|
||||
classpath -= compileJava.outputs.files
|
||||
classpath -= configurations.compile
|
||||
classpath -= configurations.runtime
|
||||
classpath += jar.outputs.files
|
||||
dependsOn jar
|
||||
}
|
|
@ -20,25 +20,42 @@ import org.elasticsearch.xpack.sql.cli.command.FetchSizeCliCommand;
|
|||
import org.elasticsearch.xpack.sql.cli.command.PrintLogoCommand;
|
||||
import org.elasticsearch.xpack.sql.cli.command.ServerInfoCliCommand;
|
||||
import org.elasticsearch.xpack.sql.cli.command.ServerQueryCliCommand;
|
||||
import org.elasticsearch.xpack.sql.client.shared.ClientException;
|
||||
import org.elasticsearch.xpack.sql.client.shared.ConnectionConfiguration;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
public class Cli extends Command {
|
||||
private final OptionSpec<Boolean> debugOption;
|
||||
private final OptionSpec<Boolean> checkOption;
|
||||
private final OptionSpec<String> connectionString;
|
||||
|
||||
public Cli() {
|
||||
private Cli() {
|
||||
super("Elasticsearch SQL CLI", Cli::configureLogging);
|
||||
this.debugOption = parser.acceptsAll(Arrays.asList("d", "debug"),
|
||||
"Enable debug logging")
|
||||
.withRequiredArg().ofType(Boolean.class)
|
||||
.defaultsTo(Boolean.parseBoolean(System.getProperty("cli.debug", "false")));
|
||||
this.checkOption = parser.acceptsAll(Arrays.asList("c", "check"),
|
||||
"Enable initial connection check on startup")
|
||||
.withRequiredArg().ofType(Boolean.class)
|
||||
.defaultsTo(Boolean.parseBoolean(System.getProperty("cli.check", "true")));
|
||||
this.connectionString = parser.nonOptions("uri");
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this VM Options to run in IntelliJ or Eclipse:
|
||||
* -Dorg.jline.terminal.type=xterm-256color
|
||||
* -Dorg.jline.terminal.jna=false
|
||||
* -Dorg.jline.terminal.jansi=false
|
||||
* -Dorg.jline.terminal.exec=false
|
||||
* -Dorg.jline.terminal.dumb=true
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
final Cli cli = new Cli();
|
||||
int status = cli.main(args, Terminal.DEFAULT);
|
||||
|
@ -60,14 +77,15 @@ public class Cli extends Command {
|
|||
@Override
|
||||
protected void execute(org.elasticsearch.cli.Terminal terminal, OptionSet options) throws Exception {
|
||||
boolean debug = debugOption.value(options);
|
||||
boolean check = checkOption.value(options);
|
||||
List<String> args = connectionString.values(options);
|
||||
if (args.size() > 1) {
|
||||
throw new UserException(ExitCodes.USAGE, "expecting a single uri");
|
||||
}
|
||||
execute(args.size() == 1 ? args.get(0) : null, debug);
|
||||
execute(args.size() == 1 ? args.get(0) : null, debug, check);
|
||||
}
|
||||
|
||||
private void execute(String uri, boolean debug) throws Exception {
|
||||
private void execute(String uri, boolean debug, boolean check) throws Exception {
|
||||
CliCommand cliCommand = new CliCommands(
|
||||
new PrintLogoCommand(),
|
||||
new ClearScreenCliCommand(),
|
||||
|
@ -78,9 +96,37 @@ public class Cli extends Command {
|
|||
);
|
||||
try (CliTerminal cliTerminal = new JLineTerminal()) {
|
||||
ConnectionBuilder connectionBuilder = new ConnectionBuilder(cliTerminal);
|
||||
CliSession cliSession = new CliSession(new CliHttpClient(connectionBuilder.buildConnection(uri)));
|
||||
ConnectionConfiguration con = connectionBuilder.buildConnection(uri);
|
||||
CliSession cliSession = new CliSession(new CliHttpClient(con));
|
||||
cliSession.setDebug(debug);
|
||||
if (check) {
|
||||
checkConnection(cliSession, cliTerminal, con);
|
||||
}
|
||||
new CliRepl(cliTerminal, cliSession, cliCommand).execute();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkConnection(CliSession cliSession, CliTerminal cliTerminal, ConnectionConfiguration con) throws UserException {
|
||||
try {
|
||||
cliSession.checkConnection();
|
||||
} catch (ClientException ex) {
|
||||
if (cliSession.isDebug()) {
|
||||
cliTerminal.error("Client Exception", ex.getMessage());
|
||||
cliTerminal.println();
|
||||
cliTerminal.printStackTrace(ex);
|
||||
cliTerminal.flush();
|
||||
}
|
||||
if (ex.getCause() != null && ex.getCause() instanceof ConnectException) {
|
||||
// Most likely Elasticsearch is not running
|
||||
throw new UserException(ExitCodes.IO_ERROR,
|
||||
"Cannot connect to the server " + con.connectionString() + " - " + ex.getCause().getMessage());
|
||||
} else {
|
||||
// Most likely we connected to an old version of Elasticsearch or not Elasticsearch at all
|
||||
throw new UserException(ExitCodes.DATA_ERROR,
|
||||
"Cannot communicate with the server " + con.connectionString() +
|
||||
". This version of CLI only works with Elasticsearch version " + Version.version());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,13 @@
|
|||
package org.elasticsearch.xpack.sql.cli.command;
|
||||
|
||||
import org.elasticsearch.xpack.sql.cli.CliHttpClient;
|
||||
import org.elasticsearch.xpack.sql.cli.net.protocol.InfoResponse;
|
||||
import org.elasticsearch.xpack.sql.client.shared.ClientException;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
import org.elasticsearch.xpack.sql.protocol.shared.AbstractQueryInitRequest;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Stores information about the current session
|
||||
*/
|
||||
|
@ -18,7 +23,7 @@ public class CliSession {
|
|||
private boolean debug;
|
||||
|
||||
public CliSession(CliHttpClient cliHttpClient) {
|
||||
this.cliHttpClient = cliHttpClient;
|
||||
this.cliHttpClient = cliHttpClient;
|
||||
}
|
||||
|
||||
public CliHttpClient getClient() {
|
||||
|
@ -51,4 +56,17 @@ public class CliSession {
|
|||
public boolean isDebug() {
|
||||
return debug;
|
||||
}
|
||||
|
||||
public void checkConnection() throws ClientException {
|
||||
InfoResponse response;
|
||||
try {
|
||||
response = cliHttpClient.serverInfo();
|
||||
} catch (SQLException ex) {
|
||||
throw new ClientException(ex);
|
||||
}
|
||||
// TODO: We can relax compatibility requirement later when we have a better idea about protocol compatibility guarantees
|
||||
if (response.majorVersion != Version.versionMajor() || response.minorVersion != Version.versionMinor()) {
|
||||
throw new ClientException("This alpha version of CLI is only compatible with Elasticsearch version " + Version.version());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.cli;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.sql.cli.command.CliSession;
|
||||
import org.elasticsearch.xpack.sql.cli.net.protocol.InfoResponse;
|
||||
import org.elasticsearch.xpack.sql.client.shared.ClientException;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class CliSessionTests extends ESTestCase {
|
||||
|
||||
public void testProperConnection() throws Exception {
|
||||
CliHttpClient cliHttpClient = mock(CliHttpClient.class);
|
||||
when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5),
|
||||
(byte) Version.versionMajor(), (byte) Version.versionMinor(),
|
||||
randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5)));
|
||||
CliSession cliSession = new CliSession(cliHttpClient);
|
||||
cliSession.checkConnection();
|
||||
verify(cliHttpClient, times(1)).serverInfo();
|
||||
verifyNoMoreInteractions(cliHttpClient);
|
||||
}
|
||||
|
||||
public void testConnection() throws Exception {
|
||||
CliHttpClient cliHttpClient = mock(CliHttpClient.class);
|
||||
when(cliHttpClient.serverInfo()).thenThrow(new SQLException("Cannot connect"));
|
||||
CliSession cliSession = new CliSession(cliHttpClient);
|
||||
expectThrows(ClientException.class, cliSession::checkConnection);
|
||||
verify(cliHttpClient, times(1)).serverInfo();
|
||||
verifyNoMoreInteractions(cliHttpClient);
|
||||
}
|
||||
|
||||
public void testWrongServerVersion() throws Exception {
|
||||
CliHttpClient cliHttpClient = mock(CliHttpClient.class);
|
||||
byte minor;
|
||||
byte major;
|
||||
if (randomBoolean()) {
|
||||
minor = (byte) Version.versionMinor();
|
||||
major = (byte) (Version.versionMajor() + 1);
|
||||
} else {
|
||||
minor = (byte) (Version.versionMinor() + 1);
|
||||
major = (byte) Version.versionMajor();
|
||||
|
||||
}
|
||||
when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5),
|
||||
minor, major, randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5)));
|
||||
CliSession cliSession = new CliSession(cliHttpClient);
|
||||
expectThrows(ClientException.class, cliSession::checkConnection);
|
||||
verify(cliHttpClient, times(1)).serverInfo();
|
||||
verifyNoMoreInteractions(cliHttpClient);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.cli;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
public class VersionTests extends ESTestCase {
|
||||
public void testVersionIsCurrent() {
|
||||
/* This test will only work properly in gradle because in gradle we run the tests
|
||||
* using the jar. */
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.versionNumber());
|
||||
assertNotNull(Version.versionHash());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.major, Version.versionMajor());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.versionMinor());
|
||||
}
|
||||
|
||||
}
|
|
@ -8,7 +8,7 @@ package org.elasticsearch.xpack.sql.jdbc.jdbc;
|
|||
import org.elasticsearch.xpack.sql.client.shared.ConnectionConfiguration;
|
||||
import org.elasticsearch.xpack.sql.client.shared.StringUtils;
|
||||
import org.elasticsearch.xpack.sql.jdbc.JdbcSQLException;
|
||||
import org.elasticsearch.xpack.sql.jdbc.util.Version;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.net.URI;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.elasticsearch.xpack.sql.jdbc.JdbcSQLException;
|
|||
import org.elasticsearch.xpack.sql.jdbc.net.client.Cursor;
|
||||
import org.elasticsearch.xpack.sql.jdbc.net.protocol.ColumnInfo;
|
||||
import org.elasticsearch.xpack.sql.jdbc.net.protocol.MetaColumnInfo;
|
||||
import org.elasticsearch.xpack.sql.jdbc.util.Version;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
|
|
@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.jdbc.jdbc;
|
|||
|
||||
import org.elasticsearch.xpack.sql.jdbc.JdbcSQLException;
|
||||
import org.elasticsearch.xpack.sql.jdbc.debug.Debug;
|
||||
import org.elasticsearch.xpack.sql.jdbc.util.Version;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.elasticsearch.xpack.sql.client.shared.ConnectionConfiguration;
|
|||
import org.elasticsearch.xpack.sql.jdbc.debug.Debug;
|
||||
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration;
|
||||
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConnection;
|
||||
import org.elasticsearch.xpack.sql.jdbc.util.Version;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.sql.client.shared.Version;
|
||||
|
||||
public class VersionTests extends ESTestCase {
|
||||
public void testVersionIsCurrent() {
|
||||
/* This test will only work properly in gradle because in gradle we run the tests
|
||||
* using the jar. */
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.versionNumber());
|
||||
assertNotNull(Version.versionHash());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.major, Version.versionMajor());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.versionMinor());
|
||||
}
|
||||
}
|
|
@ -3,9 +3,7 @@
|
|||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc.util;
|
||||
|
||||
import org.elasticsearch.xpack.sql.client.shared.StringUtils;
|
||||
package org.elasticsearch.xpack.sql.client.shared;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
@ -29,7 +27,7 @@ public abstract class Version {
|
|||
return new int[] { Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]) };
|
||||
}
|
||||
else {
|
||||
throw new Error("Detected Elasticsearch SQL JDBC driver but found invalid version " + ver);
|
||||
throw new Error("Detected Elasticsearch SQL jar but found invalid version " + ver);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,7 +39,7 @@ public abstract class Version {
|
|||
try {
|
||||
res = Version.class.getClassLoader().getResources(target);
|
||||
} catch (IOException ex) {
|
||||
throw new Error("Cannot detect Elasticsearch SQL JDBC driver jar; it typically indicates a deployment issue...");
|
||||
throw new Error("Cannot detect Elasticsearch SQL jar; it typically indicates a deployment issue...");
|
||||
}
|
||||
|
||||
if (res != null) {
|
||||
|
@ -55,7 +53,7 @@ public abstract class Version {
|
|||
int foundJars = 0;
|
||||
if (normalized.size() > 1) {
|
||||
StringBuilder sb = new StringBuilder(
|
||||
"Multiple Elasticsearch SQL JDBC driver versions detected in the classpath; please use only one\n");
|
||||
"Multiple Elasticsearch SQL versions detected in the classpath; please use only one\n");
|
||||
for (String s : normalized) {
|
||||
if (s.contains("jar:")) {
|
||||
foundJars++;
|
||||
|
@ -88,7 +86,7 @@ public abstract class Version {
|
|||
min = vers[1];
|
||||
rev = vers[2];
|
||||
} catch (Exception ex) {
|
||||
throw new Error("Detected Elasticsearch SQL JDBC driver but cannot retrieve its version", ex);
|
||||
throw new Error("Detected Elasticsearch SQL jar but cannot retrieve its version", ex);
|
||||
}
|
||||
}
|
||||
VER_MAJ = maj;
|
|
@ -3,20 +3,11 @@
|
|||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.jdbc.util;
|
||||
package org.elasticsearch.xpack.sql.client.shared;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
public class VersionTests extends ESTestCase {
|
||||
public void testVersionIsCurrent() {
|
||||
/* This test will only work properly in gradle because in gradle we run the tests
|
||||
* using the jar. */
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.versionNumber());
|
||||
assertNotNull(Version.versionHash());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.major, Version.versionMajor());
|
||||
assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.versionMinor());
|
||||
}
|
||||
|
||||
public void test70Version() {
|
||||
int[] ver = Version.from("7.0.0-alpha");
|
||||
assertEquals(7, ver[0]);
|
||||
|
@ -40,6 +31,6 @@ public class VersionTests extends ESTestCase {
|
|||
|
||||
public void testInvalidVersion() {
|
||||
Error err = expectThrows(Error.class, () -> Version.from("7.1"));
|
||||
assertEquals("Detected Elasticsearch SQL JDBC driver but found invalid version 7.1", err.getMessage());
|
||||
assertEquals("Detected Elasticsearch SQL jar but found invalid version 7.1", err.getMessage());
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ import java.nio.file.Path;
|
|||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -99,7 +100,7 @@ public class CliFixture {
|
|||
command.add("-Dorg.jline.terminal.dumb=true");
|
||||
command.add("-jar");
|
||||
command.add(cliJar.toString());
|
||||
command.add(url);
|
||||
command.addAll(Arrays.asList(url.split(" ")));
|
||||
ProcessBuilder cliBuilder = new ProcessBuilder(command);
|
||||
cliBuilder.redirectErrorStream(true);
|
||||
Process process = cliBuilder.start();
|
||||
|
|
Loading…
Reference in New Issue