Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access control list to CRO #253

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions packages/blueprints/src/AccessControlList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
ActionType,
type CRO,
type Operation,
type ResolveConflictsType,
Role,
SemanticsType,
type Vertex,
} from "@topology-foundation/object";

export class ACL implements CRO {
operations: string[] = ["grant", "revoke"];
roles: Map<string, number>;
semanticsType = SemanticsType.pair;

constructor(nodeIds?: string[] | undefined) {
this.roles = new Map<string, Role>();
if (nodeIds) {
for (const nodeId of nodeIds) {
this.roles.set(nodeId, Role.ADMIN);
}
}
}

private _grant(nodeId: string): void {
if (!this.roles.get(nodeId) || this.roles.get(nodeId) === Role.NONE) {
this.roles.set(nodeId, Role.ADMIN);
}
}

grant(nodeId: string): void {
this._grant(nodeId);
}

private _revoke(nodeId: string): void {
if (this.roles.get(nodeId) && this.roles.get(nodeId) !== Role.ADMIN) {
this.roles.set(nodeId, Role.NONE);
}
}

revoke(nodeId: string): void {
this._revoke(nodeId);
}

hasRole(nodeId: string, role: number): boolean {
return this.roles.get(nodeId) === role;
}

getNodesWithWritePermission(): string[] {
return Array.from(this.roles.entries())
.filter(([_, role]) => role !== Role.NONE)
.map(([value, _]) => value);
}

getNodesWithAdminPermission(): string[] {
return Array.from(this.roles.entries())
.filter(([_, role]) => role === Role.ADMIN)
.map(([value, _]) => value);
}

resolveConflicts(vertices: Vertex[]): ResolveConflictsType {
if (
vertices[0].operation &&
vertices[1].operation &&
vertices[0].operation?.type !== vertices[1].operation?.type &&
vertices[0].operation?.value === vertices[1].operation?.value
) {
return vertices[0].operation.type === "revoke"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
}
return { action: ActionType.Nop };
}

mergeCallback(operations: Operation[]): void {
const adminNodeIds = this.getNodesWithAdminPermission();
this.roles = new Map<string, Role>();
for (const nodeId of adminNodeIds) {
this.roles.set(nodeId, Role.ADMIN);
}

for (const op of operations) {
switch (op.type) {
case "grant":
if (op.value !== null) this._grant(op.value);
break;
case "revoke":
if (op.value !== null) this._revoke(op.value);
break;
default:
break;
}
}
}
}
37 changes: 32 additions & 5 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import {
SemanticsType,
type Vertex,
} from "@topology-foundation/object";
import { ACL } from "../AccessControlList/index.js";

export class AddWinsSet<T> implements CRO {
operations: string[] = ["add", "remove"];
operations: string[] = ["add", "remove", "grant", "revoke"];
state: Map<T, boolean>;
semanticsType = SemanticsType.pair;
accessControlList: ACL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we decided that ACL will be enabled by default, and may be not used. So the attribute should also appear in the interface CRO definition


constructor() {
constructor(nodeIds?: string[] | undefined) {
this.state = new Map<T, boolean>();
this.accessControlList = new ACL(nodeIds);
}

private _add(value: T): void {
Expand Down Expand Up @@ -51,16 +54,32 @@ export class AddWinsSet<T> implements CRO {
vertices[0].operation?.type !== vertices[1].operation?.type &&
vertices[0].operation?.value === vertices[1].operation?.value
) {
return vertices[0].operation.type === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
const vertex0Type =
vertices[0].operation.type === "add" ||
vertices[0].operation.type === "remove";
const vertex1Type =
vertices[1].operation.type === "add" ||
vertices[1].operation.type === "remove";

if (vertex0Type !== vertex1Type) {
return { action: ActionType.Nop };
}

if (vertex0Type) {
return vertices[0].operation.type === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
}

return this.accessControlList.resolveConflicts(vertices);
}
return { action: ActionType.Nop };
}

// merged at HG level and called as a callback
mergeCallback(operations: Operation[]): void {
this.state = new Map<T, boolean>();
const aclOperations: Operation[] = [];
for (const op of operations) {
switch (op.type) {
case "add":
Expand All @@ -69,9 +88,17 @@ export class AddWinsSet<T> implements CRO {
case "remove":
if (op.value !== null) this._remove(op.value);
break;
case "grant":
aclOperations.push(op);
break;
case "revoke":
aclOperations.push(op);
break;
default:
break;
}
}

this.accessControlList.mergeCallback(aclOperations);
}
}
6 changes: 6 additions & 0 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export enum OperationType {
NOP = "-1",
}

export enum Role {
NONE = 0,
GUEST = 1,
ADMIN = 2,
}

export enum ActionType {
DropLeft = 0,
DropRight = 1,
Expand Down