Search in sources :

Example 1 with ContinuousIntegrationException

use of de.tum.in.www1.artemis.exception.ContinuousIntegrationException in project Artemis by ls1intum.

the class JenkinsUserManagementService method createUser.

/**
 * Creates a user in Jenkins. Note that the user login acts as
 * a unique identifier in Jenkins.
 *
 * @param user The user to create
 */
@Override
public void createUser(User user) throws ContinuousIntegrationException {
    if (user.getLogin().equals(jenkinsAdminUsername)) {
        log.debug("Jenkins createUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only create a user if it doesn't already exist.
    if (getUser(user.getLogin()) != null) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login already exists");
    }
    // Make sure the user login contains legal characters.
    if (!isUserLoginLegal(user)) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login contains illegal characters");
    }
    try {
        // Create the Jenkins user
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("securityRealm", "createAccountByAdmin").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, getCreateUserFormHttpEntity(user), Void.class);
        // Adds the user to groups of existing programming exercises
        addUserToGroups(user.getLogin(), getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot create user: " + user.getLogin(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Example 2 with ContinuousIntegrationException

use of de.tum.in.www1.artemis.exception.ContinuousIntegrationException in project ArTEMiS by ls1intum.

the class JenkinsUserManagementService method createUser.

/**
 * Creates a user in Jenkins. Note that the user login acts as
 * a unique identifier in Jenkins.
 *
 * @param user     The user to create
 * @param password The user's password
 */
@Override
public void createUser(User user, String password) throws ContinuousIntegrationException {
    if (user.getLogin().equals(jenkinsAdminUsername)) {
        log.debug("Jenkins createUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only create a user if it doesn't already exist.
    if (getUser(user.getLogin()) != null) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login already exists");
    }
    // Make sure the user login contains legal characters.
    if (!isUserLoginLegal(user)) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login contains illegal characters");
    }
    try {
        // Create the Jenkins user
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("securityRealm", "createAccountByAdmin").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, getCreateUserFormHttpEntity(user, password), Void.class);
        // Adds the user to groups of existing programming exercises
        addUserToGroups(user.getLogin(), getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot create user: " + user.getLogin(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Example 3 with ContinuousIntegrationException

use of de.tum.in.www1.artemis.exception.ContinuousIntegrationException in project ArTEMiS by ls1intum.

the class JenkinsUserManagementService method deleteUser.

@Override
public void deleteUser(User user) throws ContinuousIntegrationException {
    var userLogin = user.getLogin();
    if (userLogin.equals(jenkinsAdminUsername)) {
        log.debug("Jenkins deleteUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only delete a user if it exists.
    var jenkinsUser = getUser(userLogin);
    if (jenkinsUser == null) {
        return;
    }
    try {
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("user", userLogin, "doDelete").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, null, Void.class);
        removeUserFromGroups(userLogin, getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot delete user: " + userLogin, e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Example 4 with ContinuousIntegrationException

use of de.tum.in.www1.artemis.exception.ContinuousIntegrationException in project ArTEMiS by ls1intum.

the class JenkinsUserManagementService method addUserToGroups.

/**
 * Adds the Artemis user to a group in Jenkins. Jenkins does not support
 * groups so this function fetches all programming exercises belonging to
 * the groups and assigns the user permissions to them.
 *
 * @param userLogin The user login to add to the group
 * @param groups    The groups to add the user to
 */
@Override
public void addUserToGroups(String userLogin, Set<String> groups) throws ContinuousIntegrationException {
    var exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groups);
    log.info("Update Jenkins permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    // TODO: in case we update a tutor group / role here, the tutor should NOT get access to exam exercises before the exam has finished
    exercises.forEach(exercise -> {
        // The exercise's project key is also the name of the Jenkins job that groups all build plans
        // for students, solution, and template.
        var jobName = exercise.getProjectKey();
        var course = exercise.getCourseViaExerciseGroupOrCourseMember();
        if (groups.contains(course.getInstructorGroupName())) {
            try {
                // We are assigning instructor permissions since the exercise's course instructor group is the same as the one that is specified.
                jenkinsJobPermissionsService.addPermissionsForUserToFolder(userLogin, jobName, JenkinsJobPermission.getInstructorPermissions());
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign instructor permissions to user: " + userLogin, e);
            }
        } else if (groups.contains(course.getEditorGroupName())) {
            try {
                // We are assigning editor permissions since the exercise's course editor group is the same as the one that is specified.
                jenkinsJobPermissionsService.addPermissionsForUserToFolder(userLogin, jobName, JenkinsJobPermission.getEditorPermissions());
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign editor permissions to user: " + userLogin, e);
            }
        } else if (groups.contains(course.getTeachingAssistantGroupName())) {
            try {
                // We are assigning teaching assistant permissions since the exercise's course teaching assistant group is the same as the one that is specified.
                jenkinsJobPermissionsService.addTeachingAssistantPermissionsToUserForFolder(userLogin, jobName);
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign teaching assistant permissions to user: " + userLogin, e);
            }
        }
    });
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

Example 5 with ContinuousIntegrationException

use of de.tum.in.www1.artemis.exception.ContinuousIntegrationException in project Artemis by ls1intum.

the class JenkinsUserManagementService method deleteUser.

@Override
public void deleteUser(User user) throws ContinuousIntegrationException {
    var userLogin = user.getLogin();
    if (userLogin.equals(jenkinsAdminUsername)) {
        log.debug("Jenkins deleteUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only delete a user if it exists.
    var jenkinsUser = getUser(userLogin);
    if (jenkinsUser == null) {
        return;
    }
    try {
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("user", userLogin, "doDelete").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, null, Void.class);
        removeUserFromGroups(userLogin, getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot delete user: " + userLogin, e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Aggregations

JenkinsException (de.tum.in.www1.artemis.exception.JenkinsException)10 IOException (java.io.IOException)6 RestClientException (org.springframework.web.client.RestClientException)6 ContinuousIntegrationException (de.tum.in.www1.artemis.exception.ContinuousIntegrationException)4 ProgrammingExerciseStudentParticipation (de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation)2 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)2 FeatureToggle (de.tum.in.www1.artemis.service.feature.FeatureToggle)2 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)2 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Matcher (java.util.regex.Matcher)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2