-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: rename services to System #883
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3f7a0dd
Refactor: use get_services_container_dn for service container DN retr…
fd408db
Refactor: improve attribute update queries in alembic migration
c07fe6d
Refactor: simplify directory selection queries in alembic migration f…
61b0d80
Enhance entrypoint script: add config fix function and periodic updat…
a7cadbd
Update Kerberos configuration handling: add shared volume for KDC fil…
1f4697d
Remove outdated comment regarding services container name in Kerberos…
b45356a
Update test case in kadmin.py to reflect new expected hash value for …
08cd05c
Refactor alembic migration to rename 'services' to 'System' and updat…
53854a7
Refactor alembic migration to improve session handling and streamline…
b15fbde
Update down_revision in alembic migration to reflect the latest migra…
a6abcd0
Refactor: rename get_services_container_dn to get_system_container_dn…
f076c03
Add shared volume for KDC files in development and package docker-com…
a6d5882
Remove unused downgrade function for updating descendant paths in ale…
4601e4c
Refactor alembic migration to consolidate attribute update logic for …
91a143f
Fix comment formatting in downgrade function of alembic migration for…
25c1749
Enhance alembic migration for renaming 'services' to 'System' by cons…
e2f9114
Remove redundant existence check for 'System' directory in alembic mi…
7b01fe1
Remove unused base directory checks in alembic migration for renaming…
87d07d6
Update down_revision in alembic migration for renaming 'services' to …
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
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
143 changes: 143 additions & 0 deletions
143
app/alembic/versions/a1b2c3d4e5f6_rename_services_to_system.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,143 @@ | ||
| """Rename services container to System for AD compatibility. | ||
|
|
||
| Revision ID: a1b2c3d4e5f6 | ||
| Revises: 6c858cc05da7 | ||
| Create Date: 2026-01-13 12:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from alembic import op | ||
| from dishka import AsyncContainer, Scope | ||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession | ||
|
|
||
| from entities import Attribute, Directory | ||
| from repo.pg.tables import queryable_attr as qa | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: None | str = "a1b2c3d4e5f6" | ||
| down_revision: None | str = "c5a9b3f2e8d7" | ||
| branch_labels: None | list[str] = None | ||
| depends_on: None | list[str] = None | ||
|
|
||
|
|
||
| async def _update_descendants( | ||
| session: AsyncSession, | ||
| parent_id: int, | ||
| ou_from: str, | ||
| ou_to: str, | ||
| ) -> None: | ||
| """Recursively update paths of all descendants.""" | ||
| child_dirs = await session.scalars( | ||
| select(Directory) | ||
| .where(qa(Directory.parent_id) == parent_id), | ||
| ) # fmt: skip | ||
|
|
||
| for child_dir in child_dirs: | ||
| child_dir.path = [ou_from if p == ou_to else p for p in child_dir.path] | ||
| await session.flush() | ||
| await _update_descendants( | ||
| session, | ||
| child_dir.id, | ||
| ou_from=ou_from, | ||
| ou_to=ou_to, | ||
| ) | ||
|
|
||
|
|
||
| async def _update_attributes( | ||
| session: AsyncSession, | ||
| old_value: str, | ||
| new_value: str, | ||
| ) -> None: | ||
| """Update attribute values during downgrade.""" | ||
| result = await session.execute( | ||
| select(Attribute) | ||
| .where( | ||
| Attribute.value.ilike(f"%{old_value}%"), # type: ignore | ||
| ), | ||
| ) # fmt: skip | ||
| attributes = result.scalars().all() | ||
|
|
||
| for attr in attributes: | ||
| if attr.value and old_value in attr.value: | ||
| attr.value = attr.value.replace(old_value, new_value) | ||
|
|
||
| await session.flush() | ||
|
|
||
|
|
||
| def upgrade(container: AsyncContainer) -> None: | ||
| """Upgrade: Rename 'services' container to 'System'.""" | ||
|
|
||
| async def _rename_services_to_system(connection: AsyncConnection) -> None: # noqa: ARG001 | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| service_dir = await session.scalar( | ||
| select(Directory).where( | ||
| qa(Directory.name) == "services", | ||
| qa(Directory.is_system).is_(True), | ||
| ), | ||
| ) | ||
| if not service_dir: | ||
| return | ||
| ou_to = "ou=System" | ||
| ou_from = "ou=services" | ||
|
|
||
| service_dir.name = "System" | ||
| service_dir.path = [ | ||
| ou_to if p == ou_from else p for p in service_dir.path | ||
| ] | ||
|
|
||
| await session.flush() | ||
| await _update_descendants( | ||
| session, | ||
| service_dir.id, | ||
| ou_from=ou_from, | ||
| ou_to=ou_to, | ||
| ) | ||
|
|
||
| await _update_attributes(session, ou_from, ou_to) | ||
| await session.commit() | ||
|
|
||
| op.run_async(_rename_services_to_system) | ||
|
|
||
|
|
||
| def downgrade(container: AsyncContainer) -> None: | ||
| """Downgrade: Rename 'System' container back to 'services'.""" | ||
|
|
||
| async def _rename_system_to_services(connection: AsyncConnection) -> None: # noqa: ARG001 | ||
| async with container(scope=Scope.REQUEST) as cnt: | ||
| session = await cnt.get(AsyncSession) | ||
|
|
||
| system_dir = await session.scalar( | ||
| select(Directory).where( | ||
| qa(Directory.name) == "System", | ||
| qa(Directory.is_system).is_(True), | ||
| ), | ||
| ) | ||
| if not system_dir: | ||
| return | ||
| ou_to = "ou=services" | ||
| ou_from = "ou=System" | ||
|
|
||
| system_dir.name = "services" | ||
| system_dir.path = [ | ||
| ou_to if p == ou_from else p for p in system_dir.path | ||
| ] | ||
|
|
||
| await session.flush() | ||
| await _update_descendants( | ||
| session, | ||
| system_dir.id, | ||
| ou_from=ou_from, | ||
| ou_to=ou_to, | ||
| ) | ||
|
|
||
| await _update_attributes( | ||
| session, | ||
| ou_from, | ||
| ou_to, | ||
| ) | ||
| await session.commit() | ||
|
|
||
| op.run_async(_rename_system_to_services) |
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 |
|---|---|---|
| @@ -1,46 +1,89 @@ | ||
| """Kerberos update config. | ||
| """Kerberos configuration update script. | ||
|
|
||
| Copyright (c) 2025 MultiFactor | ||
| License: https://github.com/MultiDirectoryLab/MultiDirectory/blob/main/LICENSE | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from loguru import logger | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from config import Settings | ||
| from ldap_protocol.kerberos import AbstractKadmin | ||
| from ldap_protocol.kerberos.utils import get_system_container_dn | ||
| from ldap_protocol.utils.queries import get_base_directories | ||
|
|
||
| KRB5_CONF_PATH = Path("/etc/krb5kdc/krb5.conf") | ||
| KDC_CONF_PATH = Path("/etc/krb5kdc/kdc.conf") | ||
| STASH_FILE_PATH = Path("/etc/krb5kdc/krb5.d/stash.keyfile") | ||
|
|
||
|
|
||
| def _migrate_legacy_dns(content: str) -> str: | ||
| """Replace legacy DN formats with current ones. | ||
|
|
||
| :param content: File content to migrate. | ||
| :return: Migrated content. | ||
| """ | ||
| return content.replace("ou=services", "ou=System").replace( | ||
| "ou=users", | ||
| "cn=users", | ||
| ) | ||
|
|
||
|
|
||
| async def update_krb5_config( | ||
| kadmin: AbstractKadmin, | ||
| session: AsyncSession, | ||
| settings: Settings, | ||
| ) -> None: | ||
| """Update kerberos config.""" | ||
| if not (await kadmin.get_status(wait_for_positive=True)): | ||
| logger.error("kadmin_api is not running") | ||
| return | ||
| """Update Kerberos configuration files via direct write to shared volume. | ||
|
|
||
| base_dn_list = await get_base_directories(session) | ||
| base_dn = base_dn_list[0].path_dn | ||
| domain: str = base_dn_list[0].name | ||
| Renders krb5.conf and kdc.conf from templates and writes them directly | ||
| to the shared volume. Also migrates legacy DN formats in stash.keyfile | ||
| if present (ou=services -> ou=System, ou=users -> cn=users). | ||
|
|
||
| krbadmin = "cn=krbadmin,cn=users," + base_dn | ||
| services_container = "ou=services," + base_dn | ||
| :param session: Database session for fetching base directories. | ||
| :param settings: Application settings with template environment. | ||
| :raises Exception: If config rendering or writing fails. | ||
| """ | ||
| if not KRB5_CONF_PATH.parent.exists(): | ||
| logger.error( | ||
| f"Config directory {KRB5_CONF_PATH.parent} not found, " | ||
| "kerberos volume not mounted", | ||
| ) | ||
| return | ||
|
|
||
| krb5_template = settings.TEMPLATES.get_template("krb5.conf") | ||
| kdc_template = settings.TEMPLATES.get_template("kdc.conf") | ||
| base_dn_list = await get_base_directories(session) | ||
| if not base_dn_list: | ||
| logger.error("No base directories found") | ||
| return | ||
|
|
||
| kdc_config = await kdc_template.render_async(domain=domain) | ||
| base_dn = base_dn_list[0].path_dn | ||
| domain = base_dn_list[0].name | ||
| krbadmin = f"cn=krbadmin,cn=users,{base_dn}" | ||
| services_container = get_system_container_dn(base_dn) | ||
|
|
||
| krb5_config = await krb5_template.render_async( | ||
| krb5_config = await settings.TEMPLATES.get_template( | ||
| "krb5.conf", | ||
| ).render_async( | ||
| domain=domain, | ||
| krbadmin=krbadmin, | ||
| services_container=services_container, | ||
| ldap_uri=settings.KRB5_LDAP_URI, | ||
| mfa_push_url=settings.KRB5_MFA_PUSH_URL, | ||
| sync_password_url=settings.KRB5_SYNC_PASSWORD_URL, | ||
| ) | ||
| kdc_config = await settings.TEMPLATES.get_template( | ||
| "kdc.conf", | ||
| ).render_async( | ||
| domain=domain, | ||
| ) | ||
|
|
||
| KRB5_CONF_PATH.write_text(krb5_config, encoding="utf-8") | ||
| KDC_CONF_PATH.write_text(kdc_config, encoding="utf-8") | ||
|
|
||
| await kadmin.setup_configs(krb5_config, kdc_config) | ||
| if STASH_FILE_PATH.exists(): | ||
| stash_content = STASH_FILE_PATH.read_text(encoding="utf-8") | ||
| if "ou=services" in stash_content or "ou=users" in stash_content: | ||
| STASH_FILE_PATH.write_text( | ||
| _migrate_legacy_dns(stash_content), | ||
| encoding="utf-8", | ||
| ) |
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.