Remove bwc code to read 1.x licenses from cluster state
In 2.x, 1.x license format in cluster state was upgraded to the 2.x format. This commit removes the code to read 1.x license format from cluster state in 5.x, as 2.x clusters will already upgrade the license format to 2.x format. Original commit: elastic/x-pack-elasticsearch@77f18ffc76
This commit is contained in:
parent
dc6672693b
commit
92e357f838
|
@ -5,29 +5,16 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.license;
|
package org.elasticsearch.license;
|
||||||
|
|
||||||
import org.apache.lucene.util.CollectionUtil;
|
|
||||||
import org.elasticsearch.Version;
|
|
||||||
import org.elasticsearch.cluster.AbstractDiffable;
|
import org.elasticsearch.cluster.AbstractDiffable;
|
||||||
import org.elasticsearch.cluster.metadata.MetaData;
|
import org.elasticsearch.cluster.metadata.MetaData;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
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.license.core.License;
|
import org.elasticsearch.license.core.License;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.elasticsearch.license.core.CryptUtils.decrypt;
|
|
||||||
import static org.elasticsearch.license.core.CryptUtils.encrypt;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains metadata about registered licenses
|
* Contains metadata about registered licenses
|
||||||
|
@ -101,34 +88,13 @@ class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implements Meta
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LicensesMetaData fromXContent(XContentParser parser) throws IOException {
|
public LicensesMetaData fromXContent(XContentParser parser) throws IOException {
|
||||||
List<License> pre20Licenses = new ArrayList<>(1);
|
|
||||||
License license = LICENSE_TOMBSTONE;
|
License license = LICENSE_TOMBSTONE;
|
||||||
XContentParser.Token token;
|
XContentParser.Token token;
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
String fieldName = parser.currentName();
|
String fieldName = parser.currentName();
|
||||||
if (fieldName != null) {
|
if (fieldName != null) {
|
||||||
// for back compat with 1.x license metadata
|
if (fieldName.equals(Fields.LICENSE)) {
|
||||||
if (fieldName.equals(Fields.TRIAL_LICENSES) || fieldName.equals(Fields.SIGNED_LICENCES)) {
|
|
||||||
token = parser.nextToken();
|
|
||||||
if (token == XContentParser.Token.START_ARRAY) {
|
|
||||||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
|
|
||||||
if (parser.currentToken().isValue()) {
|
|
||||||
// trial license
|
|
||||||
byte[] data = decrypt(Base64.getDecoder().decode(parser.text()));
|
|
||||||
try (XContentParser trialLicenseParser =
|
|
||||||
XContentFactory.xContent(XContentType.JSON).createParser(data)) {
|
|
||||||
trialLicenseParser.nextToken();
|
|
||||||
License pre20TrialLicense = License.fromXContent(trialLicenseParser);
|
|
||||||
pre20Licenses.add(TrialLicense.create(License.builder().fromPre20LicenseSpec(pre20TrialLicense)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// signed license
|
|
||||||
pre20Licenses.add(License.fromXContent(parser));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (fieldName.equals(Fields.LICENSE)) {
|
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
if (token == XContentParser.Token.START_OBJECT) {
|
if (token == XContentParser.Token.START_OBJECT) {
|
||||||
license = License.fromXContent(parser);
|
license = License.fromXContent(parser);
|
||||||
|
@ -139,23 +105,6 @@ class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implements Meta
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// when we see old license metadata,
|
|
||||||
// we try to choose the license that has the latest issue date that is not expired
|
|
||||||
if (!pre20Licenses.isEmpty()) {
|
|
||||||
// take the best unexpired license
|
|
||||||
CollectionUtil.timSort(pre20Licenses, License.LATEST_ISSUE_DATE_FIRST);
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
for (License oldLicense : pre20Licenses) {
|
|
||||||
if (oldLicense.expiryDate() > now) {
|
|
||||||
license = oldLicense;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// take the best expired license
|
|
||||||
if (license == LICENSE_TOMBSTONE && !pre20Licenses.isEmpty()) {
|
|
||||||
license = pre20Licenses.get(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new LicensesMetaData(license);
|
return new LicensesMetaData(license);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,23 +122,6 @@ class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implements Meta
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTo(StreamOutput streamOutput) throws IOException {
|
public void writeTo(StreamOutput streamOutput) throws IOException {
|
||||||
if (streamOutput.getVersion().before(Version.V_2_0_0)) {
|
|
||||||
if (license == LICENSE_TOMBSTONE) {
|
|
||||||
streamOutput.writeVInt(0); // no signed license
|
|
||||||
streamOutput.writeVInt(0); // no trial license
|
|
||||||
} else if (!License.isAutoGeneratedLicense(license.signature())) {
|
|
||||||
streamOutput.writeVInt(1); // one signed license
|
|
||||||
license.writeTo(streamOutput);
|
|
||||||
streamOutput.writeVInt(0); // no trial license
|
|
||||||
} else {
|
|
||||||
streamOutput.writeVInt(0); // no signed license
|
|
||||||
streamOutput.writeVInt(1); // one trial license
|
|
||||||
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
|
|
||||||
license.toXContent(contentBuilder,
|
|
||||||
new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
|
|
||||||
streamOutput.writeString(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (license == LICENSE_TOMBSTONE) {
|
if (license == LICENSE_TOMBSTONE) {
|
||||||
streamOutput.writeBoolean(false); // no license
|
streamOutput.writeBoolean(false); // no license
|
||||||
} else {
|
} else {
|
||||||
|
@ -197,51 +129,17 @@ class LicensesMetaData extends AbstractDiffable<MetaData.Custom> implements Meta
|
||||||
license.writeTo(streamOutput);
|
license.writeTo(streamOutput);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LicensesMetaData readFrom(StreamInput streamInput) throws IOException {
|
public LicensesMetaData readFrom(StreamInput streamInput) throws IOException {
|
||||||
License license = LICENSE_TOMBSTONE;
|
License license = LICENSE_TOMBSTONE;
|
||||||
if (streamInput.getVersion().before(Version.V_2_0_0)) {
|
|
||||||
int size = streamInput.readVInt();
|
|
||||||
List<License> licenses = new ArrayList<>();
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
licenses.add(License.readLicense(streamInput));
|
|
||||||
}
|
|
||||||
int numTrialLicenses = streamInput.readVInt();
|
|
||||||
for (int i = 0; i < numTrialLicenses; i++) {
|
|
||||||
byte[] data = decrypt(Base64.getDecoder().decode(streamInput.readString()));
|
|
||||||
try (XContentParser trialLicenseParser = XContentFactory.xContent(XContentType.JSON).createParser(data)) {
|
|
||||||
trialLicenseParser.nextToken();
|
|
||||||
License pre20TrialLicense = License.fromXContent(trialLicenseParser);
|
|
||||||
licenses.add(TrialLicense.create(License.builder().fromPre20LicenseSpec(pre20TrialLicense)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// when we see read licenses from old pre v2.0,
|
|
||||||
// we try to choose the license that has the latest issue date that is not expired
|
|
||||||
CollectionUtil.timSort(licenses, License.LATEST_ISSUE_DATE_FIRST);
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
for (License oldLicense : licenses) {
|
|
||||||
if (oldLicense.expiryDate() > now) {
|
|
||||||
license = oldLicense;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// take the best expired license
|
|
||||||
if (license == LICENSE_TOMBSTONE && !licenses.isEmpty()) {
|
|
||||||
license = licenses.get(0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (streamInput.readBoolean()) {
|
if (streamInput.readBoolean()) {
|
||||||
license = License.readLicense(streamInput);
|
license = License.readLicense(streamInput);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return new LicensesMetaData(license);
|
return new LicensesMetaData(license);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class Fields {
|
private static final class Fields {
|
||||||
private static final String SIGNED_LICENCES = "signed_licenses";
|
|
||||||
private static final String TRIAL_LICENSES = "trial_licenses";
|
|
||||||
private static final String LICENSE = "license";
|
private static final String LICENSE = "license";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,10 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.license;
|
package org.elasticsearch.license;
|
||||||
|
|
||||||
import org.elasticsearch.Version;
|
|
||||||
import org.elasticsearch.cluster.metadata.MetaData;
|
import org.elasticsearch.cluster.metadata.MetaData;
|
||||||
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
|
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
|
||||||
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
|
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
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;
|
||||||
|
@ -23,11 +20,9 @@ import org.elasticsearch.common.xcontent.XContentType;
|
||||||
import org.elasticsearch.license.core.License;
|
import org.elasticsearch.license.core.License;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
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.notNullValue;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
@ -90,120 +85,6 @@ public class LicensesMetaDataSerializationTests extends ESTestCase {
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(trialLicense));
|
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(trialLicense));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test1xLicensesMetaDataFromXContent() throws Exception {
|
|
||||||
License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
|
|
||||||
long issueDate = signedLicense.issueDate() - TimeValue.timeValueMillis(200).getMillis();
|
|
||||||
License.Builder specBuilder = License.builder()
|
|
||||||
.uid(UUID.randomUUID().toString())
|
|
||||||
.issuedTo("customer")
|
|
||||||
.maxNodes(5)
|
|
||||||
.issueDate(issueDate)
|
|
||||||
.expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis());
|
|
||||||
final License trialLicense = TrialLicense.create(specBuilder);
|
|
||||||
// trial license
|
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
||||||
builder.startObject();
|
|
||||||
builder.startObject("licenses");
|
|
||||||
builder.startArray("trial_licenses");
|
|
||||||
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
|
|
||||||
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
|
|
||||||
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
|
|
||||||
builder.endArray();
|
|
||||||
builder.startArray("signed_licenses");
|
|
||||||
builder.endArray();
|
|
||||||
builder.endObject();
|
|
||||||
builder.endObject();
|
|
||||||
LicensesMetaData licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
|
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(trialLicense));
|
|
||||||
|
|
||||||
// signed license
|
|
||||||
builder = XContentFactory.jsonBuilder();
|
|
||||||
builder.startObject();
|
|
||||||
builder.startObject("licenses");
|
|
||||||
builder.startArray("trial_licenses");
|
|
||||||
builder.endArray();
|
|
||||||
builder.startArray("signed_licenses");
|
|
||||||
signedLicense.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
||||||
builder.endArray();
|
|
||||||
builder.endObject();
|
|
||||||
builder.endObject();
|
|
||||||
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
|
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicense));
|
|
||||||
|
|
||||||
// trial and signed license
|
|
||||||
builder = XContentFactory.jsonBuilder();
|
|
||||||
builder.startObject();
|
|
||||||
builder.startObject("licenses");
|
|
||||||
builder.startArray("trial_licenses");
|
|
||||||
contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
|
|
||||||
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
|
|
||||||
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
|
|
||||||
builder.endArray();
|
|
||||||
builder.startArray("signed_licenses");
|
|
||||||
signedLicense.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
||||||
builder.endArray();
|
|
||||||
builder.endObject();
|
|
||||||
builder.endObject();
|
|
||||||
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
|
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicense));
|
|
||||||
|
|
||||||
// license with later issue date is selected
|
|
||||||
long laterIssueDate = trialLicense.issueDate() + TimeValue.timeValueHours(2).getMillis();
|
|
||||||
License signedLicenseIssuedLater = TestUtils.generateSignedLicense(laterIssueDate, TimeValue.timeValueHours(2));
|
|
||||||
builder = XContentFactory.jsonBuilder();
|
|
||||||
builder.startObject();
|
|
||||||
builder.startObject("licenses");
|
|
||||||
builder.startArray("trial_licenses");
|
|
||||||
contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
|
|
||||||
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
|
|
||||||
builder.value(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
|
|
||||||
builder.endArray();
|
|
||||||
builder.startArray("signed_licenses");
|
|
||||||
signedLicense.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
||||||
signedLicenseIssuedLater.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
||||||
builder.endArray();
|
|
||||||
builder.endObject();
|
|
||||||
builder.endObject();
|
|
||||||
licensesMetaDataFromXContent = getLicensesMetaDataFromXContent(builder.bytes());
|
|
||||||
assertThat(licensesMetaDataFromXContent.getLicense(), equalTo(signedLicenseIssuedLater));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test1xLicensesMetaDataFromStream() throws Exception {
|
|
||||||
long issueDate = System.currentTimeMillis();
|
|
||||||
License.Builder specBuilder = License.builder()
|
|
||||||
.uid(UUID.randomUUID().toString())
|
|
||||||
.issuedTo("customer")
|
|
||||||
.maxNodes(5)
|
|
||||||
.issueDate(issueDate)
|
|
||||||
.expiryDate(issueDate + TimeValue.timeValueHours(1).getMillis());
|
|
||||||
final License trialLicense = TrialLicense.create(specBuilder);
|
|
||||||
// trial license
|
|
||||||
BytesStreamOutput output = new BytesStreamOutput();
|
|
||||||
output.writeVInt(0);
|
|
||||||
output.writeVInt(1);
|
|
||||||
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
|
|
||||||
trialLicense.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true")));
|
|
||||||
output.writeString(Base64.getEncoder().encodeToString(encrypt(BytesReference.toBytes(contentBuilder.bytes()))));
|
|
||||||
try (StreamInput input = output.bytes().streamInput()) {
|
|
||||||
input.setVersion(Version.V_2_0_0_beta1);
|
|
||||||
LicensesMetaData licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
|
|
||||||
assertThat(licensesMetaData.getLicense(), equalTo(trialLicense));
|
|
||||||
}
|
|
||||||
|
|
||||||
// signed license
|
|
||||||
License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
|
|
||||||
output = new BytesStreamOutput();
|
|
||||||
output.writeVInt(1);
|
|
||||||
signedLicense.writeTo(output);
|
|
||||||
output.writeVInt(0);
|
|
||||||
try (StreamInput input = output.bytes().streamInput()) {
|
|
||||||
input.setVersion(Version.V_2_0_0_beta1);
|
|
||||||
LicensesMetaData licensesMetaData = LicensesMetaData.PROTO.readFrom(input);
|
|
||||||
assertThat(licensesMetaData.getLicense(), equalTo(signedLicense));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLicenseTombstoneFromXContext() throws Exception {
|
public void testLicenseTombstoneFromXContext() throws Exception {
|
||||||
final XContentBuilder builder = XContentFactory.jsonBuilder();
|
final XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
builder.startObject("licenses");
|
builder.startObject("licenses");
|
||||||
|
|
Loading…
Reference in New Issue