-
Notifications
You must be signed in to change notification settings - Fork 0
Update objectsid logic 1051 #944
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
Open
Misha-Shvets
wants to merge
11
commits into
dev
Choose a base branch
from
update_objectsid_logic_1051
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b4a09f1
Add: New SID prefix and Group RID enums
794c90a
Add: Implement RID Manager and update object SID handling across the …
26ed42b
Refactor: Clean up join statement in get_directory_by_rid function
c6b6c1e
Refactor: Simplify object SID extraction and update RID Manager use c…
39db0bf
Refactor: Rename and update RID Manager setup method to inherit ACEs
23ff5d3
Update down_revision in Alembic migration to reflect new dependency
fb2497a
Update test constants and modify test cases to use AsyncSession; adju…
7cc1410
Refactor: Remove unused RID set check and enhance RID Manager functio…
ae07319
Add: Introduce new pytest fixtures for RID Manager gateway and use ca…
d537773
Enhance: Update test_api_modify_dn_with_level_up to include session e…
4d6e04f
Refactor: Replace ValueError with specific RID Manager exceptions for…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
300 changes: 300 additions & 0 deletions
300
app/alembic/versions/552b4eafb1aa_remove_objectsid_vals.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| """Add rIDManager and rIDSet objectClasses to LDAP schema. | ||
|
|
||
| Revision ID: 552b4eafb1aa | ||
| Revises: 2dadf40c026a | ||
| Create Date: 2026-02-17 09:24:57.906080 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from dishka import AsyncContainer, Scope | ||
| from sqlalchemy import delete, select, update | ||
| from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession | ||
|
|
||
| from entities import Attribute, Directory, EntityType | ||
| from enums import EntityTypeNames | ||
| from ldap_protocol.ldap_schema.dto import EntityTypeDTO | ||
| from ldap_protocol.ldap_schema.entity_type_use_case import EntityTypeUseCase | ||
| from ldap_protocol.rid_manager.exceptions import RIDManagerNotFoundError | ||
| from ldap_protocol.rid_manager.gateways import RIDManagerGateway | ||
| from ldap_protocol.rid_manager.use_cases import ( | ||
| RID_AVAILABLE_MAX, | ||
| RIDManagerSetupUseCase, | ||
| ) | ||
| from ldap_protocol.rid_manager.utils import create_qword | ||
| from ldap_protocol.utils.queries import get_base_directories | ||
| from repo.pg.tables import queryable_attr as qa | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: None | str = "552b4eafb1aa" | ||
| down_revision: None | str = "19d86e660cf2" | ||
| branch_labels: None | list[str] = None | ||
| depends_on: None | list[str] = None | ||
|
|
||
|
|
||
| def upgrade(container: AsyncContainer) -> None: # noqa: C901 | ||
| """Add rIDManager and rIDSet objectClasses to LDAP schema.""" | ||
|
|
||
| async def _create_entity_types( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Create rIDManager and rIDSet Entity Types.""" | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
| entity_type_use_case = await cnt.get(EntityTypeUseCase) | ||
|
|
||
| if not await get_base_directories(session): | ||
| return | ||
|
|
||
| await entity_type_use_case.create( | ||
| EntityTypeDTO( | ||
| name=EntityTypeNames.RID_MANAGER, | ||
| object_class_names=[ | ||
| "top", | ||
| "rIDManager", | ||
| ], | ||
| is_system=True, | ||
| ), | ||
| ) | ||
|
|
||
| await entity_type_use_case.create( | ||
| EntityTypeDTO( | ||
| name=EntityTypeNames.RID_SET, | ||
| object_class_names=[ | ||
| "top", | ||
| "rIDSet", | ||
| ], | ||
| is_system=True, | ||
| ), | ||
| ) | ||
|
|
||
| await session.commit() | ||
|
|
||
| op.run_async(_create_entity_types) | ||
|
|
||
| async def _migrate_object_sids( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Move Directory.objectSid values into Attributes table. | ||
|
|
||
| Additionally, for domain directories move the domain SID prefix part | ||
| into the ``DomainIdentifier`` attribute. | ||
| """ | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| directories = await session.scalars(select(Directory)) | ||
|
|
||
| for directory in directories: | ||
| if not directory.object_sid: | ||
| continue | ||
|
|
||
| existing_attr = await session.scalar( | ||
| select(Attribute).where( | ||
| qa(Attribute.directory_id) == directory.id, | ||
| qa(Attribute.name) == "objectSid", | ||
| ), | ||
| ) | ||
|
|
||
| if not existing_attr: | ||
| session.add( | ||
| Attribute( | ||
| name="objectSid", | ||
| value=directory.object_sid, | ||
| directory_id=directory.id, | ||
| ), | ||
| ) | ||
|
|
||
| if directory.name == "domain": | ||
| identifier = directory.object_sid.split("-")[ | ||
| -1 | ||
| ] # remove sid prefix | ||
|
|
||
| session.add( | ||
| Attribute( | ||
| name="DomainIdentifier", | ||
| value=identifier, | ||
| directory_id=directory.id, | ||
| ), | ||
| ) | ||
|
|
||
| await session.commit() | ||
|
|
||
| op.run_async(_migrate_object_sids) | ||
|
|
||
| async def _init_rid_manager( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Initialize RID Manager and RID Set for existing data.""" | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
| rid_setup_use_case = await cnt.get(RIDManagerSetupUseCase) | ||
| rid_gateway = await cnt.get(RIDManagerGateway) | ||
|
|
||
| if not await get_base_directories(session): | ||
| return | ||
|
|
||
| try: | ||
| await rid_gateway.get_rid_manager() | ||
| except RIDManagerNotFoundError: | ||
| await rid_setup_use_case.setup() | ||
| await rid_gateway.get_rid_manager() | ||
|
|
||
| rid_set_dir = await rid_gateway.get_rid_set() | ||
| if not rid_set_dir: | ||
| return | ||
|
|
||
| base_domain = await rid_gateway.get_base_domain() | ||
| domain_identifier = await rid_gateway.get_domain_identifier( | ||
| base_domain, | ||
| ) | ||
| sid_prefix = f"S-1-5-21-{domain_identifier}-" | ||
|
|
||
| sid_values = await session.scalars( | ||
| select(Attribute).where( | ||
| qa(Attribute.name) == "objectSid", | ||
| qa(Attribute.value).like(f"{sid_prefix}%"), | ||
| ), | ||
| ) | ||
|
|
||
| max_rid = 0 | ||
| for sid_value in sid_values: | ||
| if not sid_value or not sid_value.value: | ||
| continue | ||
| try: | ||
| parts = sid_value.value.split("-") | ||
| rid = int(parts[-1]) | ||
| except (ValueError, IndexError): | ||
| continue | ||
| if rid > max_rid: | ||
| max_rid = rid | ||
|
|
||
| start_rid = max(max_rid, RIDManagerSetupUseCase.RID_USER_MIN) | ||
|
|
||
| qword = create_qword(start_rid, RID_AVAILABLE_MAX) | ||
| await rid_gateway.update_available_pool(qword) | ||
|
|
||
| result = await session.execute( | ||
| update(Attribute) | ||
| .where( | ||
| qa(Attribute.directory_id) == rid_set_dir.id, | ||
| qa(Attribute.name) == "rIDNextRID", | ||
| ) | ||
| .values(value=str(start_rid)), | ||
| ) | ||
| if result.rowcount == 0: | ||
| session.add( | ||
| Attribute( | ||
| directory_id=rid_set_dir.id, | ||
| name="rIDNextRID", | ||
| value=str(start_rid), | ||
| ), | ||
| ) | ||
|
|
||
| await session.commit() | ||
|
|
||
| op.run_async(_init_rid_manager) | ||
|
|
||
| op.drop_column("Directory", "objectSid") | ||
|
|
||
|
|
||
| def downgrade(container: AsyncContainer) -> None: | ||
| """Remove rIDManager and rIDSet objectClasses from LDAP schema.""" | ||
| op.add_column( | ||
| "Directory", | ||
| sa.Column("objectSid", sa.String(), nullable=True), | ||
| ) | ||
|
|
||
| async def _delete_entity_types( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Delete rIDManager and rIDSet Entity Types.""" | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| if not await get_base_directories(session): | ||
| return | ||
|
|
||
| await session.execute( | ||
| delete(EntityType).where( | ||
| qa(EntityType.name).in_( | ||
| [ | ||
| EntityTypeNames.RID_MANAGER, | ||
| EntityTypeNames.RID_SET, | ||
| ], | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| await session.commit() | ||
|
|
||
| op.run_async(_delete_entity_types) | ||
|
|
||
| async def _delete_rid_manager_dirs( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Delete RID Manager and RID Set directories.""" | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| if not await get_base_directories(session): | ||
| return | ||
|
|
||
| await session.execute( | ||
| delete(Directory).where( | ||
| qa(Directory.name).in_( | ||
| [ | ||
| "RID Manager$", | ||
| "RID Set", | ||
| ], | ||
| ), | ||
| ), | ||
| ) | ||
| await session.commit() | ||
|
|
||
| op.run_async(_delete_rid_manager_dirs) | ||
|
|
||
| async def _rollback_object_sids( | ||
| connection: AsyncConnection, # noqa: ARG001 | ||
| ) -> None: | ||
| """Restore Directory.objectSid values from Attributes. | ||
|
|
||
| Also removes the DomainIdentifier attribute that was introduced in | ||
| upgrade for domain directories. | ||
| """ | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| directories = await session.scalars(select(Directory)) | ||
|
|
||
| for directory in directories: | ||
| await session.execute( | ||
| delete(Attribute).where( | ||
| qa(Attribute.directory_id) == directory.id, | ||
| qa(Attribute.name) == "DomainIdentifier", | ||
| ), | ||
| ) | ||
|
|
||
| attr = await session.scalar( | ||
| select(Attribute).where( | ||
| qa(Attribute.directory_id) == directory.id, | ||
| qa(Attribute.name) == "objectSid", | ||
| ), | ||
| ) | ||
|
|
||
| if not attr or not attr.value: | ||
| continue | ||
|
|
||
| directory.object_sid = attr.value | ||
|
|
||
| await session.execute( | ||
| delete(Attribute).where( | ||
| qa(Attribute.directory_id) == directory.id, | ||
| qa(Attribute.name) == "objectSid", | ||
| ), | ||
| ) | ||
|
|
||
| await session.commit() | ||
|
|
||
| op.run_async(_rollback_object_sids) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сделай property object_sid в классе Directory. Здесь получай его из директории: directory.object_sid.