Search in sources :

Example 26 with XAttr

use of org.apache.hadoop.fs.XAttr in project hadoop by apache.

the class FSDirXAttrOp method filterINodeXAttrs.

/**
   * Filter XAttrs from a list of existing XAttrs. Removes matched XAttrs from
   * toFilter and puts them into filtered. Upon completion,
   * toFilter contains the filter XAttrs that were not found, while
   * fitleredXAttrs contains the XAttrs that were found.
   *
   * @param existingXAttrs Existing XAttrs to be filtered
   * @param toFilter XAttrs to filter from the existing XAttrs
   * @param filtered Return parameter, XAttrs that were filtered
   * @return List of XAttrs that does not contain filtered XAttrs
   */
@VisibleForTesting
static List<XAttr> filterINodeXAttrs(final List<XAttr> existingXAttrs, final List<XAttr> toFilter, final List<XAttr> filtered) throws AccessControlException {
    if (existingXAttrs == null || existingXAttrs.isEmpty() || toFilter == null || toFilter.isEmpty()) {
        return existingXAttrs;
    }
    // Populate a new list with XAttrs that pass the filter
    List<XAttr> newXAttrs = Lists.newArrayListWithCapacity(existingXAttrs.size());
    for (XAttr a : existingXAttrs) {
        boolean add = true;
        for (ListIterator<XAttr> it = toFilter.listIterator(); it.hasNext(); ) {
            XAttr filter = it.next();
            Preconditions.checkArgument(!KEYID_XATTR.equalsIgnoreValue(filter), "The encryption zone xattr should never be deleted.");
            if (UNREADABLE_BY_SUPERUSER_XATTR.equalsIgnoreValue(filter)) {
                throw new AccessControlException("The xattr '" + SECURITY_XATTR_UNREADABLE_BY_SUPERUSER + "' can not be deleted.");
            }
            if (a.equalsIgnoreValue(filter)) {
                add = false;
                it.remove();
                filtered.add(filter);
                break;
            }
        }
        if (add) {
            newXAttrs.add(a);
        }
    }
    return newXAttrs;
}
Also used : AccessControlException(org.apache.hadoop.security.AccessControlException) XAttr(org.apache.hadoop.fs.XAttr) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 27 with XAttr

use of org.apache.hadoop.fs.XAttr in project hadoop by apache.

the class FSDirXAttrOp method getXAttrs.

static List<XAttr> getXAttrs(FSDirectory fsd, final String srcArg, List<XAttr> xAttrs) throws IOException {
    String src = srcArg;
    checkXAttrsConfigFlag(fsd);
    FSPermissionChecker pc = fsd.getPermissionChecker();
    final boolean isRawPath = FSDirectory.isReservedRawName(src);
    boolean getAll = xAttrs == null || xAttrs.isEmpty();
    if (!getAll) {
        XAttrPermissionFilter.checkPermissionForApi(pc, xAttrs, isRawPath);
    }
    final INodesInPath iip = fsd.resolvePath(pc, src, DirOp.READ);
    if (fsd.isPermissionEnabled()) {
        fsd.checkPathAccess(pc, iip, FsAction.READ);
    }
    List<XAttr> all = FSDirXAttrOp.getXAttrs(fsd, iip);
    List<XAttr> filteredAll = XAttrPermissionFilter.filterXAttrsForApi(pc, all, isRawPath);
    if (getAll) {
        return filteredAll;
    }
    if (filteredAll == null || filteredAll.isEmpty()) {
        throw new IOException("At least one of the attributes provided was not found.");
    }
    List<XAttr> toGet = Lists.newArrayListWithCapacity(xAttrs.size());
    for (XAttr xAttr : xAttrs) {
        boolean foundIt = false;
        for (XAttr a : filteredAll) {
            if (xAttr.getNameSpace() == a.getNameSpace() && xAttr.getName().equals(a.getName())) {
                toGet.add(a);
                foundIt = true;
                break;
            }
        }
        if (!foundIt) {
            throw new IOException("At least one of the attributes provided was not found.");
        }
    }
    return toGet;
}
Also used : IOException(java.io.IOException) XAttr(org.apache.hadoop.fs.XAttr)

Example 28 with XAttr

use of org.apache.hadoop.fs.XAttr in project hadoop by apache.

the class FSDirErasureCodingOp method getErasureCodingPolicyForPath.

private static ErasureCodingPolicy getErasureCodingPolicyForPath(FSDirectory fsd, INodesInPath iip) throws IOException {
    Preconditions.checkNotNull(iip, "INodes cannot be null");
    fsd.readLock();
    try {
        List<INode> inodes = iip.getReadOnlyINodes();
        for (int i = inodes.size() - 1; i >= 0; i--) {
            final INode inode = inodes.get(i);
            if (inode == null) {
                continue;
            }
            if (inode.isFile()) {
                byte id = inode.asFile().getErasureCodingPolicyID();
                return id < 0 ? null : ErasureCodingPolicyManager.getPolicyByID(id);
            }
            // TODO: properly support symlinks
            if (inode.isSymlink()) {
                return null;
            }
            final XAttrFeature xaf = inode.getXAttrFeature();
            if (xaf != null) {
                XAttr xattr = xaf.getXAttr(XATTR_ERASURECODING_POLICY);
                if (xattr != null) {
                    ByteArrayInputStream bIn = new ByteArrayInputStream(xattr.getValue());
                    DataInputStream dIn = new DataInputStream(bIn);
                    String ecPolicyName = WritableUtils.readString(dIn);
                    return ErasureCodingPolicyManager.getPolicyByName(ecPolicyName);
                }
            }
        }
    } finally {
        fsd.readUnlock();
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) XAttr(org.apache.hadoop.fs.XAttr)

Example 29 with XAttr

use of org.apache.hadoop.fs.XAttr in project hadoop by apache.

the class FSDirErasureCodingOp method removeErasureCodingPolicyXAttr.

private static List<XAttr> removeErasureCodingPolicyXAttr(final FSNamesystem fsn, final INodesInPath srcIIP) throws IOException {
    FSDirectory fsd = fsn.getFSDirectory();
    assert fsd.hasWriteLock();
    Preconditions.checkNotNull(srcIIP, "INodes cannot be null");
    String src = srcIIP.getPath();
    final INode inode = srcIIP.getLastINode();
    if (inode == null) {
        throw new FileNotFoundException("Path not found: " + srcIIP.getPath());
    }
    if (!inode.isDirectory()) {
        throw new IOException("Cannot unset an erasure coding policy " + "on a file " + src);
    }
    // Check whether the directory has a specific erasure coding policy
    // directly on itself.
    final XAttr ecXAttr = getErasureCodingPolicyXAttrForINode(fsn, inode);
    if (ecXAttr == null) {
        return null;
    }
    final List<XAttr> xattrs = Lists.newArrayListWithCapacity(1);
    xattrs.add(ecXAttr);
    FSDirXAttrOp.unprotectedRemoveXAttrs(fsd, srcIIP.getPath(), xattrs);
    return xattrs;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) XAttr(org.apache.hadoop.fs.XAttr)

Example 30 with XAttr

use of org.apache.hadoop.fs.XAttr in project hadoop by apache.

the class FSDirErasureCodingOp method getErasureCodingPolicyXAttrForINode.

private static XAttr getErasureCodingPolicyXAttrForINode(FSNamesystem fsn, INode inode) throws IOException {
    // INode can be null
    if (inode == null) {
        return null;
    }
    FSDirectory fsd = fsn.getFSDirectory();
    fsd.readLock();
    try {
        // TODO: properly support symlinks
        if (inode.isSymlink()) {
            return null;
        }
        final XAttrFeature xaf = inode.getXAttrFeature();
        if (xaf != null) {
            XAttr xattr = xaf.getXAttr(XATTR_ERASURECODING_POLICY);
            if (xattr != null) {
                return xattr;
            }
        }
    } finally {
        fsd.readUnlock();
    }
    return null;
}
Also used : XAttr(org.apache.hadoop.fs.XAttr)

Aggregations

XAttr (org.apache.hadoop.fs.XAttr)43 IOException (java.io.IOException)13 Test (org.junit.Test)7 HdfsProtos (org.apache.hadoop.hdfs.protocol.proto.HdfsProtos)5 FileNotFoundException (java.io.FileNotFoundException)4 XAttrProto (org.apache.hadoop.hdfs.protocol.proto.XAttrProtos.XAttrProto)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 HadoopIllegalArgumentException (org.apache.hadoop.HadoopIllegalArgumentException)2 CipherSuite (org.apache.hadoop.crypto.CipherSuite)2 CryptoProtocolVersion (org.apache.hadoop.crypto.CryptoProtocolVersion)2 AccessControlException (org.apache.hadoop.security.AccessControlException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1