Merge branch 'master' into feature/shield-ui

Original commit: elastic/x-pack-elasticsearch@81c98000d0
This commit is contained in:
Lukas Olson 2016-04-14 19:04:22 -07:00
commit 5752fd43ef
13 changed files with 130 additions and 32 deletions

View File

@ -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) {

View File

@ -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);
}
});
}

View File

@ -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
}
}

View File

@ -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: {}

View File

@ -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"));

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -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() {

View File

@ -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)) {

View File

@ -31,6 +31,10 @@ public class DefaultTextTemplateEngine extends AbstractComponent implements Text
@Override
public String render(TextTemplate template, Map<String, Object> model) {
if (template == null) {
return null;
}
XContentType contentType = detectContentType(template);
Map<String, String> compileParams = compileParams(contentType);
template = trimContentType(template);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -13,6 +13,10 @@ import java.util.Map;
public class MockTextTemplateEngine implements TextTemplateEngine {
@Override
public String render(TextTemplate template, Map<String, Object> model) {
if (template == null ) {
return null;
}
return template.getTemplate();
}
}