From 091b4a801dc5a7f558fdea273fcbed579f950021 Mon Sep 17 00:00:00 2001 From: Marek Wolan Date: Wed, 2 Aug 2023 13:43:31 +0100 Subject: [PATCH] Make some progress on accounts --- src/primaite/simulator/domain/__init__.py | 3 + src/primaite/simulator/domain/account.py | 92 +++++++++++++++++++ src/primaite/simulator/domain/controller.py | 13 +++ .../simulator/domain_controller/__init__.py | 0 .../simulator/domain_controller/account.py | 8 -- 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/primaite/simulator/domain/__init__.py create mode 100644 src/primaite/simulator/domain/account.py create mode 100644 src/primaite/simulator/domain/controller.py delete mode 100644 src/primaite/simulator/domain_controller/__init__.py delete mode 100644 src/primaite/simulator/domain_controller/account.py diff --git a/src/primaite/simulator/domain/__init__.py b/src/primaite/simulator/domain/__init__.py new file mode 100644 index 00000000..6f59cf49 --- /dev/null +++ b/src/primaite/simulator/domain/__init__.py @@ -0,0 +1,3 @@ +from primaite.simulator.domain.account import Account + +__all__ = ["Account"] diff --git a/src/primaite/simulator/domain/account.py b/src/primaite/simulator/domain/account.py new file mode 100644 index 00000000..374675a0 --- /dev/null +++ b/src/primaite/simulator/domain/account.py @@ -0,0 +1,92 @@ +"""User account simulation.""" +from enum import Enum +from typing import Dict, List, Set, TypeAlias + +from primaite import getLogger +from primaite.simulator.core import SimComponent + +_LOGGER = getLogger(__name__) + + +__temp_node = TypeAlias() # placeholder while nodes don't exist + + +class AccountType(Enum): + """Whether the account is intended for a user to log in or for a service to use.""" + + service = 1 + "Service accounts are used to grant permissions to software on nodes to perform actions" + user = 2 + "User accounts are used to allow agents to log in and perform actions" + + +class AccountGroup(Enum): + """Permissions are set at group-level and accounts can belong to these groups.""" + + local_user = 1 + "For performing basic actions on a node" + domain_user = 2 + "For performing basic actions to the domain" + local_admin = 3 + "For full access to actions on a node" + domain_admin = 4 + "For full access" + + +class AccountStatus(Enum): + """Whether the account is active.""" + + enabled = 1 + disabled = 2 + + +class Account(SimComponent): + """User accounts.""" + + num_logons: int = 0 + "The number of times this account was logged into since last reset." + num_logoffs: int = 0 + "The number of times this account was logged out of since last reset." + num_group_changes: int = 0 + "The number of times this account was moved in or out of an AccountGroup." + username: str + "Account username." + password: str + "Account password." + account_type: AccountType + "Account Type, currently this can be service account (used by apps) or user account." + domain_groups: Set[AccountGroup] = [] + "Domain-wide groups that this account belongs to." + local_groups: Dict[__temp_node, List[AccountGroup]] + "For each node, whether this account has local/admin privileges on that node." + status: AccountStatus = AccountStatus.disabled + + def add_to_domain_group(self, group: AccountGroup) -> None: + """ + Add this account to a domain group. + + If the account is already a member of this group, nothing happens. + + :param group: The group to which to add this account. + :type group: AccountGroup + """ + self.domain_groups.add(group) + + def remove_from_domain_group(self, group: AccountGroup) -> None: + """ + Remove this account from a domain group. + + If the account is already not a member of that group, nothing happens. + + :param group: The group from which this account should be removed. + :type group: AccountGroup + """ + self.domain_groups.discard(group) + + def enable_account(self): + """Set the status to enabled.""" + self.status = AccountStatus.enabled + + def disable_account(self): + """Set the status to disabled.""" + self.status = AccountStatus.disabled diff --git a/src/primaite/simulator/domain/controller.py b/src/primaite/simulator/domain/controller.py new file mode 100644 index 00000000..5a14e80e --- /dev/null +++ b/src/primaite/simulator/domain/controller.py @@ -0,0 +1,13 @@ +from typing import Set, TypeAlias + +from primaite.simulator.core import SimComponent +from primaite.simulator.domain import Account + +__temp_node = TypeAlias() # placeholder while nodes don't exist + + +class DomainController(SimComponent): + """Main object for controlling the domain.""" + + nodes: Set(__temp_node) = set() + accounts: Set(Account) = set() diff --git a/src/primaite/simulator/domain_controller/__init__.py b/src/primaite/simulator/domain_controller/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/primaite/simulator/domain_controller/account.py b/src/primaite/simulator/domain_controller/account.py deleted file mode 100644 index 1f3ac900..00000000 --- a/src/primaite/simulator/domain_controller/account.py +++ /dev/null @@ -1,8 +0,0 @@ -"""User account simulation.""" -from primaite.simulator.core import SimComponent - - -class Account(SimComponent): - """User accounts.""" - - uid: int