Remove excess permissions

Least privilege means each account keeps only what its job needs. The first step is to list what is extra.

What to build

For each account, list the permissions it has but does not need.

Requirements

  • Each line is account | granted | needed, where granted and needed are comma-separated lists (either may be empty).
  • Print account: remove p1, p2 with the extra permissions in their granted order, or account: ok if there are none.
  • At the end print total removed: N.

Input

One account per line.

Output

One line per account, then the total.

Constraints

Up to 50 accounts.

Example

Input:

lab-printer | print, read-files, admin | print
backup-job | read-files, write-files, delete-files | read-files

Output:

lab-printer: remove read-files, admin
backup-job: remove write-files, delete-files
total removed: 4

Why it works

Anything granted but not needed only adds to what a stolen or misused account can do, so the difference between the two lists is exactly what least privilege removes.

Examples

Example 1 · Input
lab-printer | print, read-files, admin | print
backup-job | read-files, write-files, delete-files | read-files
Expected output
lab-printer: remove read-files, admin
backup-job: remove write-files, delete-files
total removed: 4
Example 2 · Input
rahim | read-courses, submit | read-courses, submit
Expected output
rahim: ok
total removed: 0

Hints

Hint 1

line.split("|").map((s) => s.trim()) gives the three parts; split the lists on commas and drop empty items.

Solution

Show one correct solution (try it yourself first)
let total = 0;
const list = (s) => (s ?? "").split(",").map((x) => x.trim()).filter(Boolean);
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const [account, granted, needed] = line.split("|").map((s) => s.trim());
    const need = new Set(list(needed));
    const extra = list(granted).filter((p) => !need.has(p));
    total += extra.length;
    console.log(extra.length ? `${account}: remove ${extra.join(", ")}` : `${account}: ok`);
  }
  line = prompt();
}
console.log(`total removed: ${total}`);

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Least privilege and access control in practice →