-
Notifications
You must be signed in to change notification settings - Fork 214
/
ActivateTokenIntegrationStrategy.java
155 lines (136 loc) · 7.75 KB
/
ActivateTokenIntegrationStrategy.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
/*
* 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.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTag;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.policies.api.PoliciesValidator;
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.PolicyBuilder;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.Subject;
import org.eclipse.ditto.policies.model.SubjectAnnouncement;
import org.eclipse.ditto.policies.model.SubjectExpiry;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.SubjectType;
import org.eclipse.ditto.policies.model.signals.commands.actions.ActivateTokenIntegration;
import org.eclipse.ditto.policies.model.signals.commands.actions.ActivateTokenIntegrationResponse;
import org.eclipse.ditto.policies.model.signals.events.PolicyActionEvent;
import org.eclipse.ditto.policies.model.signals.events.SubjectCreated;
import org.eclipse.ditto.policies.model.signals.events.SubjectModified;
import org.eclipse.ditto.policies.model.signals.events.SubjectsModifiedPartially;
import org.eclipse.ditto.policies.service.common.config.PolicyConfig;
import org.apache.pekko.actor.ActorSystem;
/**
* This strategy handles the {@link ActivateTokenIntegration} command.
*/
final class ActivateTokenIntegrationStrategy
extends AbstractPolicyActionCommandStrategy<ActivateTokenIntegration> {
private static final String MESSAGE_PATTERN_SUBJECT_TYPE = "added via action <{0}> at <{1}>";
ActivateTokenIntegrationStrategy(final PolicyConfig policyConfig, final ActorSystem system) {
super(ActivateTokenIntegration.class, policyConfig, system);
}
@Override
protected Result<PolicyActionEvent<?>> doApply(final Context<PolicyId> context,
@Nullable final Policy policy,
final long nextRevision,
final ActivateTokenIntegration command,
@Nullable final Metadata metadata) {
final Policy nonNullPolicy = checkNotNull(policy, "policy");
final DittoHeaders commandHeaders = command.getDittoHeaders();
final PolicyId policyId = context.getState();
final Label label = command.getLabel();
final SubjectExpiry commandSubjectExpiry = command.getSubjectExpiry();
final Optional<PolicyEntry> optionalEntry = nonNullPolicy.getEntryFor(label)
.filter(entry -> command.isApplicable(entry, command.getDittoHeaders().getAuthorizationContext()));
if (optionalEntry.isPresent()) {
final PolicyEntry policyEntry = optionalEntry.get();
final Set<SubjectId> subjectIds = subjectIdFromActionResolver.resolveSubjectIds(policyEntry, command);
final SubjectType subjectType = PoliciesModelFactory.newSubjectType(
MessageFormat.format(MESSAGE_PATTERN_SUBJECT_TYPE, command.getName(), Instant.now().toString()));
final SubjectExpiry adjustedSubjectExpiry = roundPolicySubjectExpiry(commandSubjectExpiry);
final SubjectAnnouncement adjustedSubjectAnnouncement =
roundSubjectAnnouncement(command.getSubjectAnnouncement().orElse(null));
final ActivateTokenIntegration adjustedCommand = ActivateTokenIntegration.of(
command.getEntityId(), command.getLabel(), subjectIds, adjustedSubjectExpiry.getTimestamp(),
commandHeaders);
final Set<Subject> adjustedSubjects = subjectIds.stream()
.map(subjectId -> Subject.newInstance(subjectId, subjectType, adjustedSubjectExpiry,
adjustedSubjectAnnouncement))
.collect(Collectors.toCollection(LinkedHashSet::new));
final PolicyBuilder policyBuilder = nonNullPolicy.toBuilder();
adjustedSubjects.forEach(subject -> policyBuilder.setSubjectFor(label, subject));
// Validation is necessary because activation may add expiry to the policy admin subject.
final Policy newPolicy = policyBuilder.build();
final Optional<Result<PolicyActionEvent<?>>> alreadyExpiredSubject =
checkForAlreadyExpiredSubject(newPolicy, commandHeaders, command);
if (alreadyExpiredSubject.isPresent()) {
return alreadyExpiredSubject.get();
}
final PoliciesValidator validator = PoliciesValidator.newInstance(newPolicy);
if (validator.isValid()) {
final PolicyActionEvent<?> event;
final Instant eventTimestamp = getEventTimestamp();
if (adjustedSubjects.size() == 1) {
final Subject adjustedSubject = adjustedSubjects.stream().findFirst().orElseThrow();
final SubjectId subjectId = adjustedSubject.getId();
if (policyEntry.getSubjects().getSubject(subjectId).isPresent()) {
event = SubjectModified.of(policyId, label, adjustedSubject, nextRevision, eventTimestamp,
commandHeaders, metadata);
} else {
event = SubjectCreated.of(policyId, label, adjustedSubject, nextRevision, eventTimestamp,
commandHeaders, metadata);
}
} else {
event = SubjectsModifiedPartially.of(policyId, Map.of(label, adjustedSubjects), nextRevision,
eventTimestamp, commandHeaders, metadata);
}
final ActivateTokenIntegrationResponse rawResponse =
ActivateTokenIntegrationResponse.of(policyId, label, subjectIds, commandHeaders);
// do not append ETag - activated subjects do not support ETags.
return ResultFactory.newMutationResult(adjustedCommand, event, rawResponse);
} else {
return ResultFactory.newErrorResult(
policyEntryInvalid(policyId, label, validator.getReason().orElse(null), commandHeaders),
command);
}
} else {
return ResultFactory.newErrorResult(command.getNotApplicableException(command.getDittoHeaders()), command);
}
}
@Override
public Optional<EntityTag> previousEntityTag(final ActivateTokenIntegration command,
@Nullable final Policy previousEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
@Override
public Optional<EntityTag> nextEntityTag(final ActivateTokenIntegration command, @Nullable final Policy newEntity) {
// activated subjects do not support entity tag
return Optional.empty();
}
}