NullPointerException when creating a watch with Jira action (#41922) (#42081) (#42873)

NullPointerException when secured_url does not use proper scheme in jira action. 
This commit will handle Expection and display proper message.
This commit is contained in:
James Baiera 2019-06-05 16:03:07 -04:00 committed by GitHub
parent 757c6a45a0
commit 1300183001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.watcher.common.http;
import java.util.Locale;
import java.util.Objects;
public enum Scheme {
@ -29,6 +30,7 @@ public enum Scheme {
}
public static Scheme parse(String value) {
Objects.requireNonNull(value, "Scheme should not be Null");
value = value.toLowerCase(Locale.ROOT);
switch (value) {
case "http":

View File

@ -61,6 +61,9 @@ public class JiraAccount {
String url = getSetting(name, settings, SECURE_URL_SETTING);
try {
URI uri = new URI(url);
if (uri.getScheme() == null) {
throw new URISyntaxException("null", "No scheme defined in url");
}
Scheme protocol = Scheme.parse(uri.getScheme());
if ((protocol == Scheme.HTTP) && (Booleans.isTrue(settings.get(ALLOW_HTTP_SETTING)) == false)) {
throw new SettingsException("invalid jira [" + name + "] account settings. unsecure scheme [" + protocol + "]");
@ -68,7 +71,7 @@ public class JiraAccount {
this.url = uri;
} catch (URISyntaxException | IllegalArgumentException e) {
throw new SettingsException(
"invalid jira [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", e);
"invalid jira [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", e);
}
this.user = getSetting(name, settings, SECURE_USER_SETTING);
this.password = getSetting(name, settings, SECURE_PASSWORD_SETTING);

View File

@ -80,6 +80,16 @@ public class JiraAccountTests extends ESTestCase {
assertThat(e.getMessage(), containsString("invalid jira [test] account settings. missing required [secure_password] setting"));
}
public void testInvalidSchemeUrl() throws Exception{
MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString(JiraAccount.SECURE_URL_SETTING.getKey(),"test"); //Setting test as invalid scheme url
secureSettings.setString(JiraAccount.SECURE_USER_SETTING.getKey(), "foo");
secureSettings.setString(JiraAccount.SECURE_PASSWORD_SETTING.getKey(), "password");
Settings settings = Settings.builder().setSecureSettings(secureSettings).build();
SettingsException e = expectThrows(SettingsException.class, () -> new JiraAccount("test", settings, null));
assertThat(e.getMessage(), containsString("invalid jira [test] account settings. invalid [secure_url] setting"));
}
public void testUnsecureAccountUrl() throws Exception {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString(JiraAccount.SECURE_USER_SETTING.getKey(), "foo");