Search in sources :

Example 6 with LeafExpressionNode

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

the class ExpAsStringVisibilityLabelServiceImpl method extractLabels.

private void extractLabels(ExpressionNode node, List<String> labels, List<String> notLabels) {
    if (node.isSingleNode()) {
        if (node instanceof NonLeafExpressionNode) {
            // This is a NOT node.
            LeafExpressionNode lNode = (LeafExpressionNode) ((NonLeafExpressionNode) node).getChildExps().get(0);
            notLabels.add(lNode.getIdentifier());
        } else {
            labels.add(((LeafExpressionNode) node).getIdentifier());
        }
    } else {
        // A non leaf expression of labels with & operator.
        NonLeafExpressionNode nlNode = (NonLeafExpressionNode) node;
        assert nlNode.getOperator() == Operator.AND;
        List<ExpressionNode> childExps = nlNode.getChildExps();
        for (ExpressionNode child : childExps) {
            extractLabels(child, labels, notLabels);
        }
    }
}
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) 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)

Example 7 with LeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode 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 8 with LeafExpressionNode

use of org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode 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)8 LeafExpressionNode (org.apache.hadoop.hbase.security.visibility.expression.LeafExpressionNode)8 NonLeafExpressionNode (org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 Operator (org.apache.hadoop.hbase.security.visibility.expression.Operator)1