Question: Java Instructions Access control matrix For this homework, you will implement a simple security program using access control matrix. You will load a plain text
Java
Instructions
Access control matrix For this homework, you will implement a simple security program using access control matrix. You will load a plain text le with the following format.
alice, bob, carol, eve top-secret, secret, public rw, r, rw, rw , r, rw , , r
The rst two lines are comma-separated lists of subjects and objects respectively. The subsequent lines are access control matrix, where each cell is a string that contains read and/or write permissions. The matrix has the following meaning.
top-secret secret public alice rw bob r rw rw carol r rw eve r
where r means readable, w means writable, and rw means both readable and writable. The absence of an entry means neither readable nor writable. For example, given this matrix, alice can read or write top-secret les but she cant read or write secret or public les.
Requirement You should implement a class AccessControl using the provided template, which contains the following methods. void load(String fileName) loads an access control le in the format as described above. To read the le, you can place the text le in the root directory of your Eclipse project folder and use the declaration
1
Scanner in = new Scanner(new FileReader(fileName));
to dene a scanner object in. public String toString() returns a string representation of the access control matrix in the format as shown above. boolean check(String subj, String op, String obj) returns true if and only if subj can perform operation op on obj according to the access control matrix.
The code template includes a main method that will rst print the content of the access control matrix and then starts a loop to ask user to enter a command such as alice r secret. This will be successfull since the access control policy allows alice to read secret. The command has the format of subject operation object. We only have two operations r or w. If user types a subject or an object that is not in the matrix, then you should throw an exception. The loop terminates if you enter empty string. Below is the test output of running the program.
top-secret secret public alice rw bob r rw rw carol r rw eve r
Enter your command (e.g. alice r secret): alice r secret alice is NOT allowed to perform r operation on secret
Enter your command alice r top-secret alice is allowed to perform r operation on top-secret
Enter your command bob w secret bob is allowed to perform w operation on secret
Enter your command eve r public eve is allowed to perform r operation on public
Enter your command eve w public eve is NOT allowed to perform w operation on public
Enter your command
2
alex r secret java.lang.RuntimeException: alex is not found allowed subjects are [alice, bob, carol, eve]
Enter your command carol w public carol is allowed to perform w operation on public
Enter your command carol w secrets java.lang.RuntimeException: secrets is not found allowed objects are [top-secret, secret, public]
Enter your command
Code so far below:
package hwk2;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;
/*
* Load an access control matrix from a file
* First line: subjects
* Second line: objects
* Subsequent lines: access control matrix (comma separated)
* e.g
*
* alice, bob, carol, eve
* top-secret, secret, public
* rw,
* r, rw, rw
* , r, rw
* , , r
*/
public class AccessControl {
// Define any fields or methods as necessary.
// load access control matrix from a file by the name of "fileName"
void load(String fileName) {
// TODO
}
public String toString() {
// TODO
}
boolean check(String subj, String op, String obj) {
// TODO
}
// Driver method
public static void main(String[] args) {
AccessControl ac = new AccessControl();
ac.load("ac_matrix");
System.out.println(ac);
Scanner in = new Scanner(System.in);
System.out.println("Enter your command (e.g. alice r secret):");
String cmd = in.nextLine().trim();
while(!cmd.equals("")) {
String[] triple = cmd.split(" ");
if(triple.length != 3) {
System.out.println("Illegal command. Try again");
}
else {
String subj = triple[0], op = triple[1], obj = triple[2];
try {
if(ac.check(subj, op, obj)) {
System.out.printf("%s is allowed to perform %s operation on %s ", subj, op, obj);
}
else {
System.out.printf("%s is NOT allowed to perform %s operation on %s ", subj, op, obj);
}
} catch(Exception e) { System.out.println(e); }
}
System.out.println(" Enter your command");
cmd = in.nextLine().trim();
}
in.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
