Search in sources :

Example 11 with AuditEvent

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) SQLException(java.sql.SQLException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) IOException(java.io.IOException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) AttributesModuleImplApi(cz.metacentrum.perun.core.implApi.modules.attributes.AttributesModuleImplApi)

Example 12 with AuditEvent

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);
    }
}
Also used : InvitationSentEvent(cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent) MessagingException(javax.mail.MessagingException) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) MailException(org.springframework.mail.MailException)

Example 13 with AuditEvent

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;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) AttributeRemovedForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeRemovedForGroup) DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) AllAttributesRemovedForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AllAttributesRemovedForGroup) MemberExpiredInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberExpiredInGroup) MemberValidatedInGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.MemberValidatedInGroup) IndirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.IndirectMemberAddedToGroup) AttributeSetForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeSetForGroup) AttributeSetForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeSetForGroup) AttributeChangedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForUser) AttributeRemovedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeRemovedForUser) User(cz.metacentrum.perun.core.api.User) AttributeSetForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeSetForUser) AllAttributesRemovedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AllAttributesRemovedForUser) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) Attribute(cz.metacentrum.perun.core.api.Attribute) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AttributeRemovedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeRemovedForUser) AllAttributesRemovedForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AllAttributesRemovedForGroup) AllAttributesRemovedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AllAttributesRemovedForUser) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) ArrayList(java.util.ArrayList) List(java.util.List) AttributeChangedForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForUser) AttributeSetForUser(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeSetForUser) AttributeRemovedForGroup(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeRemovedForGroup)

Example 14 with AuditEvent

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));
}
Also used : User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) EnrichedSponsorship(cz.metacentrum.perun.core.api.EnrichedSponsorship) SponsorshipExpirationInAMonth(cz.metacentrum.perun.audit.events.ExpirationNotifScheduler.SponsorshipExpirationInAMonth) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) LocalDate(java.time.LocalDate) Member(cz.metacentrum.perun.core.api.Member) Test(org.junit.Test)

Example 15 with AuditEvent

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;
}
Also used : AttributeChangedForFacility(cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForFacility) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent)

Aggregations

AuditEvent (cz.metacentrum.perun.audit.events.AuditEvent)33 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)11 Attribute (cz.metacentrum.perun.core.api.Attribute)9 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)9 AttributeSetForUser (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeSetForUser)8 AttributeRemovedForUser (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeRemovedForUser)7 User (cz.metacentrum.perun.core.api.User)7 AttributeChangedForUser (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForUser)5 AllAttributesRemovedForUser (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AllAttributesRemovedForUser)4 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)4 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)4 EnrichedSponsorship (cz.metacentrum.perun.core.api.EnrichedSponsorship)3 Facility (cz.metacentrum.perun.core.api.Facility)3 Member (cz.metacentrum.perun.core.api.Member)3 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 AttributeChangedForFacility (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForFacility)2 AttributeChangedForResourceAndMember (cz.metacentrum.perun.audit.events.AttributesManagerEvents.AttributeChangedForResourceAndMember)2