Commit a584b833 authored by Jonathan Shahen's avatar Jonathan Shahen
Browse files

Done importing the Mohawk-T input format, ready to start converting to other code bases

TODO: add more regression tests
TODO: add test-cases that are actually useful and have a known outcome
TODO: remove skeleton code that isn't used anymore
parent c5d2aa0e
...@@ -8,5 +8,6 @@ ...@@ -8,5 +8,6 @@
<classpathentry kind="lib" path="lib/stringtemplate.jar"/> <classpathentry kind="lib" path="lib/stringtemplate.jar"/>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/> <classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/> <classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
...@@ -8,5 +8,36 @@ ...@@ -8,5 +8,36 @@
* If you do not want to use Eclipse, just run "ant build.xml" * If you do not want to use Eclipse, just run "ant build.xml"
4. 4.
# Workflows
## Updating mohawk.converter.generated.MohawkTARBAC.g4 (Using Eclipse)
1. Update the file mohawk.converter.generated.MohawkTARBAC.g4 and save (you can ignore if it auto compiles)
2. In the ant window in eclipse run "mohawk_converter -> parser" (where mohawk_converter is the build.xml name)
3. Go to the project "mohawk_converter" (I called it 'Mohawk-T Conversion Tool') and "refresh it":
Right click on the project and click "Refresh" or click on the project and click 'F5' (the single key)
This step is very important
4. Check for any errors that appear in the Java code located in mohawk.converter.generated.*
5. Open up the JUnit window and run the tests associated with the mohawk conversion tool
If you do not see mohawk.converter.testing.RegressionTests then find the Java file and right click it and go to "Run As" and click on "JUnit Test"
6. Repeat until all tests have past
## Updating mohawk.converter.generated.MohawkTARBAC.g4 (Using Commandline)
NOTE: the scripts are surrently only tested on windows, so some tweaking may have to be made (like replacing 'cmd' with 'bash')
1. Update the file mohawk.converter.generated.MohawkTARBAC.g4 and save
2. Run "ant build regression", check that the output is all correct (it builds the source code and then runs the JUnit tests)
3. Repeat until done
# Troubleshooting
## RegressionTests.java cannot see other Project Files (in Eclipse)
This is a weird bug with eclipse only, check the ANT build file first to make sure it can build the JAR file properly.
To solve this problem:
1. Right click the project -> Build Path -> Configure Build Path
2. Go to the Libraries Tab
3. Click "Add Library" and then click on JUnit
4. Make sure to install JUnit 4 or higher (I doubt a lower version would give you trouble, but JUnit 4 is what is being used to build the JAR file in the ANT script)
# Contact # Contact
Author: Jonathan Shahen <jonathan.shahen@gmail.com> Author: Jonathan Shahen <jonathan.shahen@gmail.com>
\ No newline at end of file
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
<echo>java -cp ${dist}/${ant.project.name}.jar org.junit.runner.JUnitCore mohawk.testing.RegressionTests</echo> <echo>java -cp ${dist}/${ant.project.name}.jar org.junit.runner.JUnitCore mohawk.testing.RegressionTests</echo>
<exec executable="cmd" dir="." failonerror="true"> <exec executable="cmd" dir="." failonerror="true">
<arg value="/c" /> <arg value="/c" />
<arg value="java -cp &quot;${dist}/${ant.project.name}.jar&quot; org.junit.runner.JUnitCore mohawk.testing.RegressionTests" /> <arg value="java -cp &quot;${dist}/${ant.project.name}.jar&quot; org.junit.runner.JUnitCore mohawk.converter.testing.RegressionTests" />
</exec> </exec>
</target> </target>
......
/* everything after a hash tag, until the newline, is considered a comment and ignored /* everything after a //, until the newline, is considered a comment and ignored
* *
* white space is ignored, this includes tabs and spaces and newline characters * white space is ignored, this includes tabs and spaces and newline characters
* You can put as much white space anywhere and it will be ignored * You can put as much white space anywhere and it will be ignored
* *
* control chain: remove comments -> remove whitespace -> parse text into internal structure -> convert
* *
* The order of the document doesn't matter: MaxRoles can be last, first, or somewhere in the middle and same goes for all the others * The order of the document doesn't matter: Query can be last, first, or somewhere in the middle and same goes for all the others
*/ */
MaxRoles : 15 // roles count from 0 to MaxRoles - 1; represented below with 'role0' (without quotes, and the 0 can be replaced with any number in the appropriate range)
MaxTimeSlots: 10 // time slots count from 0 to MaxTimeSlots - 1; represented below with 't0' (without quotes, and the 0 can be replaced with any number in the appropriate range)
CanAssign { CanAssign {
// <Admin role, starting timeslot - end timeslot for admin role, role preconditions, [time slot rule gives role to user for, another time slot (optional)...],role to give to the user> // <Admin role, starting timeslot - end timeslot for admin role, role preconditions, [time slot rule gives role to user for, another time slot (optional)...],role to give to the user>
<role0,t0-t5,TRUE,[t1,t1],role1> //(1) <role0,t0-t5,TRUE,[t1,t1],role1> //(1)
<role0,t0-t0,role1 & NOT role2 & role3,[t1,t2],role2> //(2) <role0,t0-t0,role1 & NOT~ role2 & role3,[t1,t2],role2> //(2)
} }
CanRevoke { CanRevoke {
......
/* This test is to show that unique role names can be used, they must only contain [0-9a-zA-Z]+
*
* NOT~ cannot have a space between NOT and the tilde ~
*/
CanAssign {
<manager,t0-t5,TRUE,[t1,t1],employee> //(1)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(2)
}
CanRevoke {
// same as above
}
CanEnable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(3)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(4)
}
CanDisable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(5)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(6)
}
Query: t0,[officer,manager,employee]
\ No newline at end of file
/* This test is to show that the order of the blocks does not matter
*/
Query: t0,[officer,manager,employee]
CanDisable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(5)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(6)
}
CanEnable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(3)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(4)
}
CanRevoke {
// same as above
}
CanAssign {
<manager,t0-t5,TRUE,[t1,t1],employee> //(1)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(2)
}
package mohawk.converter; package mohawk.converter;
import java.util.logging.Logger;
public class Converter { public class Converter {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public static void main(String[] args) { public static void main(String[] args) {
ConverterInstance inst = new ConverterInstance(); ConverterInstance inst = new ConverterInstance();
......
...@@ -8,8 +8,8 @@ import java.util.logging.FileHandler; ...@@ -8,8 +8,8 @@ import java.util.logging.FileHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mohawk.logging.MohawkCSVFileFormatter; import mohawk.converter.logging.MohawkCSVFileFormatter;
import mohawk.logging.MohawkConsoleFormatter; import mohawk.converter.logging.MohawkConsoleFormatter;
import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
......
...@@ -5,37 +5,62 @@ grammar MohawkTARBAC; ...@@ -5,37 +5,62 @@ grammar MohawkTARBAC;
@header { @header {
package mohawk.converter.generated; package mohawk.converter.generated;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import mohawk.converter.pieces.*;
} }
@members { @members {
boolean maxroles_found = false; public final Logger logger = Logger.getLogger("mohawk-converter");
boolean maxtimeslots_found = false;
boolean canassign_found = false; public boolean canassign_found = false;
boolean canrevoke_found = false; public boolean canrevoke_found = false;
boolean canenable_found = false; public boolean canenable_found = false;
boolean candisable_found = false; public boolean candisable_found = false;
boolean query_found = false; public boolean query_found = false;
int tabsize = 1;
/* Global States */
public SortedSet<String> roles = new TreeSet<String>();
public SortedSet<Integer> timeslots = new TreeSet<Integer>();
/* Blocks */
public CanAssign canAssign = new CanAssign();
public CanRevoke canRevoke = new CanRevoke();
public CanEnable canEnable = new CanEnable();
public CanDisable canDisable = new CanDisable();
public Query query = new Query();
private void logmsg(String msg) {
System.out.println(StringUtils.repeat(" ", tabsize) + msg);
}
} }
init init
: :
stat stat stat stat stat stat stat /*stat stat*/ stat stat stat stat stat
; ;
stat stat
: :
{maxroles_found==false}? // {maxroles_found==false}?
//
maxroles // maxroles
{maxroles_found=true;} // {maxroles_found=true;}
//
| // |
{maxtimeslots_found==false}? // {maxtimeslots_found==false}?
//
maxtimeslots // maxtimeslots
{maxtimeslots_found=true;} // {maxtimeslots_found=true;}
//
| // |
{canassign_found==false}? {canassign_found==false}?
canassign canassign
...@@ -67,6 +92,7 @@ stat ...@@ -67,6 +92,7 @@ stat
; ;
/*
maxroles maxroles
: :
MaxRoles Colon INT MaxRoles Colon INT
...@@ -76,59 +102,198 @@ maxtimeslots ...@@ -76,59 +102,198 @@ maxtimeslots
: :
MaxTimeSlots Colon INT MaxTimeSlots Colon INT
; ;
*/
query
:
Query Colon Timeslot Comma RoleArray
;
canassign canassign
: :
CanAssign LeftBrace rules RightBrace {logmsg("Entering Can Assign");tabsize++;}
CanAssign LeftBrace
(
myrule [RuleType.ASSIGN]
{
canAssign.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Assign");}
; ;
canrevoke canrevoke
: :
CanRevoke LeftBrace rules RightBrace {logmsg("Entering Can Revoke");tabsize++;}
CanRevoke LeftBrace
(
myrule [RuleType.REVOKE]
{
canRevoke.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Revoke");}
; ;
canenable canenable
: :
CanEnable LeftBrace rules RightBrace {logmsg("Entering Can Enable"); tabsize++;}
CanEnable LeftBrace
(
myrule [RuleType.ENABLE]
{
canEnable.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Enable");}
; ;
candisable candisable
: :
CanDisable LeftBrace rules RightBrace {logmsg("Entering Can Disable");tabsize++;}
CanDisable LeftBrace
(
myrule [RuleType.DISABLE]
{
canDisable.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Disable");}
; ;
rules query
: :
myrule* Query Colon t=timeslot Comma ra=roleArray
{
query._timeslot = $t.val;
query._roles = $ra.r;
}
; ;
myrule myrule [RuleType rt] returns [Rule r]
: :
LeftAngle Role Comma TimeInteral Comma precondition Comma TimeslotArray Comma {logmsg("Adding Rule: "); tabsize++;}
Role RightAngle
LeftAngle adminRole = myrole Comma ti = timeInteral Comma pc=precondition Comma
tsa=timeslotArray Comma role = myrole RightAngle
{
$r = new Rule();
$r._type = rt;
$r._adminRole = $adminRole.role;
$r._adminTimeSlots[0] = $ti.t0;
$r._adminTimeSlots[1] = $ti.t1;
$r._preconditions = $pc.p;
$r._roleSchedule = $tsa.t;
$r._role = $role.role;
tabsize--;
logmsg($r.toString());
}
; ;
precondition precondition returns [ArrayList<Role> p] @init {
$p = new ArrayList<Role>();
}
: :
rolecondition a = rolecondition
{$p.add($a.r);}
(
AND b = rolecondition
{$p.add($b.r);}
)*
| 'TRUE' | 'TRUE'
; ;
rolecondition rolecondition returns [Role r] @init {
boolean not = false;
}
: :
Not? Role
( (
AND Not? Role Not
)* {not = true;}
)? myrole
{
$r = new Role($myrole.rolet, not);
/*logmsg("Role: "+ $r);*/
}
;
roleArray returns [ArrayList<Role> r] @init {
$r = new ArrayList<Role>();
}
:
LeftBracket a = myrole
{$r.add($a.role);}
(
Comma b = myrole
{$r.add($b.role);}
)* RightBracket
;
timeslotArray returns [ArrayList<Integer> t] @init {
$t = new ArrayList<Integer>();
}
:
LeftBracket a = timeslot
{$t.add($a.val);}
(
',' b = timeslot
{$t.add($b.val);}
)? RightBracket
;
timeInteral returns [Integer t0, Integer t1]
:
a = timeslot '-' b = timeslot
{
$t0=$a.val;
$t1=$b.val;
}
;
timeslot returns [Integer val]
:
Timeslot
{
$val = new Integer($Timeslot.text.substring(1));
timeslots.add($val);
}
;
myrole returns [String rolet, Role role]
:
Role
{
$rolet = $Role.getText();
$role = new Role($rolet);
roles.add($rolet);
}
; ;
// Key Words // Key Words
Timeslot
:
't' INT
;
CanAssign CanAssign
: :
'CanAssign' 'CanAssign'
...@@ -149,6 +314,7 @@ CanDisable ...@@ -149,6 +314,7 @@ CanDisable
'CanDisable' 'CanDisable'
; ;
/* NOT USED ANYMORE
MaxRoles MaxRoles
: :
'MaxRoles' 'MaxRoles'
...@@ -158,43 +324,21 @@ MaxTimeSlots ...@@ -158,43 +324,21 @@ MaxTimeSlots
: :
'MaxTimeSlots' 'MaxTimeSlots'
; ;
*/
Query Query
: :
'Query' 'Query'
; ;
// Special Notation // Special Notation
/*
Timeslot RoleStrict
:
't' INT
;
Role
: :
'role' INT 'role' INT
; ;*/
Role
RoleArray
:
LeftBracket Role
(
Comma Role
)* RightBracket
;
TimeslotArray
:
LeftBracket Timeslot
(
',' Timeslot
)? RightBracket
;
TimeInteral
: :
Timeslot '-' Timeslot [A-Za-z0-9]+
; ;
// Literals // Literals
...@@ -236,7 +380,7 @@ AND ...@@ -236,7 +380,7 @@ AND
Not Not
: :
'NOT' 'NOT~'
; ;
Colon Colon
...@@ -249,11 +393,6 @@ Comma ...@@ -249,11 +393,6 @@ Comma