Search in sources :

Example 1 with ValidationError

use of com.google.gerrit.server.git.ValidationError in project gerrit by GerritCodeReview.

the class ReceiveCommits method validateConfigPush.

/**
 * Validates a push to refs/meta/config, and reject the command if it fails.
 */
private void validateConfigPush(ReceiveCommand cmd) throws PermissionBackendException {
    try (TraceTimer traceTimer = newTimer("validateConfigPush")) {
        logger.atFine().log("Processing %s command", cmd.getRefName());
        try {
            permissions.check(ProjectPermission.WRITE_CONFIG);
        } catch (AuthException e) {
            reject(cmd, String.format("must be either project owner or have %s permission", ProjectPermission.WRITE_CONFIG.describeForException()));
            return;
        }
        switch(cmd.getType()) {
            case CREATE:
            case UPDATE:
            case UPDATE_NONFASTFORWARD:
                try {
                    ProjectConfig cfg = projectConfigFactory.create(project.getNameKey());
                    cfg.load(project.getNameKey(), receivePack.getRevWalk(), cmd.getNewId());
                    if (!cfg.getValidationErrors().isEmpty()) {
                        addError("Invalid project configuration:");
                        for (ValidationError err : cfg.getValidationErrors()) {
                            addError("  " + err.getMessage());
                        }
                        reject(cmd, "invalid project configuration");
                        logger.atSevere().log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
                        return;
                    }
                    Project.NameKey newParent = cfg.getProject().getParent(allProjectsName);
                    Project.NameKey oldParent = project.getParent(allProjectsName);
                    if (oldParent == null) {
                        // update of the 'All-Projects' project
                        if (newParent != null) {
                            reject(cmd, "invalid project configuration: root project cannot have parent");
                            return;
                        }
                    } else {
                        if (!oldParent.equals(newParent)) {
                            if (allowProjectOwnersToChangeParent) {
                                try {
                                    permissionBackend.user(user).project(project.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
                                } catch (AuthException e) {
                                    reject(cmd, "invalid project configuration: only project owners can set parent");
                                    return;
                                }
                            } else {
                                try {
                                    permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
                                } catch (AuthException e) {
                                    reject(cmd, "invalid project configuration: only Gerrit admin can set parent");
                                    return;
                                }
                            }
                        }
                        if (!projectCache.get(newParent).isPresent()) {
                            reject(cmd, "invalid project configuration: parent does not exist");
                            return;
                        }
                    }
                    validatePluginConfig(cmd, cfg);
                } catch (Exception e) {
                    reject(cmd, "invalid project configuration");
                    logger.atSevere().withCause(e).log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
                    return;
                }
                break;
            case DELETE:
                break;
            default:
                reject(cmd, "prohibited by Gerrit: don't know how to handle config update of type " + cmd.getType());
        }
    }
}
Also used : BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) AuthException(com.google.gerrit.extensions.restapi.AuthException) ValidationError(com.google.gerrit.server.git.ValidationError) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestCancelledException(com.google.gerrit.server.cancellation.RequestCancelledException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) UpdateException(com.google.gerrit.server.update.UpdateException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionDeniedException(com.google.gerrit.server.permissions.PermissionDeniedException) CmdLineException(org.kohsuke.args4j.CmdLineException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) InvalidDeadlineException(com.google.gerrit.server.InvalidDeadlineException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RefOperationValidationException(com.google.gerrit.server.git.validators.RefOperationValidationException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) StorageException(com.google.gerrit.exceptions.StorageException) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException)

Example 2 with ValidationError

use of com.google.gerrit.server.git.ValidationError in project gerrit by GerritCodeReview.

the class AccountValidator method loadAccount.

private Optional<Account> loadAccount(Account.Id accountId, Repository allUsersRepo, RevWalk rw, ObjectId commit, @Nullable ImmutableList.Builder<String> messages) throws IOException, ConfigInvalidException {
    rw.reset();
    AccountConfig accountConfig = new AccountConfig(accountId, allUsersName, allUsersRepo);
    accountConfig.load(allUsersName, rw, commit);
    if (messages != null) {
        messages.addAll(accountConfig.getValidationErrors().stream().map(ValidationError::getMessage).collect(toSet()));
    }
    return accountConfig.getLoadedAccount();
}
Also used : ValidationError(com.google.gerrit.server.git.ValidationError) AccountConfig(com.google.gerrit.server.account.AccountConfig)

Aggregations

ValidationError (com.google.gerrit.server.git.ValidationError)2 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)1 Project (com.google.gerrit.entities.Project)1 StorageException (com.google.gerrit.exceptions.StorageException)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1 InvalidDeadlineException (com.google.gerrit.server.InvalidDeadlineException)1 AccountConfig (com.google.gerrit.server.account.AccountConfig)1 RequestCancelledException (com.google.gerrit.server.cancellation.RequestCancelledException)1 RefOperationValidationException (com.google.gerrit.server.git.validators.RefOperationValidationException)1 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 PermissionDeniedException (com.google.gerrit.server.permissions.PermissionDeniedException)1 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)1 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)1 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)1 UpdateException (com.google.gerrit.server.update.UpdateException)1