Search in sources :

Example 6 with Pair

use of cz.metacentrum.perun.core.api.Pair in project perun by CESNET.

the class urn_perun_member_resource_attribute_def_virt_isBanned method getAffectedMemberResourceObjects.

/**
	 * Get List of pairs of Resource and Member affected by change in Ban (from user and facility)
	 *
	 * @param perunSession
	 * @param user affected user
	 * @param facility affected facility
	 * @return list of pairs of REsource and Members affected by Ban
	 * @throws InternalErrorException
	 */
private List<Pair<Resource, Member>> getAffectedMemberResourceObjects(PerunSessionImpl perunSession, User user, Facility facility) throws InternalErrorException {
    List<Pair<Resource, Member>> listOfAffectedObjects = new ArrayList<>();
    List<Resource> assignedResources = perunSession.getPerunBl().getFacilitiesManagerBl().getAssignedResources(perunSession, facility);
    for (Resource resource : assignedResources) {
        List<Member> assignedMembers = perunSession.getPerunBl().getResourcesManagerBl().getAssignedMembers(perunSession, resource);
        for (Member member : assignedMembers) {
            if (member.getUserId() == user.getId()) {
                listOfAffectedObjects.add(new Pair(resource, member));
                break;
            }
        }
    }
    return listOfAffectedObjects;
}
Also used : ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) Member(cz.metacentrum.perun.core.api.Member) Pair(cz.metacentrum.perun.core.api.Pair)

Example 7 with Pair

use of cz.metacentrum.perun.core.api.Pair in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_systemUnixGID method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Integer gid = (Integer) attribute.getValue();
    //Gid should not be null if is system unix group or if less than 1
    Attribute isSystemGroup = new Attribute();
    if (gid == null) {
        try {
            isSystemGroup = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, group, A_GR_systemIsUnixGroup);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException("Not exist Attribute " + A_GR_systemIsUnixGroup + " for group " + group, ex);
        }
        if (isSystemGroup.getValue() != null && (Integer) isSystemGroup.getValue() == 1) {
            throw new WrongReferenceAttributeValueException(attribute, "Attribute cant be null if " + group + " on " + resource + " is system unix group.");
        }
    } else if (gid < 1) {
        throw new WrongAttributeValueException(attribute, "GID number less than 1 is not allowed value.");
    }
    //Get facility for the resource
    Facility facility = sess.getPerunBl().getResourcesManagerBl().getFacility(sess, resource);
    //List of pairs (group and resource) which has the attribute with the value
    List<Pair<Group, Resource>> listGroupPairsResource = sess.getPerunBl().getGroupsManagerBl().getGroupResourcePairsByAttribute(sess, attribute);
    //Searching through all pairs and if is not checking group/resource/attribute, then try for being on the same facility, if yes then throw exception but only if these groups have not the same GroupName too.
    for (Pair<Group, Resource> p : listGroupPairsResource) {
        if (!p.getLeft().equals(group) || !p.getRight().equals(resource)) {
            Facility facilityForTest = sess.getPerunBl().getResourcesManagerBl().getFacility(sess, p.getRight());
            Attribute group1GroupName = new Attribute();
            Attribute group2GroupName = new Attribute();
            try {
                group1GroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, group, A_GR_systemUnixGroupName);
            } catch (AttributeNotExistsException ex) {
                throw new ConsistencyErrorException("Attribute " + A_GR_systemUnixGroupName + " not exists for group " + group, ex);
            }
            try {
                group2GroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, p.getRight(), p.getLeft(), A_GR_systemUnixGroupName);
            } catch (AttributeNotExistsException ex) {
                throw new ConsistencyErrorException("Attribute " + A_GR_systemUnixGroupName + " not exists for group " + p.getLeft(), ex);
            }
            if (facilityForTest.equals(facility) && !(group1GroupName.getValue().equals(group2GroupName.getValue())))
                throw new WrongAttributeValueException(attribute, "Gid " + gid + "is allready used by another group-resource.  " + p.getLeft() + " " + p.getRight());
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) Pair(cz.metacentrum.perun.core.api.Pair)

Example 8 with Pair

use of cz.metacentrum.perun.core.api.Pair in project perun by CESNET.

the class SchedulingPoolImpl method addToPool.

@Override
public int addToPool(Task task, DispatcherQueue dispatcherQueue) throws InternalErrorException {
    int engineId = (dispatcherQueue == null) ? -1 : dispatcherQueue.getClientID();
    if (task.getId() == 0) {
        // ExecService,Facility pair
        synchronized (tasksByServiceAndFacility) {
            if (!tasksByServiceAndFacility.containsKey(new Pair<Integer, Integer>(task.getExecServiceId(), task.getFacilityId()))) {
                log.debug("Adding new task to pool " + task);
                if (null == task.getStatus()) {
                    task.setStatus(TaskStatus.NONE);
                }
                try {
                    int id = taskManager.scheduleNewTask(task, engineId);
                    task.setId(id);
                } catch (InternalErrorException e) {
                    log.error("Error storing task " + task + " into database: " + e.getMessage());
                    throw new InternalErrorException("Could not assign id to newly created task", e);
                }
                tasksByServiceAndFacility.put(new Pair<Integer, Integer>(task.getExecServiceId(), task.getFacilityId()), task);
                tasksById.put(task.getId(), new Pair<Task, DispatcherQueue>(task, dispatcherQueue));
                List<Task> list = pool.get(task.getStatus());
                if (list == null) {
                    log.info("Making new list for task status " + task.getStatus().toString());
                    list = new ArrayList<Task>();
                    pool.put(task.getStatus(), list);
                }
                list.add(task);
            } else {
                log.debug("There already is task for given ExecService and Facility pair");
            }
        }
    } else {
        // weird - we should not be adding tasks with id present...
        synchronized (tasksById) {
            if (!tasksById.containsKey(task.getId())) {
                log.debug("Adding task to pool " + task);
                if (null == task.getStatus()) {
                    task.setStatus(TaskStatus.NONE);
                }
                tasksById.put(task.getId(), new Pair<Task, DispatcherQueue>(task, dispatcherQueue));
                tasksByServiceAndFacility.put(new Pair<Integer, Integer>(task.getExecServiceId(), task.getFacilityId()), task);
                List<Task> list = pool.get(task.getStatus());
                if (list == null) {
                    log.info("Making new list for task status " + task.getStatus().toString());
                    list = new ArrayList<Task>();
                    pool.put(task.getStatus(), list);
                }
                list.add(task);
            // pool.get(task.getStatus()).add(task);
            }
        }
        try {
            Task existingTask = taskManager.getTaskById(task.getId());
            if (existingTask == null) {
                taskManager.scheduleNewTask(task, engineId);
            } else {
                taskManager.updateTask(task);
            }
        } catch (InternalErrorException e) {
            log.error("Error storing task " + task + " into database: " + e.getMessage());
        }
    }
    return getSize();
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) DispatcherQueue(cz.metacentrum.perun.dispatcher.jms.DispatcherQueue) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Pair(cz.metacentrum.perun.core.api.Pair)

Example 9 with Pair

use of cz.metacentrum.perun.core.api.Pair in project perun by CESNET.

the class TaskSchedulerImpl method scheduleTask.

// TODO ensure dependant tasks with scope DESTINATION go to the same engine
@Override
public Boolean scheduleTask(Task task) {
    ExecService execService = task.getExecService();
    Facility facility = task.getFacility();
    Date time = new Date(System.currentTimeMillis());
    DispatcherQueue dispatcherQueue = null;
    if (task.getStatus().equals(TaskStatus.PROCESSING) && !task.isPropagationForced()) {
        log.debug("Task {} already processing, will not schedule again.", task.toString());
        return true;
    }
    log.debug("Scheduling TASK " + task.toString());
    try {
        dispatcherQueue = schedulingPool.getQueueForTask(task);
        log.debug("Task {} is assigned to queue {}", task.getId(), (dispatcherQueue == null) ? "null" : dispatcherQueue.getClientID());
    } catch (InternalErrorException e) {
        log.warn("Task {} is not assigned to any queue", task.getId());
    }
    // check if the engine is still registered
    if (dispatcherQueue != null && !dispatcherQueuePool.isThereDispatcherQueueForClient(dispatcherQueue.getClientID())) {
        dispatcherQueue = null;
    }
    if (dispatcherQueue == null) {
        // where should we send the task?
        dispatcherQueue = dispatcherQueuePool.getAvailableQueue();
        if (dispatcherQueue != null) {
            try {
                schedulingPool.setQueueForTask(task, dispatcherQueue);
            } catch (InternalErrorException e) {
                log.error("Could not set client queue for task {}: {}", task.getId(), e.getMessage());
                return true;
            }
            log.debug("Assigned new queue " + dispatcherQueue.getQueueName() + " to task " + task.getId());
        } else {
            // bad luck...
            log.error("Task " + task.toString() + " has no engine assigned and there are no engines registered...");
            return true;
        }
    }
    log.debug("Facility to be processed: " + facility.getId() + ", ExecService to be processed: " + execService.getId());
    Boolean abortTask = false;
    try {
        refetchTaskInformation(task);
        List<Service> assignedServices = perun.getServicesManagerBl().getAssignedServices(perunSession, task.getFacility());
        if (!assignedServices.contains(execService.getService())) {
            log.debug("Task {} has no longer service {} assigned, aborting.", task.getId(), execService.getId());
            abortTask = true;
        }
    } catch (FacilityNotExistsException e1) {
        log.debug("Facility {} for task {} no longer exists, aborting", facility.getId(), task.getId());
        abortTask = true;
    } catch (ServiceNotExistsException e1) {
        log.debug("Service {} for task {} no longer exists, aborting", execService.getId(), task.getId());
        abortTask = true;
    } catch (InternalErrorException e1) {
        log.error("Error checking facility or exec service for updates, task will not run now: {}", e1.getMessage());
        return true;
    } catch (PrivilegeException e1) {
        log.error("Error checking facility or exec service for updates, task will not run now: {}", e1.getMessage());
        return true;
    }
    // GEN tasks that we depend on...
    if (abortTask && execService.getExecServiceType() == ExecServiceType.GENERATE) {
        // GEN tasks may be aborted immediately
        abortTask(task);
        return false;
    }
    // do not perform further checks for task that is going to be aborted
    if (!abortTask) {
        log.debug("Is the execService ID:" + execService.getId() + " enabled globally?");
        if (execService.isEnabled()) {
            log.debug("   Yes, it is globally enabled.");
        } else {
            log.debug("   No, execService ID: " + execService.getId() + " is not enabled globally. Task will not run.");
            return true;
        }
        log.debug("   Is the execService ID: " + execService.getId() + " denied on facility ID:" + facility.getId() + "?");
        try {
            if (!denialsResolver.isExecServiceDeniedOnFacility(execService, facility)) {
                log.debug("   No, it is not.");
            } else {
                log.debug("   Yes, the execService ID: " + execService.getId() + " is denied on facility ID: " + facility.getId() + ". Task will not run.");
                return true;
            }
        } catch (InternalErrorException e) {
            log.error("Error getting disabled status for execService, task will not run now.");
            return true;
        }
    }
    List<ExecService> dependantServices = null;
    List<Pair<ExecService, DependencyScope>> dependencies = null;
    // If any of the ExecServices that depends on this one is running
    // PROCESSING
    // we will put the ExecService,Facility pair back to the pool.
    // #################################################################################
    log.debug("   Is there any execService that depends on [" + execService.getId() + "] in \"PROCESSING\" state?");
    dependantServices = dependenciesResolver.listDependantServices(execService);
    boolean proceed = true;
    for (ExecService dependantService : dependantServices) {
        Task dependantServiceTask = schedulingPool.getTask(dependantService, facility);
        if (dependantServiceTask != null) {
            if (dependantServiceTask.getStatus().equals(TaskStatus.PROCESSING)) {
                log.debug("   There is a service [" + dependantServiceTask.getId() + "] running that depends on this one [" + execService + "], so we put this to sleep...");
                // schedulingPool.addToPool(new Pair<ExecService,
                // Facility>(execService, facility));
                proceed = false;
                break;
            }
        // This is probably wrong, so commenting out:
        //   1) if there is some dispatcher queue active at the moment, dispatcherQueue is not null
        //   2) if there is no dispatcher queue available, no point in setting the same queue another task has
        //   3) the dependency should be handled the other way round, from dependant to dependent
        /*
				 * try {
				 *	if (dispatcherQueue == null &&
				 *			schedulingPool.getQueueForTask(dependantServiceTask) != null) {
				 *		schedulingPool.setQueueForTask(task, schedulingPool.getQueueForTask(dependantServiceTask));
				 *	}
				 * } catch (InternalErrorException e) {
				 *	log.debug("    Failed to set destination queue for task. This is weird, aborting.");
				 *	proceed = false;
				 *}
				 */
        }
    }
    if (proceed) {
        log.debug("   No, it is not. No dependent service is running, we can proceed.");
        // If it is an ExecService of type SEND, we have to check its
        // dependencies.
        // We can skip this for GENERATE type (it has no dependencies by
        // design).
        // ########################################################################
        log.debug("   Check whether the execService [" + execService.getId() + "] is of type SEND");
        if (execService.getExecServiceType().equals(ExecServiceType.SEND)) {
            log.debug("   Well, it is, so we have to check it's dependencies.");
            // We check the status of all the ExecServices this ExecService
            // depends on.
            //
            //
            // Current approach disregards any SEND/GENERATE differences.
            // Dependency on a GENERATE service is being treated as same as
            // any other SEND dependency
            // but for a small exception regarding ERROR and DONE states,
            // see below:
            //
            // If the dependency is in one of the following states, we do:
            // NONE Schedule it and wait (put this [ExecService,Facility]
            // pair back to the SchedulingPool for a while).
            // PROCESSING Wait
            // ERROR IF dependency is GENERATE THEN DO
            // Schedule it and wait (put this [ExecService,Facility] pair
            // back to the SchedulingPool for a while).
            // ELSE IF dependency is SEND THEN DO
            // End with ERROR. (no point in trying, something is probably
            // amiss on destination nodes...)
            // ELSE
            // throw new IllegalArgumentException
            // FI
            // DONE IF dependency is GENERATE THEN DO
            // Schedule it and wait (put this [ExecService,Facility] pair
            // back to the SchedulingPool for a while).
            //
            // It might look like we get an infinite loop where GENERATE
            // will be in DONE and then rescheduled again and again.
            // It is not so because PropagationMaintainer sets its state to
            // NONE as soon as the SEND, that depends on it,
            // enters either DONE or ERROR states (one of its finite
            // states).
            // ELSE IF dependency is SEND THEN DO
            // Proceed (Yes, no need to schedule this dependency, it is done
            // already and we don't care for how long it has been so at this
            // point.)
            // ELSE
            // throw new IllegalArgumentException
            // FI
            // :-)
            // #######################################################################################################
            proceed = true;
            dependencies = dependenciesResolver.listDependenciesAndScope(execService);
            log.debug("   We are about to loop over execService [" + execService.getId() + "] dependencies.");
            log.debug("   Number of dependencies:" + dependencies);
            DispatcherQueue dependencyQueue = null;
            for (Pair<ExecService, DependencyScope> dependencyPair : dependencies) {
                ExecService dependency = dependencyPair.getLeft();
                DependencyScope dependencyScope = dependencyPair.getRight();
                Task dependencyServiceTask = schedulingPool.getTask(dependency, facility);
                if (dependencyServiceTask == null) {
                    if (abortTask) {
                        log.info("   Task {} is going to be aborted, the dependency exec service {} will not be scheduled now.", task.getId(), dependency.getId());
                    } else {
                        // Dependency being NULL is equivalent to being in NONE
                        // state.
                        log.info("   Last Task [dependency:" + dependency.getId() + ", facility:" + facility.getId() + "] was NULL, we are gonna propagate.");
                        if (scheduleItAndWait(dependency, facility, execService, dispatcherQueue, time)) {
                        // task sucessfully scheduled, nothing to do
                        } else {
                        // TODO: task aborted - maybe set this one to error?
                        }
                    }
                    proceed = false;
                } else {
                    boolean wasDependencyServiceTaskForced = dependencyServiceTask.isPropagationForced();
                    dependencyServiceTask.setPropagationForced(task.isPropagationForced());
                    switch(dependencyServiceTask.getStatus()) {
                        case DONE:
                            switch(dependency.getExecServiceType()) {
                                case GENERATE:
                                    if (task.isSourceUpdated()) {
                                        // we need to reschedule the GEN task as the source data has changed
                                        log.debug("   Dependency ID " + dependency.getId() + " is in DONE and is going to be rescheduled as we need fresh data.");
                                        rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                                        proceed = false;
                                    } else {
                                        log.debug("   Dependency ID " + dependency.getId() + " is in DONE and it is of type GENERATE, we can proceed.");
                                        // Nothing, we can proceed...
                                        try {
                                            dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                        } catch (InternalErrorException e) {
                                            log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                        }
                                    }
                                    break;
                                case SEND:
                                    log.debug("   Dependency ID " + dependencyServiceTask.getId() + " is in DONE and it is of type SEND, we can proceed.");
                                    // Nothing, we can proceed...
                                    break;
                                default:
                                    throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
                            }
                            break;
                        case ERROR:
                            switch(dependency.getExecServiceType()) {
                                case GENERATE:
                                    log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in ERROR and it is of type GENERATE, we are gonna propagate.");
                                    // scheduleItAndWait(dependency, facility,
                                    // execService, dispatcherQueue);
                                    // try to run the generate task again
                                    rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                                    proceed = false;
                                    break;
                                case SEND:
                                    log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in ERROR and it is of type SEND, we are gonna end with ERROR.");
                                    proceed = false;
                                    // We end Task with error immediately.
                                    schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
                                    // facility);
                                    break;
                                default:
                                    throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
                            }
                            break;
                        case NONE:
                            log.info("   Last Task {} [dependency:" + dependency.getId() + ", facility:" + facility.getId() + "] was NONE, we are gonna propagate.", dependencyServiceTask.getId());
                            rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                            proceed = false;
                            break;
                        case PLANNED:
                            log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in PLANNED so we are gonna wait.");
                            // justWait(facility, execService);
                            if (dependencyScope.equals(DependencyScope.SERVICE)) {
                                proceed = false;
                            } else {
                                try {
                                    dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                } catch (InternalErrorException e) {
                                    log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                }
                            }
                            break;
                        case PROCESSING:
                            log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in PROCESSING so we are gonna wait.");
                            // justWait(facility, execService);
                            if (dependencyScope.equals(DependencyScope.SERVICE)) {
                                proceed = false;
                            } else {
                                try {
                                    dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                } catch (InternalErrorException e) {
                                    log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                }
                            }
                            if (dependencyServiceTask.isPropagationForced() && !wasDependencyServiceTaskForced) {
                                // reschedule dependant only if originally was not forced !!!
                                rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                            // XXX - should we proceed here?
                            }
                            break;
                        default:
                            throw new IllegalArgumentException("Unknown Task status. Expected DONE, ERROR, NONE, PLANNED or PROCESSING.");
                    }
                }
            }
            // #########################################
            if (proceed) {
                if (abortTask) {
                    // the SEND task is going to be aborted now
                    abortTask(task);
                    return false;
                } else {
                    if (dependencyQueue != null && dependencyQueue != dispatcherQueue) {
                        log.debug("Changing task {} destination queue to {} to match dependency task", task.getId(), dependencyQueue.getClientID());
                        try {
                            schedulingPool.setQueueForTask(task, dependencyQueue);
                        } catch (InternalErrorException e) {
                            log.error("Could not change task {} destination queue: {}", task.getId(), e.getMessage());
                        }
                    }
                    log.info("   SCHEDULING task [" + task.getId() + "], execService [" + execService.getId() + "] facility [" + facility.getId() + "] as PLANNED.");
                    task.setSchedule(time);
                    schedulingPool.setTaskStatus(task, TaskStatus.PLANNED);
                    sendToEngine(task);
                }
            // manipulateTasks(execService, facility, task);
            } else {
                if (abortTask) {
                    // the SEND task is going to be aborted now
                    abortTask(task);
                    return false;
                } else {
                    // If we can not proceed, we just end here.
                    // ########################################
                    // The current ExecService,Facility pair should be sleeping
                    // in SchedulingPool at the moment...
                    log.info("   Task {} state set to NONE, will be scheduled again at the next cycle.", task.getId());
                    schedulingPool.setTaskStatus(task, TaskStatus.NONE);
                }
            }
        } else if (execService.getExecServiceType().equals(ExecServiceType.GENERATE)) {
            log.debug("   Well, it is not. ExecService of type GENERATE does not have any dependencies by design, so we schedule it immediately.");
            log.info("   SCHEDULING task [" + task.getId() + "], execService [" + execService.getId() + "] facility [" + facility.getId() + "] as PLANNED.");
            task.setSchedule(time);
            schedulingPool.setTaskStatus(task, TaskStatus.PLANNED);
            sendToEngine(task);
        // manipulateTasks(execService, facility, task);
        } else {
            throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
        }
    } else {
        log.debug("   We do not proceed, we put the task [" + task.getId() + "], [" + execService.getId() + "] execService to sleep.");
    }
    return true;
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) DependencyScope(cz.metacentrum.perun.taskslib.dao.ExecServiceDependencyDao.DependencyScope) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) DispatcherQueue(cz.metacentrum.perun.dispatcher.jms.DispatcherQueue) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Facility(cz.metacentrum.perun.core.api.Facility) Pair(cz.metacentrum.perun.core.api.Pair)

Example 10 with Pair

use of cz.metacentrum.perun.core.api.Pair in project perun by CESNET.

the class ExtSourceEGISSO method getCertficiateSubjectAndIssure.

private Pair<String, String> getCertficiateSubjectAndIssure(byte[] certInDER, int counter) throws InternalErrorException {
    String additionalValue = "additionalues_" + counter;
    Pair<String, String> subjectCert;
    //save cartificate to /tmp directory to use it for openssl program
    Path tempDirectory = null;
    File byteFile = null;
    File infoFile = null;
    try {
        tempDirectory = Files.createTempDirectory("certificates");
        byteFile = File.createTempFile("egi-sso-cert-byte", ".txt", tempDirectory.toFile());
        infoFile = File.createTempFile("egi-sso-cert-info", ".txt", tempDirectory.toFile());
        try (FileOutputStream fos = new FileOutputStream(byteFile)) {
            fos.write(certInDER);
        }
        Process p;
        p = new ProcessBuilder("openssl", "x509", "-in", byteFile.getAbsolutePath(), "-subject", "-issuer", "-noout", "-inform", "DER").inheritIO().redirectOutput(infoFile).redirectErrorStream(true).directory(tempDirectory.toFile()).start();
        int exit;
        exit = p.waitFor();
        if (exit != 0) {
            throw new InternalErrorException("Return code of openssl is " + exit);
        }
        //read file again and export certificate infos
        String certIssuer = null;
        String certSubj = null;
        try (BufferedReader br = new BufferedReader(new FileReader(infoFile))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (line.matches("^issuer= .*")) {
                    certIssuer = line.replaceAll("^issuer= ", "");
                } else if (line.matches("^subject= .*")) {
                    certSubj = line.replaceAll("^subject= ", "");
                }
            }
        }
        if (certIssuer == null || certSubj == null)
            throw new InternalErrorException("CertSubject or CertIssuer is null.");
        subjectCert = new Pair(additionalValue, certIssuer + "|" + "cz.metacentrum.perun.core.impl.ExtSourceX509" + "|" + certSubj + "|2");
    } catch (IOException ex) {
        throw new InternalErrorException("IOException while working with files.", ex);
    } catch (InterruptedException ex) {
        throw new InternalErrorException("Process openssl was interrupted.", ex);
    } finally {
        try {
            if (byteFile != null)
                Files.deleteIfExists(byteFile.toPath());
            if (infoFile != null)
                Files.deleteIfExists(infoFile.toPath());
            if (tempDirectory != null)
                Files.delete(tempDirectory);
        } catch (IOException e) {
            throw new InternalErrorException("Can't delete some files", e);
        }
    }
    return subjectCert;
}
Also used : Path(java.nio.file.Path) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Pair(cz.metacentrum.perun.core.api.Pair)

Aggregations

Pair (cz.metacentrum.perun.core.api.Pair)12 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)8 Attribute (cz.metacentrum.perun.core.api.Attribute)4 Facility (cz.metacentrum.perun.core.api.Facility)4 Resource (cz.metacentrum.perun.core.api.Resource)4 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)3 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)3 DispatcherQueue (cz.metacentrum.perun.dispatcher.jms.DispatcherQueue)3 Task (cz.metacentrum.perun.taskslib.model.Task)3 ArrayList (java.util.ArrayList)3 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)2 Group (cz.metacentrum.perun.core.api.Group)2 Member (cz.metacentrum.perun.core.api.Member)2 User (cz.metacentrum.perun.core.api.User)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)2 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)2 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)2 Date (java.util.Date)2