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.BytesRefIterator;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.InvalidKeyException;
|
||||
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.Lifecycle;
|
||||
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.Joda;
|
||||
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.support.clock.Clock;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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.
|
||||
* Registered listeners are notified using {@link #onUpdate(LicensesMetaData)}
|
||||
*/
|
||||
@Singleton
|
||||
public class LicensesService extends AbstractLifecycleComponent implements ClusterStateListener, LicensesManagerService,
|
||||
LicenseeRegistry, SchedulerEngine.Listener {
|
||||
|
||||
|
@ -248,7 +242,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
|
|||
public void registerLicense(final PutLicenseRequest request, final ActionListener<PutLicenseResponse> listener) {
|
||||
final License newLicense = request.license();
|
||||
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));
|
||||
} else if (newLicense.expiryDate() < now) {
|
||||
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) {
|
||||
return TimeValue.timeValueHours(days * 24);
|
||||
|
@ -588,7 +570,7 @@ public class LicensesService extends AbstractLifecycleComponent implements Clust
|
|||
} else {
|
||||
boolean autoGeneratedLicense = License.isAutoGeneratedLicense(license.signature());
|
||||
if ((autoGeneratedLicense && TrialLicense.verify(license))
|
||||
|| (!autoGeneratedLicense && verifyLicense(license))) {
|
||||
|| (!autoGeneratedLicense && LicenseVerifier.verifyLicense(license))) {
|
||||
return license;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,11 +118,12 @@ public class TestUtils {
|
|||
}
|
||||
|
||||
public static License generateExpiredLicense() throws Exception {
|
||||
long expiryDate = System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis();
|
||||
final License.Builder builder = License.builder()
|
||||
.uid(UUID.randomUUID().toString())
|
||||
.version(License.VERSION_CURRENT)
|
||||
.expiryDate(System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis())
|
||||
.issueDate(System.currentTimeMillis())
|
||||
.expiryDate(expiryDate)
|
||||
.issueDate(expiryDate - TimeValue.timeValueMinutes(10).getMillis())
|
||||
.type(randomFrom("basic", "silver", "dev", "gold", "platinum"))
|
||||
.issuedTo("customer")
|
||||
.issuer("elasticsearch")
|
||||
|
|
|
@ -21,11 +21,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(postExpirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||
assertThat(post.matches(now + postExpiryDuration.getMillis(), now), equalTo(false));
|
||||
|
@ -36,11 +32,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, null, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, null, timeValueMillis(10));
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||
}
|
||||
|
@ -50,11 +42,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(null, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(null, max, timeValueMillis(10));
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||
}
|
||||
|
@ -64,12 +52,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||
assertThat(pre.matches(now - expiryDuration.getMillis(), now), equalTo(false));
|
||||
|
@ -80,11 +63,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||
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 min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Pre pre = new NoopPreExpirationCallback(min, max, timeValueMillis(10));
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||
int delta = randomIntBetween(1, 1000);
|
||||
|
@ -115,11 +90,7 @@ public class ExpirationCallbackTests extends ESTestCase {
|
|||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||
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 min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
final ExpirationCallback.Post post = new NoopPostExpirationCallback(min, max, timeValueMillis(10));
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||
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(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…
Reference in New Issue