use of cz.metacentrum.perun.audit.events.AuditEvent in project perun by CESNET.
the class Auditer method checkRegisteredAttributesModules.
/**
* Takes a list of input messages (messages) and list of already resolved messages (alreadyResolvedMessages). Then it process
* these messages by all registered modules and for every such module can generate new resolved messages. If any resolved message was
* generated by processing input messages it will check if there is a cycle (module X is generating message for module Y and otherwise)
* and if not, it will continue by processing these new input messages by calling itself in recursion. It also updates the list of
* already resolved messages and send this updated list too. If no new resolved message was generated, recursion will stop and
* returns the list of all resolved messages.
*
* This method is recursive.
*
* When cycle is detected, method will end the recursion and return already generated resolved messages. Also inform about
* this state to the error log. In this case list of resolved messages can be incomplete.
*
* @param session perun session
* @param messages input messages which can cause generating of new resolving messages by registered attr modules
* @param alreadyResolvedMessages LinkedHashSet of all already generated audit messages (used for checking a cycle between two or more registered modules)
*
* @return LinkedHashSet of all resolved messages generated by registered attr modules (unique set with preserved insertion order)
*/
private LinkedHashSet<AuditerMessage> checkRegisteredAttributesModules(PerunSession session, Collection<AuditerMessage> messages, LinkedHashSet<AuditerMessage> alreadyResolvedMessages) {
LinkedHashSet<AuditerMessage> addedResolvedMessages = new LinkedHashSet<>();
for (AuditerMessage message : messages) {
for (AttributesModuleImplApi attributesModuleImplApi : registeredAttributesModules) {
log.info("Message {} is given to module {}", message, attributesModuleImplApi.getClass().getSimpleName());
try {
List<AuditEvent> auditEvents = attributesModuleImplApi.resolveVirtualAttributeValueChange((PerunSessionImpl) session, message.getEvent());
for (AuditEvent auditEvent : auditEvents) {
AuditerMessage msg = new AuditerMessage(session, auditEvent);
// do not store message duplicates created by this first pass through the message processing cycle
addedResolvedMessages.add(msg);
}
} catch (InternalErrorException | WrongAttributeAssignmentException | AttributeNotExistsException | WrongReferenceAttributeValueException ex) {
log.error("Error when auditer trying to resolve messages in modules.", ex);
} catch (Exception ex) {
log.error("An unexpected exception happened when trying to resolve message: {} in module {}, exception: {}", message, attributesModuleImplApi.getAttributeDefinition().getFriendlyName(), ex);
}
}
}
// We still have new resolving messages, so we need to detect if there isn't cycle and if not, continue the recursion
if (!addedResolvedMessages.isEmpty()) {
// Cycle detection
Iterator<AuditerMessage> msgIterator = addedResolvedMessages.iterator();
while (msgIterator.hasNext()) {
AuditerMessage addedResolvedMessage = msgIterator.next();
// If message is already present in the list of resolving messages, remove it from the list of added resolved messages, log it and continue
if (alreadyResolvedMessages.contains(addedResolvedMessage)) {
log.error("There is a cycle for resolving message {}. This message won't be processed more than once!", addedResolvedMessage);
msgIterator.remove();
}
}
// Update list of already resolved messages
alreadyResolvedMessages.addAll(addedResolvedMessages);
// Continue of processing newly generated messages
addedResolvedMessages.addAll(checkRegisteredAttributesModules(session, addedResolvedMessages, alreadyResolvedMessages));
}
// Nothing new to resolve, we can return last state
return addedResolvedMessages;
}
use of cz.metacentrum.perun.audit.events.AuditEvent in project perun by CESNET.
the class MailManagerImpl method sendInvitationMail.
/**
* Send invitation email to one user
*/
private void sendInvitationMail(PerunSession sess, Vo vo, Group group, String email, String language, MimeMessage message, Application app) throws RegistrarException {
try {
mailSender.send(message);
User sendingUser = sess.getPerunPrincipal().getUser();
AuditEvent event = new InvitationSentEvent(sendingUser, email, language, group, vo);
sess.getPerun().getAuditer().log(sess, event);
log.info("[MAIL MANAGER] Sending mail: USER_INVITE to: {} / {} / {}", message.getAllRecipients(), app.getVo(), app.getGroup());
} catch (MailException | MessagingException ex) {
log.error("[MAIL MANAGER] Sending mail: USER_INVITE failed because of exception.", ex);
throw new RegistrarException("Unable to send e-mail.", ex);
}
}
use of cz.metacentrum.perun.audit.events.AuditEvent in project perun by CESNET.
the class urn_perun_user_attribute_def_virt_eduPersonScopedAffiliations method resolveVirtualAttributeValueChange.
@Override
public List<AuditEvent> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, AuditEvent message) throws WrongReferenceAttributeValueException, AttributeNotExistsException, WrongAttributeAssignmentException {
// generic handling
List<AuditEvent> resolvingMessages = super.resolveVirtualAttributeValueChange(perunSession, message);
if (message instanceof AttributeSetForUser && ((AttributeSetForUser) message).getAttribute().getFriendlyName().equals(getSecondarySourceAttributeFriendlyName())) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), ((AttributeSetForUser) message).getUser()));
} else if (message instanceof AttributeRemovedForUser && ((AttributeRemovedForUser) message).getAttribute().getFriendlyName().equals(getSecondarySourceAttributeFriendlyName())) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), ((AttributeRemovedForUser) message).getUser()));
} else if (message instanceof AllAttributesRemovedForUser) {
boolean skip = false;
try {
AttributeDefinition sourceExists = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getSecondarySourceAttributeName());
User user = perunSession.getPerunBl().getUsersManagerBl().getUserById(perunSession, ((AllAttributesRemovedForUser) message).getUser().getId());
} catch (AttributeNotExistsException | UserNotExistsException ex) {
// silently skip this event, since source attribute couldn't be between deleted
// or user no longer exist
skip = true;
}
if (!skip) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), ((AllAttributesRemovedForUser) message).getUser()));
}
}
if (message instanceof AttributeSetForGroup && !VosManager.MEMBERS_GROUP.equals(((AttributeSetForGroup) message).getGroup().getName()) && ((AttributeSetForGroup) message).getAttribute().getName().equals(getTertiarySourceAttributeName())) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
// TODO - get only active group users, since expired are not affected by current group affiliations
List<User> users = perunSession.getPerunBl().getGroupsManagerBl().getGroupUsers(perunSession, ((AttributeSetForGroup) message).getGroup());
for (User user : users) {
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), user));
}
} else if (message instanceof AttributeRemovedForGroup && !VosManager.MEMBERS_GROUP.equals(((AttributeRemovedForGroup) message).getGroup().getName()) && ((AttributeRemovedForGroup) message).getAttribute().getName().equals(getTertiarySourceAttributeName())) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
// TODO - get only active group users, since expired are not affected by current group affiliations
List<User> users = perunSession.getPerunBl().getGroupsManagerBl().getGroupUsers(perunSession, ((AttributeRemovedForGroup) message).getGroup());
for (User user : users) {
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), user));
}
} else if (message instanceof AllAttributesRemovedForGroup && !VosManager.MEMBERS_GROUP.equals(((AllAttributesRemovedForGroup) message).getGroup().getName())) {
boolean skip = false;
try {
AttributeDefinition sourceExists = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getTertiarySourceAttributeName());
Group group = perunSession.getPerunBl().getGroupsManagerBl().getGroupById(perunSession, ((AllAttributesRemovedForGroup) message).getGroup().getId());
} catch (AttributeNotExistsException | GroupNotExistsException ex) {
// silently skip this event, since source attribute couldn't be between deleted
// or group no longer exist.
skip = true;
}
if (!skip) {
AttributeDefinition attributeDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, getDestinationAttributeName());
// TODO - get only active group users, since expired are not affected by current group affiliations
List<User> users = perunSession.getPerunBl().getGroupsManagerBl().getGroupUsers(perunSession, ((AllAttributesRemovedForGroup) message).getGroup());
for (User user : users) {
resolvingMessages.add(new AttributeChangedForUser(new Attribute(attributeDefinition), user));
}
}
}
return resolvingMessages;
}
use of cz.metacentrum.perun.audit.events.AuditEvent in project perun by CESNET.
the class ExpirationNotifSchedulerTest method testSponsorshipExpirationIsAuditedAMonthBefore.
@Test
public void testSponsorshipExpirationIsAuditedAMonthBefore() throws Exception {
System.out.println(CLASS_NAME + "testSponsorshipExpirationIsAuditedAMonthBefore");
LocalDate today = LocalDate.of(2020, 2, 2);
when(spyScheduler.getCurrentLocalDate()).thenReturn(today);
Member member = setUpMember();
User sponsor = perun.getUsersManagerBl().getUserByMember(session, setUpMember());
AuthzResolverBlImpl.setRole(session, sponsor, vo, Role.SPONSOR);
LocalDate nextDay = today.plusDays(28);
perun.getMembersManagerBl().setSponsorshipForMember(session, member, sponsor, nextDay);
ReflectionTestUtils.invokeMethod(spyScheduler, "auditSponsorshipExpirations");
EnrichedSponsorship es = new EnrichedSponsorship();
es.setSponsoredMember(perun.getMembersManagerBl().getMemberById(session, member.getId()));
es.setSponsor(perun.getUsersManagerBl().getUserById(session, sponsor.getId()));
AuditEvent expectedEvent = new SponsorshipExpirationInAMonth(es);
verify(auditerMock).log(any(), eq(expectedEvent));
}
use of cz.metacentrum.perun.audit.events.AuditEvent in project perun by CESNET.
the class urn_perun_facility_attribute_def_virt_voShortNames method resolveEvent.
private List<AuditEvent> resolveEvent(PerunSessionImpl sess, Facility facility) throws AttributeNotExistsException, WrongAttributeAssignmentException {
List<AuditEvent> resolvingMessages = new ArrayList<>();
AttributeDefinition attributeDefinition = sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, NS_FACILITY_ATTR_VIRT + ":voShortNames");
resolvingMessages.add(new AttributeChangedForFacility(new Attribute(attributeDefinition), facility));
return resolvingMessages;
}
Aggregations