-
Notifications
You must be signed in to change notification settings - Fork 215
/
TopLevelPolicyActionCommandStrategyTest.java
403 lines (375 loc) · 21.5 KB
/
TopLevelPolicyActionCommandStrategyTest.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2021 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.policies.service.persistence.actors.strategies.commands;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.policies.service.persistence.TestConstants.Policy.LABEL;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.placeholders.UnresolvedPlaceholderException;
import org.eclipse.ditto.policies.api.Permission;
import org.eclipse.ditto.policies.model.Label;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.Subject;
import org.eclipse.ditto.policies.model.SubjectExpiry;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.SubjectIdInvalidException;
import org.eclipse.ditto.policies.model.SubjectIssuer;
import org.eclipse.ditto.policies.model.signals.commands.actions.ActivateTokenIntegration;
import org.eclipse.ditto.policies.model.signals.commands.actions.DeactivateTokenIntegration;
import org.eclipse.ditto.policies.model.signals.commands.actions.TopLevelPolicyActionCommand;
import org.eclipse.ditto.policies.model.signals.commands.actions.TopLevelPolicyActionCommandResponse;
import org.eclipse.ditto.policies.model.signals.events.SubjectsDeletedPartially;
import org.eclipse.ditto.policies.model.signals.events.SubjectsModifiedPartially;
import org.eclipse.ditto.policies.service.common.config.DefaultPolicyConfig;
import org.eclipse.ditto.policies.service.common.config.PolicyConfig;
import org.eclipse.ditto.policies.service.persistence.TestConstants;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mutabilitydetector.unittesting.AllowedReason;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorSystem;
/**
* Tests {@link org.eclipse.ditto.policies.service.persistence.actors.strategies.commands.TopLevelPolicyActionCommandStrategy}.
*/
public final class TopLevelPolicyActionCommandStrategyTest extends AbstractPolicyCommandStrategyTest {
private static final Label DUMMY_LABEL = Label.of("-");
private TopLevelPolicyActionCommandStrategy underTest;
@Before
public void setUp() {
final PolicyConfig policyConfig = DefaultPolicyConfig.of(ConfigFactory.load("policy-test"));
final ActorSystem system = ActorSystem.create("test");
underTest = new TopLevelPolicyActionCommandStrategy(policyConfig, system);
}
@Test
public void assertImmutability() {
assertInstancesOf(TopLevelPolicyActionCommandStrategy.class, areImmutable(),
AllowedReason.assumingFields("policyActionCommandStrategyMap")
.areSafelyCopiedUnmodifiableCollectionsWithImmutableElements());
}
@Test
public void activateTokenIntegration() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{policy-entry:label}}:this-is-me");
final SubjectId expectedSubjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, LABEL + ":this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(LABEL)
);
assertModificationResult(underTest, TestConstants.Policy.POLICY, command,
SubjectsModifiedPartially.class,
event -> {
assertThat(event.getModifiedSubjects()).containsOnlyKeys(LABEL);
assertThat(event.getModifiedSubjects().get(LABEL).stream().findFirst().orElseThrow().getId())
.isEqualTo(expectedSubjectId);
assertThat(event.getModifiedSubjects().get(LABEL).stream().findFirst().orElseThrow().getExpiry())
.isNotEmpty();
},
TopLevelPolicyActionCommandResponse.class,
response -> assertThat(response)
.isEqualTo(TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders)));
}
@Test
public void activateTokenIntegrationWhenAtLeastOncePolicyEntryMatches() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{policy-entry:label}}:this-is-me");
final SubjectId expectedSubjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, LABEL + ":this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(LABEL)
);
final Label label = Label.of("non-matching-entry");
final Policy policy = TestConstants.Policy.POLICY.toBuilder()
.forLabel(label)
.setSubject(Subject.newInstance(SubjectId.newInstance("foo:bar")))
.setGrantedPermissions(ResourceKey.newInstance("thing:/"), Permission.READ)
.build();
assertModificationResult(underTest, policy, command,
SubjectsModifiedPartially.class,
event -> {
assertThat(event.getModifiedSubjects()).containsOnlyKeys(LABEL);
assertThat(event.getModifiedSubjects().get(LABEL).stream().findFirst().orElseThrow().getId())
.isEqualTo(expectedSubjectId);
assertThat(event.getModifiedSubjects().get(LABEL).stream().findFirst().orElseThrow().getExpiry())
.isNotEmpty();
},
TopLevelPolicyActionCommandResponse.class,
response -> assertThat(response)
.isEqualTo(TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders)));
}
@Test
public void activateInvalidSubject() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("{{policy-entry:label}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
SubjectIdInvalidException.newBuilder(LABEL).build());
}
@Test
public void activateUnresolvableSubject() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{fn:delete()}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
UnresolvedPlaceholderException.newBuilder("integration:{{fn:delete()}}").build());
}
@Test
public void activateTokenIntegrationWithUnsupportedPlaceholder() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("{{request:subjectId}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
UnresolvedPlaceholderException.newBuilder("{{request:subjectId}}").build());
}
@Test
public void activate2SubjectsIn2Entries() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance(SubjectIssuer.INTEGRATION, LABEL + ":this-is-me");
final SubjectId subjectId2 = SubjectId.newInstance(SubjectIssuer.INTEGRATION, LABEL + ":this-is-me-2");
final Set<SubjectId> subjectIds = Set.of(subjectId, subjectId2);
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final Label label2 = Label.of("label2");
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, subjectIds, expiry, dittoHeaders),
List.of(LABEL, label2)
);
final PolicyEntry entryToCopy = TestConstants.Policy.POLICY.getEntryFor(LABEL).orElseThrow();
final Policy policy = TestConstants.Policy.POLICY.toBuilder()
.forLabel(label2)
.setSubjects(entryToCopy.getSubjects())
.setResources(entryToCopy.getResources())
.build();
assertModificationResult(underTest, policy, command,
SubjectsModifiedPartially.class,
event -> {
assertThat(event.getModifiedSubjects()).containsOnlyKeys(LABEL, label2);
final Map<Label, Collection<Subject>> modifiedSubjects = event.getModifiedSubjects();
final Set<SubjectId> labelSubjectIds =
modifiedSubjects.get(LABEL).stream().map(Subject::getId).collect(Collectors.toSet());
final Set<SubjectId> label2SubjectIds =
modifiedSubjects.get(label2).stream().map(Subject::getId).collect(Collectors.toSet());
assertThat(labelSubjectIds)
.describedAs("Subject IDs in entry <{}>", LABEL)
.isEqualTo(subjectIds);
assertThat(label2SubjectIds)
.describedAs("Subject IDs in entry <{}>", label2)
.isEqualTo(subjectIds);
},
TopLevelPolicyActionCommandResponse.class,
response -> assertThat(response)
.isEqualTo(TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders)));
}
@Test
public void rejectEmptyLabels() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("integration:this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of()
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
command.getNotApplicableException(dittoHeaders));
}
@Test
public void rejectNonexistentLabel() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Label nonexistentLabel = Label.of("nonexistent-label");
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("integration:this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(nonexistentLabel)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
command.getNotApplicableException(dittoHeaders));
}
@Test
public void rejectEntryWithoutThingReadPermission() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Label label = Label.of("empty-entry");
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("integration:this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(label)
);
final Policy policy = TestConstants.Policy.POLICY.toBuilder()
.forLabel(label)
.setSubject(TestConstants.Policy.SUPPORT_SUBJECT)
.setGrantedPermissions(ResourceKey.newInstance("policy:/"), Permission.READ)
.build();
assertErrorResult(underTest, policy, command,
command.getNotApplicableException(dittoHeaders));
}
@Test
public void rejectEntryWithoutAuthenticatedSubjectIdPresentPermission() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final Label label = Label.of("empty-entry");
final Instant expiry = Instant.now().plus(Duration.ofDays(1L));
final SubjectId subjectId = SubjectId.newInstance("integration:this-is-me");
final DittoHeaders dittoHeaders = DittoHeaders.empty();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
ActivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId), expiry,
dittoHeaders),
List.of(label)
);
final Policy policy = TestConstants.Policy.POLICY.toBuilder()
.forLabel(label)
.setSubject(TestConstants.Policy.SUPPORT_SUBJECT)
.setGrantedPermissions(ResourceKey.newInstance("thing:/"), Permission.READ)
.setGrantedPermissions(ResourceKey.newInstance("policy:/"), Permission.READ)
.build();
assertErrorResult(underTest, policy, command, command.getNotApplicableException(dittoHeaders));
}
@Test
public void deactivateTokenIntegration() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final SubjectId subjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{policy-entry:label}}:this-is-me");
final SubjectId expectedSubjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, LABEL + ":this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
DeactivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId),
dittoHeaders),
List.of(LABEL)
);
final Policy policy = TestConstants.Policy.POLICY.toBuilder()
.setSubjectFor(LABEL, PoliciesModelFactory.newSubject(expectedSubjectId,
PoliciesModelFactory.newSubjectType(MessageFormat.format("via action <{0}>",
ActivateTokenIntegration.NAME)),
SubjectExpiry.newInstance(Instant.now().plus(Duration.ofDays(1L)))))
.build();
assertModificationResult(underTest, policy, command,
SubjectsDeletedPartially.class,
event -> {
assertThat(event.getDeletedSubjectIds()).containsOnlyKeys(LABEL);
assertThat(event.getDeletedSubjectIds().get(LABEL))
.containsExactly(expectedSubjectId);
},
TopLevelPolicyActionCommandResponse.class,
response -> assertThat(response)
.isEqualTo(TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders)));
}
@Test
public void deactivateNonexistentSubject() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final SubjectId subjectId =
SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{policy-entry:label}}:this-is-me");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
DeactivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId),
dittoHeaders),
List.of(LABEL)
);
assertModificationResult(underTest, TestConstants.Policy.POLICY, command,
SubjectsDeletedPartially.class,
// nonexistent subjects are present in the event.
event -> assertThat(event.getDeletedSubjectIds()).containsExactly(
Map.entry(LABEL,
Collections.singleton(SubjectId.newInstance("integration:" + LABEL + ":this-is-me")))),
TopLevelPolicyActionCommandResponse.class,
response -> assertThat(response)
.isEqualTo(TopLevelPolicyActionCommandResponse.of(context.getState(), dittoHeaders)));
}
@Test
public void deactivateInvalidSubject() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final SubjectId subjectId = SubjectId.newInstance("{{policy-entry:label}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
DeactivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId),
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
SubjectIdInvalidException.newBuilder(LABEL).build());
}
@Test
public void deactivateUnresolvableSubject() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final SubjectId subjectId = SubjectId.newInstance(SubjectIssuer.INTEGRATION, "{{fn:delete()}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
DeactivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId),
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
UnresolvedPlaceholderException.newBuilder("integration:{{fn:delete()}}").build());
}
@Test
public void deactivateTokenIntegrationWithUnsupportedPlaceholder() {
final CommandStrategy.Context<PolicyId> context = getDefaultContext();
final SubjectId subjectId = SubjectId.newInstance("{{request:subjectId}}");
final DittoHeaders dittoHeaders = buildActivateTokenIntegrationHeaders();
final TopLevelPolicyActionCommand command = TopLevelPolicyActionCommand.of(
DeactivateTokenIntegration.of(context.getState(), DUMMY_LABEL, Collections.singleton(subjectId),
dittoHeaders),
List.of(LABEL)
);
assertErrorResult(underTest, TestConstants.Policy.POLICY, command,
UnresolvedPlaceholderException.newBuilder("{{request:subjectId}}").build());
}
}