Skip to content

Commit

Permalink
Throw special thing exceptions when policyEnforcer could not be loaded
Browse files Browse the repository at this point in the history
* This is the case when the thing exists but it's corresponding policy does not

Signed-off-by: Yannic Klem <Yannic.Klem@bosch.io>
  • Loading branch information
Yannic92 committed Jun 28, 2022
1 parent f96518e commit 0de1b4c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
Expand Up @@ -304,41 +304,6 @@ static JsonObject getJsonViewForCommandResponse(final JsonObject responseEntity,
THING_QUERY_COMMAND_RESPONSE_ALLOWLIST, Permissions.newInstance(Permission.READ));
}

/**
* Create error for commands to an existing thing whose policy is deleted.
*
* @param thingCommand the triggering command.
* @param thingId ID of the thing.
* @param policyId ID of the deleted policy.
* @return an appropriate error.
*/
private static DittoRuntimeException errorForExistingThingWithDeletedPolicy(final ThingCommand<?> thingCommand,
final ThingId thingId, final CharSequence policyId) {

//TODO: Yannic check when this exception should be thrown

final var message = String.format(
"The Thing with ID '%s' could not be accessed as its Policy with ID '%s' is not or no longer existing.",
thingId, policyId);
final var description = String.format(
"Recreate/create the Policy with ID '%s' in order to get access to the Thing again.",
policyId);

if (thingCommand instanceof ThingModifyCommand) {
return ThingNotModifiableException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
} else {
return ThingNotAccessibleException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
}
}

/**
* Create error due to failing to execute a thing-command in the expected way.
*
Expand Down
Expand Up @@ -63,7 +63,9 @@
import org.eclipse.ditto.things.model.signals.commands.exceptions.PolicyInvalidException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotCreatableException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotModifiableException;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;

import akka.actor.ActorRef;
import akka.actor.Props;
Expand Down Expand Up @@ -133,7 +135,51 @@ protected CompletionStage<Optional<PolicyEnforcer>> loadPolicyEnforcer(final Sig
return loadPolicyEnforcerForCreateThing(createThing)
.thenApply(Optional::of);
} else {
return super.loadPolicyEnforcer(signal);
return providePolicyIdForEnforcement(signal)
.thenCompose(policyId -> providePolicyEnforcer(policyId)
.thenApply(policyEnforcer -> {
if (policyId != null &&
policyEnforcer == null &&
signal instanceof ThingCommand<?> thingCommand) {
throw errorForExistingThingWithDeletedPolicy(thingCommand, policyId);
} else {
return Optional.ofNullable(policyEnforcer);
}
}));
}
}

/**
* Create error for commands to an existing thing whose policy is deleted.
*
* @param thingCommand the triggering command.
* @param policyId ID of the deleted policy.
* @return an appropriate error.
*/
private static DittoRuntimeException errorForExistingThingWithDeletedPolicy(final ThingCommand<?> thingCommand,
final PolicyId policyId) {

final ThingId thingId = thingCommand.getEntityId();

final var message = String.format(
"The Thing with ID '%s' could not be accessed as its Policy with ID '%s' is not or no longer existing.",
thingId, policyId);
final var description = String.format(
"Recreate/create the Policy with ID '%s' in order to get access to the Thing again.",
policyId);

if (thingCommand instanceof ThingModifyCommand) {
return ThingNotModifiableException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
} else {
return ThingNotAccessibleException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
}
}

Expand Down

0 comments on commit 0de1b4c

Please sign in to comment.