From 4ddd7af3340f478def65097ddc20c5be5a8991af Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Tue, 22 Dec 2015 11:39:58 +0100 Subject: [PATCH] Fix issue where license metadata deserialization swallows other custom metadata Closes elastic/elasticsearch#1190 Original commit: elastic/x-pack-elasticsearch@5c5280fee5312e6581edcfe66c437004a1693aff --- .../license/plugin/core/LicensesMetaData.java | 4 +-- .../LicensesMetaDataSerializationTests.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java index 8e726588e40..77e55d37e08 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java @@ -102,8 +102,8 @@ public class LicensesMetaData extends AbstractDiffable implemen public LicensesMetaData fromXContent(XContentParser parser) throws IOException { List pre20Licenses = new ArrayList<>(1); License license = LICENSE_TOMBSTONE; - while (parser.currentToken() != XContentParser.Token.END_OBJECT) { - XContentParser.Token token = parser.nextToken(); + XContentParser.Token token; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { String fieldName = parser.text(); if (fieldName != null) { diff --git a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java index e2bee38d75b..821ded1dcef 100644 --- a/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java +++ b/elasticsearch/x-pack/license-plugin/src/test/java/org/elasticsearch/license/plugin/core/LicensesMetaDataSerializationTests.java @@ -6,16 +6,22 @@ package org.elasticsearch.license.plugin.core; import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.cluster.metadata.RepositoriesMetaData; +import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.Base64; import org.elasticsearch.common.io.stream.ByteBufferStreamInput; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.license.core.License; +import org.elasticsearch.license.plugin.LicensePlugin; import org.elasticsearch.license.plugin.TestUtils; import org.elasticsearch.test.ESTestCase; @@ -25,6 +31,7 @@ import java.util.UUID; import static org.elasticsearch.license.core.CryptUtils.encrypt; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; public class LicensesMetaDataSerializationTests extends ESTestCase { @@ -41,6 +48,34 @@ public class LicensesMetaDataSerializationTests extends ESTestCase { assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(license)); } + public void testLicenseMetadataParsingDoesNotSwallowOtherMetaData() throws Exception { + new LicensePlugin(Settings.EMPTY); // makes sure LicensePlugin is registered in Custom MetaData + License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2)); + LicensesMetaData licensesMetaData = new LicensesMetaData(license); + RepositoryMetaData repositoryMetaData = new RepositoryMetaData("repo", "fs", Settings.EMPTY); + RepositoriesMetaData repositoriesMetaData = new RepositoriesMetaData(repositoryMetaData); + final MetaData.Builder metaDataBuilder = MetaData.builder(); + if (randomBoolean()) { // random order of insertion + metaDataBuilder.putCustom(licensesMetaData.type(), licensesMetaData); + metaDataBuilder.putCustom(repositoriesMetaData.type(), repositoriesMetaData); + } else { + metaDataBuilder.putCustom(repositoriesMetaData.type(), repositoriesMetaData); + metaDataBuilder.putCustom(licensesMetaData.type(), licensesMetaData); + } + // serialize metadata + XContentBuilder builder = XContentFactory.jsonBuilder(); + Params params = new ToXContent.MapParams(Collections.singletonMap(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_GATEWAY)); + builder.startObject(); + builder = metaDataBuilder.build().toXContent(builder, params); + builder.endObject(); + String serializedMetaData = builder.string(); + // deserialize metadata again + MetaData metaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(serializedMetaData)); + // check that custom metadata still present + assertThat(metaData.custom(licensesMetaData.type()), notNullValue()); + assertThat(metaData.custom(repositoriesMetaData.type()), notNullValue()); + } + public void testXContentSerializationOneTrial() throws Exception { long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder()