mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-10 23:15:04 +00:00
address feedback
Original commit: elastic/x-pack-elasticsearch@f6b1d58c5b
This commit is contained in:
parent
ce10289540
commit
b1886ce978
@ -7,12 +7,15 @@ package org.elasticsearch.license.core;
|
|||||||
|
|
||||||
import org.apache.lucene.util.BytesRef;
|
import org.apache.lucene.util.BytesRef;
|
||||||
import org.apache.lucene.util.BytesRefIterator;
|
import org.apache.lucene.util.BytesRefIterator;
|
||||||
|
import org.elasticsearch.common.io.Streams;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
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.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
@ -72,4 +75,16 @@ public class LicenseVerifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean verifyLicense(final License license) {
|
||||||
|
final byte[] publicKeyBytes;
|
||||||
|
try (InputStream is = LicenseVerifier.class.getResourceAsStream("/public.key")) {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
Streams.copy(is, out);
|
||||||
|
publicKeyBytes = out.toByteArray();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new IllegalStateException(ex);
|
||||||
|
}
|
||||||
|
return verifyLicense(license, publicKeyBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,6 @@ import org.elasticsearch.common.Nullable;
|
|||||||
import org.elasticsearch.common.component.AbstractLifecycleComponent;
|
import org.elasticsearch.common.component.AbstractLifecycleComponent;
|
||||||
import org.elasticsearch.common.component.Lifecycle;
|
import org.elasticsearch.common.component.Lifecycle;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.inject.Singleton;
|
|
||||||
import org.elasticsearch.common.io.Streams;
|
|
||||||
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
|
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
|
||||||
import org.elasticsearch.common.joda.Joda;
|
import org.elasticsearch.common.joda.Joda;
|
||||||
import org.elasticsearch.common.logging.LoggerMessageFormat;
|
import org.elasticsearch.common.logging.LoggerMessageFormat;
|
||||||
@ -43,9 +41,6 @@ import org.elasticsearch.transport.TransportService;
|
|||||||
import org.elasticsearch.xpack.scheduler.SchedulerEngine;
|
import org.elasticsearch.xpack.scheduler.SchedulerEngine;
|
||||||
import org.elasticsearch.xpack.support.clock.Clock;
|
import org.elasticsearch.xpack.support.clock.Clock;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -76,7 +71,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
* When a new license is notified as enabled to the registered listener, a notification is scheduled at the time of license expiry.
|
* When a new license is notified as enabled to the registered listener, a notification is scheduled at the time of license expiry.
|
||||||
* Registered listeners are notified using {@link #onUpdate(LicensesMetaData)}
|
* Registered listeners are notified using {@link #onUpdate(LicensesMetaData)}
|
||||||
*/
|
*/
|
||||||
@Singleton
|
|
||||||
public class LicensesService extends AbstractLifecycleComponent implements ClusterStateListener, LicensesManagerService,
|
public class LicensesService extends AbstractLifecycleComponent implements ClusterStateListener, LicensesManagerService,
|
||||||
LicenseeRegistry, SchedulerEngine.Listener {
|
LicenseeRegistry, SchedulerEngine.Listener {
|
||||||
|
|
||||||
@ -248,7 +242,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
|
|||||||
public void registerLicense(final PutLicenseRequest request, final ActionListener<PutLicenseResponse> listener) {
|
public void registerLicense(final PutLicenseRequest request, final ActionListener<PutLicenseResponse> listener) {
|
||||||
final License newLicense = request.license();
|
final License newLicense = request.license();
|
||||||
final long now = clock.millis();
|
final long now = clock.millis();
|
||||||
if (!verifyLicense(newLicense) || newLicense.issueDate() > now) {
|
if (!LicenseVerifier.verifyLicense(newLicense) || newLicense.issueDate() > now) {
|
||||||
listener.onResponse(new PutLicenseResponse(true, LicensesStatus.INVALID));
|
listener.onResponse(new PutLicenseResponse(true, LicensesStatus.INVALID));
|
||||||
} else if (newLicense.expiryDate() < now) {
|
} else if (newLicense.expiryDate() < now) {
|
||||||
listener.onResponse(new PutLicenseResponse(true, LicensesStatus.EXPIRED));
|
listener.onResponse(new PutLicenseResponse(true, LicensesStatus.EXPIRED));
|
||||||
@ -294,18 +288,6 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean verifyLicense(final License license) {
|
|
||||||
final byte[] publicKeyBytes;
|
|
||||||
try (InputStream is = LicensesService.class.getResourceAsStream("/public.key")) {
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
Streams.copy(is, out);
|
|
||||||
publicKeyBytes = out.toByteArray();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new IllegalStateException(ex);
|
|
||||||
}
|
|
||||||
return LicenseVerifier.verifyLicense(license, publicKeyBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static TimeValue days(int days) {
|
static TimeValue days(int days) {
|
||||||
return TimeValue.timeValueHours(days * 24);
|
return TimeValue.timeValueHours(days * 24);
|
||||||
@ -588,7 +570,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
|
|||||||
} else {
|
} else {
|
||||||
boolean autoGeneratedLicense = License.isAutoGeneratedLicense(license.signature());
|
boolean autoGeneratedLicense = License.isAutoGeneratedLicense(license.signature());
|
||||||
if ((autoGeneratedLicense && TrialLicense.verify(license))
|
if ((autoGeneratedLicense && TrialLicense.verify(license))
|
||||||
|| (!autoGeneratedLicense && verifyLicense(license))) {
|
|| (!autoGeneratedLicense && LicenseVerifier.verifyLicense(license))) {
|
||||||
return license;
|
return license;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,11 +118,12 @@ public class TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static License generateExpiredLicense() throws Exception {
|
public static License generateExpiredLicense() throws Exception {
|
||||||
|
long expiryDate = System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis();
|
||||||
final License.Builder builder = License.builder()
|
final License.Builder builder = License.builder()
|
||||||
.uid(UUID.randomUUID().toString())
|
.uid(UUID.randomUUID().toString())
|
||||||
.version(License.VERSION_CURRENT)
|
.version(License.VERSION_CURRENT)
|
||||||
.expiryDate(System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis())
|
.expiryDate(expiryDate)
|
||||||
.issueDate(System.currentTimeMillis())
|
.issueDate(expiryDate - TimeValue.timeValueMinutes(10).getMillis())
|
||||||
.type(randomFrom("basic", "silver", "dev", "gold", "platinum"))
|
.type(randomFrom("basic", "silver", "dev", "gold", "platinum"))
|
||||||
.issuedTo("customer")
|
.issuedTo("customer")
|
||||||
.issuer("elasticsearch")
|
.issuer("elasticsearch")
|
||||||
|
@ -21,11 +21,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(postExpirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(postExpirySeconds + randomIntBetween(1, 10));
|
||||||
|
|
||||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||||
assertThat(post.matches(now + postExpiryDuration.getMillis(), now), equalTo(false));
|
assertThat(post.matches(now + postExpiryDuration.getMillis(), now), equalTo(false));
|
||||||
@ -36,11 +32,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds);
|
TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||||
|
|
||||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, null, timeValueMillis(10)) {
|
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, null, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||||
}
|
}
|
||||||
@ -50,11 +42,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
|
|
||||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(null, max, timeValueMillis(10)) {
|
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(null, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||||
}
|
}
|
||||||
@ -64,12 +52,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
|
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||||
assertThat(pre.matches(now - expiryDuration.getMillis(), now), equalTo(false));
|
assertThat(pre.matches(now - expiryDuration.getMillis(), now), equalTo(false));
|
||||||
@ -80,11 +63,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||||
final long now = expiryDate - max.millis() + randomIntBetween(1, ((int) min.getMillis()));
|
final long now = expiryDate - max.millis() + randomIntBetween(1, ((int) min.getMillis()));
|
||||||
@ -97,11 +76,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||||
int delta = randomIntBetween(1, 1000);
|
int delta = randomIntBetween(1, 1000);
|
||||||
@ -115,11 +90,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||||
final long now = expiryDate + min.millis() + randomIntBetween(1, ((int) (max.getMillis() - min.getMillis())));
|
final long now = expiryDate + min.millis() + randomIntBetween(1, ((int) (max.getMillis() - min.getMillis())));
|
||||||
@ -132,11 +103,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||||
@Override
|
|
||||||
public void on(License license) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||||
int delta = randomIntBetween(1, 1000);
|
int delta = randomIntBetween(1, 1000);
|
||||||
@ -145,4 +112,24 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||||||
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(expiryDate + min.getMillis()));
|
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(expiryDate + min.getMillis()));
|
||||||
assertThat(schedule.nextScheduledTimeAfter(1, now), equalTo(-1L));
|
assertThat(schedule.nextScheduledTimeAfter(1, now), equalTo(-1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class NoopPostExpirationCallback extends ExpirationCallback.Post {
|
||||||
|
|
||||||
|
public NoopPostExpirationCallback(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||||
|
super(min, max, frequency);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on(License license) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class NoopPreExpirationCallback extends ExpirationCallback.Pre {
|
||||||
|
|
||||||
|
public NoopPreExpirationCallback(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||||
|
super(min, max, frequency);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on(License license) {}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user