[PLUGIN] Licenses should only be valid between issue and expiration date

NOTE: Version bumped to beta2

closes elastic/elasticsearch#36

Original commit: elastic/x-pack-elasticsearch@c713a3c889
This commit is contained in:
Areek Zillur 2014-12-15 15:20:09 -05:00
parent fd1011b32a
commit a8aa84cdd9
9 changed files with 58 additions and 18 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>elasticsearch-license</artifactId>
<groupId>org.elasticsearch</groupId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>elasticsearch-license</artifactId>
<groupId>org.elasticsearch</groupId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@ -17,7 +17,7 @@
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-license-core-shaded</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</dependency>
</dependencies>
<build>

View File

@ -191,7 +191,14 @@ public class License implements ToXContent {
}
builder.startObject();
if (restViewMode) {
builder.field(XFields.STATUS, ((expiryDate - System.currentTimeMillis()) > 0l) ? "active" : "expired");
String status = "active";
long now = System.currentTimeMillis();
if (issueDate > now) {
status = "invalid";
} else if (expiryDate < now) {
status = "expired";
}
builder.field(XFields.STATUS, status);
}
builder.field(XFields.UID, uid);
builder.field(XFields.TYPE, type);

View File

@ -77,12 +77,14 @@ public class LicenseSerializationTests extends ElasticsearchTestCase {
long now = System.currentTimeMillis();
String expiredLicenseExpiryDate = TestUtils.dateMathString("now-1d/d", now);
String validLicenseIssueDate = TestUtils.dateMathString("now-10d/d", now);
String validLicenseExpiryDate = TestUtils.dateMathString("now+1d/d", now);
String invalidLicenseIssueDate = TestUtils.dateMathString("now+1d/d", now);
String validLicenseExpiryDate = TestUtils.dateMathString("now+2d/d", now);
Set<License> licenses = TestUtils.generateLicenses(Arrays.asList(new TestUtils.LicenseSpec("expired_feature", validLicenseIssueDate, expiredLicenseExpiryDate)
, new TestUtils.LicenseSpec("valid_feature", validLicenseIssueDate, validLicenseExpiryDate)));
, new TestUtils.LicenseSpec("valid_feature", validLicenseIssueDate, validLicenseExpiryDate),
new TestUtils.LicenseSpec("invalid_feature", invalidLicenseIssueDate, validLicenseExpiryDate)));
assertThat(licenses.size(), equalTo(2));
assertThat(licenses.size(), equalTo(3));
for (License license : licenses) {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
license.toXContent(builder, new ToXContent.MapParams(ImmutableMap.of(Licenses.REST_VIEW_MODE, "true")));
@ -95,8 +97,10 @@ public class LicenseSerializationTests extends ElasticsearchTestCase {
assertThat(map.get("expiry_date"), notNullValue());
if (license.feature().equals("valid_feature")) {
assertThat((String) map.get("status"), equalTo("active"));
} else {
} else if (license.feature().equals("expired_feature")) {
assertThat((String) map.get("status"), equalTo("expired"));
} else if (license.feature().equals("invalid_feature")) {
assertThat((String) map.get("status"), equalTo("invalid"));
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>elasticsearch-license</artifactId>
<groupId>org.elasticsearch</groupId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -19,7 +19,7 @@
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-license-core</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>elasticsearch-license</artifactId>
<groupId>org.elasticsearch</groupId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -21,13 +21,13 @@
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-license-licensor</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-license-core</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -458,9 +458,11 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
long nextScheduleFrequency = -1l;
for (ListenerHolder listenerHolder : registeredListeners) {
long expiryDate = expiryDateForFeature(listenerHolder.feature, currentLicensesMetaData);
long expiryDuration = expiryDate - System.currentTimeMillis();
long issueDate = issueDateForFeature(listenerHolder.feature, currentLicensesMetaData);
long now = System.currentTimeMillis();
long expiryDuration = expiryDate - now;
if (expiryDuration > 0l) {
if (expiryDuration > 0l && (now - issueDate) >= 0l) {
listenerHolder.enableFeatureIfNeeded();
if (nextScheduleFrequency == -1l) {
nextScheduleFrequency = expiryDuration;
@ -597,6 +599,15 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
return true;
}
private long issueDateForFeature(String feature, final LicensesMetaData currentLicensesMetaData) {
final Map<String, License> effectiveLicenses = getEffectiveLicenses(currentLicensesMetaData);
License featureLicense;
if ((featureLicense = effectiveLicenses.get(feature)) != null) {
return featureLicense.issueDate();
}
return -1l;
}
private long expiryDateForFeature(String feature, final LicensesMetaData currentLicensesMetaData) {
final Map<String, License> effectiveLicenses = getEffectiveLicenses(currentLicensesMetaData);
License featureLicense;

View File

@ -42,6 +42,21 @@ public class LicensesClientServiceTests extends AbstractLicensesServiceTests {
assertClientListenerNotificationCount(clientListener, actions);
}
@Test
public void testLicenseWithFutureIssueDate() throws Exception {
final LicensesClientService clientService = licensesClientService();
final TestTrackingClientListener clientListener = new TestTrackingClientListener();
String feature = "feature";
List<Action> actions = new ArrayList<>();
long now = System.currentTimeMillis();
long issueDate = dateMath("now+10d/d", now);
actions.add(registerWithTrialLicense(clientService, clientListener, feature, TimeValue.timeValueSeconds(2)));
actions.add(generateAndPutSignedLicenseAction(masterLicensesManagerService(), feature, issueDate, TimeValue.timeValueHours(24 * 20)));
actions.add(assertExpiryAction(feature, "trial", TimeValue.timeValueSeconds(2)));
assertClientListenerNotificationCount(clientListener, actions);
}
@Test
public void testMultipleClientSignedLicenseEnforcement() throws Exception {
// multiple client registration with null trial license and then different expiry signed license
@ -190,19 +205,22 @@ public class LicensesClientServiceTests extends AbstractLicensesServiceTests {
}
private Action generateAndPutSignedLicenseAction(final LicensesManagerService masterLicensesManagerService, final String feature, final TimeValue expiryDuration) throws Exception {
return generateAndPutSignedLicenseAction(masterLicensesManagerService, feature, -1, expiryDuration);
}
private Action generateAndPutSignedLicenseAction(final LicensesManagerService masterLicensesManagerService, final String feature, final long issueDate, final TimeValue expiryDuration) throws Exception {
return new Action(new Runnable() {
@Override
public void run() {
License license;
try {
license = generateSignedLicense(feature, expiryDuration);
license = generateSignedLicense(feature, issueDate, expiryDuration);
} catch (Exception e) {
fail(e.getMessage());
return;
}
registerAndAckSignedLicenses(masterLicensesManagerService, Arrays.asList(license), LicensesStatus.VALID);
}
}, 0, 1, "should trigger onEnable for " + feature + " once [signed license]");
}, 0, (issueDate <= System.currentTimeMillis()) ? 1 : 0, "should trigger onEnable for " + feature + " once [signed license]");
}
private Action registerWithoutTrialLicense(final LicensesClientService clientService, final LicensesClientService.Listener clientListener, final String feature) {

View File

@ -7,7 +7,7 @@
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-license</artifactId>
<packaging>pom</packaging>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
<modules>
<module>core-shaded</module>
<module>core</module>