/** * Returns a new builder for a {@code Policy} based on the given {@code existingPolicy}. * * @param existingPolicy the existing Policy to instantiate the builder with. * @return the new builder. * @throws NullPointerException if {@code existingPolicy} is {@code null}. * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static PolicyBuilder of(final Policy existingPolicy) { checkNotNull(existingPolicy, "existing Policy"); @SuppressWarnings("ConstantConditions") final ImmutablePolicyBuilder result = new ImmutablePolicyBuilder() .setLifecycle(existingPolicy.getLifecycle().orElse(null)) .setRevision(existingPolicy.getRevision().orElse(null)) .setModified(existingPolicy.getModified().orElse(null)); existingPolicy.getId().ifPresent(result::setId); existingPolicy.forEach(result::set); return result; }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { return Optional.ofNullable(policyCreated).map(obj -> obj.toJson(schemaVersion, FieldType.notHidden())); }
@Override protected Optional<PolicyEntry> determineETagEntity(final RetrievePolicyEntry command) { return Optional.ofNullable(policy) .flatMap(p -> p.getEntryFor(command.getLabel())); } }
/** * Creates a response to a {@code RetrievePolicy} command. * * @param policyId the Policy ID of the retrieved Policy. * @param policy the retrieved Policy. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrievePolicyResponse of(final String policyId, final Policy policy, final DittoHeaders dittoHeaders) { return new RetrievePolicyResponse(policyId, HttpStatusCode.OK, checkNotNull(policy, "Policy").toJson( dittoHeaders.getSchemaVersion().orElse(policy.getLatestSchemaVersion())), dittoHeaders); }
@Override public String getId() { return policy.getId().orElseThrow(() -> new NullPointerException("The Policy has no ID!")); }
public PolicyAssert isEqualEqualToButModified(final Policy expected) { assertThat(expected).isNotNull(); assertThat(actual).isNotNull(); assertThat(actual.getModified()).isPresent(); assertThat(actual.getId()).isEqualTo(expected.getId()); assertThat(actual.getEntriesSet()).isEqualTo(expected.getEntriesSet()); return this; }
private void handleSyncPolicyResponse(final Thing syncedThing, final SudoRetrievePolicyResponse response) { log.debug("Retrieved policy response='{}' for thing ID='{}' and policyId='{}' (attempt={}).", response, thingId, policyId, syncAttempts); log.debug("Policy from retrieved policy response is: {}", response.getPolicy()); final Policy policy = response.getPolicy(); final boolean isExpectedPolicyId = policy.getId() .filter(policyId::equals) .isPresent(); if (isExpectedPolicyId) { policyRevision = policy.getRevision().map(PolicyRevision::toLong).orElse(UNKNOWN_REVISION); final Enforcer thePolicyEnforcer = PolicyEnforcers.defaultEvaluator(policy); this.policyEnforcer = thePolicyEnforcer; updateSearchIndexWithPolicy(syncedThing, thePolicyEnforcer); } else { log.warning("Received policy ID <{0}> is not expected ID <{1}>!", policy.getId(), policyId); } }
Policy tmpPolicy = policy; for (final ResourceKey rk : rks) { tmpPolicy = tmpPolicy.removeResourceFor(label, rk); final long policyLength = tmpPolicy.toJsonString().length(); final long resourcesLength = resources.toJsonString() .length() + 5L; if (policy.getEntryFor(label).isPresent()) { final PoliciesValidator validator = PoliciesValidator.newInstance(policy.setResourcesFor(label, resources));
.match(PolicyCreated.class, pc -> policy = pc.getPolicy().toBuilder() .setLifecycle(PolicyLifecycle.ACTIVE) .setRevision(lastSequenceNr()) final PolicyBuilder copyBuilder = policy.toBuilder(); copyBuilder.setAll(pm.getPolicy().getEntriesSet()); // add the new ones policy = copyBuilder.setRevision(lastSequenceNr()) .setModified(pm.getTimestamp().orElse(null)) policy = policy.toBuilder() .setLifecycle(PolicyLifecycle.DELETED) .setRevision(lastSequenceNr()) .match(PolicyEntriesModified.class, pem -> policy = policy.toBuilder() .removeAll(policy.getEntriesSet()) .setAll(pem.getPolicyEntries()) .setRevision(lastSequenceNr()) .match(PolicyEntryCreated.class, pec -> policy = policy.toBuilder() .set(pec.getPolicyEntry()) .setRevision(lastSequenceNr()) .match(PolicyEntryModified.class, pem -> policy = policy.toBuilder() .set(pem.getPolicyEntry()) .setRevision(lastSequenceNr()) .match(PolicyEntryDeleted.class, ped -> policy = policy.toBuilder() .remove(ped.getLabel())
@Override protected void doApply(final RetrievePolicyEntries command) { final RetrievePolicyEntriesResponse response = RetrievePolicyEntriesResponse.of(policyId, policy.getEntriesSet(), command.getDittoHeaders()); sendSuccessResponse(command, response); }
/** * Indicates whether this Policy has the given lifecycle. * * @param lifecycle the lifecycle to be checked for. * @return {@code true} if this Policy has {@code lifecycle} as its lifecycle, {@code false} else. */ default boolean hasLifecycle(@Nullable final PolicyLifecycle lifecycle) { return getLifecycle() .filter(actualLifecycle -> Objects.equals(actualLifecycle, lifecycle)) .isPresent(); }
private static Entry<Enforcer> handleSudoRetrievePolicyResponse(final Object response) { if (response instanceof SudoRetrievePolicyResponse) { final SudoRetrievePolicyResponse sudoRetrievePolicyResponse = (SudoRetrievePolicyResponse) response; final Policy policy = sudoRetrievePolicyResponse.getPolicy(); final long revision = policy.getRevision().map(PolicyRevision::toLong) .orElseThrow(badPolicyResponse("no revision")); return Entry.of(revision, PolicyEnforcers.defaultEvaluator(policy)); } else if (response instanceof PolicyNotAccessibleException) { return Entry.nonexistent(); } else { throw new IllegalStateException("expect SudoRetrievePolicyResponse, got: " + response); } }
final DittoHeaders dittoHeaders = command.getDittoHeaders(); final Optional<PolicyEntry> optionalEntry = policy.getEntryFor(label); if (optionalEntry.isPresent()) { final PolicyEntry policyEntry = optionalEntry.get(); PoliciesValidator.newInstance(policy.removeResourceFor(label, resourceKey));
final DittoHeaders dittoHeaders = command.getDittoHeaders(); final Optional<PolicyEntry> optionalEntry = policy.getEntryFor(label); if (optionalEntry.isPresent()) { final PolicyEntry policyEntry = optionalEntry.get(); if (policyEntry.getSubjects().getSubject(subjectId).isPresent()) { final PoliciesValidator validator = PoliciesValidator.newInstance(policy.removeSubjectFor(label, subjectId));
public PolicyAssert isModifiedAfter(final Instant Instant) { isNotNull(); assertThat(actual.getModified()).isPresent(); final Instant modified = actual.getModified().get(); assertThat(modified.isAfter(Instant)) // .overridingErrorMessage("Expected <%s> to be after <%s> but it was not", modified, Instant) // .isTrue(); return this; }
/** * Removes the resource identified by the specified resource path from this Policy. * * @param label the label identifying the PolicyEntry to modify. * @param resourceType the type of the Resource to remove from the PolicyEntry identified by the {@code label}. * @param resourcePath the path of the Resource to remove from the PolicyEntry identified by the {@code label}. * @return a copy of this Policy with the removed subject. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} or {@code resourceType} is empty. */ default Policy removeResourceFor(final CharSequence label, final String resourceType, final CharSequence resourcePath) { return removeResourceFor(label, PoliciesModelFactory.newResourceKey(resourceType, resourcePath)); }
/** * Removes the specified subject from this Policy. * * @param label the label identifying the PolicyEntry to modify. * @param subject the Subject to remove from the PolicyEntry identified by the {@code label}. * @return a copy of this Policy with the removed subject. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. */ default Policy removeSubjectFor(final CharSequence label, final Subject subject) { checkNotNull(subject, "subject to be removed"); return removeSubjectFor(label, subject.getId()); }
@Override public String getId() { return policy.getId().orElseThrow(() -> new NullPointerException("The Policy has no ID!")); }
/** * Creates a response to a {@code RetrievePolicy} command. * * @param policyId the Policy ID of the retrieved Policy. * @param policy the retrieved Policy. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrievePolicyResponse of(final String policyId, final Policy policy, final DittoHeaders dittoHeaders) { return new RetrievePolicyResponse(policyId, HttpStatusCode.OK, checkNotNull(policy, "Policy").toJson( dittoHeaders.getSchemaVersion().orElse(policy.getLatestSchemaVersion())), dittoHeaders); }
private void handleSyncPolicyResponse(final Thing syncedThing, final SudoRetrievePolicyResponse response) { log.debug("Retrieved policy response='{}' for thing ID='{}' and policyId='{}' (attempt={}).", response, thingId, policyId, syncAttempts); log.debug("Policy from retrieved policy response is: {}", response.getPolicy()); final Policy policy = response.getPolicy(); final boolean isExpectedPolicyId = policy.getId() .filter(policyId::equals) .isPresent(); if (isExpectedPolicyId) { policyRevision = policy.getRevision().map(PolicyRevision::toLong).orElse(UNKNOWN_REVISION); final Enforcer thePolicyEnforcer = PolicyEnforcers.defaultEvaluator(policy); this.policyEnforcer = thePolicyEnforcer; updateSearchIndexWithPolicy(syncedThing, thePolicyEnforcer); } else { log.warning("Received policy ID <{0}> is not expected ID <{1}>!", policy.getId(), policyId); } }