Search in sources :

Example 11 with NonLeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.

the class TestExpressionParser method testCasesSeperatedByDoubleQuotes.

@Test
public void testCasesSeperatedByDoubleQuotes() throws Exception {
    ExpressionNode node = null;
    try {
        node = parser.parse("'&\"|+&?");
        fail("Excpetion must be thrown as there are special characters without quotes");
    } catch (ParseException e) {
    }
    node = parser.parse(CellVisibility.quote("'") + "&" + CellVisibility.quote("\"") + "|" + CellVisibility.quote("+" + "&" + "?"));
    assertTrue(node instanceof NonLeafExpressionNode);
    NonLeafExpressionNode nlNode = (NonLeafExpressionNode) node;
    assertEquals(Operator.OR, nlNode.getOperator());
    assertEquals(2, nlNode.getChildExps().size());
    assertEquals("+" + "&" + "?", ((LeafExpressionNode) nlNode.getChildExps().get(1)).getIdentifier());
    assertTrue(nlNode.getChildExps().get(0) instanceof NonLeafExpressionNode);
    nlNode = (NonLeafExpressionNode) nlNode.getChildExps().get(0);
    assertEquals(Operator.AND, nlNode.getOperator());
    assertEquals(2, nlNode.getChildExps().size());
    assertEquals("\"", ((LeafExpressionNode) nlNode.getChildExps().get(1)).getIdentifier());
    assertEquals("'", ((LeafExpressionNode) nlNode.getChildExps().get(0)).getIdentifier());
    try {
        node = parser.parse(CellVisibility.quote("'&\\") + "|" + CellVisibility.quote("+" + "&" + "\\") + CellVisibility.quote("$$\""));
        fail("Excpetion must be thrown as there is not operator");
    } catch (ParseException e) {
    }
    node = parser.parse(CellVisibility.quote("'" + "&" + "\\") + "|" + CellVisibility.quote("?" + "&" + "\\") + "&" + CellVisibility.quote("$$\""));
    assertTrue(node instanceof NonLeafExpressionNode);
    nlNode = (NonLeafExpressionNode) node;
    assertEquals(Operator.AND, nlNode.getOperator());
    assertEquals(2, nlNode.getChildExps().size());
    assertEquals("$$\"", ((LeafExpressionNode) nlNode.getChildExps().get(1)).getIdentifier());
    assertTrue(nlNode.getChildExps().get(0) instanceof NonLeafExpressionNode);
    nlNode = (NonLeafExpressionNode) nlNode.getChildExps().get(0);
    assertEquals(Operator.OR, nlNode.getOperator());
    assertEquals(2, nlNode.getChildExps().size());
    assertEquals("'" + "&" + "\\", ((LeafExpressionNode) nlNode.getChildExps().get(0)).getIdentifier());
    assertEquals("?" + "&" + "\\", ((LeafExpressionNode) nlNode.getChildExps().get(1)).getIdentifier());
    try {
        node = parser.parse(CellVisibility.quote("+&\\") + "|" + CellVisibility.quote("'&\\") + "&" + "\"$$");
        fail("Excpetion must be thrown as there is no end quote");
    } catch (ParseException e) {
    }
}
Also used : NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) ExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) Test(org.junit.Test)

Example 12 with NonLeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.

the class VisibilityUtils method createVisibilityExpTags.

public static List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat, boolean checkAuths, Set<Integer> auths, VisibilityLabelOrdinalProvider ordinalProvider) throws IOException {
    ExpressionNode node = null;
    try {
        node = EXP_PARSER.parse(visExpression);
    } catch (ParseException e) {
        throw new IOException(e);
    }
    node = EXP_EXPANDER.expand(node);
    List<Tag> tags = new ArrayList<>();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    List<Integer> labelOrdinals = new ArrayList<>();
    // tag indicates we are supporting deletes with cell visibility
    if (withSerializationFormat) {
        tags.add(VisibilityUtils.SORTED_ORDINAL_SERIALIZATION_FORMAT_TAG);
    }
    if (node.isSingleNode()) {
        getLabelOrdinals(node, labelOrdinals, auths, checkAuths, ordinalProvider);
        writeLabelOrdinalsToStream(labelOrdinals, dos);
        tags.add(new ArrayBackedTag(VISIBILITY_TAG_TYPE, baos.toByteArray()));
        baos.reset();
    } else {
        NonLeafExpressionNode nlNode = (NonLeafExpressionNode) node;
        if (nlNode.getOperator() == Operator.OR) {
            for (ExpressionNode child : nlNode.getChildExps()) {
                getLabelOrdinals(child, labelOrdinals, auths, checkAuths, ordinalProvider);
                writeLabelOrdinalsToStream(labelOrdinals, dos);
                tags.add(new ArrayBackedTag(VISIBILITY_TAG_TYPE, baos.toByteArray()));
                baos.reset();
                labelOrdinals.clear();
            }
        } else {
            getLabelOrdinals(nlNode, labelOrdinals, auths, checkAuths, ordinalProvider);
            writeLabelOrdinalsToStream(labelOrdinals, dos);
            tags.add(new ArrayBackedTag(VISIBILITY_TAG_TYPE, baos.toByteArray()));
            baos.reset();
        }
    }
    return tags;
}
Also used : ExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag) Tag(org.apache.hadoop.hbase.Tag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag)

Example 13 with NonLeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.

the class ExpressionExpander method expand.

public ExpressionNode expand(ExpressionNode src) {
    if (!src.isSingleNode()) {
        NonLeafExpressionNode nlExp = (NonLeafExpressionNode) src;
        List<ExpressionNode> childExps = nlExp.getChildExps();
        Operator outerOp = nlExp.getOperator();
        if (isToBeExpanded(childExps)) {
            // Any of the child exp is a non leaf exp with & or | operator
            NonLeafExpressionNode newNode = new NonLeafExpressionNode(nlExp.getOperator());
            for (ExpressionNode exp : childExps) {
                if (exp.isSingleNode()) {
                    newNode.addChildExp(exp);
                } else {
                    newNode.addChildExp(expand(exp));
                }
            }
            nlExp = expandNonLeaf(newNode, outerOp);
        }
        return nlExp;
    }
    if (src instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) src).getOperator() == Operator.NOT) {
        // Negate the exp
        return negate((NonLeafExpressionNode) src);
    }
    return src;
}
Also used : Operator(org.apache.hadoop.hbase.security.visibility.expression.Operator) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) ExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode)

Example 14 with NonLeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.

the class ExpressionParser method processANDorOROp.

private void processANDorOROp(Operator op, Stack<ExpressionNode> expStack, String expS, int index) throws ParseException {
    if (expStack.isEmpty()) {
        throw new ParseException("Error parsing expression " + expS + " at column : " + index);
    }
    ExpressionNode top = expStack.pop();
    if (top.isSingleNode()) {
        if (top == LeafExpressionNode.OPEN_PARAN_NODE) {
            throw new ParseException("Error parsing expression " + expS + " at column : " + index);
        }
        expStack.push(new NonLeafExpressionNode(op, top));
    } else {
        NonLeafExpressionNode nlTop = (NonLeafExpressionNode) top;
        if (nlTop.getChildExps().size() != 2) {
            throw new ParseException("Error parsing expression " + expS + " at column : " + index);
        }
        expStack.push(new NonLeafExpressionNode(op, nlTop));
    }
}
Also used : NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) ExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode)

Example 15 with NonLeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.

the class ExpressionParser method parse.

public ExpressionNode parse(String expS) throws ParseException {
    expS = expS.trim();
    Stack<ExpressionNode> expStack = new Stack<>();
    int index = 0;
    byte[] exp = Bytes.toBytes(expS);
    int endPos = exp.length;
    while (index < endPos) {
        byte b = exp[index];
        switch(b) {
            case OPEN_PARAN:
                processOpenParan(expStack, expS, index);
                index = skipSpaces(exp, index);
                break;
            case CLOSE_PARAN:
                processCloseParan(expStack, expS, index);
                index = skipSpaces(exp, index);
                break;
            case AND:
            case OR:
                processANDorOROp(getOperator(b), expStack, expS, index);
                index = skipSpaces(exp, index);
                break;
            case NOT:
                processNOTOp(expStack, expS, index);
                break;
            case DOUBLE_QUOTES:
                int labelOffset = ++index;
                // We have to rewrite the expression within double quotes as incase of expressions 
                // with escape characters we may have to avoid them as the original expression did
                // not have them
                List<Byte> list = new ArrayList<>();
                while (index < endPos && !endDoubleQuotesFound(exp[index])) {
                    if (exp[index] == '\\') {
                        index++;
                        if (exp[index] != '\\' && exp[index] != '"')
                            throw new ParseException("invalid escaping with quotes " + expS + " at column : " + index);
                    }
                    list.add(exp[index]);
                    index++;
                }
                // The expression has come to the end. still no double quotes found 
                if (index == endPos) {
                    throw new ParseException("No terminating quotes " + expS + " at column : " + index);
                }
                // This could be costly. but do we have any alternative?
                // If we don't do this way then we may have to handle while checking the authorizations.
                // Better to do it here.
                byte[] array = com.google.common.primitives.Bytes.toArray(list);
                String leafExp = Bytes.toString(array).trim();
                if (leafExp.isEmpty()) {
                    throw new ParseException("Error parsing expression " + expS + " at column : " + index);
                }
                processLabelExpNode(new LeafExpressionNode(leafExp), expStack, expS, index);
                index = skipSpaces(exp, index);
                break;
            default:
                labelOffset = index;
                do {
                    if (!VisibilityLabelsValidator.isValidAuthChar(exp[index])) {
                        throw new ParseException("Error parsing expression " + expS + " at column : " + index);
                    }
                    index++;
                } while (index < endPos && !isEndOfLabel(exp[index]));
                leafExp = new String(exp, labelOffset, index - labelOffset).trim();
                if (leafExp.isEmpty()) {
                    throw new ParseException("Error parsing expression " + expS + " at column : " + index);
                }
                processLabelExpNode(new LeafExpressionNode(leafExp), expStack, expS, index);
                // We already crossed the label node index. So need to reduce 1 here.
                index--;
                index = skipSpaces(exp, index);
        }
        index++;
    }
    if (expStack.size() != 1) {
        throw new ParseException("Error parsing expression " + expS);
    }
    ExpressionNode top = expStack.pop();
    if (top == LeafExpressionNode.OPEN_PARAN_NODE) {
        throw new ParseException("Error parsing expression " + expS);
    }
    if (top instanceof NonLeafExpressionNode) {
        NonLeafExpressionNode nlTop = (NonLeafExpressionNode) top;
        if (nlTop.getOperator() == Operator.NOT) {
            if (nlTop.getChildExps().size() != 1) {
                throw new ParseException("Error parsing expression " + expS);
            }
        } else if (nlTop.getChildExps().size() != 2) {
            throw new ParseException("Error parsing expression " + expS);
        }
    }
    return top;
}
Also used : ArrayList(java.util.ArrayList) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) Stack(java.util.Stack) NonLeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode) ExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode) LeafExpressionNode(org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode)

Aggregations

ExpressionNode (org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode)15 LeafExpressionNode (org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode)15 NonLeafExpressionNode (org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode)15 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 ArrayBackedTag (org.apache.hadoop.hbase.ArrayBackedTag)2 Tag (org.apache.hadoop.hbase.Tag)2 Operator (org.apache.hadoop.hbase.security.visibility.expression.Operator)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Stack (java.util.Stack)1