Search in sources :

Example 1 with AclException

use of dmg.util.AclException in project dcache by dCache.

the class UserAdminShell method sendToMany.

/**
 * Concurrently sends a command to several cells and collects the result from each.
 */
private String sendToMany(Iterable<String> destinations, Serializable object) throws AclException {
    /* Check permissions */
    try {
        checkPermission("cell.*.execute");
    } catch (AclException e) {
        for (String cell : destinations) {
            checkPermission("cell." + cell + ".execute");
        }
    }
    /* Submit */
    List<Map.Entry<String, ListenableFuture<Serializable>>> futures = new ArrayList<>();
    for (String cell : destinations) {
        futures.add(immutableEntry(cell, _cellStub.send(new CellPath(cell), object, Serializable.class, _timeout)));
    }
    /* Collect results */
    StringBuilder result = new StringBuilder();
    for (Map.Entry<String, ListenableFuture<Serializable>> entry : futures) {
        result.append(Ansi.ansi().bold().a(entry.getKey()).boldOff()).append(":");
        try {
            String reply = Objects.toString(entry.getValue().get(), "");
            if (reply.isEmpty()) {
                result.append(Ansi.ansi().fg(GREEN).a(" OK").reset()).append("\n");
            } else {
                result.append("\n");
                for (String s : reply.split("\n")) {
                    result.append("    ").append(s).append("\n");
                }
            }
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof NoRouteToCellException) {
                result.append(Ansi.ansi().fg(RED).a(" Cell is unreachable.").reset()).append("\n");
            } else {
                result.append(" ").append(Ansi.ansi().fg(RED).a(cause.getMessage()).reset()).append("\n");
            }
        } catch (InterruptedException e) {
            result.append(" ^C\n");
            /* Cancel all uncompleted tasks. Doesn't actually cancel any requests, but will cause
                 * the remaining uncompleted futures to throw a CancellationException.
                 */
            for (Map.Entry<String, ListenableFuture<Serializable>> entry2 : futures) {
                entry2.getValue().cancel(true);
            }
        } catch (CancellationException e) {
            result.append(" ^C\n");
        }
    }
    return result.toString();
}
Also used : CellPath(dmg.cells.nucleus.CellPath) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) AuthorizedString(dmg.util.AuthorizedString) AclException(dmg.util.AclException) CommandAclException(dmg.util.CommandAclException) Maps.immutableEntry(com.google.common.collect.Maps.immutableEntry) DirectoryEntry(org.dcache.util.list.DirectoryEntry) CancellationException(java.util.concurrent.CancellationException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Example 2 with AclException

use of dmg.util.AclException in project dcache by dCache.

the class UserAdminShell method checkPermission.

/**
 * Checks that the current effective user has the given acl.
 *
 * @throws AclException if the current user does not have the given {@code aclName}
 */
public void checkPermission(String aclName) throws AclException {
    Object[] request = new Object[5];
    request[0] = "request";
    request[1] = "<nobody>";
    request[2] = "check-permission";
    request[3] = getUser();
    request[4] = aclName;
    Object[] r;
    try {
        r = _acmStub.sendAndWait(request, Object[].class);
    } catch (TimeoutCacheException | NoRouteToCellException e) {
        throw new AclException(e.getMessage());
    } catch (CacheException | InterruptedException e) {
        throw new AclException("Problem: " + e.getMessage());
    }
    if (r.length < 6 || !(r[5] instanceof Boolean)) {
        throw new AclException("Protocol violation 4456");
    }
    if (!((Boolean) r[5])) {
        throw new AclException(getUser(), aclName);
    }
}
Also used : TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) CacheException(diskCacheV111.util.CacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) AclException(dmg.util.AclException) CommandAclException(dmg.util.CommandAclException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException)

Example 3 with AclException

use of dmg.util.AclException in project dcache by dCache.

the class LegacyAdminShell method checkCdPermission.

private void checkCdPermission(String remoteName) throws AclException {
    int pos = remoteName.indexOf('-');
    String prefix = null;
    if (pos > 0) {
        prefix = remoteName.substring(0, pos);
    }
    try {
        checkPermission("cell.*.execute");
    } catch (AclException acle) {
        try {
            checkPermission("cell." + remoteName + ".execute");
        } catch (AclException acle2) {
            if (prefix == null) {
                throw acle2;
            }
            try {
                checkPermission("cell." + prefix + "-pools.execute");
            } catch (AclException acle3) {
                throw new AclException(getUser(), remoteName);
            }
        }
    }
}
Also used : AuthorizedString(dmg.util.AuthorizedString) AclException(dmg.util.AclException) CellEndpoint(dmg.cells.nucleus.CellEndpoint)

Example 4 with AclException

use of dmg.util.AclException in project dcache by dCache.

the class RemotePermission method checkPermission.

@Override
public void checkPermission(Authorizable auth, String aclName) throws AclException {
    Object[] request = new Object[5];
    request[0] = "request";
    request[1] = "<nobody>";
    request[2] = "check-permission";
    request[3] = auth.getAuthorizedPrincipal();
    request[4] = aclName;
    CellMessage reply;
    try {
        reply = _cell.getNucleus().sendAndWait(new CellMessage(_path, request), _timeout);
        if (reply == null) {
            throw new AclException("Request timed out (" + _path + ')');
        }
    } catch (Exception ee) {
        throw new AclException("Problem : " + ee.getMessage());
    }
    Object r = reply.getMessageObject();
    if ((r == null) || (!(r instanceof Object[])) || (((Object[]) r).length < 6) || (!(((Object[]) r)[5] instanceof Boolean))) {
        throw new AclException("Protocol violation 4456");
    }
    if (!((Boolean) ((Object[]) r)[5])) {
        throw new AclException(auth, aclName);
    }
}
Also used : CellMessage(dmg.cells.nucleus.CellMessage) AclException(dmg.util.AclException) AclException(dmg.util.AclException)

Example 5 with AclException

use of dmg.util.AclException in project dcache by dCache.

the class UserAdminShell method completeRemote.

/**
 * Completes remote commands using a particular cell as a source for completion candidates.
 */
private int completeRemote(CellPath cell, String buffer, int cursor, List<CharSequence> candidates) {
    try {
        Serializable help = sendObject(cell, "help");
        if (help == null) {
            return -1;
        }
        HelpCompleter completer = new HelpCompleter(String.valueOf(help));
        return completer.complete(buffer, cursor, candidates);
    } catch (NoRouteToCellException | CommandException | AclException e) {
        LOGGER.info("Completion failed: {}", e.toString());
        return -1;
    } catch (InterruptedException e) {
        return -1;
    }
}
Also used : Serializable(java.io.Serializable) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) CommandException(dmg.util.CommandException) AclException(dmg.util.AclException) CommandAclException(dmg.util.CommandAclException)

Aggregations

AclException (dmg.util.AclException)8 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)4 AuthorizedString (dmg.util.AuthorizedString)4 CommandAclException (dmg.util.CommandAclException)4 CacheException (diskCacheV111.util.CacheException)2 TimeoutCacheException (diskCacheV111.util.TimeoutCacheException)2 CellEndpoint (dmg.cells.nucleus.CellEndpoint)2 Serializable (java.io.Serializable)2 Maps.immutableEntry (com.google.common.collect.Maps.immutableEntry)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 CellMessage (dmg.cells.nucleus.CellMessage)1 CellPath (dmg.cells.nucleus.CellPath)1 CommandException (dmg.util.CommandException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 DirectoryEntry (org.dcache.util.list.DirectoryEntry)1