use of org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode 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) {
}
}
use of org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode 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;
}
use of org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode 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;
}
use of org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode 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));
}
}
use of org.apache.hadoop.hbase.security.visibility.expression.ExpressionNode 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;
}
Aggregations