From 1ef246adabbf430372a1c38dd1be7510aeac6121 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 14 Apr 2016 09:03:24 +0200 Subject: [PATCH 1/6] Watcher: Fall back on default format color in hipchat action Our documentation states that we have default attributes for message.format and message.color, which in fact we do not have as an NPE was triggered in that case. This commit falls back to unset defaults and allows for hipchat messages to be sent without having to configure color/format in the action or the account. Closes elastic/elasticsearch#1666 Original commit: elastic/x-pack-elasticsearch@bfb7e35112ce63edd906e6fbb0378b6e0997bfba --- .../messy/tests/HipChatServiceIT.java | 29 ++++++++++--- .../text/DefaultTextTemplateEngine.java | 4 ++ .../hipchat/service/UserAccountTests.java | 41 +++++++++++++++++++ .../support/text/TextTemplateTests.java | 5 +++ .../watcher/test/MockTextTemplateEngine.java | 4 ++ 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/HipChatServiceIT.java b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/HipChatServiceIT.java index 1d518dd8417..cfd68f10af1 100644 --- a/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/HipChatServiceIT.java +++ b/elasticsearch/qa/messy-test-xpack-with-mustache/src/test/java/org/elasticsearch/messy/tests/HipChatServiceIT.java @@ -90,7 +90,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { public void testSendMessageV1Account() throws Exception { HipChatService service = getInstanceFromMaster(HipChatService.class); HipChatMessage hipChatMessage = new HipChatMessage( - "/code HipChatServiceTests#testSendMessage_V1Account", + "HipChatServiceTests#testSendMessage_V1Account", new String[] { "test-watcher", "test-watcher-2" }, null, // users are unsupported in v1 "watcher-tests", @@ -108,7 +108,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { HipChatService service = getInstanceFromMaster(HipChatService.class); HipChatMessage.Color color = randomFrom(HipChatMessage.Color.values()); HipChatMessage hipChatMessage = new HipChatMessage( - "/code HipChatServiceTests#testSendMessage_IntegrationAccount colored " + color.value(), + "HipChatServiceTests#testSendMessage_IntegrationAccount colored " + color.value(), null, // custom rooms are unsupported by integration profiles null, // users are unsupported by integration profiles null, // custom "from" is not supported by integration profiles @@ -126,7 +126,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { HipChatService service = getInstanceFromMaster(HipChatService.class); HipChatMessage.Color color = randomFrom(HipChatMessage.Color.values()); HipChatMessage hipChatMessage = new HipChatMessage( - "/code HipChatServiceTests#testSendMessage_UserAccount colored " + color.value(), + "HipChatServiceTests#testSendMessage_UserAccount colored " + color.value(), new String[] { "test-watcher", "test-watcher-2" }, new String[] { "watcher@elastic.co" }, null, // custom "from" is not supported by integration profiles @@ -148,7 +148,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { switch (profile) { case USER: account = "user_account"; - actionBuilder = hipchatAction(account, "/code {{ctx.payload.ref}}") + actionBuilder = hipchatAction(account, "{{ctx.payload.ref}}") .addRooms("test-watcher", "test-watcher-2") .addUsers("watcher@elastic.co") .setFormat(HipChatMessage.Format.TEXT) @@ -158,7 +158,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { case INTEGRATION: account = "integration_account"; - actionBuilder = hipchatAction(account, "/code {{ctx.payload.ref}}") + actionBuilder = hipchatAction(account, "{{ctx.payload.ref}}") .setFormat(HipChatMessage.Format.TEXT) .setColor(color) .setNotify(false); @@ -167,7 +167,7 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { default: assertThat(profile, is(HipChatAccount.Profile.V1)); account = "v1_account"; - actionBuilder = hipchatAction(account, "/code {{ctx.payload.ref}}") + actionBuilder = hipchatAction(account, "{{ctx.payload.ref}}") .addRooms("test-watcher", "test-watcher-2") .setFrom("watcher-test") .setFormat(HipChatMessage.Format.TEXT) @@ -201,6 +201,23 @@ public class HipChatServiceIT extends AbstractWatcherIntegrationTestCase { assertThat(response.getHits().getTotalHits(), is(1L)); } + public void testDefaultValuesForColorAndFormatWorks() { + HipChatService service = getInstanceFromMaster(HipChatService.class); + HipChatMessage hipChatMessage = new HipChatMessage( + "HipChatServiceTests#testSendMessage_UserAccount with default Color and text", + new String[] { "test-watcher" }, + new String[] { "watcher@elastic.co" }, + null, // custom "from" is not supported by integration profiles + null, + null, + false); + + HipChatAccount account = service.getAccount("user_account"); + assertThat(account, notNullValue()); + SentMessages messages = account.send(hipChatMessage); + assertSentMessagesAreValid(2, messages); + } + private void assertSentMessagesAreValid(int expectedMessageSize, SentMessages messages) { assertThat(messages.count(), is(expectedMessageSize)); for (SentMessages.SentMessage message : messages) { diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/DefaultTextTemplateEngine.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/DefaultTextTemplateEngine.java index cd1e934e9cf..c079e5da222 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/DefaultTextTemplateEngine.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/DefaultTextTemplateEngine.java @@ -31,6 +31,10 @@ public class DefaultTextTemplateEngine extends AbstractComponent implements Text @Override public String render(TextTemplate template, Map model) { + if (template == null) { + return null; + } + XContentType contentType = detectContentType(template); Map compileParams = compileParams(contentType); template = trimContentType(template); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/actions/hipchat/service/UserAccountTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/actions/hipchat/service/UserAccountTests.java index b4d54e5fe78..dd2c01ef294 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/actions/hipchat/service/UserAccountTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/actions/hipchat/service/UserAccountTests.java @@ -17,8 +17,11 @@ import org.elasticsearch.watcher.support.http.HttpMethod; import org.elasticsearch.watcher.support.http.HttpRequest; import org.elasticsearch.watcher.support.http.HttpResponse; import org.elasticsearch.watcher.support.http.Scheme; +import org.elasticsearch.watcher.support.text.TextTemplate; +import org.elasticsearch.watcher.test.MockTextTemplateEngine; import java.io.IOException; +import java.util.HashMap; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.arrayContaining; @@ -33,6 +36,7 @@ import static org.mockito.Mockito.when; * */ public class UserAccountTests extends ESTestCase { + public void testSettings() throws Exception { String accountName = "_name"; @@ -246,4 +250,41 @@ public class UserAccountTests extends ESTestCase { verify(httpClient).execute(reqU2); verify(httpClient).execute(reqU2); } + + public void testColorIsOptional() throws Exception { + Settings settings = Settings.builder() + .put("user", "testuser") + .put("auth_token", "awesome-auth-token") + .build(); + UserAccount userAccount = createUserAccount(settings); + + TextTemplate body = TextTemplate.inline("body").build(); + TextTemplate[] rooms = new TextTemplate[] { TextTemplate.inline("room").build() }; + HipChatMessage.Template template = new HipChatMessage.Template(body, rooms, null, "sender", HipChatMessage.Format.TEXT, null, true); + + HipChatMessage message = userAccount.render("watchId", "actionId", new MockTextTemplateEngine(), template, new HashMap<>()); + assertThat(message.color, is(nullValue())); + } + + public void testFormatIsOptional() throws Exception { + Settings settings = Settings.builder() + .put("user", "testuser") + .put("auth_token", "awesome-auth-token") + .build(); + UserAccount userAccount = createUserAccount(settings); + + TextTemplate body = TextTemplate.inline("body").build(); + TextTemplate[] rooms = new TextTemplate[] { TextTemplate.inline("room").build() }; + HipChatMessage.Template template = new HipChatMessage.Template(body, rooms, null, "sender", null, + TextTemplate.inline("yellow").build(), true); + + HipChatMessage message = userAccount.render("watchId", "actionId", new MockTextTemplateEngine(), template, new HashMap<>()); + assertThat(message.format, is(nullValue())); + } + + private UserAccount createUserAccount(Settings settings) { + HipChatServer hipChatServer = mock(HipChatServer.class); + HttpClient httpClient = mock(HttpClient.class); + return new UserAccount("notify-monitoring", settings, hipChatServer, httpClient, logger); + } } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java index 481efc09365..2731fcd8501 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/support/text/TextTemplateTests.java @@ -34,6 +34,7 @@ import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -186,6 +187,10 @@ public class TextTemplateTests extends ESTestCase { } } + public void testNullObject() throws Exception { + assertThat(engine.render(null ,new HashMap<>()), is(nullValue())); + } + private TextTemplate.Builder templateBuilder(ScriptType type, String text) { switch (type) { case INLINE: return TextTemplate.inline(text); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/MockTextTemplateEngine.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/MockTextTemplateEngine.java index c5236983706..933f9bd79e0 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/MockTextTemplateEngine.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/MockTextTemplateEngine.java @@ -13,6 +13,10 @@ import java.util.Map; public class MockTextTemplateEngine implements TextTemplateEngine { @Override public String render(TextTemplate template, Map model) { + if (template == null ) { + return null; + } + return template.getTemplate(); } } From 5f7220dea4514373e1676e619e2127b942dd68b7 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 14 Apr 2016 14:46:08 +0200 Subject: [PATCH 2/6] Fix compile errors due to upstream changes in HasChild- and HasParentQueryBuilder Original commit: elastic/x-pack-elasticsearch@9945e89b6e5a75470269d059ad00673e536ad85f --- .../integration/DocumentLevelSecurityTests.java | 17 +++++++++-------- .../integration/FieldLevelSecurityTests.java | 5 +++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java index 3ad58bad8ad..1b02791285e 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.integration; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetResponse; @@ -548,13 +549,13 @@ public class DocumentLevelSecurityTests extends ShieldIntegTestCase { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery())) + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery())) + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) .addSort("_uid", SortOrder.ASC) .get(); assertHitCount(searchResponse, 3L); @@ -565,39 +566,39 @@ public class DocumentLevelSecurityTests extends ShieldIntegTestCase { // Both user1 and user2 can't see field1 and field2, no parent/child query should yield results: searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery())) + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery())) + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery())) + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery())) + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) .get(); assertHitCount(searchResponse, 0L); // user 3 can see them but not c3 searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery())) + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery())) + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) .get(); assertHitCount(searchResponse, 2L); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1")); diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/FieldLevelSecurityTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/FieldLevelSecurityTests.java index 23b12fc8563..1585d33b9e4 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/FieldLevelSecurityTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/integration/FieldLevelSecurityTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.integration; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.fieldstats.FieldStatsResponse; import org.elasticsearch.action.get.GetResponse; @@ -1177,7 +1178,7 @@ public class FieldLevelSecurityTests extends ShieldIntegTestCase { SearchResponse searchResponse = client() .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("field1", "yellow"))) + .setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -1186,7 +1187,7 @@ public class FieldLevelSecurityTests extends ShieldIntegTestCase { searchResponse = client() .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("field1", "yellow"))) + .setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)) .get(); assertHitCount(searchResponse, 0L); } From 2dc8a720c2f3786ef8420e2458eeebd836ffd3dc Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 14 Apr 2016 14:00:02 +0100 Subject: [PATCH 3/6] Fix Eclipse Compile error in ReservedRealmTests The eclipse compiler errors on this class because "the method containsInAnyOrder(T...) of type Matchers is not applicable as the formal varargs element type T is not accessible here". This is because the first common superclass of `XPackUser` and `KibanaUser` is `ReservedUser` which is package protected and not available to this test class. This change casts to `User` so the error does not occur in Eclipse. Original commit: elastic/x-pack-elasticsearch@be8fa82720d077fd20be69b1bb5135fc4a530d05 --- .../elasticsearch/shield/authc/esnative/ReservedRealmTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java index a1765ec9334..9baf301bba2 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java @@ -129,6 +129,6 @@ public class ReservedRealmTests extends ESTestCase { assertThat(ReservedRealm.isReserved(notExpected), is(false)); assertThat(ReservedRealm.getUser(notExpected), nullValue()); - assertThat(ReservedRealm.users(), containsInAnyOrder(XPackUser.INSTANCE, KibanaUser.INSTANCE)); + assertThat(ReservedRealm.users(), containsInAnyOrder((User) XPackUser.INSTANCE, KibanaUser.INSTANCE)); } } From 5eb8a603c921cf32961c9ac361a8a44f80480585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 14 Apr 2016 16:23:51 +0200 Subject: [PATCH 4/6] Adapt to api change in es core Original commit: elastic/x-pack-elasticsearch@4d6f6abf0296fdadd57dea30803733b6c289ce3d --- .../watcher/support/WatcherUtils.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java index bc3cceba7f7..86f3067c83d 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java @@ -5,6 +5,17 @@ */ package org.elasticsearch.watcher.support; +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.WatcherDateTimeUtils.formatDate; + +import java.io.IOException; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchType; @@ -27,17 +38,6 @@ import org.elasticsearch.watcher.execution.WatchExecutionContext; import org.elasticsearch.watcher.watch.Payload; import org.joda.time.DateTime; -import java.io.IOException; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.watcher.support.WatcherDateTimeUtils.formatDate; - /** */ public final class WatcherUtils { @@ -113,7 +113,7 @@ public final class WatcherUtils { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); if (ParseFieldMatcher.STRICT.match(currentFieldName, BODY_FIELD)) { - searchRequest.source(SearchSourceBuilder.fromXContent(parser, context, aggParsers, suggesters)); + searchRequest.source(SearchSourceBuilder.fromXContent(context, aggParsers, suggesters)); } } else if (token == XContentParser.Token.START_ARRAY) { if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_FIELD)) { From fc1c13d8a40ac8aa1919c25096e32f496f834f1c Mon Sep 17 00:00:00 2001 From: jaymode Date: Thu, 14 Apr 2016 13:32:04 -0400 Subject: [PATCH 5/6] Fix compile error due to change in DateFieldMapper Original commit: elastic/x-pack-elasticsearch@995dde2a36d893996c4a0d13b514994e14018a03 --- .../org/elasticsearch/watcher/support/WatcherDateTimeUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java index efcc04658df..5a6c27d09eb 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java @@ -25,7 +25,7 @@ import java.util.concurrent.Callable; */ public class WatcherDateTimeUtils { - public static final FormatDateTimeFormatter dateTimeFormatter = DateFieldMapper.Defaults.DATE_TIME_FORMATTER; + public static final FormatDateTimeFormatter dateTimeFormatter = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER; public static final DateMathParser dateMathParser = new DateMathParser(dateTimeFormatter); private WatcherDateTimeUtils() { From e5c2a44d5d065b8f2be902b13c1eef1dfd032fc5 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Thu, 14 Apr 2016 16:36:58 -0400 Subject: [PATCH 6/6] Return 404 status code when no license is installed closes elastic/elasticsearch#2000 Original commit: elastic/x-pack-elasticsearch@3bd4193cf8fc65a09b6c92add9dcf84cc0a8cd46 --- .../license/plugin/rest/RestGetLicenseAction.java | 6 ++++-- .../resources/rest-api-spec/api/license.delete.json | 12 ++++++++++++ .../rest-api-spec/test/license/20_put_license.yaml | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/api/license.delete.json diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java index 74325358f3b..bf8200f30ad 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.Map; import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestStatus.NOT_FOUND; import static org.elasticsearch.rest.RestStatus.OK; public class RestGetLicenseAction extends BaseRestHandler { @@ -58,14 +59,15 @@ public class RestGetLicenseAction extends BaseRestHandler { if (!request.hasParam("pretty")) { builder.prettyPrint().lfAtEnd(); } + boolean hasLicense = response.license() != null; builder.startObject(); - if (response.license() != null) { + if (hasLicense) { builder.startObject("license"); response.license().toInnerXContent(builder, params); builder.endObject(); } builder.endObject(); - return new BytesRestResponse(OK, builder); + return new BytesRestResponse(hasLicense ? OK : NOT_FOUND, builder); } }); } diff --git a/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/api/license.delete.json b/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/api/license.delete.json new file mode 100644 index 00000000000..903a0ed361d --- /dev/null +++ b/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/api/license.delete.json @@ -0,0 +1,12 @@ +{ + "license.delete": { + "documentation": "https://www.elastic.co/guide/en/shield/current/license-management.html", + "methods": ["DELETE"], + "url": { + "path": "/_license", + "paths": ["/_license"], + "parts" : {} + }, + "body": null + } +} diff --git a/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yaml b/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yaml index 1d5fb822c7e..be2592d265a 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yaml +++ b/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yaml @@ -55,3 +55,14 @@ - length: { license: 10 } - match: { license.uid: "893361dc-9749-4997-93cb-802e3dofh7aa" } +--- +"Should throw 404 after license deletion": + + - do: + license.delete: {} + + - match: { acknowledged: true } + + - do: + catch: missing + license.get: {}