Search in sources :

Example 26 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class ProjectControlHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    String projectName = params.getParameter(0);
    while (projectName.endsWith("/")) {
        projectName = projectName.substring(0, projectName.length() - 1);
    }
    while (projectName.startsWith("/")) {
        // Be nice and drop the leading "/" if supplied by an absolute path.
        // We don't have a file system hierarchy, just a flat namespace in
        // the database's Project entities. We never encode these with a
        // leading '/' but users might accidentally include them in Git URLs.
        //
        projectName = projectName.substring(1);
    }
    String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
    Project.NameKey nameKey = new Project.NameKey(nameWithoutSuffix);
    ProjectControl control;
    try {
        control = projectControlFactory.controlFor(nameKey, user.get());
        permissionBackend.user(user).project(nameKey).check(ProjectPermission.ACCESS);
    } catch (AuthException e) {
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    } catch (NoSuchProjectException e) {
        throw new CmdLineException(owner, e.getMessage());
    } catch (PermissionBackendException | IOException e) {
        log.warn("Cannot load project " + nameWithoutSuffix, e);
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    }
    setter.addValue(control);
    return 1;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) ProjectControl(com.google.gerrit.server.project.ProjectControl) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 27 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class ShowCaches method run.

@Override
protected void run() throws UnloggedFailure {
    nw = columns - 50;
    Date now = new Date();
    stdout.format("%-25s %-20s      now  %16s\n", "Gerrit Code Review", Version.getVersion() != null ? Version.getVersion() : "", new SimpleDateFormat("HH:mm:ss   zzz").format(now));
    stdout.format("%-25s %-20s   uptime %16s\n", "", "", uptime(now.getTime() - serverStarted));
    stdout.print('\n');
    stdout.print(//
    String.format(//
    "%1s %-" + nw + "s|%-21s|  %-5s |%-9s|\n", //
    "", //
    "Name", //
    "Entries", //
    "AvgGet", //
    "Hit Ratio"));
    stdout.print(//
    String.format(//
    "%1s %-" + nw + "s|%6s %6s %7s|  %-5s  |%-4s %-4s|\n", //
    "", //
    "", //
    "Mem", //
    "Disk", //
    "Space", //
    "", //
    "Mem", //
    "Disk"));
    stdout.print("--");
    for (int i = 0; i < nw; i++) {
        stdout.print('-');
    }
    stdout.print("+---------------------+---------+---------+\n");
    Collection<CacheInfo> caches = getCaches();
    printMemoryCoreCaches(caches);
    printMemoryPluginCaches(caches);
    printDiskCaches(caches);
    stdout.print('\n');
    boolean showJvm;
    try {
        permissionBackend.user(self).check(GlobalPermission.MAINTAIN_SERVER);
        showJvm = true;
    } catch (AuthException | PermissionBackendException e) {
        // Silently ignore and do not display detailed JVM information.
        showJvm = false;
    }
    if (showJvm) {
        sshSummary();
        SummaryInfo summary = getSummary.setGc(gc).setJvm(showJVM).apply(new ConfigResource());
        taskSummary(summary.taskSummary);
        memSummary(summary.memSummary);
        threadSummary(summary.threadSummary);
        if (showJVM && summary.jvmSummary != null) {
            jvmSummary(summary.jvmSummary);
        }
    }
    stdout.flush();
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) CacheInfo(com.google.gerrit.server.config.ListCaches.CacheInfo) ThreadSummaryInfo(com.google.gerrit.server.config.GetSummary.ThreadSummaryInfo) JvmSummaryInfo(com.google.gerrit.server.config.GetSummary.JvmSummaryInfo) MemSummaryInfo(com.google.gerrit.server.config.GetSummary.MemSummaryInfo) TaskSummaryInfo(com.google.gerrit.server.config.GetSummary.TaskSummaryInfo) SummaryInfo(com.google.gerrit.server.config.GetSummary.SummaryInfo) ConfigResource(com.google.gerrit.server.config.ConfigResource)

Example 28 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class KillCommand method run.

@Override
protected void run() {
    ConfigResource cfgRsrc = new ConfigResource();
    for (String id : taskIds) {
        try {
            TaskResource taskRsrc = tasksCollection.parse(cfgRsrc, IdString.fromDecoded(id));
            deleteTask.apply(taskRsrc, null);
        } catch (AuthException | ResourceNotFoundException | PermissionBackendException e) {
            stderr.print("kill: " + id + ": No such task\n");
        }
    }
}
Also used : TaskResource(com.google.gerrit.server.config.TaskResource) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ConfigResource(com.google.gerrit.server.config.ConfigResource)

Example 29 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class CheckAccess method apply.

@Override
public AccessCheckInfo apply(ConfigResource unused, AccessCheckInput input) throws OrmException, PermissionBackendException, RestApiException, IOException {
    permissionBackend.user(currentUser.get()).check(GlobalPermission.ADMINISTRATE_SERVER);
    if (input == null) {
        throw new BadRequestException("input is required");
    }
    if (Strings.isNullOrEmpty(input.account)) {
        throw new BadRequestException("input requires 'account'");
    }
    if (Strings.isNullOrEmpty(input.project)) {
        throw new BadRequestException("input requires 'project'");
    }
    Account match = accountResolver.find(db.get(), input.account);
    if (match == null) {
        throw new BadRequestException(String.format("cannot find account %s", input.account));
    }
    AccessCheckInfo info = new AccessCheckInfo();
    Project.NameKey key = new Project.NameKey(input.project);
    if (projectCache.get(key) == null) {
        info.message = String.format("project %s does not exist", key);
        info.status = HttpServletResponse.SC_NOT_FOUND;
        return info;
    }
    IdentifiedUser user = userFactory.create(match.getId());
    try {
        permissionBackend.user(user).project(key).check(ProjectPermission.ACCESS);
    } catch (AuthException | PermissionBackendException e) {
        info.message = String.format("user %s (%s) cannot see project %s", user.getNameEmail(), user.getAccount().getId(), key);
        info.status = HttpServletResponse.SC_FORBIDDEN;
        return info;
    }
    if (!Strings.isNullOrEmpty(input.ref)) {
        try {
            permissionBackend.user(user).ref(new Branch.NameKey(key, input.ref)).check(RefPermission.READ);
        } catch (AuthException | PermissionBackendException e) {
            info.status = HttpServletResponse.SC_FORBIDDEN;
            info.message = String.format("user %s (%s) cannot see ref %s in project %s", user.getNameEmail(), user.getAccount().getId(), input.ref, key);
            return info;
        }
    }
    info.status = HttpServletResponse.SC_OK;
    return info;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Project(com.google.gerrit.reviewdb.client.Project) AccessCheckInfo(com.google.gerrit.extensions.api.config.AccessCheckInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 30 with AuthException

use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.

the class CheckConsistency method apply.

@Override
public ConsistencyCheckInfo apply(ConfigResource resource, ConsistencyCheckInput input) throws RestApiException, IOException {
    IdentifiedUser user = userProvider.get();
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    if (!user.getCapabilities().canAccessDatabase()) {
        throw new AuthException("not allowed to run consistency checks");
    }
    if (input == null || input.checkAccountExternalIds == null) {
        throw new BadRequestException("input required");
    }
    ConsistencyCheckInfo consistencyCheckInfo = new ConsistencyCheckInfo();
    if (input.checkAccountExternalIds != null) {
        consistencyCheckInfo.checkAccountExternalIdsResult = new CheckAccountExternalIdsResultInfo(externalIdsConsistencyChecker.check());
    }
    return consistencyCheckInfo;
}
Also used : ConsistencyCheckInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) CheckAccountExternalIdsResultInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.CheckAccountExternalIdsResultInfo)

Aggregations

AuthException (com.google.gerrit.extensions.restapi.AuthException)68 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)22 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)20 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)15 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)14 Change (com.google.gerrit.reviewdb.client.Change)13 IOException (java.io.IOException)12 Account (com.google.gerrit.reviewdb.client.Account)11 Project (com.google.gerrit.reviewdb.client.Project)11 CurrentUser (com.google.gerrit.server.CurrentUser)11 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)11 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)11 ArrayList (java.util.ArrayList)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)8 ChangeControl (com.google.gerrit.server.project.ChangeControl)7 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)6 OrmException (com.google.gwtorm.server.OrmException)6 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)6