Skip to content

Commit 1d8b5fc

Browse files
committed
Fail command-line client's auto-URL detection with helpful message (elastic#40151)
The setup-passwords tool gives cryptic messages in case where custom discovery providers are used (see elastic#33580). As the URL auto-detection logic should be seen as best effort, this commit improves the exception message to make it clearer what needs to be done to fix the issue. Relates elastic#33580
1 parent cb29d3d commit 1d8b5fc

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.io.OutputStream;
30-
import java.io.UncheckedIOException;
3130
import java.net.HttpURLConnection;
3231
import java.net.InetAddress;
3332
import java.net.URL;
@@ -154,13 +153,13 @@ String getDefaultURL() {
154153
// this sucks but a port can be specified with a value of 0, we'll never be able to connect to it so just default to
155154
// what we know
156155
if (port <= 0) {
157-
throw new IllegalStateException("unable to determine http port from settings, please use the -u option to provide the" +
158-
" url");
156+
throw new IllegalStateException("unable to determine http port from settings");
159157
}
160158
}
161159
return scheme + "://" + InetAddresses.toUriString(publishAddress) + ":" + port;
162-
} catch (IOException e) {
163-
throw new UncheckedIOException("failed to resolve default URL", e);
160+
} catch (Exception e) {
161+
throw new IllegalStateException("unable to determine default URL from settings, please use the -u option to explicitly " +
162+
"provide the url", e);
164163
}
165164
}
166165

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClientTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.nio.charset.StandardCharsets;
2828
import java.nio.file.Path;
2929

30+
import static org.hamcrest.Matchers.containsString;
31+
3032
/**
3133
* This class tests {@link CommandLineHttpClient} For extensive tests related to
3234
* ssl settings can be found {@link SSLConfigurationSettingsTests}
@@ -63,6 +65,15 @@ public void testCommandLineHttpClientCanExecuteAndReturnCorrectResultUsingSSLSet
6365
assertEquals("Http response body does not match", "complete", httpResponse.getResponseBody().get("test"));
6466
}
6567

68+
public void testGetDefaultURLFailsWithHelpfulMessage() {
69+
Settings settings = Settings.builder()
70+
.put("network.host", "_ec2:privateIpv4_")
71+
.build();
72+
CommandLineHttpClient client = new CommandLineHttpClient(settings, environment);
73+
assertThat(expectThrows(IllegalStateException.class, () -> client.getDefaultURL()).getMessage(),
74+
containsString("unable to determine default URL from settings, please use the -u option to explicitly provide the url"));
75+
}
76+
6677
private MockWebServer createMockWebServer() {
6778
Path certPath = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt");
6879
Path keyPath = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem");

0 commit comments

Comments
 (0)