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

#1844 fix resolving revision or timestamp from the future was not directly denied #1845

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -57,7 +57,7 @@ public final class ConnectionHistoryNotAccessibleException extends DittoRuntimeE

private static final String DEFAULT_DESCRIPTION =
"Check if the ID of your requested Connection was correct, you have sufficient permissions and ensure that the " +
"asked for revision/timestamp does not exceed the history-retention-duration.";
"asked for revision/timestamp does not exceed the history-retention-duration or is from the future.";

private static final long serialVersionUID = -998877665544332221L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Cancellable;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.pattern.Patterns;
import org.apache.pekko.persistence.RecoveryCompleted;
import org.apache.pekko.persistence.RecoveryTimedOut;
import org.apache.pekko.persistence.SaveSnapshotFailure;
import org.apache.pekko.persistence.SaveSnapshotSuccess;
import org.apache.pekko.persistence.SnapshotOffer;
import org.apache.pekko.persistence.SnapshotProtocol;
import org.apache.pekko.persistence.SnapshotSelectionCriteria;
import org.apache.pekko.persistence.query.EventEnvelope;
import org.apache.pekko.stream.javadsl.Sink;
import org.bson.BsonDocument;
import org.eclipse.ditto.base.api.commands.sudo.SudoCommand;
import org.eclipse.ditto.base.model.entity.id.EntityId;
Expand All @@ -40,11 +53,11 @@
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.events.EventsourcedEvent;
import org.eclipse.ditto.base.model.signals.events.GlobalEventRegistry;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.internal.utils.pekko.PingCommand;
import org.eclipse.ditto.internal.utils.pekko.PingCommandResponse;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.internal.utils.persistence.SnapshotAdapter;
import org.eclipse.ditto.internal.utils.persistence.mongo.AbstractMongoEventAdapter;
import org.eclipse.ditto.internal.utils.persistence.mongo.DittoBsonJson;
Expand All @@ -64,19 +77,6 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;

import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Cancellable;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.pattern.Patterns;
import org.apache.pekko.persistence.RecoveryCompleted;
import org.apache.pekko.persistence.RecoveryTimedOut;
import org.apache.pekko.persistence.SaveSnapshotFailure;
import org.apache.pekko.persistence.SaveSnapshotSuccess;
import org.apache.pekko.persistence.SnapshotOffer;
import org.apache.pekko.persistence.SnapshotProtocol;
import org.apache.pekko.persistence.SnapshotSelectionCriteria;
import org.apache.pekko.persistence.query.EventEnvelope;
import org.apache.pekko.stream.javadsl.Sink;
import scala.Option;

/**
Expand Down Expand Up @@ -377,10 +377,28 @@ private void handleHistoricalRetrieveCommand(final C command) {
.ofNullable(command.getDittoHeaders().get(DittoHeaderDefinition.AT_HISTORICAL_REVISION.getKey()))
.map(Long::parseLong)
.orElseGet(this::lastSequenceNr);
if (atHistoricalRevision > lastSequenceNr()) {
getSender().tell(
newHistoryNotAccessibleExceptionBuilder(atHistoricalRevision)
.dittoHeaders(command.getDittoHeaders())
.build(),
getSelf()
);
return;
}
final Instant atHistoricalTimestamp = Optional
.ofNullable(command.getDittoHeaders().get(DittoHeaderDefinition.AT_HISTORICAL_TIMESTAMP.getKey()))
.map(Instant::parse)
.orElse(Instant.EPOCH);
if (atHistoricalTimestamp.isAfter(Instant.now())) {
getSender().tell(
newHistoryNotAccessibleExceptionBuilder(atHistoricalTimestamp)
.dittoHeaders(command.getDittoHeaders())
.build(),
getSelf()
);
return;
}

loadSnapshot(persistenceId(), SnapshotSelectionCriteria.create(
atHistoricalRevision,
Expand Down Expand Up @@ -466,7 +484,7 @@ private void historicalRetrieveHandleLoadSnapshotResult(final C command,
}
})
.reduce((ewe1, ewe2) -> new EntityWithEvent(
eventStrategy.handle(ewe2.event, ewe2.entity, ewe2.revision),
eventStrategy.handle(ewe2.event, ewe1.entity, ewe2.revision),
ewe2.event
))
.runWith(Sink.foreach(entityWithEvent ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class PolicyHistoryNotAccessibleException extends DittoRuntimeExcep

private static final String DEFAULT_DESCRIPTION =
"Check if the ID of your requested Policy was correct, you have sufficient permissions and ensure that the " +
"asked for revision/timestamp does not exceed the history-retention-duration.";
"asked for revision/timestamp does not exceed the history-retention-duration or is from the future.";

private static final long serialVersionUID = 4242422323239998882L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class ThingHistoryNotAccessibleException extends DittoRuntimeExcept

private static final String DEFAULT_DESCRIPTION =
"Check if the ID of your requested Thing was correct, you have sufficient permissions and ensure that the " +
"asked for revision/timestamp does not exceed the history-retention-duration.";
"asked for revision/timestamp does not exceed the history-retention-duration or is from the future.";

private static final long serialVersionUID = 8883736111094383234L;

Expand Down
Loading