Fix writing of SecurityFeatureSetUsage to pre-7.1 (#38922)

This change makes the writing of new usage data conditional based on
the version that is being written to. A test has also been added to
ensure serialization works as expected to an older version.

Relates #38687, #38917
This commit is contained in:
Jay Modi 2019-02-14 16:28:52 -07:00 committed by GitHub
parent 7d449c5f65
commit 5d06226507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -80,8 +80,10 @@ public class SecurityFeatureSetUsage extends XPackFeatureSet.Usage {
out.writeMap(realmsUsage);
out.writeMap(rolesStoreUsage);
out.writeMap(sslUsage);
out.writeMap(tokenServiceUsage);
out.writeMap(apiKeyServiceUsage);
if (out.getVersion().onOrAfter(Version.V_7_1_0)) {
out.writeMap(tokenServiceUsage);
out.writeMap(apiKeyServiceUsage);
}
out.writeMap(auditUsage);
out.writeMap(ipFilterUsage);
if (out.getVersion().before(Version.V_6_0_0_beta1)) {

View File

@ -5,16 +5,19 @@
*/
package org.elasticsearch.xpack.security;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;
import org.elasticsearch.xpack.core.XPackSettings;
@ -250,5 +253,19 @@ public class SecurityFeatureSetTests extends ESTestCase {
assertThat(source.getValue("roles"), is(nullValue()));
}
}
out = new BytesStreamOutput();
out.setVersion(VersionUtils.randomVersionBetween(random(), Version.V_6_7_0, Version.V_7_0_0));
securityUsage.writeTo(out);
StreamInput input = out.bytes().streamInput();
input.setVersion(out.getVersion());
serializedUsage = new SecurityFeatureSetUsage(input);
XContentSource source;
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
serializedUsage.toXContent(builder, ToXContent.EMPTY_PARAMS);
source = new XContentSource(builder);
}
assertThat(source.getValue("token_service"), is(nullValue()));
assertThat(source.getValue("api_key_service"), is(nullValue()));
}
}