/
AbstractPolicyCommandStrategy.java
333 lines (294 loc) · 15.3 KB
/
AbstractPolicyCommandStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.policies.persistence.actors.strategies.commands;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.entity.metadata.Metadata;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoDuration;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.policies.Label;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.model.policies.PolicyEntry;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.ResourceKey;
import org.eclipse.ditto.model.policies.Subject;
import org.eclipse.ditto.model.policies.SubjectExpiry;
import org.eclipse.ditto.model.policies.SubjectExpiryInvalidException;
import org.eclipse.ditto.model.policies.Subjects;
import org.eclipse.ditto.services.policies.common.config.PolicyConfig;
import org.eclipse.ditto.services.utils.headers.conditional.ConditionalHeadersValidator;
import org.eclipse.ditto.services.utils.persistentactors.etags.AbstractConditionHeaderCheckingCommandStrategy;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyEntryModificationInvalidException;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyEntryNotAccessibleException;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyModificationInvalidException;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.signals.commands.policies.exceptions.ResourceNotAccessibleException;
import org.eclipse.ditto.signals.commands.policies.exceptions.SubjectNotAccessibleException;
import org.eclipse.ditto.signals.events.policies.PolicyEvent;
/**
* Abstract base class for {@link org.eclipse.ditto.signals.commands.policies.PolicyCommand} strategies.
*
* @param <C> the type of the handled command - of type {@code Command} as also
* {@link org.eclipse.ditto.services.models.policies.commands.sudo.SudoCommand} are handled which are no PolicyCommands.
* @param <E> the type of the emitted events
*/
abstract class AbstractPolicyCommandStrategy<C extends Command<C>, E extends PolicyEvent<?>>
extends AbstractConditionHeaderCheckingCommandStrategy<C, Policy, PolicyId, E> {
private final PolicyExpiryGranularity policyExpiryGranularity;
private final Duration policyExpiryNotificationGranularity;
AbstractPolicyCommandStrategy(final Class<C> theMatchingClass, final PolicyConfig policyConfig) {
super(theMatchingClass);
policyExpiryGranularity = calculateTemporalUnitAndAmount(policyConfig);
policyExpiryNotificationGranularity = policyConfig.getSubjectExpiryNotificationGranularity();
}
static PolicyExpiryGranularity calculateTemporalUnitAndAmount(final PolicyConfig policyConfig) {
final Duration granularity = policyConfig.getSubjectExpiryGranularity();
final long amount;
final Duration days = granularity.truncatedTo(ChronoUnit.DAYS);
if (!days.isZero()) {
amount = days.dividedBy(ChronoUnit.DAYS.getDuration());
return new PolicyExpiryGranularity(ChronoUnit.DAYS, amount, ChronoUnit.MONTHS);
}
final Duration hours = granularity.truncatedTo(ChronoUnit.HOURS);
if (!hours.isZero()) {
amount = hours.dividedBy(ChronoUnit.HOURS.getDuration());
return new PolicyExpiryGranularity(ChronoUnit.HOURS, amount, ChronoUnit.DAYS);
}
final Duration minutes = granularity.truncatedTo(ChronoUnit.MINUTES);
if (!minutes.isZero()) {
amount = minutes.dividedBy(ChronoUnit.MINUTES.getDuration());
return new PolicyExpiryGranularity(ChronoUnit.MINUTES, amount, ChronoUnit.HOURS);
}
final Duration seconds = granularity.truncatedTo(ChronoUnit.SECONDS);
if (!seconds.isZero()) {
amount = seconds.dividedBy(ChronoUnit.SECONDS.getDuration());
return new PolicyExpiryGranularity(ChronoUnit.SECONDS, amount, ChronoUnit.MINUTES);
}
final Duration millis = granularity.truncatedTo(ChronoUnit.MILLIS);
if (!millis.isZero()) {
amount = millis.dividedBy(ChronoUnit.MILLIS.getDuration());
return new PolicyExpiryGranularity(ChronoUnit.MILLIS, amount, ChronoUnit.SECONDS);
}
return new PolicyExpiryGranularity(ChronoUnit.NANOS, granularity.toNanos(), ChronoUnit.MILLIS);
}
@Override
public ConditionalHeadersValidator getValidator() {
return PoliciesConditionalHeadersValidatorProvider.getInstance();
}
@Override
public boolean isDefined(final C command) {
return true;
}
@Override
protected Optional<Metadata> calculateRelativeMetadata(@Nullable final Policy entity, final C command) {
return Optional.empty();
}
/**
* Takes the passed in {@code policyEntries} and potentially adjusts the contained {@link Subjects} in the following
* way:
* <ul>
* <li>If a {@link SubjectExpiry} was contained in the Subject, it is rounded up according to the configured
* {@link PolicyConfig#getSubjectExpiryGranularity()}.</li>
* </ul>
*
* @param policyEntries the PolicyEntries to be potentially adjusted.
* @return the adjusted PolicyEntries or - if not adjusted - the original.
*/
protected Set<PolicyEntry> potentiallyAdjustPolicyEntries(final Iterable<PolicyEntry> policyEntries) {
return StreamSupport.stream(policyEntries.spliterator(), false)
.map(this::potentiallyAdjustPolicyEntry)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
/**
* Takes the passed in {@code policyEntry} and potentially adjusts the contained {@link Subjects} in the following
* way:
* <ul>
* <li>If a {@link SubjectExpiry} was contained in the Subject, it is rounded up according to the configured
* {@link PolicyConfig#getSubjectExpiryGranularity()}.</li>
* </ul>
*
* @param policyEntry the PolicyEntry to be potentially adjusted.
* @return the adjusted PolicyEntry or - if not adjusted - the original.
*/
protected PolicyEntry potentiallyAdjustPolicyEntry(final PolicyEntry policyEntry) {
final Subjects adjustedSubjects = potentiallyAdjustSubjects(policyEntry.getSubjects());
return PolicyEntry.newInstance(policyEntry.getLabel(), adjustedSubjects, policyEntry.getResources());
}
/**
* Takes the passed in {@code subjects} and potentially adjusts the contained {@link Subject}s in the following way:
* <ul>
* <li>If a {@link SubjectExpiry} was contained in the Subject, it is rounded up according to the configured
* {@link PolicyConfig#getSubjectExpiryGranularity()}.</li>
* </ul>
*
* @param subjects the Subjects to be potentially adjusted.
* @return the adjusted Subjects or - if not adjusted - the original.
*/
protected Subjects potentiallyAdjustSubjects(final Subjects subjects) {
return Subjects.newInstance(subjects.stream()
.map(this::potentiallyAdjustSubject)
.collect(Collectors.toList())
);
}
/**
* Takes the passed in {@code subject} and potentially adjusts in the following way:
* <ul>
* <li>If a {@link SubjectExpiry} was contained in the Subject, it is rounded up according to the configured
* {@link PolicyConfig#getSubjectExpiryGranularity()}.</li>
* </ul>
*
* @param subject the Subject to be potentially adjusted.
* @return the adjusted Subject or - if not adjusted - the original.
*/
protected Subject potentiallyAdjustSubject(final Subject subject) {
final Optional<SubjectExpiry> expiryOptional = subject.getExpiry();
if (expiryOptional.isPresent()) {
final SubjectExpiry subjectExpiry = expiryOptional.get();
return Subject.newInstance(subject.getId(), subject.getType(), roundPolicySubjectExpiry(subjectExpiry));
} else {
return subject;
}
}
/**
* Roundgs up the provided {@code expiry} according to the configured
* {@link PolicyConfig#getSubjectExpiryGranularity()}.
*
* @param expiry the SubjectExpiry to round up.
* @return the rounded SubjectExpiry.
*/
protected SubjectExpiry roundPolicySubjectExpiry(final SubjectExpiry expiry) {
final Instant timestamp = expiry.getTimestamp();
final Instant truncated = timestamp.truncatedTo(policyExpiryGranularity.temporalUnit);
final Instant truncatedParent;
if (policyExpiryGranularity.parentTemporalUnit == ChronoUnit.MONTHS) {
final ZonedDateTime truncatedToMonth = ZonedDateTime.ofInstant(timestamp, ZoneId.systemDefault())
.truncatedTo(ChronoUnit.DAYS)
.withDayOfMonth(1);
truncatedParent = truncatedToMonth.toInstant();
} else {
truncatedParent = timestamp.truncatedTo(policyExpiryGranularity.parentTemporalUnit);
}
final long amountBetween = policyExpiryGranularity.temporalUnit.between(truncatedParent, truncated);
final long deltaModulo = amountBetween % policyExpiryGranularity.amount;
if (truncated.equals(timestamp) && deltaModulo == 0) {
// shortcut, when truncating leads to the given expiry timestamp, don't adjust the subjectExpiry at all:
return expiry;
}
final long toAdd = policyExpiryGranularity.amount - deltaModulo;
final Instant roundedUp = truncated.plus(toAdd, policyExpiryGranularity.temporalUnit);
final var roundedUpNotifyBefore = expiry.getNotifyBefore()
.map(notifyBefore -> {
final var roundedUpDuration =
roundUpDuration(notifyBefore.getDuration(), policyExpiryNotificationGranularity);
return notifyBefore.setAmount(roundedUpDuration);
})
.orElse(null);
return SubjectExpiry.newInstance(roundedUp, roundedUpNotifyBefore);
}
/**
* Checks the passed {@code PolicyEntry}s whether there is an an already expired subject in there.
* <p>
* If there is, the Optional will contain an Result of type "error" containing the
* {@link SubjectExpiryInvalidException}.
*
* @param entries the policy entries to check for an expiry date in the past.
* @param dittoHeaders the DittoHeaders to use for building the exception.
* @param command the command which caused the change of the policy entries.
* @param <T> the type of the emitted event
* @return an Optional with ErrorResponse if a subject was invalid, an empty Optional if everything was valid.
*/
protected static <T extends PolicyEvent<?>> Optional<Result<T>> checkForAlreadyExpiredSubject(
final Iterable<PolicyEntry> entries,
final DittoHeaders dittoHeaders, final Command<?> command) {
return StreamSupport.stream(entries.spliterator(), false)
.map(PolicyEntry::getSubjects)
.flatMap(Subjects::stream)
.map(Subject::getExpiry)
.flatMap(Optional::stream)
.filter(SubjectExpiry::isExpired)
.findFirst()
.map(subjectExpiry -> {
final String expiryString = subjectExpiry.getTimestamp().toString();
return ResultFactory.newErrorResult(
SubjectExpiryInvalidException.newBuilderTimestampInThePast(expiryString)
.dittoHeaders(dittoHeaders)
.build(),
command);
});
}
static DittoRuntimeException policyEntryNotFound(final PolicyId policyId, final Label label,
final DittoHeaders dittoHeaders) {
return PolicyEntryNotAccessibleException.newBuilder(policyId, label).dittoHeaders(dittoHeaders).build();
}
static DittoRuntimeException subjectNotFound(final PolicyId policyId, final Label label,
final CharSequence subjectId, final DittoHeaders dittoHeaders) {
return SubjectNotAccessibleException.newBuilder(policyId, label.toString(), subjectId)
.dittoHeaders(dittoHeaders)
.build();
}
static DittoRuntimeException resourceNotFound(final PolicyId policyId, final Label label,
final ResourceKey resourceKey, final DittoHeaders dittoHeaders) {
return ResourceNotAccessibleException.newBuilder(policyId, label, resourceKey.toString())
.dittoHeaders(dittoHeaders)
.build();
}
static DittoRuntimeException policyNotFound(final PolicyId policyId, final DittoHeaders dittoHeaders) {
return PolicyNotAccessibleException.newBuilder(policyId).dittoHeaders(dittoHeaders).build();
}
static DittoRuntimeException policyInvalid(final PolicyId policyId, @Nullable final String message,
final DittoHeaders dittoHeaders) {
return PolicyModificationInvalidException.newBuilder(policyId)
.description(message)
.dittoHeaders(dittoHeaders)
.build();
}
static DittoRuntimeException policyEntryInvalid(final PolicyId policyId, final Label label,
@Nullable final String description, final DittoHeaders dittoHeaders) {
return PolicyEntryModificationInvalidException.newBuilder(policyId, label)
.description(description)
.dittoHeaders(dittoHeaders)
.build();
}
private static Duration roundUpDuration(final Duration duration, final Duration granularity) {
final long granularitySeconds = Math.max(1L, granularity.getSeconds());
final long durationSeconds = Math.max(granularitySeconds, duration.getSeconds());
final long roundedUpSeconds =
((durationSeconds + granularitySeconds - 1L) / granularitySeconds) * granularitySeconds;
return Duration.ofSeconds(roundedUpSeconds);
}
private static class PolicyExpiryGranularity {
private final ChronoUnit temporalUnit;
private final long amount;
private final ChronoUnit parentTemporalUnit;
private PolicyExpiryGranularity(final ChronoUnit temporalUnit,
final long amount,
final ChronoUnit parentTemporalUnit) {
this.temporalUnit = temporalUnit;
this.amount = amount;
this.parentTemporalUnit = parentTemporalUnit;
}
}
}