From d065b087ee1bde075132346d46712249a96073cc Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 23 Jan 2018 18:08:54 +0100 Subject: [PATCH] Revert "Cleanup: Remove HaltedClock (elastic/x-pack-elasticsearch#3664)" This reverts commit elastic/x-pack-elasticsearch@f91c401a60c28d2c22bf1a3c5c206988cf2d9e1b due to failing tests, like ./gradlew :x-pack-elasticsearch:plugin:watcher:test -Dtests.seed=AE30350FCE96D26D -Dtests.class=org.elasticsearch.xpack.watcher.watch.WatchTests -Dtests.method="testParserSelfGenerated" -Dtests.security.manager=true -Dtests.locale=ja-JP -Dtests.timezone=EET Original commit: elastic/x-pack-elasticsearch@e45d79d6430cbaf4bcae5e90746ccd7469d91d6e --- .../xpack/common/time/HaltedClock.java | 47 +++++++++++++++++++ .../SamlLogoutRequestMessageBuilderTests.java | 11 +++-- .../xpack/watcher/watch/WatchParser.java | 8 ++-- 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 plugin/core/src/main/java/org/elasticsearch/xpack/common/time/HaltedClock.java diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/common/time/HaltedClock.java b/plugin/core/src/main/java/org/elasticsearch/xpack/common/time/HaltedClock.java new file mode 100644 index 00000000000..809a737abb6 --- /dev/null +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/common/time/HaltedClock.java @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.common.time; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; + +public class HaltedClock extends Clock { + + private final DateTime now; + + public HaltedClock(DateTime now) { + this.now = now.toDateTime(DateTimeZone.UTC); + } + + @Override + public ZoneId getZone() { + return ZoneOffset.UTC; + } + + @Override + public Clock withZone(ZoneId zoneId) { + if (zoneId.equals(ZoneOffset.UTC)) { + return this; + } + + throw new IllegalArgumentException("Halted clock time zone cannot be changed"); + } + + @Override + public long millis() { + return now.getMillis(); + } + + @Override + public Instant instant() { + return Instant.ofEpochMilli(now.getMillis()); + } +} diff --git a/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java b/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java index 252cf2f0d1c..88d574de489 100644 --- a/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java +++ b/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java @@ -6,10 +6,11 @@ package org.elasticsearch.xpack.security.authc.saml; import java.time.Clock; -import java.time.Instant; -import java.time.ZoneOffset; +import org.elasticsearch.xpack.common.time.HaltedClock; import org.hamcrest.Matchers; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.junit.Before; import org.opensaml.saml.common.xml.SAMLConstants; import org.opensaml.saml.saml2.core.LogoutRequest; @@ -66,8 +67,8 @@ public class SamlLogoutRequestMessageBuilderTests extends SamlTestCase { "http://idp.example.com/saml/logout/artifact"); idpRole.getSingleLogoutServices().add(sloArtifact); - Clock fixedClock = Clock.fixed(Instant.now(), ZoneOffset.UTC); - final SamlLogoutRequestMessageBuilder builder = new SamlLogoutRequestMessageBuilder(fixedClock, sp, idp, nameId, session); + final DateTime now = DateTime.now(DateTimeZone.UTC); + final SamlLogoutRequestMessageBuilder builder = new SamlLogoutRequestMessageBuilder(new HaltedClock(now), sp, idp, nameId, session); final LogoutRequest logoutRequest = builder.build(); assertThat(logoutRequest, notNullValue()); assertThat(logoutRequest.getReason(), nullValue()); @@ -81,7 +82,7 @@ public class SamlLogoutRequestMessageBuilderTests extends SamlTestCase { assertThat(logoutRequest.getConsent(), nullValue()); assertThat(logoutRequest.getNotOnOrAfter(), nullValue()); assertThat(logoutRequest.getIssueInstant(), notNullValue()); - assertThat(logoutRequest.getIssueInstant().getMillis(), equalTo(fixedClock.millis())); + assertThat(logoutRequest.getIssueInstant(), equalTo(now)); assertThat(logoutRequest.getSessionIndexes(), iterableWithSize(1)); assertThat(logoutRequest.getSessionIndexes().get(0).getSessionIndex(), equalTo(session)); assertThat(logoutRequest.getDestination(), equalTo("http://idp.example.com/saml/logout/redirect")); diff --git a/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java b/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java index bb68d1217ae..7a8dda3fc86 100644 --- a/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java +++ b/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java @@ -29,12 +29,11 @@ import org.elasticsearch.xpack.watcher.support.xcontent.WatcherXContentParser; import org.elasticsearch.xpack.watcher.transform.ExecutableTransform; import org.elasticsearch.xpack.watcher.trigger.Trigger; import org.elasticsearch.xpack.watcher.trigger.TriggerService; +import org.elasticsearch.xpack.common.time.HaltedClock; import org.joda.time.DateTime; import java.io.IOException; import java.time.Clock; -import java.time.Instant; -import java.time.ZoneOffset; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -103,9 +102,8 @@ public class WatchParser extends AbstractComponent { XContentParser parser = null; try { // EMPTY is safe here because we never use namedObject - Clock fixedClock = Clock.fixed(Instant.now(), ZoneOffset.UTC); - parser = new WatcherXContentParser(xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, source), fixedClock, - withSecrets ? cryptoService : null); + parser = new WatcherXContentParser(xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, source), + new HaltedClock(now), withSecrets ? cryptoService : null); parser.nextToken(); return parse(id, includeStatus, parser); } catch (IOException ioe) {