Merge pull request elastic/elasticsearch#1190 from ywelsch/fix/license-metadata-deserialization
License metadata deserialization swallows other custom metadata Original commit: elastic/x-pack-elasticsearch@e821f5985e
This commit is contained in:
commit
c6d8d0a381
|
@ -102,8 +102,8 @@ public class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implemen
|
||||||
public LicensesMetaData fromXContent(XContentParser parser) throws IOException {
|
public LicensesMetaData fromXContent(XContentParser parser) throws IOException {
|
||||||
List<License> pre20Licenses = new ArrayList<>(1);
|
List<License> pre20Licenses = new ArrayList<>(1);
|
||||||
License license = LICENSE_TOMBSTONE;
|
License license = LICENSE_TOMBSTONE;
|
||||||
while (parser.currentToken() != XContentParser.Token.END_OBJECT) {
|
XContentParser.Token token;
|
||||||
XContentParser.Token token = parser.nextToken();
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
String fieldName = parser.text();
|
String fieldName = parser.text();
|
||||||
if (fieldName != null) {
|
if (fieldName != null) {
|
||||||
|
|
|
@ -6,16 +6,22 @@
|
||||||
package org.elasticsearch.license.plugin.core;
|
package org.elasticsearch.license.plugin.core;
|
||||||
|
|
||||||
import org.elasticsearch.Version;
|
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.Base64;
|
||||||
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
|
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
|
||||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent.Params;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
import org.elasticsearch.license.core.License;
|
import org.elasticsearch.license.core.License;
|
||||||
|
import org.elasticsearch.license.plugin.LicensePlugin;
|
||||||
import org.elasticsearch.license.plugin.TestUtils;
|
import org.elasticsearch.license.plugin.TestUtils;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
|
@ -25,6 +31,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
import static org.elasticsearch.license.core.CryptUtils.encrypt;
|
import static org.elasticsearch.license.core.CryptUtils.encrypt;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
public class LicensesMetaDataSerializationTests extends ESTestCase {
|
public class LicensesMetaDataSerializationTests extends ESTestCase {
|
||||||
|
@ -41,6 +48,34 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(license));
|
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 {
|
public void testXContentSerializationOneTrial() throws Exception {
|
||||||
long issueDate = System.currentTimeMillis();
|
long issueDate = System.currentTimeMillis();
|
||||||
License.Builder specBuilder = License.builder()
|
License.Builder specBuilder = License.builder()
|
||||||
|
|
Loading…
Reference in New Issue