Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -19,11 +20,10 @@
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;

import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -38,7 +38,7 @@
import static org.hamcrest.Matchers.nullValue;

public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<AutoFollowStatsMonitoringDoc> {

private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
private AutoFollowStats autoFollowStats;

@Before
Expand Down Expand Up @@ -94,7 +94,7 @@ public void testToXContent() throws IOException {
equalTo(
"{"
+ "\"cluster_uuid\":\"_cluster\","
+ "\"timestamp\":\"" + new DateTime(timestamp, DateTimeZone.UTC).toString() + "\","
+ "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\","
+ "\"interval_ms\":" + intervalMillis + ","
+ "\"type\":\"ccr_auto_follow_stats\","
+ "\"source_node\":{"
Expand All @@ -103,7 +103,7 @@ public void testToXContent() throws IOException {
+ "\"transport_address\":\"_addr\","
+ "\"ip\":\"_ip\","
+ "\"name\":\"_name\","
+ "\"timestamp\":\"" + new DateTime(nodeTimestamp, DateTimeZone.UTC).toString() + "\""
+ "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\""
+ "},"
+ "\"ccr_auto_follow_stats\":{"
+ "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + ","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -19,11 +20,10 @@
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;

import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
Expand All @@ -40,7 +40,7 @@
import static org.mockito.Mockito.mock;

public class FollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<FollowStatsMonitoringDoc> {

private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
private ShardFollowNodeTaskStatus status;

@Override
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testToXContent() throws IOException {
equalTo(
"{"
+ "\"cluster_uuid\":\"_cluster\","
+ "\"timestamp\":\"" + new DateTime(timestamp, DateTimeZone.UTC).toString() + "\","
+ "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\","
+ "\"interval_ms\":" + intervalMillis + ","
+ "\"type\":\"ccr_stats\","
+ "\"source_node\":{"
Expand All @@ -155,7 +155,7 @@ public void testToXContent() throws IOException {
+ "\"transport_address\":\"_addr\","
+ "\"ip\":\"_ip\","
+ "\"name\":\"_name\","
+ "\"timestamp\":\"" + new DateTime(nodeTimestamp, DateTimeZone.UTC).toString() + "\""
+ "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\""
+ "},"
+ "\"ccr_stats\":{"
+ "\"remote_cluster\":\"leader_cluster\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
package org.elasticsearch.xpack.core.scheduler;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.ToXContentFragment;
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.time.ZoneOffset;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -199,8 +198,8 @@
* @author Refactoring from CronTrigger to CronExpression by Aaron Craven
*/
public class Cron implements ToXContentFragment {
protected static final TimeZone UTC = DateTimeZone.UTC.toTimeZone();
protected static final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss");
protected static final TimeZone UTC = TimeZone.getTimeZone(ZoneOffset.UTC);
protected static final DateFormatter formatter = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss");

private static final int SECOND = 0;
private static final int MINUTE = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Objects;

/**
Expand All @@ -27,7 +28,7 @@ public class CertificateInfo implements ToXContentObject, Writeable {
private final String subjectDn;
private final String serialNumber;
private final boolean hasPrivateKey;
private final DateTime expiry;
private final ZonedDateTime expiry;

public CertificateInfo(String path, String format, String alias, boolean hasPrivateKey, X509Certificate certificate) {
Objects.requireNonNull(certificate, "Certificate cannot be null");
Expand All @@ -37,7 +38,7 @@ public CertificateInfo(String path, String format, String alias, boolean hasPriv
this.subjectDn = Objects.requireNonNull(certificate.getSubjectDN().getName());
this.serialNumber = certificate.getSerialNumber().toString(16);
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 {
Expand All @@ -47,7 +48,7 @@ public CertificateInfo(StreamInput in) throws IOException {
this.subjectDn = in.readString();
this.serialNumber = in.readString();
this.hasPrivateKey = in.readBoolean();
this.expiry = new DateTime(in.readLong(), DateTimeZone.UTC);
this.expiry = Instant.ofEpochMilli(in.readLong()).atZone(ZoneOffset.UTC);
}

@Override
Expand All @@ -58,7 +59,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(subjectDn);
out.writeString(serialNumber);
out.writeBoolean(hasPrivateKey);
out.writeLong(expiry.getMillis());
out.writeLong(expiry.toInstant().toEpochMilli());
}

public String path() {
Expand All @@ -81,7 +82,7 @@ public String serialNumber() {
return serialNumber;
}

public DateTime expiry() {
public ZonedDateTime expiry() {
return expiry;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
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> {

@Override
protected Response createTestInstance() {
return new Response(randomBoolean(), new DateTime(randomDateTimeZone()).toDate());
return new Response(randomBoolean(), Date.from(ZonedDateTime.now(randomZone()).toInstant()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
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.RuleCondition;
import org.joda.time.DateTime;

import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand All @@ -29,7 +29,7 @@
public class ScheduledEventTests extends AbstractSerializingTestCase<ScheduledEvent> {

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)),
calendarId, null);
}
Expand Down Expand Up @@ -120,4 +120,4 @@ public void testLenientParser() throws IOException {
ScheduledEvent.LENIENT_PARSER.apply(parser, null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.joda.time.DateTime;

import java.time.ZonedDateTime;
import java.util.Date;

import static org.hamcrest.Matchers.greaterThan;
Expand All @@ -21,9 +21,13 @@ public static DataCounts createTestInstance(String jobId) {
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(),
new DateTime(randomDateTimeZone()).toDate(), new DateTime(randomDateTimeZone()).toDate(),
new DateTime(randomDateTimeZone()).toDate());
dateWithRandomTimeZone(), dateWithRandomTimeZone(),
dateWithRandomTimeZone(), dateWithRandomTimeZone(),
dateWithRandomTimeZone());
}

private static Date dateWithRandomTimeZone() {
return Date.from(ZonedDateTime.now(randomZone()).toInstant());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.test.junit.annotations.Network;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.ssl.cert.CertificateInfo;
import org.joda.time.DateTime;
import org.junit.Before;
import org.mockito.ArgumentCaptor;

Expand All @@ -49,6 +48,7 @@
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -563,7 +563,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("PEM"));
assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56"));
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));

cert = iterator.next();
Expand All @@ -572,7 +572,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("580db8ad52bb168a4080e1df122a3f56"));
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));

cert = iterator.next();
Expand All @@ -581,7 +581,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("3151a81eec8d4e34c56a8466a8510bcfbe63cc31"));
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));

cert = iterator.next();
Expand All @@ -590,7 +590,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("d3850b2b1995ad5f"));
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));

cert = iterator.next();
Expand All @@ -599,7 +599,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("b9d497f2924bbe29"));
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));

cert = iterator.next();
Expand All @@ -608,7 +608,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("c0ea4216e8ff0fd8"));
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));

cert = iterator.next();
Expand All @@ -617,7 +617,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("223c736a"));
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));

cert = iterator.next();
Expand All @@ -626,7 +626,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("7268203b"));
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));

cert = iterator.next();
Expand All @@ -635,7 +635,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("jks"));
assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb"));
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));

cert = iterator.next();
Expand All @@ -644,7 +644,7 @@ public void testReadCertificateInformation() throws Exception {
assertThat(cert.format(), equalTo("PKCS12"));
assertThat(cert.serialNumber(), equalTo("b8b96c37e332cccb"));
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));

assertFalse(iterator.hasNext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
import org.elasticsearch.xpack.core.rollup.RollupField;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.joda.time.DateTimeZone;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -220,7 +220,7 @@ private static List<AggregationBuilder> translateDateHistogram(DateHistogramAggr
rolledDateHisto.interval(source.interval());
}

String timezone = source.timeZone() == null ? DateTimeZone.UTC.toString() : source.timeZone().toString();
String timezone = source.timeZone() == null ? ZoneOffset.UTC.toString() : source.timeZone().toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this get serialized over the wire somewhere? If so we may need to convert it using DateUtils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is read in ValuesSourceAggregationBuilder so I guess it is already taken care of?
with

 if (in.getVersion().before(Version.V_7_0_0)) {
            timeZone = DateUtils.dateTimeZoneToZoneId(in.readOptionalTimeZone());
        } else {
            timeZone = in.readOptionalZoneId();
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

filterConditions.add(new TermQueryBuilder(RollupField.formatFieldName(source,
DateHistogramGroupConfig.TIME_ZONE), timezone));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
import org.elasticsearch.xpack.rollup.RollupJobIdentifierUtils;
import org.elasticsearch.xpack.rollup.RollupRequestTranslator;
import org.elasticsearch.xpack.rollup.RollupResponseTranslator;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -288,7 +288,7 @@ static QueryBuilder rewriteQuery(QueryBuilder builder, Set<RollupJobCaps> jobCap
String fieldName = range.fieldName();
// Many range queries don't include the timezone because the default is UTC, but the query
// builder will return null so we need to set it here
String timeZone = range.timeZone() == null ? DateTimeZone.UTC.toString() : range.timeZone();
String timeZone = range.timeZone() == null ? ZoneOffset.UTC.toString() : range.timeZone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is read from RangeQueryBuilder
does not seem to be using DateUtils
timeZone = in.readOptionalZoneId();
do you think that should be fixed in server?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please open an issue for that, so we do not forget about it


String rewrittenFieldName = rewriteFieldName(jobCaps, RangeQueryBuilder.NAME, fieldName, timeZone);
RangeQueryBuilder rewritten = new RangeQueryBuilder(rewrittenFieldName)
Expand Down
Loading