Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHCLOUD-36917] Debug/Update recipients authorization criterion management #3213

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void extract(final Exchange exchange, final JsonObject cloudEventData) {
exchange.setProperty(ExchangeProperty.SUBSCRIBED_BY_DEFAULT, cloudEventData.getBoolean("subscribed_by_default"));
exchange.setProperty(ExchangeProperty.SUBSCRIBERS, subscribers);
exchange.setProperty(ExchangeProperty.UNSUBSCRIBERS, unsubscribers);
exchange.setProperty(ExchangeProperty.AUTHORIZATION_CRITERIA, recipientsAuthorizationCriterion);
exchange.setProperty(ExchangeProperty.RECIPIENTS_AUTHORIZATION_CRITERION, recipientsAuthorizationCriterion);
exchange.setProperty(ExchangeProperty.EMAIL_RECIPIENTS, emails);
exchange.setProperty(ExchangeProperty.EMAIL_SENDER, cloudEventData.getString("email_sender"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ public class ExchangeProperty {

public static final String USE_EMAIL_BOP_V1_SSL = "use_email_bop_V1_ssl";

public static final String AUTHORIZATION_CRITERIA = "authorization_criteria";
public static final String RECIPIENTS_AUTHORIZATION_CRITERION = "recipients_authorization_criterion";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public class RecipientsQuery {

public boolean subscribedByDefault;

public JsonObject authorizationCriteria;
public JsonObject recipientsAuthorizationCriterion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void process(final Exchange exchange) throws JsonProcessingException {
List<RecipientSettings> recipientSettings = exchange.getProperty(ExchangeProperty.RECIPIENT_SETTINGS, List.class);
Set<String> subscribers = exchange.getProperty(ExchangeProperty.SUBSCRIBERS, Set.class);
Set<String> unsubscribers = exchange.getProperty(ExchangeProperty.UNSUBSCRIBERS, Set.class);
JsonObject authorizationCriteria = exchange.getProperty(ExchangeProperty.AUTHORIZATION_CRITERIA, JsonObject.class);
JsonObject recipientsAuthorizationCriterion = exchange.getProperty(ExchangeProperty.RECIPIENTS_AUTHORIZATION_CRITERION, JsonObject.class);

boolean subscribedByDefault = exchange.getProperty(ExchangeProperty.SUBSCRIBED_BY_DEFAULT, boolean.class);
final String orgId = exchange.getProperty(ORG_ID, String.class);
Expand All @@ -37,7 +37,7 @@ public void process(final Exchange exchange) throws JsonProcessingException {
recipientsQuery.orgId = orgId;
recipientsQuery.recipientSettings = Set.copyOf(recipientSettings);
recipientsQuery.subscribedByDefault = subscribedByDefault;
recipientsQuery.authorizationCriteria = authorizationCriteria;
recipientsQuery.recipientsAuthorizationCriterion = recipientsAuthorizationCriterion;

// Serialize the payload.
exchange.getMessage().setBody(objectMapper.writeValueAsString(recipientsQuery));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redhat.cloud.notifications.processors;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import com.redhat.cloud.notifications.models.EmailAggregation;
import com.redhat.cloud.notifications.models.Event;
import com.redhat.cloud.notifications.transformers.BaseTransformer;
Expand All @@ -9,29 +10,29 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class ExternalAuthorizationCriterionExtractor {
import static com.redhat.cloud.notifications.transformers.BaseTransformer.RECIPIENTS_AUTHORIZATION_CRITERION;

public static final String EXTERNAL_AUTHORIZATION_CRITERIA = "recipients_authorization_criterion";
@ApplicationScoped
public class RecipientsAuthorizationCriterionExtractor {

@Inject
BaseTransformer baseTransformer;

@Inject
ObjectMapper objectMapper;

public ExternalAuthorizationCriterion extract(Event event) {
public RecipientsAuthorizationCriterion extract(Event event) {
return extract(baseTransformer.toJsonObject(event));
}

public ExternalAuthorizationCriterion extract(EmailAggregation emailAggregation) {
public RecipientsAuthorizationCriterion extract(EmailAggregation emailAggregation) {
return extract(emailAggregation.getPayload());
}

private ExternalAuthorizationCriterion extract(JsonObject data) {
if (null != data.getJsonObject(BaseTransformer.CONTEXT) && null != data.getJsonObject(BaseTransformer.CONTEXT).getJsonObject(EXTERNAL_AUTHORIZATION_CRITERIA)) {
private RecipientsAuthorizationCriterion extract(JsonObject data) {
if (null != data.getJsonObject(RECIPIENTS_AUTHORIZATION_CRITERION)) {
try {
return objectMapper.convertValue(data.getJsonObject(BaseTransformer.CONTEXT).getJsonObject(EXTERNAL_AUTHORIZATION_CRITERIA), ExternalAuthorizationCriterion.class);
return objectMapper.convertValue(data.getJsonObject(RECIPIENTS_AUTHORIZATION_CRITERION), RecipientsAuthorizationCriterion.class);
} catch (IllegalArgumentException e) {
Log.error("Error parsing authorization criteria", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.redhat.cloud.notifications.processors.drawer;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import com.redhat.cloud.notifications.models.DrawerEntryPayload;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterion;
import com.redhat.cloud.notifications.processors.email.connector.dto.RecipientSettings;
import java.util.Collection;

Expand All @@ -20,6 +20,6 @@ public record DrawerNotificationToConnector(
@JsonProperty("unsubscribers")
Collection<String> unsubscribers,

@JsonProperty("authorization_criteria")
ExternalAuthorizationCriterion externalAuthorizationCriteria
@JsonProperty("recipients_authorization_criterion")
RecipientsAuthorizationCriterion recipientsAuthorizationCriterion
) { }
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.redhat.cloud.notifications.models.Event;
import com.redhat.cloud.notifications.models.IntegrationTemplate;
import com.redhat.cloud.notifications.processors.ConnectorSender;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.RecipientsAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.SystemEndpointTypeProcessor;
import com.redhat.cloud.notifications.processors.email.connector.dto.RecipientSettings;
import com.redhat.cloud.notifications.templates.TemplateService;
Expand Down Expand Up @@ -70,7 +70,7 @@ public class DrawerProcessor extends SystemEndpointTypeProcessor {
BundleRepository bundleRepository;

@Inject
ExternalAuthorizationCriterionExtractor externalAuthorizationCriteriaExtractor;
RecipientsAuthorizationCriterionExtractor recipientsAuthorizationCriterionExtractor;

@Override
public void process(Event event, List<Endpoint> endpoints) {
Expand Down Expand Up @@ -101,7 +101,7 @@ public void process(Event event, List<Endpoint> endpoints) {
drawerEntryPayload,
recipientSettings,
unsubscribers,
externalAuthorizationCriteriaExtractor.extract(event)
recipientsAuthorizationCriterionExtractor.extract(event)
);

connectorSender.send(event, endpoints.getFirst(), JsonObject.mapFrom(drawerNotificationToConnector));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.redhat.cloud.notifications.events.EventWrapperCloudEvent;
import com.redhat.cloud.notifications.ingress.Action;
import com.redhat.cloud.notifications.ingress.Recipient;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import com.redhat.cloud.notifications.models.EmailAggregation;
import com.redhat.cloud.notifications.models.EmailAggregationKey;
import com.redhat.cloud.notifications.models.Endpoint;
Expand All @@ -20,8 +21,7 @@
import com.redhat.cloud.notifications.models.EventType;
import com.redhat.cloud.notifications.models.NotificationsConsoleCloudEvent;
import com.redhat.cloud.notifications.models.SubscriptionType;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterion;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.RecipientsAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.email.aggregators.AbstractEmailPayloadAggregator;
import com.redhat.cloud.notifications.processors.email.aggregators.EmailPayloadAggregatorFactory;
import com.redhat.cloud.notifications.recipients.User;
Expand Down Expand Up @@ -82,7 +82,7 @@ public class EmailAggregator {
private static final String RECIPIENTS_KEY = "recipients";

@Inject
ExternalAuthorizationCriterionExtractor externalAuthorizationCriteriaExtractor;
RecipientsAuthorizationCriterionExtractor recipientsAuthorizationCriterionExtractor;

@ConfigProperty(name = "notifications.aggregation.max-page-size", defaultValue = "100")
int maxPageSize;
Expand Down Expand Up @@ -147,7 +147,7 @@ private void aggregationBasedOnEmailAggregation(EmailAggregationKey aggregationK

Set<String> subscribers = subscribersByEventType.getOrDefault(eventType.getName(), Collections.emptySet());
Set<String> unsubscribers = unsubscribersByEventType.getOrDefault(eventType.getName(), Collections.emptySet());
ExternalAuthorizationCriterion externalAuthorizationCriteria = externalAuthorizationCriteriaExtractor.extract(aggregation);
RecipientsAuthorizationCriterion externalAuthorizationCriteria = recipientsAuthorizationCriterionExtractor.extract(aggregation);

Set<User> recipients = externalRecipientsResolver.recipientUsers(
aggregationKey.getOrgId(),
Expand Down Expand Up @@ -209,7 +209,7 @@ private void aggregationBasedOnEvent(EventAggregationCriteria eventAggregationCr
Set<String> subscribers = subscribersByEventType.getOrDefault(eventType.getName(), Collections.emptySet());
Set<String> unsubscribers = unsubscribersByEventType.getOrDefault(eventType.getName(), Collections.emptySet());
aggregation.setEventWrapper(getEventWrapper(aggregation.getPayload()));
ExternalAuthorizationCriterion externalAuthorizationCriterion = externalAuthorizationCriteriaExtractor.extract(aggregation);
RecipientsAuthorizationCriterion externalAuthorizationCriterion = recipientsAuthorizationCriterionExtractor.extract(aggregation);

Log.info("Start calling external resolver service ");
Set<User> recipients = externalRecipientsResolver.recipientUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.redhat.cloud.notifications.models.Event;
import com.redhat.cloud.notifications.models.InstantEmailTemplate;
import com.redhat.cloud.notifications.processors.ConnectorSender;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.RecipientsAuthorizationCriterionExtractor;
import com.redhat.cloud.notifications.processors.SystemEndpointTypeProcessor;
import com.redhat.cloud.notifications.processors.email.connector.dto.EmailNotification;
import com.redhat.cloud.notifications.processors.email.connector.dto.RecipientSettings;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class EmailProcessor extends SystemEndpointTypeProcessor {
SubscriptionRepository subscriptionRepository;

@Inject
ExternalAuthorizationCriterionExtractor externalAuthorizationCriteriaExtractor;
RecipientsAuthorizationCriterionExtractor recipientsAuthorizationCriterionExtractor;

@Override
public void process(final Event event, final List<Endpoint> endpoints) {
Expand Down Expand Up @@ -123,7 +123,7 @@ public void process(final Event event, final List<Endpoint> endpoints, final boo
subscribers,
unsubscribers,
event.getEventType().isSubscribedByDefault(),
externalAuthorizationCriteriaExtractor.extract(event)
recipientsAuthorizationCriterionExtractor.extract(event)
);

Log.debugf("[org_id: %s] Sending email notification to connector", emailNotification);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.redhat.cloud.notifications.processors.email.connector.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterion;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import java.util.Collection;

/**
Expand All @@ -21,7 +21,7 @@
* event type.
* @param subscribedByDefault true if the event type is subscribed by
* default.
* @param externalAuthorizationCriteria forward received authorization criteria.
* @param recipientsAuthorizationCriterion forward received authorization criterion.
*
*/
public record EmailNotification(
Expand All @@ -33,5 +33,5 @@ public record EmailNotification(
@JsonProperty("subscribers") Collection<String> subscribers,
@JsonProperty("unsubscribers") Collection<String> unsubscribers,
@JsonProperty("subscribed_by_default") boolean subscribedByDefault,
@JsonProperty("external_authorization_criterion") ExternalAuthorizationCriterion externalAuthorizationCriteria
@JsonProperty("recipients_authorization_criterion") RecipientsAuthorizationCriterion recipientsAuthorizationCriterion
) { }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.redhat.cloud.notifications.recipients.recipientsresolver;

import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterion;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import com.redhat.cloud.notifications.recipients.RecipientSettings;
import com.redhat.cloud.notifications.recipients.User;
import com.redhat.cloud.notifications.recipients.recipientsresolver.pojo.RecipientsQuery;
Expand Down Expand Up @@ -59,7 +59,7 @@ private <T> T retryOnError(final CheckedSupplier<T> usersServiceCall) {
}

@CacheResult(cacheName = "recipients-resolver-results")
public Set<User> recipientUsers(String orgId, Set<RecipientSettings> recipientSettings, Set<String> subscribers, Set<String> unsubscribers, boolean subscribedByDefault, ExternalAuthorizationCriterion externalAuthorizationCriteria) {
public Set<User> recipientUsers(String orgId, Set<RecipientSettings> recipientSettings, Set<String> subscribers, Set<String> unsubscribers, boolean subscribedByDefault, RecipientsAuthorizationCriterion recipientsAuthorizationCriterion) {
RecipientsQuery recipientsQuery = new RecipientsQuery();
recipientsQuery.subscribers = Set.copyOf(subscribers);
recipientsQuery.unsubscribers = Set.copyOf(unsubscribers);
Expand All @@ -71,7 +71,7 @@ public Set<User> recipientUsers(String orgId, Set<RecipientSettings> recipientSe

recipientsQuery.recipientSettings = recipientSettingsSet;
recipientsQuery.subscribedByDefault = subscribedByDefault;
recipientsQuery.externalAuthorizationCriteria = externalAuthorizationCriteria;
recipientsQuery.recipientsAuthorizationCriterion = recipientsAuthorizationCriterion;
Set<User> recipientsList = retryOnError(() -> recipientsResolverService.getRecipients(recipientsQuery));
return recipientsList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.redhat.cloud.notifications.recipients.recipientsresolver.pojo;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.redhat.cloud.notifications.processors.ExternalAuthorizationCriterion;
import com.redhat.cloud.notifications.ingress.RecipientsAuthorizationCriterion;
import com.redhat.cloud.notifications.processors.email.connector.dto.RecipientSettings;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand All @@ -24,5 +24,5 @@ public class RecipientsQuery {

public boolean subscribedByDefault;

public ExternalAuthorizationCriterion externalAuthorizationCriteria;
public RecipientsAuthorizationCriterion recipientsAuthorizationCriterion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class BaseTransformer {
public static final String PAYLOAD = "payload";
public static final String SOURCE = "source";
public static final String TIMESTAMP = "timestamp";
public static final String RECIPIENTS_AUTHORIZATION_CRITERION = "recipients_authorization_criterion";

/**
* Transforms the given event into a {@link JsonObject}.
Expand All @@ -45,6 +46,9 @@ public JsonObject toJsonObject(final Event event) {
message.put(BUNDLE, action.getBundle());
message.put(CONTEXT, JsonObject.mapFrom(action.getContext()));
message.put(EVENT_TYPE, action.getEventType());
if (action.getRecipientsAuthorizationCriterion() != null) {
message.put(RECIPIENTS_AUTHORIZATION_CRITERION, JsonObject.mapFrom(action.getRecipientsAuthorizationCriterion()));
}
message.put(
EVENTS,
new JsonArray(
Expand Down
Loading
Loading