/
MessageServiceImpl.java
419 lines (363 loc) · 16.9 KB
/
MessageServiceImpl.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.rest.api.service.impl;
import static io.gravitee.rest.api.service.impl.MessageServiceImpl.MessageEvent.MESSAGE_SENT;
import io.gravitee.common.http.HttpMethod;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.ApiRepository;
import io.gravitee.repository.management.api.SubscriptionRepository;
import io.gravitee.repository.management.api.search.SubscriptionCriteria;
import io.gravitee.repository.management.model.Api;
import io.gravitee.repository.management.model.Audit;
import io.gravitee.repository.management.model.Subscription;
import io.gravitee.rest.api.model.*;
import io.gravitee.rest.api.model.application.ApplicationListItem;
import io.gravitee.rest.api.model.parameters.Key;
import io.gravitee.rest.api.model.parameters.ParameterReferenceType;
import io.gravitee.rest.api.model.permissions.RoleScope;
import io.gravitee.rest.api.model.permissions.SystemRole;
import io.gravitee.rest.api.model.v4.api.GenericApiModel;
import io.gravitee.rest.api.service.*;
import io.gravitee.rest.api.service.builder.EmailNotificationBuilder;
import io.gravitee.rest.api.service.common.ExecutionContext;
import io.gravitee.rest.api.service.exceptions.*;
import io.gravitee.rest.api.service.notification.ApiHook;
import io.gravitee.rest.api.service.notification.Hook;
import io.gravitee.rest.api.service.notification.NotificationTemplateService;
import io.gravitee.rest.api.service.notification.PortalHook;
import io.gravitee.rest.api.service.v4.ApiTemplateService;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
public class MessageServiceImpl extends AbstractService implements MessageService, InitializingBean {
private static final String API_SUBSCRIBERS = "API_SUBSCRIBERS";
private final Logger LOGGER = LoggerFactory.getLogger(MessageServiceImpl.class);
@Lazy
@Autowired
ApiRepository apiRepository;
@Autowired
MembershipService membershipService;
@Lazy
@Autowired
SubscriptionRepository subscriptionRepository;
@Autowired
PortalNotificationService portalNotificationService;
@Autowired
UserService userService;
@Autowired
AuditService auditService;
@Autowired
EmailService emailService;
@Autowired
ApiTemplateService apiTemplateService;
@Autowired
ApplicationService applicationService;
@Autowired
RoleService roleService;
@Autowired
GroupService groupService;
@Autowired
HttpClientService httpClientService;
@Autowired
SubscriptionService subscriptionService;
@Autowired
private NotificationTemplateService notificationTemplateService;
@Value("${notifiers.webhook.enabled:true}")
private boolean httpEnabled;
@Autowired
private Environment environment;
@Autowired
private ParameterService parameterService;
private List<String> httpWhitelist;
@Override
public void afterPropertiesSet() {
int i = 0;
httpWhitelist = new ArrayList<>();
String whitelistUrl;
while ((whitelistUrl = environment.getProperty("notifiers.webhook.whitelist[" + i + "]")) != null) {
httpWhitelist.add(whitelistUrl);
i++;
}
}
@Override
public int create(final ExecutionContext executionContext, String apiId, MessageEntity message) {
assertMessageNotEmpty(message);
try {
Optional<Api> optionalApi = apiRepository.findById(apiId);
if (!optionalApi.isPresent()) {
throw new ApiNotFoundException(apiId);
}
Api api = optionalApi.get();
int msgSize = send(executionContext, api, message, getRecipientsId(executionContext, api, message));
auditService.createApiAuditLog(executionContext, apiId, Collections.emptyMap(), MESSAGE_SENT, new Date(), null, message);
return msgSize;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to get create a message", ex);
throw new TechnicalManagementException("An error occurs while trying to create a message", ex);
}
}
@Override
public int create(final ExecutionContext executionContext, MessageEntity message) {
assertMessageNotEmpty(message);
int msgSize = send(executionContext, null, message, getRecipientsId(executionContext, message));
auditService.createAuditLog(executionContext, Collections.emptyMap(), MESSAGE_SENT, new Date(), null, message);
return msgSize;
}
private int send(ExecutionContext executionContext, Api api, MessageEntity message, Set<String> recipientsId) {
switch (message.getChannel()) {
case MAIL:
Set<String> mails = getRecipientsEmails(executionContext, recipientsId);
if (!mails.isEmpty()) {
emailService.sendAsyncEmailNotification(
executionContext,
new EmailNotificationBuilder()
.to(EmailService.DEFAULT_MAIL_TO)
.bcc(mails.toArray(new String[0]))
.template(EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_GENERIC_MESSAGE)
.param("message", message.getText())
.param("messageSubject", message.getTitle())
.build()
);
}
return mails.size();
case PORTAL:
Hook hook = api == null ? PortalHook.MESSAGE : ApiHook.MESSAGE;
portalNotificationService.create(executionContext, hook, new ArrayList<>(recipientsId), getPortalParams(api, message));
return recipientsId.size();
case HTTP:
if (!httpEnabled) {
throw new NotifierDisabledException();
}
String url = recipientsId.iterator().next();
environment.getProperty("notifiers.webhook.whitelist");
if (httpWhitelist != null && !httpWhitelist.isEmpty()) {
// Check the provided url is allowed.
if (
httpWhitelist
.stream()
.noneMatch(whitelistUrl ->
whitelistUrl.endsWith("/")
? url.startsWith(whitelistUrl)
: (url.equals(whitelistUrl) || url.startsWith(whitelistUrl + '/'))
)
) {
throw new MessageUrlForbiddenException();
}
}
httpClientService.request(
HttpMethod.POST,
url,
message.getParams(),
getPostMessage(executionContext, api, message, executionContext.getOrganizationId()),
Boolean.valueOf(message.isUseSystemProxy())
);
return 1;
default:
return 0;
}
}
@Override
public Set<String> getRecipientsId(final ExecutionContext context, MessageEntity message) {
if (MessageChannel.HTTP.equals(message.getChannel())) {
return Collections.singleton(message.getRecipient().getUrl());
}
return getRecipientsId(context, null, message);
}
@Override
public Set<String> getRecipientsId(final ExecutionContext context, Api api, MessageEntity message) {
if (message != null && MessageChannel.HTTP.equals(message.getChannel())) {
return Collections.singleton(message.getRecipient().getUrl());
}
assertRecipientsNotEmpty(message);
MessageRecipientEntity recipientEntity = message.getRecipient();
// 2 cases are implemented :
// - global sending (no apiId provided) + scope ENVIRONMENT
// - api consumer (apiId provided) + scope APPLICATION
// the first 2 cases are for admin communication, the last one for the api publisher communication.
try {
final Set<String> recipientIds = new HashSet<>();
// CASE 1 : global sending
if (api == null && RoleScope.ENVIRONMENT.name().equals(recipientEntity.getRoleScope())) {
for (String roleName : recipientEntity.getRoleValues()) {
Optional<RoleEntity> optRole = roleService.findByScopeAndName(
RoleScope.valueOf(recipientEntity.getRoleScope()),
roleName,
context.getOrganizationId()
);
if (optRole.isPresent()) {
recipientIds.addAll(
membershipService
.getMembershipsByReferenceAndRole(
MembershipReferenceType.ENVIRONMENT,
context.getEnvironmentId(),
optRole.get().getId()
)
.stream()
.map(MembershipEntity::getMemberId)
.collect(Collectors.toSet())
);
}
}
}
// CASE 2 : specific api consumers
else if (api != null && RoleScope.APPLICATION.name().equals(recipientEntity.getRoleScope())) {
// Get apps allowed to consume the api
List<String> applicationIds = subscriptionRepository
.search(
SubscriptionCriteria
.builder()
.apis(Collections.singleton(api.getId()))
.statuses(List.of(Subscription.Status.ACCEPTED.name()))
.build()
)
.stream()
.map(Subscription::getApplication)
.collect(Collectors.toList());
// Get members of the applications (direct members and group members)
for (String roleName : recipientEntity.getRoleValues()) {
Optional<RoleEntity> optRole = roleService.findByScopeAndName(
RoleScope.APPLICATION,
roleName,
context.getOrganizationId()
);
if (optRole.isPresent()) {
// get all directs members
recipientIds.addAll(
membershipService
.getMembershipsByReferencesAndRole(
MembershipReferenceType.APPLICATION,
applicationIds,
optRole.get().getId()
)
.stream()
.map(MembershipEntity::getMemberId)
.collect(Collectors.toSet())
);
recipientIds.addAll(this.getIndirectMemberIds(context, applicationIds, optRole.get()));
} else if (roleName.equals(API_SUBSCRIBERS)) {
Collection<SubscriptionEntity> subscriptions = subscriptionService.findByApi(context, api.getId());
List<String> subscribersId = subscriptions
.stream()
.map(SubscriptionEntity::getSubscribedBy)
.distinct()
.collect(Collectors.toList());
recipientIds.addAll(subscribersId);
}
}
}
return recipientIds;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to get recipients", ex);
throw new TechnicalManagementException("An error occurs while trying to get recipients", ex);
}
}
private Set<String> getIndirectMemberIds(ExecutionContext context, List<String> applicationIds, RoleEntity roleEntity) {
if (applicationIds.isEmpty() || SystemRole.PRIMARY_OWNER.name().equals(roleEntity.getName())) {
return Collections.emptySet();
}
// get all indirect members
List<String> applicationsGroups = applicationService
.findByIds(context, applicationIds)
.stream()
.filter(application -> application.getGroups() != null)
.flatMap((ApplicationListItem applicationListItem) -> applicationListItem.getGroups().stream())
.distinct()
.collect(Collectors.toList());
if (applicationsGroups.isEmpty()) {
return Collections.emptySet();
}
return membershipService
.getMembershipsByReferencesAndRole(MembershipReferenceType.GROUP, applicationsGroups, roleEntity.getId())
.stream()
.map(MembershipEntity::getMemberId)
.collect(Collectors.toSet());
}
private Set<String> getRecipientsEmails(ExecutionContext executionContext, Set<String> recipientsId) {
if (recipientsId.isEmpty()) {
return Collections.emptySet();
}
final boolean isTrialInstance = parameterService.findAsBoolean(executionContext, Key.TRIAL_INSTANCE, ParameterReferenceType.SYSTEM);
final Predicate<UserEntity> excludeIfTrialAndNotOptedIn = userEntity -> !isTrialInstance || userEntity.optedIn();
return userService
.findByIds(executionContext, new ArrayList<>(recipientsId))
.stream()
.filter(userEntity -> !StringUtils.isEmpty(userEntity.getEmail()))
.filter(excludeIfTrialAndNotOptedIn)
.map(UserEntity::getEmail)
.collect(Collectors.toSet());
}
private void assertMessageNotEmpty(MessageEntity messageEntity) {
if (messageEntity == null || (StringUtils.isEmpty(messageEntity.getTitle()) && StringUtils.isEmpty(messageEntity.getText()))) {
throw new MessageEmptyException();
}
}
private void assertRecipientsNotEmpty(MessageEntity messageEntity) {
if (
messageEntity == null ||
messageEntity.getRecipient() == null ||
messageEntity.getChannel() == null ||
messageEntity.getRecipient().getRoleScope() == null ||
messageEntity.getRecipient().getRoleValues() == null ||
messageEntity.getRecipient().getRoleValues().isEmpty()
) {
throw new MessageRecipientFormatException();
}
}
private Map<String, Object> getPortalParams(Api api, MessageEntity message) {
Map<String, Object> params = new HashMap<>();
params.put("title", message.getTitle());
params.put("message", message.getText());
if (api != null) {
Api paramApi = new Api();
paramApi.setId(api.getId());
paramApi.setName(api.getName());
paramApi.setVersion(api.getVersion());
params.put("api", paramApi);
}
return params;
}
private String getPostMessage(ExecutionContext executionContext, Api api, MessageEntity message, String organizationId) {
if (message.getText() == null || api == null) {
return message.getText();
}
GenericApiModel genericApiModel = apiTemplateService.findByIdForTemplates(executionContext, api.getId());
Map<String, Object> model = new HashMap<>();
model.put("api", genericApiModel);
return this.notificationTemplateService.resolveInlineTemplateWithParam(
organizationId,
new Date().toString(),
message.getText(),
model
);
}
public enum MessageEvent implements Audit.AuditEvent {
MESSAGE_SENT,
}
}