Skip to content

Commit

Permalink
feat: thingName wildcard prefix in selectionRule (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwab12 authored and MikeDombo committed Nov 28, 2023
1 parent bb94e01 commit b597065
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TOKEN:
{
< OR: "OR" >
| < AND: "AND" >
| < THINGNAME: (<ALPHANUMERIC> | "-" | "_" | "\\:")+("*")? | "*" > // Only allow escaped colons
| < THINGNAME: ("*")?(<ALPHANUMERIC> | "-" | "_" | "\\:")+("*")? | "*" > // Only allow escaped colons
| < ALPHANUMERIC: [ "a"-"z" ] | [ "A"-"Z" ] | [ "0"-"9" ] >
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,75 @@ private final int jjStopStringLiteralDfa_0(int pos, long active0){
if ((active0 & 0x130L) != 0L)
{
jjmatchedKind = 6;
return 5;
return 6;
}
return -1;
case 1:
if ((active0 & 0x120L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 1;
return 5;
return 6;
}
if ((active0 & 0x10L) != 0L)
return 5;
return 6;
return -1;
case 2:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 2;
return 5;
return 6;
}
if ((active0 & 0x20L) != 0L)
return 5;
return 6;
return -1;
case 3:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 3;
return 5;
return 6;
}
return -1;
case 4:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 4;
return 5;
return 6;
}
return -1;
case 5:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 5;
return 5;
return 6;
}
return -1;
case 6:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 6;
return 5;
return 6;
}
return -1;
case 7:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 7;
return 5;
return 6;
}
return -1;
case 8:
if ((active0 & 0x100L) != 0L)
{
jjmatchedKind = 6;
jjmatchedPos = 8;
return 5;
return 6;
}
return -1;
default :
Expand Down Expand Up @@ -130,7 +130,7 @@ private int jjMoveStringLiteralDfa1_0(long active0){
return jjMoveStringLiteralDfa2_0(active0, 0x20L);
case 82:
if ((active0 & 0x10L) != 0L)
return jjStartNfaWithStates_0(1, 4, 5);
return jjStartNfaWithStates_0(1, 4, 6);
break;
case 104:
return jjMoveStringLiteralDfa2_0(active0, 0x100L);
Expand All @@ -151,7 +151,7 @@ private int jjMoveStringLiteralDfa2_0(long old0, long active0){
{
case 68:
if ((active0 & 0x20L) != 0L)
return jjStartNfaWithStates_0(2, 5, 5);
return jjStartNfaWithStates_0(2, 5, 6);
break;
case 105:
return jjMoveStringLiteralDfa3_0(active0, 0x100L);
Expand Down Expand Up @@ -292,7 +292,7 @@ private int jjStartNfaWithStates_0(int pos, int kind, int state)
private int jjMoveNfa_0(int startState, int curPos)
{
int startsAt = 0;
jjnewStateCnt = 5;
jjnewStateCnt = 6;
int i = 1;
jjstateSet[0] = startState;
int kind = 0x7fffffff;
Expand All @@ -307,7 +307,7 @@ private int jjMoveNfa_0(int startState, int curPos)
{
switch(jjstateSet[--i])
{
case 4:
case 6:
if ((0x3ff200000000000L & l) != 0L)
{
if (kind > 6)
Expand All @@ -319,13 +319,8 @@ else if (curChar == 42)
if (kind > 6)
kind = 6;
}
if ((0x3ff000000000000L & l) != 0L)
{
if (kind > 7)
kind = 7;
}
break;
case 5:
case 4:
if ((0x3ff200000000000L & l) != 0L)
{
if (kind > 6)
Expand All @@ -336,6 +331,12 @@ else if (curChar == 42)
{
if (kind > 6)
kind = 6;
{ jjCheckNAddTwoStates(0, 2); }
}
if ((0x3ff000000000000L & l) != 0L)
{
if (kind > 7)
kind = 7;
}
break;
case 0:
Expand All @@ -352,9 +353,16 @@ else if (curChar == 42)
{ jjCheckNAddStates(0, 2); }
break;
case 3:
if (curChar == 42)
if (curChar == 42 && kind > 6)
kind = 6;
break;
case 5:
if (curChar != 42)
break;
if (kind > 6)
kind = 6;
{ jjCheckNAddTwoStates(0, 2); }
break;
default : break;
}
} while(i != startsAt);
Expand All @@ -366,7 +374,7 @@ else if (curChar < 128)
{
switch(jjstateSet[--i])
{
case 4:
case 6:
if ((0x7fffffe87fffffeL & l) != 0L)
{
if (kind > 6)
Expand All @@ -375,13 +383,8 @@ else if (curChar < 128)
}
else if (curChar == 92)
jjstateSet[jjnewStateCnt++] = 1;
if ((0x7fffffe07fffffeL & l) != 0L)
{
if (kind > 7)
kind = 7;
}
break;
case 5:
case 4:
if ((0x7fffffe87fffffeL & l) != 0L)
{
if (kind > 6)
Expand All @@ -390,6 +393,11 @@ else if (curChar == 92)
}
else if (curChar == 92)
jjstateSet[jjnewStateCnt++] = 1;
if ((0x7fffffe07fffffeL & l) != 0L)
{
if (kind > 7)
kind = 7;
}
break;
case 0:
if ((0x7fffffe87fffffeL & l) == 0L)
Expand Down Expand Up @@ -425,7 +433,7 @@ else if (curChar == 92)
kind = 0x7fffffff;
}
++curPos;
if ((i = jjnewStateCnt) == (startsAt = 5 - (jjnewStateCnt = startsAt)))
if ((i = jjnewStateCnt) == (startsAt = 6 - (jjnewStateCnt = startsAt)))
return curPos;
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) { return curPos; }
Expand Down Expand Up @@ -621,7 +629,7 @@ private void ReInitRounds()
{
int i;
jjround = 0x80000001;
for (i = 5; i-- > 0;)
for (i = 6; i-- > 0;)
jjrounds[i] = 0x80000000;
}

Expand Down Expand Up @@ -666,8 +674,8 @@ public void SwitchTo(int lexState)
};
protected SimpleCharStream input_stream;

private final int[] jjrounds = new int[5];
private final int[] jjstateSet = new int[2 * 5];
private final int[] jjrounds = new int[6];
private final int[] jjstateSet = new int[2 * 6];
private final StringBuilder jjimage = new StringBuilder();
private StringBuilder image = jjimage;
private int jjimageLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public WildcardSuffixAttribute(String attributeValue) {

@Override
public boolean matches(@NonNull String expr) {
if (expr.endsWith("*")) {
if (expr.length() > 1 && expr.startsWith("*") && expr.endsWith("*")) {
return value.contains(expr.substring(1, expr.length() - 1));
} else if (expr.startsWith("*")) {
return value.endsWith(expr.substring(1));
} else if (expr.endsWith("*")) {
return value.startsWith(expr.substring(0, expr.length() - 1));
} else {
return value.equals(expr);
Expand Down
2 changes: 1 addition & 1 deletion src/main/javacc/RuleExpression.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TOKEN:
{
< OR: "OR" >
| < AND: "AND" >
| < THINGNAME: (<ALPHANUMERIC> | "-" | "_" | "\\:")+("*")? | "*" > // Only allow escaped colons
| < THINGNAME: ("*")?(<ALPHANUMERIC> | "-" | "_" | "\\:")+("*")? | "*" > // Only allow escaped colons
| < ALPHANUMERIC: [ "a"-"z" ] | [ "A"-"Z" ] | [ "0"-"9" ] >
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void GIVEN_groupDefinitionAndMatchingSession_WHEN_containsSession_THEN_returnsTr
}

@Test
void GIVEN_groupDefinitionWithWildcardAndMatchingSession_WHEN_containsSession_THEN_returnsTrue()
void GIVEN_groupDefinitionWithTrailingWildcardAndMatchingSession_WHEN_containsSession_THEN_returnsTrue()
throws ParseException {
GroupDefinition groupDefinition = new GroupDefinition("thingName: thing*", "Policy1");
Session session = Mockito.mock(Session.class);
Expand All @@ -42,6 +42,26 @@ void GIVEN_groupDefinitionWithWildcardAndMatchingSession_WHEN_containsSession_TH
assertThat(groupDefinition.containsClientDevice(session), is(true));
}

@Test
void GIVEN_groupDefinitionWithLeadingWildcardAndMatchingSession_WHEN_containsSession_THEN_returnsTrue()
throws ParseException {
GroupDefinition groupDefinition = new GroupDefinition("thingName: *thing", "Policy1");
Session session = Mockito.mock(Session.class);
DeviceAttribute attribute = new WildcardSuffixAttribute("A-thing");
Mockito.when(session.getSessionAttribute(any(), any())).thenReturn(attribute);
assertThat(groupDefinition.containsClientDevice(session), is(true));
}

@Test
void GIVEN_groupDefinitionWithLeadingAndTrailingWildcardAndMatchingSession_WHEN_containsSession_THEN_returnsTrue()
throws ParseException {
GroupDefinition groupDefinition = new GroupDefinition("thingName: *thing*", "Policy1");
Session session = Mockito.mock(Session.class);
DeviceAttribute attribute = new WildcardSuffixAttribute("A-thing-B");
Mockito.when(session.getSessionAttribute(any(), any())).thenReturn(attribute);
assertThat(groupDefinition.containsClientDevice(session), is(true));
}

@Test
void GIVEN_groupDefinitionAndNonMatchingSession_WHEN_containsSession_THEN_returnsFalse() throws ParseException {
GroupDefinition groupDefinition = new GroupDefinition("thingName: thing", "Policy1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ void GIVEN_thingNameWithNoSpace_WHEN_RuleExpression_THEN_ruleIsParsed() throws P
void GIVEN_wildcardAsThingName_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: *");
}
@Test
void GIVEN_thingNameWithLeadingWildcard_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: *Thing");
}

@Test
void GIVEN_thingNameWithTrailingWildcard_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: Thing*");
}

@Test
void GIVEN_thingNameWithLeadingAndTrailingWildcard_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: *Thing*");
}

@Test
void GIVEN_LogicalORExpressionWithWildcards_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: Thing* OR thingName: *Thing");
}

@Test
void GIVEN_LogicalANDExpressionWithWildcard_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
expectValidExpression("thingName: Thing* AND thingName: *Thing");
}

@Test
void GIVEN_basicLogicalORExpression_WHEN_RuleExpression_THEN_ruleIsParsed() throws ParseException {
Expand Down Expand Up @@ -128,7 +152,6 @@ void GIVEN_thingNameWithUnescapedColon_WHEN_RuleExpression_THEN_exceptionIsThrow

@Test
void GIVEN_thingNameWithNonTrailingWildcard_WHEN_RuleExpression_THEN_exceptionIsThrown() {
expectParseException("thingName: *thing");
expectParseException("thingName: thing*2");
}
}
Loading

0 comments on commit b597065

Please sign in to comment.