Revert "Cleanup: Remove HaltedClock (elastic/x-pack-elasticsearch#3664)"

This reverts commit elastic/x-pack-elasticsearch@f91c401a60 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@e45d79d643
This commit is contained in:
Alexander Reelsen 2018-01-23 18:08:54 +01:00
parent 697a08e742
commit d065b087ee
3 changed files with 56 additions and 10 deletions

View File

@ -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());
}
}

View File

@ -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"));

View File

@ -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) {