Search in sources :

Example 1 with PrefixRestriction

use of org.dcache.auth.attributes.PrefixRestriction in project dcache by dCache.

the class Gplazma2LoginStrategy method setUploadPath.

public void setUploadPath(String s) {
    if (Strings.isNullOrEmpty(s) || !s.startsWith("/")) {
        _createPrefixRestriction = path -> new PrefixRestriction(path);
    } else {
        FsPath uploadPath = FsPath.create(s);
        _createPrefixRestriction = path -> new PrefixRestriction(path, uploadPath);
    }
}
Also used : PrefixRestriction(org.dcache.auth.attributes.PrefixRestriction) FsPath(diskCacheV111.util.FsPath)

Example 2 with PrefixRestriction

use of org.dcache.auth.attributes.PrefixRestriction in project dcache by dCache.

the class MacaroonRequestHandler method buildContext.

private MacaroonContext buildContext(String target, Request request) throws ErrorResponseException {
    MacaroonContext context = new MacaroonContext();
    FsPath desiredPath = _pathMapper.asDcachePath(request, target);
    FsPath userRoot = FsPath.ROOT;
    FsPath prefixRestrictionPath = null;
    for (LoginAttribute attr : AuthenticationHandler.getLoginAttributes(request)) {
        if (attr instanceof HomeDirectory) {
            context.setHome(FsPath.ROOT.resolve(((HomeDirectory) attr).getHome()));
        } else if (attr instanceof RootDirectory) {
            userRoot = FsPath.ROOT.resolve(((RootDirectory) attr).getRoot());
        } else if (attr instanceof Expiry) {
            context.updateExpiry(((Expiry) attr).getExpiry());
        } else if (attr instanceof DenyActivityRestriction) {
            context.removeActivities(((DenyActivityRestriction) attr).getDenied());
        } else if (attr instanceof PrefixRestriction) {
            ImmutableSet<FsPath> paths = ((PrefixRestriction) attr).getPrefixes();
            if (target.equals("/")) {
                checkArgument(paths.size() == 1, "Cannot serialise with multiple path restrictions");
                prefixRestrictionPath = paths.iterator().next();
            } else {
                prefixRestrictionPath = paths.stream().filter(desiredPath::hasPrefix).findFirst().orElseThrow(() -> new ErrorResponseException(SC_BAD_REQUEST, "Bad request path: Desired path not within existing path"));
            }
        } else if (attr instanceof Restriction) {
            throw new ErrorResponseException(SC_BAD_REQUEST, "Cannot serialise restriction " + attr.getClass().getSimpleName());
        } else if (attr instanceof MaxUploadSize) {
            try {
                context.updateMaxUpload(((MaxUploadSize) attr).getMaximumSize());
            } catch (InvalidCaveatException e) {
                throw new ErrorResponseException(SC_BAD_REQUEST, "Cannot add max-upload: " + e.getMessage());
            }
        }
    }
    Subject subject = getSubject();
    context.setUid(Subjects.getUid(subject));
    context.setGids(Subjects.getGids(subject));
    context.setUsername(Subjects.getUserName(subject));
    FsPath effectiveRoot = _pathMapper.effectiveRoot(userRoot, m -> new ErrorResponseException(SC_BAD_REQUEST, m));
    context.setRoot(effectiveRoot);
    FsPath path = prefixRestrictionPath != null ? prefixRestrictionPath : target.equals("/") ? null : desiredPath;
    if (path != null) {
        context.setPath(path.stripPrefix(effectiveRoot));
    }
    return context;
}
Also used : PrefixRestriction(org.dcache.auth.attributes.PrefixRestriction) InvalidCaveatException(org.dcache.macaroons.InvalidCaveatException) HomeDirectory(org.dcache.auth.attributes.HomeDirectory) LoginAttribute(org.dcache.auth.attributes.LoginAttribute) MaxUploadSize(org.dcache.auth.attributes.MaxUploadSize) RootDirectory(org.dcache.auth.attributes.RootDirectory) Subject(javax.security.auth.Subject) MacaroonContext(org.dcache.macaroons.MacaroonContext) Restriction(org.dcache.auth.attributes.Restriction) PrefixRestriction(org.dcache.auth.attributes.PrefixRestriction) DenyActivityRestriction(org.dcache.auth.attributes.DenyActivityRestriction) Expiry(org.dcache.auth.attributes.Expiry) DenyActivityRestriction(org.dcache.auth.attributes.DenyActivityRestriction) FsPath(diskCacheV111.util.FsPath)

Example 3 with PrefixRestriction

use of org.dcache.auth.attributes.PrefixRestriction in project dcache by dCache.

the class ConfigurationParser method parseAttributes.

private List<LoginAttribute> parseAttributes(String description) throws BadLineException {
    List<LoginAttribute> attributes = new ArrayList<>();
    boolean isReadOnly = false;
    Set<Class<? extends LoginAttribute>> addedAttributes = new HashSet<>();
    for (String attr : Splitter.on(' ').omitEmptyStrings().split(description)) {
        try {
            if (attr.equals("read-only")) {
                checkBadLine(!isReadOnly, "already defined 'read-only'");
                isReadOnly = true;
                attributes.add(Restrictions.readOnly());
                continue;
            }
            int idx = attr.indexOf(':');
            checkBadLine(idx > -1, "Missing ':'");
            checkBadLine(idx != 0, "Missing type");
            checkBadLine(idx < attr.length() - 1, "Missing argument");
            String type = attr.substring(0, idx);
            String arg = attr.substring(idx + 1);
            if (PATH_ATTRIBUTES.contains(type)) {
                checkBadLine(arg.startsWith("/"), "Argument must be an absolute" + " path");
            }
            LoginAttribute attribute;
            switch(type) {
                case "root":
                    attribute = new RootDirectory(arg);
                    break;
                case "home":
                    attribute = new HomeDirectory(arg);
                    break;
                case "prefix":
                    attribute = new PrefixRestriction(FsPath.create(arg));
                    break;
                case "max-upload":
                    try {
                        attribute = new MaxUploadSize(SIZE_PARSER.parse(arg));
                    } catch (NumberFormatException e) {
                        throw new BadLineException("Bad file size: " + e.getMessage());
                    }
                    break;
                default:
                    throw new BadLineException("Unknown type \"" + type + "\"");
            }
            if (!addedAttributes.add(attribute.getClass())) {
                throw new BadLineException("Multiple " + type + " defined.");
            }
            attributes.add(attribute);
        } catch (BadLineException e) {
            throw new BadLineException("Bad attribute \"" + attr + "\": " + e.getMessage());
        }
    }
    return attributes;
}
Also used : PrefixRestriction(org.dcache.auth.attributes.PrefixRestriction) HomeDirectory(org.dcache.auth.attributes.HomeDirectory) LoginAttribute(org.dcache.auth.attributes.LoginAttribute) MaxUploadSize(org.dcache.auth.attributes.MaxUploadSize) ArrayList(java.util.ArrayList) RootDirectory(org.dcache.auth.attributes.RootDirectory) HashSet(java.util.HashSet)

Aggregations

PrefixRestriction (org.dcache.auth.attributes.PrefixRestriction)3 FsPath (diskCacheV111.util.FsPath)2 HomeDirectory (org.dcache.auth.attributes.HomeDirectory)2 LoginAttribute (org.dcache.auth.attributes.LoginAttribute)2 MaxUploadSize (org.dcache.auth.attributes.MaxUploadSize)2 RootDirectory (org.dcache.auth.attributes.RootDirectory)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Subject (javax.security.auth.Subject)1 DenyActivityRestriction (org.dcache.auth.attributes.DenyActivityRestriction)1 Expiry (org.dcache.auth.attributes.Expiry)1 Restriction (org.dcache.auth.attributes.Restriction)1 InvalidCaveatException (org.dcache.macaroons.InvalidCaveatException)1 MacaroonContext (org.dcache.macaroons.MacaroonContext)1