Skip to content

Commit

Permalink
NullPointerException when creating a watch with Jira action (elastic#…
Browse files Browse the repository at this point in the history
…41922) (elastic#42081)

NullPointerException when secured_url does not use proper scheme in jira action. 
This commit will handle Expection and display proper message.
  • Loading branch information
ojasgulati authored and jbaiera committed Jun 4, 2019
1 parent 4808f48 commit eb04ea0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.elasticsearch.xpack.watcher.common.http;

import java.util.Locale;
import java.util.Objects;

public enum Scheme {

Expand All @@ -29,6 +30,7 @@ public int defaultPort() {
}

public static Scheme parse(String value) {
Objects.requireNonNull(value, "Scheme should not be Null");
value = value.toLowerCase(Locale.ROOT);
switch (value) {
case "http":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ public JiraAccount(String name, Settings settings, HttpClient httpClient) {
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 + "]");
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public void testJiraAccountSettings() {
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");
Expand Down

0 comments on commit eb04ea0

Please sign in to comment.