Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: Java - Just need some help starting it out. Purpose: To implement an a

ID: 3749415 • Letter: L

Question

Language: Java

- Just need some help starting it out.

Purpose:

To implement an authorization system using access control matrix.

Instructions:

- Access control matrix: For this task, you will implement a simple security program using access control matrix. You will load a plain text file with the following format.

alice, bob, carol, eve

top-secret, secret, public

rw,

r,                          rw,        rw

,                            r,            rw

,                            ,             r

- The first two lines are comma-separated lists of subjects and objects respectiviely. 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 files but she can’t read or write secret or public files.

__________________________________________________________________________________________

CODE:

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) {

                             // The name of the file is ac_matrix

                             //Should use “Scanner in = new Scanner(new FileReader(fileName));”

              }

              public String toString() {

                             // This should return a string representation of the access control matrix(ac_matrix)

                             // This format is 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

              }

              // 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();

              }

}

Explanation / Answer

package com.practice.sample;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.Scanner;

public class AccessControl {

//Map to hold the indexes corresponding to subjects

Map<String, Integer> rowMap = new LinkedHashMap<>();

//Map to hold the indexes corresponding to objects

Map<String, Integer> columnMap = new LinkedHashMap<>();

//Matrix to hold the Access Values

String[][] matrix;

void load(String fileName) {

int lineCount = 0;

try {

//Loading the file

Scanner in = new Scanner(new FileReader(fileName));

//Loop in to read the file line by line and put the values in map and array.

while (in.hasNextLine()) {

lineCount++;

//If first line then put the values in rowMap

if (lineCount == 1) {

String[] subjects = in.nextLine().split(",");

for (int i = 0; i < subjects.length; i++) {

rowMap.put(subjects[i].trim(), i);

}

//If second line then put the values in colMap

} else if (lineCount == 2) {

String[] objects = in.nextLine().split(",");

for (int i = 0; i < objects.length; i++) {

columnMap.put(objects[i].trim(), i);

matrix = new String[rowMap.size()][columnMap.size()];

}

//put the remaining values in the matrix

} else {

for (int i = 0; i < matrix.length; i++) {

String[] line = in.nextLine().trim().split(",");

for (int j = 0; j < line.length; j++) {

matrix[i][j] = line[j].trim();

}

}

}

}

//close the scanner once you are done with file reading

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public String toString() {

StringBuilder builder = new StringBuilder(" ");

Iterator<String> colIterator = columnMap.keySet().iterator();

Iterator<String> rowIterator = rowMap.keySet().iterator();

while (colIterator.hasNext()) {

builder.append(colIterator.next() + " ");

}

builder.append(" ");

while (rowIterator.hasNext()) {

String rowname = rowIterator.next();

int rowId = rowMap.get(rowname);

builder.append(rowname + " ");

for (int i = 0; i < matrix[rowId].length; i++) {

builder.append(matrix[rowId][i] == null ? "" : matrix[rowId][i]);

builder.append(" ");

}

builder.append(" ");

}

return builder.toString();

}

//method to check access values in the matrix for subject and object

boolean check(String subj, String op, String obj) {

int rowId = rowMap.get(subj);

int colId = columnMap.get(obj);

//check if subject, operation and object fall under the access matrix and return true else return false

if (matrix[rowId][colId] != null && matrix[rowId][colId].equalsIgnoreCase(op)) {

return true;

}

return false;

}

//Main method

public static void main(String[] args) {

AccessControl ac = new AccessControl();

//fileName with path

ac.load("D:\eclipse-workspace\JavaPractice\src\com\practice\sample\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();

}

}

Sample Input/Output :

Input: alice r secret

Output: alice is NOT allowed to perform r operation on secret

Input: bob rw secret

Output: bob is allowed to perform rw operation on secret