Search in sources :

Example 1 with UserProfile

use of org.apache.airavata.model.user.UserProfile in project airavata by apache.

the class RegistryServiceDBEventHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    logger.info("RegistryServiceDBEventHandler | Received a new message!");
    try {
        // construct dbeventmessage thrift datamodel
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        logger.info("RegistryService received db-event-message from publisher: " + dbEventMessage.getPublisherService());
        // get publisher context
        DBEventPublisherContext publisherContext = dbEventMessage.getMessageContext().getPublisher().getPublisherContext();
        logger.info("RegistryService, Replicated Entity: " + publisherContext.getEntityType());
        // this try-block is mainly for catching DuplicateEntryException
        try {
            // check type of entity-type
            switch(publisherContext.getEntityType()) {
                // Gateway related operations
                case TENANT:
                    {
                        // construct gateway datamodel from message
                        Gateway gateway = new Gateway();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), gateway);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addGateway in Registry.");
                                    registryClient.addGateway(gateway);
                                    logger.info("addGateway Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    if (!registryClient.isGatewayExist(gateway.getGatewayId())) {
                                        logger.info("Gateway doesn't exist so adding instead of updating.");
                                        registryClient.addGateway(gateway);
                                    } else {
                                        registryClient.updateGateway(gateway.getGatewayId(), gateway);
                                    }
                                    logger.info("updateGateway Replication Success!");
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    registryClient.deleteGateway(gateway.getGatewayId());
                                    logger.info("deleteGateway Replication Success!");
                                    break;
                                }
                        }
                        // break entity: gateway
                        break;
                    }
                // UserProfile related operations
                case USER_PROFILE:
                    {
                        // construct userprofile datamodel from message
                        UserProfile userProfile = new UserProfile();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), userProfile);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addUser in Registry.");
                                    registryClient.addUser(userProfile);
                                    logger.info("addUser Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                        }
                        // break entity: userprofile
                        break;
                    }
                // no handler for entity
                default:
                    {
                        logger.error("Handler not defined for Entity: " + publisherContext.getEntityType());
                    }
            }
        } catch (DuplicateEntryException ex) {
            // log this exception and proceed (do nothing)
            // this exception is thrown mostly when messages are re-consumed, hence ignore
            logger.warn("DuplicateEntryException while consuming db-event message, ex: " + ex.getMessage(), ex);
        }
        // send ack for received message
        logger.info("RegistryServiceDBEventHandler | Sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag());
        RegistryServiceDBEventMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (TException ex) {
        logger.error("Error processing message: " + ex, ex);
    } catch (ApplicationSettingsException ex) {
        logger.error("Error fetching application settings: " + ex, ex);
    } catch (AiravataException ex) {
        logger.error("Error sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag(), ex);
    }
}
Also used : TException(org.apache.thrift.TException) DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) DBEventPublisherContext(org.apache.airavata.model.dbevent.DBEventPublisherContext) Gateway(org.apache.airavata.model.workspace.Gateway) DuplicateEntryException(org.apache.airavata.model.error.DuplicateEntryException) AiravataException(org.apache.airavata.common.exception.AiravataException)

Example 2 with UserProfile

use of org.apache.airavata.model.user.UserProfile in project airavata by apache.

the class UserProfileServiceHandler method deleteUserProfile.

@Override
@SecurityCheck
public boolean deleteUserProfile(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException, AuthorizationException, TException {
    try {
        // find user-profile
        UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId);
        // delete user
        boolean deleteSuccess = userProfileRepository.delete(userId);
        logger.info("Delete UserProfile with userId: " + userId + ", " + (deleteSuccess ? "Success!" : "Failed!"));
        if (deleteSuccess) {
            // delete userProfile at end-places
            ProfileServiceUtils.getDbEventPublisher().publish(ProfileServiceUtils.getDBEventMessageContext(EntityType.USER_PROFILE, CrudType.DELETE, userProfile), DBEventManagerConstants.getRoutingKey(DBEventService.DB_EVENT.toString()));
        }
        return deleteSuccess;
    } catch (Exception e) {
        logger.error("Error while deleting user profile", e);
        UserProfileServiceException exception = new UserProfileServiceException();
        exception.setMessage("Error while deleting user profile. More info : " + e.getMessage());
        throw exception;
    }
}
Also used : UserProfile(org.apache.airavata.model.user.UserProfile) UserProfileServiceException(org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException) TException(org.apache.thrift.TException) AuthorizationException(org.apache.airavata.model.error.AuthorizationException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) UserProfileServiceException(org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException) SecurityCheck(org.apache.airavata.service.security.interceptor.SecurityCheck)

Example 3 with UserProfile

use of org.apache.airavata.model.user.UserProfile in project airavata by apache.

the class UserProfileRepository method getUserProfileByIdAndGateWay.

public UserProfile getUserProfileByIdAndGateWay(String userId, String gatewayId) {
    UserProfile userProfile = null;
    Map<String, Object> queryParam = new HashMap<String, Object>();
    queryParam.put(UserProfile._Fields.USER_ID.getFieldName(), userId);
    queryParam.put(UserProfile._Fields.GATEWAY_ID.getFieldName(), gatewayId);
    List<UserProfile> resultList = select(QueryConstants.FIND_USER_PROFILE_BY_USER_ID, 1, 0, queryParam);
    if (resultList != null && resultList.size() > 0)
        userProfile = resultList.get(0);
    return userProfile;
}
Also used : UserProfile(org.apache.airavata.model.user.UserProfile) HashMap(java.util.HashMap)

Example 4 with UserProfile

use of org.apache.airavata.model.user.UserProfile in project airavata by apache.

the class TenantManagementKeycloakImpl method findUser.

@Override
public List<UserProfile> findUser(PasswordCredential realmAdminCreds, String tenantId, String email, String userName) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(userName, null, null, email, 0, 1);
        if (!retrieveUserList.isEmpty()) {
            List<UserProfile> userList = new ArrayList<>();
            for (UserRepresentation user : retrieveUserList) {
                UserProfile profile = new UserProfile();
                profile.setUserId(user.getUsername());
                profile.setFirstName(user.getFirstName());
                profile.setLastName(user.getLastName());
                profile.setEmails(Arrays.asList(new String[] { user.getEmail() }));
                userList.add(profile);
            }
            return userList;
        } else {
            logger.error("requested User not found");
            return null;
        }
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } catch (Exception ex) {
        logger.error("Error finding user in keycloak server, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error finding user in keycloak server, reason: " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) ArrayList(java.util.ArrayList) Keycloak(org.keycloak.admin.client.Keycloak) IOException(java.io.IOException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)

Example 5 with UserProfile

use of org.apache.airavata.model.user.UserProfile in project airavata by apache.

the class SharingServiceDBEventHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    log.info("New DB Event message to sharing service.");
    try {
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        log.info("DB Event message to sharing service from " + dbEventMessage.getPublisherService());
        DBEventMessageContext dBEventMessageContext = dbEventMessage.getMessageContext();
        try {
            switch(dBEventMessageContext.getPublisher().getPublisherContext().getEntityType()) {
                case USER_PROFILE:
                    log.info("User profile specific DB Event communicated by " + dbEventMessage.getPublisherService());
                    UserProfile userProfile = new UserProfile();
                    ThriftUtils.createThriftFromBytes(dBEventMessageContext.getPublisher().getPublisherContext().getEntityDataModel(), userProfile);
                    User user = ThriftDataModelConversion.getUser(userProfile);
                    switch(dBEventMessageContext.getPublisher().getPublisherContext().getCrudType()) {
                        case CREATE:
                            log.info("Creating user. User Id : " + user.getUserId());
                            sharingRegistryClient.createUser(user);
                            log.debug("User created. User Id : " + user.getUserId());
                            break;
                        case READ:
                            // FIXME: Remove if not required
                            break;
                        case UPDATE:
                            log.info("Updating user. User Id : " + user.getUserId());
                            sharingRegistryClient.updatedUser(user);
                            log.debug("User updated. User Id : " + user.getUserId());
                            break;
                        case DELETE:
                            log.info("Deleting user. User Id : " + user.getUserId());
                            sharingRegistryClient.deleteUser(user.getDomainId(), user.getUserId());
                            log.debug("User deleted. User Id : " + user.getUserId());
                            break;
                    }
                    break;
                case TENANT:
                    log.info("Tenant specific DB Event communicated by " + dbEventMessage.getPublisherService());
                    Gateway gateway = new Gateway();
                    ThriftUtils.createThriftFromBytes(dBEventMessageContext.getPublisher().getPublisherContext().getEntityDataModel(), gateway);
                    switch(dBEventMessageContext.getPublisher().getPublisherContext().getCrudType()) {
                        case CREATE:
                        case UPDATE:
                            // Only create the domain is it doesn't already exist
                            if (sharingRegistryClient.isDomainExists(gateway.getGatewayId())) {
                                break;
                            }
                            /*
                                Following set of DB operations should happen in a transaction
                                As these are thrift calls we cannot enforce this restriction
                                If something goes wrong, message would get queued again and try to create
                                 DB entities which are already present. We catch DuplicateEntryException and
                                 log as a warning to handle such scenarios and move ahead.
                                 */
                            log.info("Creating domain. Id : " + gateway.getGatewayId());
                            Domain domain = new Domain();
                            domain.setDomainId(gateway.getGatewayId());
                            domain.setName(gateway.getGatewayName());
                            domain.setDescription("Domain entry for " + domain.name);
                            try {
                                sharingRegistryClient.createDomain(domain);
                                log.debug("Domain created. Id : " + gateway.getGatewayId());
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Domain Id : " + gateway.getGatewayId(), ex);
                            }
                            // Creating Entity Types for each domain
                            log.info("Creating entity type. Id : " + domain.domainId + ":PROJECT");
                            org.apache.airavata.sharing.registry.models.EntityType entityType = new org.apache.airavata.sharing.registry.models.EntityType();
                            entityType.setEntityTypeId(domain.domainId + ":PROJECT");
                            entityType.setDomainId(domain.domainId);
                            entityType.setName("PROJECT");
                            entityType.setDescription("Project entity type");
                            try {
                                sharingRegistryClient.createEntityType(entityType);
                                log.debug("Entity type created. Id : " + domain.domainId + ":PROJECT");
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Entity Id : " + domain.domainId + ":PROJECT", ex);
                            }
                            log.info("Creating entity type. Id : " + domain.domainId + ":EXPERIMENT");
                            entityType = new org.apache.airavata.sharing.registry.models.EntityType();
                            entityType.setEntityTypeId(domain.domainId + ":EXPERIMENT");
                            entityType.setDomainId(domain.domainId);
                            entityType.setName("EXPERIMENT");
                            entityType.setDescription("Experiment entity type");
                            try {
                                sharingRegistryClient.createEntityType(entityType);
                                log.debug("Entity type created. Id : " + domain.domainId + ":EXPERIMENT");
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Entity Id : " + domain.domainId + ":EXPERIMENT", ex);
                            }
                            log.info("Creating entity type. Id : " + domain.domainId + ":FILE");
                            entityType = new org.apache.airavata.sharing.registry.models.EntityType();
                            entityType.setEntityTypeId(domain.domainId + ":FILE");
                            entityType.setDomainId(domain.domainId);
                            entityType.setName("FILE");
                            entityType.setDescription("File entity type");
                            try {
                                sharingRegistryClient.createEntityType(entityType);
                                log.debug("Entity type created. Id : " + domain.domainId + ":FILE");
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Entity Id : " + domain.domainId + ":FILE", ex);
                            }
                            // Creating Permission Types for each domain
                            log.info("Creating Permission Type. Id : " + domain.domainId + ":READ");
                            PermissionType permissionType = new PermissionType();
                            permissionType.setPermissionTypeId(domain.domainId + ":READ");
                            permissionType.setDomainId(domain.domainId);
                            permissionType.setName("READ");
                            permissionType.setDescription("Read permission type");
                            try {
                                sharingRegistryClient.createPermissionType(permissionType);
                                log.debug("Permission Type created. Id : " + domain.domainId + ":READ");
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Permission Id : " + domain.domainId + ":READ", ex);
                            }
                            log.info("Creating Permission Type. Id : " + domain.domainId + ":WRITE");
                            permissionType = new PermissionType();
                            permissionType.setPermissionTypeId(domain.domainId + ":WRITE");
                            permissionType.setDomainId(domain.domainId);
                            permissionType.setName("WRITE");
                            permissionType.setDescription("Write permission type");
                            try {
                                sharingRegistryClient.createPermissionType(permissionType);
                                log.debug("Permission Type created. Id : " + domain.domainId + ":WRITE");
                            } catch (DuplicateEntryException ex) {
                                log.warn("DuplicateEntryException while consuming TENANT create message, ex: " + ex.getMessage() + ", Permission Id : " + domain.domainId + ":WRITE", ex);
                            }
                            break;
                    }
                    break;
                default:
                    log.error("Handler not defined for " + dBEventMessageContext.getPublisher().getPublisherContext().getEntityType());
            }
        } catch (DuplicateEntryException ex) {
            // log this exception and proceed (do nothing)
            // this exception is thrown mostly when messages are re-consumed in case of some exception, hence ignore
            log.warn("DuplicateEntryException while consuming db-event message, ex: " + ex.getMessage(), ex);
        }
        log.info("Sending ack. Message Delivery Tag : " + messageContext.getDeliveryTag());
        SharingServiceDBEventMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (TException e) {
        log.error("Error processing message.", e);
    } catch (ApplicationSettingsException e) {
        log.error("Error fetching application settings.", e);
    } catch (AiravataException e) {
        log.error("Error sending ack. Message Delivery Tag : " + messageContext.getDeliveryTag(), e);
    }
}
Also used : TException(org.apache.thrift.TException) DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) User(org.apache.airavata.sharing.registry.models.User) UserProfile(org.apache.airavata.model.user.UserProfile) PermissionType(org.apache.airavata.sharing.registry.models.PermissionType) DBEventMessageContext(org.apache.airavata.model.dbevent.DBEventMessageContext) Gateway(org.apache.airavata.model.workspace.Gateway) DuplicateEntryException(org.apache.airavata.model.error.DuplicateEntryException) Domain(org.apache.airavata.sharing.registry.models.Domain) AiravataException(org.apache.airavata.common.exception.AiravataException)

Aggregations

UserProfile (org.apache.airavata.model.user.UserProfile)10 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)5 IamAdminServicesException (org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)5 ArrayList (java.util.ArrayList)4 TException (org.apache.thrift.TException)3 AiravataException (org.apache.airavata.common.exception.AiravataException)2 PasswordCredential (org.apache.airavata.model.credential.store.PasswordCredential)2 DBEventMessage (org.apache.airavata.model.dbevent.DBEventMessage)2 DuplicateEntryException (org.apache.airavata.model.error.DuplicateEntryException)2 Gateway (org.apache.airavata.model.workspace.Gateway)2 TenantManagementKeycloakImpl (org.apache.airavata.service.profile.iam.admin.services.core.impl.TenantManagementKeycloakImpl)2 Keycloak (org.keycloak.admin.client.Keycloak)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 DBEventMessageContext (org.apache.airavata.model.dbevent.DBEventMessageContext)1 DBEventPublisherContext (org.apache.airavata.model.dbevent.DBEventPublisherContext)1 AuthorizationException (org.apache.airavata.model.error.AuthorizationException)1 AuthzToken (org.apache.airavata.model.security.AuthzToken)1 UserProfileService (org.apache.airavata.service.profile.user.cpi.UserProfileService)1 UserProfileServiceException (org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException)1