XPack: core/ccr/Security-cli migration to java-time (#38415)

part of the migrating joda time work.
refactoring x-pack plugins usages of joda to java-time
refers #27330
This commit is contained in:
Przemyslaw Gomulka 2019-02-05 22:09:32 +01:00 committed by GitHub
parent 68cb7b9879
commit afcdbd2bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 48 deletions

View File

@ -9,6 +9,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -19,11 +20,10 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<AutoFollowStatsMonitoringDoc> { public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<AutoFollowStatsMonitoringDoc> {
private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
private AutoFollowStats autoFollowStats; private AutoFollowStats autoFollowStats;
@Before @Before
@ -94,7 +94,7 @@ public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase
equalTo( equalTo(
"{" "{"
+ "\"cluster_uuid\":\"_cluster\"," + "\"cluster_uuid\":\"_cluster\","
+ "\"timestamp\":\"" + new DateTime(timestamp, DateTimeZone.UTC).toString() + "\"," + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\","
+ "\"interval_ms\":" + intervalMillis + "," + "\"interval_ms\":" + intervalMillis + ","
+ "\"type\":\"ccr_auto_follow_stats\"," + "\"type\":\"ccr_auto_follow_stats\","
+ "\"source_node\":{" + "\"source_node\":{"
@ -103,7 +103,7 @@ public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase
+ "\"transport_address\":\"_addr\"," + "\"transport_address\":\"_addr\","
+ "\"ip\":\"_ip\"," + "\"ip\":\"_ip\","
+ "\"name\":\"_name\"," + "\"name\":\"_name\","
+ "\"timestamp\":\"" + new DateTime(nodeTimestamp, DateTimeZone.UTC).toString() + "\"" + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\""
+ "}," + "},"
+ "\"ccr_auto_follow_stats\":{" + "\"ccr_auto_follow_stats\":{"
+ "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + "," + "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + ","

View File

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -19,11 +20,10 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.NavigableMap; import java.util.NavigableMap;
@ -40,7 +40,7 @@ import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
public class FollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<FollowStatsMonitoringDoc> { public class FollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<FollowStatsMonitoringDoc> {
private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
private ShardFollowNodeTaskStatus status; private ShardFollowNodeTaskStatus status;
@Override @Override
@ -146,7 +146,7 @@ public class FollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<Fol
equalTo( equalTo(
"{" "{"
+ "\"cluster_uuid\":\"_cluster\"," + "\"cluster_uuid\":\"_cluster\","
+ "\"timestamp\":\"" + new DateTime(timestamp, DateTimeZone.UTC).toString() + "\"," + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\","
+ "\"interval_ms\":" + intervalMillis + "," + "\"interval_ms\":" + intervalMillis + ","
+ "\"type\":\"ccr_stats\"," + "\"type\":\"ccr_stats\","
+ "\"source_node\":{" + "\"source_node\":{"
@ -155,7 +155,7 @@ public class FollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<Fol
+ "\"transport_address\":\"_addr\"," + "\"transport_address\":\"_addr\","
+ "\"ip\":\"_ip\"," + "\"ip\":\"_ip\","
+ "\"name\":\"_name\"," + "\"name\":\"_name\","
+ "\"timestamp\":\"" + new DateTime(nodeTimestamp, DateTimeZone.UTC).toString() + "\"" + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\""
+ "}," + "},"
+ "\"ccr_stats\":{" + "\"ccr_stats\":{"
+ "\"remote_cluster\":\"leader_cluster\"," + "\"remote_cluster\":\"leader_cluster\","

View File

@ -6,13 +6,12 @@
package org.elasticsearch.xpack.core.scheduler; package org.elasticsearch.xpack.core.scheduler;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.io.IOException; import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -199,8 +198,8 @@ import static org.elasticsearch.xpack.core.watcher.support.Exceptions.illegalArg
* @author Refactoring from CronTrigger to CronExpression by Aaron Craven * @author Refactoring from CronTrigger to CronExpression by Aaron Craven
*/ */
public class Cron implements ToXContentFragment { public class Cron implements ToXContentFragment {
protected static final TimeZone UTC = DateTimeZone.UTC.toTimeZone(); protected static final TimeZone UTC = TimeZone.getTimeZone(ZoneOffset.UTC);
protected static final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss"); protected static final DateFormatter formatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss");
private static final int SECOND = 0; private static final int SECOND = 0;
private static final int MINUTE = 1; private static final int MINUTE = 1;

View File

@ -10,11 +10,12 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.io.IOException; import java.io.IOException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Objects; import java.util.Objects;
/** /**
@ -27,7 +28,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
private final String subjectDn; private final String subjectDn;
private final String serialNumber; private final String serialNumber;
private final boolean hasPrivateKey; private final boolean hasPrivateKey;
private final DateTime expiry; private final ZonedDateTime expiry;
public CertificateInfo(String path, String format, String alias, boolean hasPrivateKey, X509Certificate certificate) { public CertificateInfo(String path, String format, String alias, boolean hasPrivateKey, X509Certificate certificate) {
Objects.requireNonNull(certificate, "Certificate cannot be null"); Objects.requireNonNull(certificate, "Certificate cannot be null");
@ -37,7 +38,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
this.subjectDn = Objects.requireNonNull(certificate.getSubjectDN().getName()); this.subjectDn = Objects.requireNonNull(certificate.getSubjectDN().getName());
this.serialNumber = certificate.getSerialNumber().toString(16); this.serialNumber = certificate.getSerialNumber().toString(16);
this.hasPrivateKey = hasPrivateKey; this.hasPrivateKey = hasPrivateKey;
this.expiry = new DateTime(certificate.getNotAfter(), DateTimeZone.UTC); this.expiry = certificate.getNotAfter().toInstant().atZone(ZoneOffset.UTC);
} }
public CertificateInfo(StreamInput in) throws IOException { public CertificateInfo(StreamInput in) throws IOException {
@ -47,7 +48,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
this.subjectDn = in.readString(); this.subjectDn = in.readString();
this.serialNumber = in.readString(); this.serialNumber = in.readString();
this.hasPrivateKey = in.readBoolean(); this.hasPrivateKey = in.readBoolean();
this.expiry = new DateTime(in.readLong(), DateTimeZone.UTC); this.expiry = Instant.ofEpochMilli(in.readLong()).atZone(ZoneOffset.UTC);
} }
@Override @Override
@ -58,7 +59,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
out.writeString(subjectDn); out.writeString(subjectDn);
out.writeString(serialNumber); out.writeString(serialNumber);
out.writeBoolean(hasPrivateKey); out.writeBoolean(hasPrivateKey);
out.writeLong(expiry.getMillis()); out.writeLong(expiry.toInstant().toEpochMilli());
} }
public String path() { public String path() {
@ -81,7 +82,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
return serialNumber; return serialNumber;
} }
public DateTime expiry() { public ZonedDateTime expiry() {
return expiry; return expiry;
} }

View File

@ -8,13 +8,15 @@ package org.elasticsearch.xpack.core.ml.action;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.core.ml.action.FlushJobAction.Response; import org.elasticsearch.xpack.core.ml.action.FlushJobAction.Response;
import org.joda.time.DateTime;
import java.time.ZonedDateTime;
import java.util.Date;
public class PostDataFlushResponseTests extends AbstractWireSerializingTestCase<Response> { public class PostDataFlushResponseTests extends AbstractWireSerializingTestCase<Response> {
@Override @Override
protected Response createTestInstance() { protected Response createTestInstance() {
return new Response(randomBoolean(), new DateTime(randomDateTimeZone()).toDate()); return new Response(randomBoolean(), Date.from(ZonedDateTime.now(randomZone()).toInstant()));
} }
@Override @Override

View File

@ -15,10 +15,9 @@ import org.elasticsearch.xpack.core.ml.job.config.DetectionRule;
import org.elasticsearch.xpack.core.ml.job.config.Operator; import org.elasticsearch.xpack.core.ml.job.config.Operator;
import org.elasticsearch.xpack.core.ml.job.config.RuleAction; import org.elasticsearch.xpack.core.ml.job.config.RuleAction;
import org.elasticsearch.xpack.core.ml.job.config.RuleCondition; import org.elasticsearch.xpack.core.ml.job.config.RuleCondition;
import org.joda.time.DateTime;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Clock;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.EnumSet; import java.util.EnumSet;
@ -29,7 +28,7 @@ import static org.hamcrest.Matchers.containsString;
public class ScheduledEventTests extends AbstractSerializingTestCase<ScheduledEvent> { public class ScheduledEventTests extends AbstractSerializingTestCase<ScheduledEvent> {
public static ScheduledEvent createScheduledEvent(String calendarId) { public static ScheduledEvent createScheduledEvent(String calendarId) {
ZonedDateTime start = ZonedDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(randomDateTimeZone()).getMillis()), ZoneOffset.UTC); ZonedDateTime start = Clock.systemUTC().instant().atZone(ZoneOffset.UTC);
return new ScheduledEvent(randomAlphaOfLength(10), start, start.plusSeconds(randomIntBetween(1, 10000)), return new ScheduledEvent(randomAlphaOfLength(10), start, start.plusSeconds(randomIntBetween(1, 10000)),
calendarId, null); calendarId, null);
} }
@ -120,4 +119,4 @@ public class ScheduledEventTests extends AbstractSerializingTestCase<ScheduledEv
ScheduledEvent.LENIENT_PARSER.apply(parser, null); ScheduledEvent.LENIENT_PARSER.apply(parser, null);
} }
} }
} }

View File

@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.AbstractSerializingTestCase;
import org.joda.time.DateTime;
import java.time.ZonedDateTime;
import java.util.Date; import java.util.Date;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
@ -21,9 +21,13 @@ public class DataCountsTests extends AbstractSerializingTestCase<DataCounts> {
randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000),
randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000),
randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000),
new DateTime(randomDateTimeZone()).toDate(), new DateTime(randomDateTimeZone()).toDate(), dateWithRandomTimeZone(), dateWithRandomTimeZone(),
new DateTime(randomDateTimeZone()).toDate(), new DateTime(randomDateTimeZone()).toDate(), dateWithRandomTimeZone(), dateWithRandomTimeZone(),
new DateTime(randomDateTimeZone()).toDate()); dateWithRandomTimeZone());
}
private static Date dateWithRandomTimeZone() {
return Date.from(ZonedDateTime.now(randomZone()).toInstant());
} }
@Override @Override

View File

@ -28,7 +28,6 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.junit.annotations.Network; import org.elasticsearch.test.junit.annotations.Network;
import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.ssl.cert.CertificateInfo; import org.elasticsearch.xpack.core.ssl.cert.CertificateInfo;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
@ -49,6 +48,7 @@ import java.security.Principal;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -563,7 +563,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("PEM")); assertThat(cert.format(), equalTo("PEM"));
assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56")); assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56"));
assertThat(cert.subjectDn(), equalTo("CN=ad-ELASTICSEARCHAD-CA, DC=ad, DC=test, DC=elasticsearch, DC=com")); assertThat(cert.subjectDn(), equalTo("CN=ad-ELASTICSEARCHAD-CA, DC=ad, DC=test, DC=elasticsearch, DC=com"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2029-08-27T16:32:42Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2029-08-27T16:32:42Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -572,7 +572,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56")); assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56"));
assertThat(cert.subjectDn(), equalTo("CN=ad-ELASTICSEARCHAD-CA, DC=ad, DC=test, DC=elasticsearch, DC=com")); assertThat(cert.subjectDn(), equalTo("CN=ad-ELASTICSEARCHAD-CA, DC=ad, DC=test, DC=elasticsearch, DC=com"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2029-08-27T16:32:42Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2029-08-27T16:32:42Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -581,7 +581,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("3151a81eec8d4e34c56a8466a8510bcfbe63cc31")); assertThat(cert.serialNumber(), equalTo("3151a81eec8d4e34c56a8466a8510bcfbe63cc31"));
assertThat(cert.subjectDn(), equalTo("CN=samba4")); assertThat(cert.subjectDn(), equalTo("CN=samba4"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2021-02-14T17:49:11.000Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2021-02-14T17:49:11.000Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -590,7 +590,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("d3850b2b1995ad5f")); assertThat(cert.serialNumber(), equalTo("d3850b2b1995ad5f"));
assertThat(cert.subjectDn(), equalTo("CN=OpenLDAP, OU=Elasticsearch, O=Elastic, L=Mountain View, ST=CA, C=US")); assertThat(cert.subjectDn(), equalTo("CN=OpenLDAP, OU=Elasticsearch, O=Elastic, L=Mountain View, ST=CA, C=US"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2027-07-23T16:41:14Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2027-07-23T16:41:14Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -599,7 +599,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("b9d497f2924bbe29")); assertThat(cert.serialNumber(), equalTo("b9d497f2924bbe29"));
assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Client, OU=elasticsearch, O=org")); assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Client, OU=elasticsearch, O=org"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2019-09-22T18:52:55Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2019-09-22T18:52:55Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -608,7 +608,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("c0ea4216e8ff0fd8")); assertThat(cert.serialNumber(), equalTo("c0ea4216e8ff0fd8"));
assertThat(cert.subjectDn(), equalTo("CN=testnode-client-profile")); assertThat(cert.subjectDn(), equalTo("CN=testnode-client-profile"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2019-09-22T18:52:56Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2019-09-22T18:52:56Z")));
assertThat(cert.hasPrivateKey(), equalTo(false)); assertThat(cert.hasPrivateKey(), equalTo(false));
cert = iterator.next(); cert = iterator.next();
@ -617,7 +617,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("223c736a")); assertThat(cert.serialNumber(), equalTo("223c736a"));
assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node")); assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2045-10-02T09:43:18.000Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2045-10-02T09:43:18.000Z")));
assertThat(cert.hasPrivateKey(), equalTo(true)); assertThat(cert.hasPrivateKey(), equalTo(true));
cert = iterator.next(); cert = iterator.next();
@ -626,7 +626,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("7268203b")); assertThat(cert.serialNumber(), equalTo("7268203b"));
assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node")); assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2045-10-02T09:36:10.000Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2045-10-02T09:36:10.000Z")));
assertThat(cert.hasPrivateKey(), equalTo(true)); assertThat(cert.hasPrivateKey(), equalTo(true));
cert = iterator.next(); cert = iterator.next();
@ -635,7 +635,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("jks")); assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb")); assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb"));
assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node, OU=elasticsearch, O=org")); assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node, OU=elasticsearch, O=org"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2019-09-22T18:52:57.000Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2019-09-22T18:52:57.000Z")));
assertThat(cert.hasPrivateKey(), equalTo(true)); assertThat(cert.hasPrivateKey(), equalTo(true));
cert = iterator.next(); cert = iterator.next();
@ -644,7 +644,7 @@ public class SSLServiceTests extends ESTestCase {
assertThat(cert.format(), equalTo("PKCS12")); assertThat(cert.format(), equalTo("PKCS12"));
assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb")); assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb"));
assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node, OU=elasticsearch, O=org")); assertThat(cert.subjectDn(), equalTo("CN=Elasticsearch Test Node, OU=elasticsearch, O=org"));
assertThat(cert.expiry(), equalTo(DateTime.parse("2019-09-22T18:52:57Z"))); assertThat(cert.expiry(), equalTo(ZonedDateTime.parse("2019-09-22T18:52:57Z")));
assertThat(cert.hasPrivateKey(), equalTo(true)); assertThat(cert.hasPrivateKey(), equalTo(true));
assertFalse(iterator.hasNext()); assertFalse(iterator.hasNext());

View File

@ -34,8 +34,6 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.network.InetAddressHelper; import org.elasticsearch.common.network.InetAddressHelper;
import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.network.NetworkAddress;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import javax.net.ssl.X509ExtendedKeyManager; import javax.net.ssl.X509ExtendedKeyManager;
import javax.net.ssl.X509ExtendedTrustManager; import javax.net.ssl.X509ExtendedTrustManager;
@ -54,6 +52,9 @@ import java.security.SecureRandom;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.sql.Date;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
@ -152,11 +153,11 @@ public class CertGenUtils {
int days, String signatureAlgorithm) int days, String signatureAlgorithm)
throws NoSuchAlgorithmException, CertificateException, CertIOException, OperatorCreationException { throws NoSuchAlgorithmException, CertificateException, CertIOException, OperatorCreationException {
Objects.requireNonNull(keyPair, "Key-Pair must not be null"); Objects.requireNonNull(keyPair, "Key-Pair must not be null");
final DateTime notBefore = new DateTime(DateTimeZone.UTC); final ZonedDateTime notBefore = ZonedDateTime.now(ZoneOffset.UTC);
if (days < 1) { if (days < 1) {
throw new IllegalArgumentException("the certificate must be valid for at least one day"); throw new IllegalArgumentException("the certificate must be valid for at least one day");
} }
final DateTime notAfter = notBefore.plusDays(days); final ZonedDateTime notAfter = notBefore.plusDays(days);
final BigInteger serial = CertGenUtils.getSerial(); final BigInteger serial = CertGenUtils.getSerial();
JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils(); JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
@ -176,7 +177,8 @@ public class CertGenUtils {
JcaX509v3CertificateBuilder builder = JcaX509v3CertificateBuilder builder =
new JcaX509v3CertificateBuilder(issuer, serial, new JcaX509v3CertificateBuilder(issuer, serial,
new Time(notBefore.toDate(), Locale.ROOT), new Time(notAfter.toDate(), Locale.ROOT), subject, keyPair.getPublic()); new Time(Date.from(notBefore.toInstant()), Locale.ROOT), new Time(Date.from(notAfter.toInstant()), Locale.ROOT), subject,
keyPair.getPublic());
builder.addExtension(Extension.subjectKeyIdentifier, false, extUtils.createSubjectKeyIdentifier(keyPair.getPublic())); builder.addExtension(Extension.subjectKeyIdentifier, false, extUtils.createSubjectKeyIdentifier(keyPair.getPublic()));
builder.addExtension(Extension.authorityKeyIdentifier, false, authorityKeyIdentifier); builder.addExtension(Extension.authorityKeyIdentifier, false, authorityKeyIdentifier);