-
Notifications
You must be signed in to change notification settings - Fork 1
[DOP-19931] Add manage_superusers script #137
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
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add new environment variable ``SYNCMASTER__ENTRYPOINT__SUPERUSERS`` to Docker image entrypoint. Here you can pass usernames which should be automatically promoted to SUPERUSER role during backend startup. |
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
File renamed without changes.
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,105 @@ | ||
| #!/bin/env python3 | ||
|
|
||
| # SPDX-FileCopyrightText: 2023-2024 MTS PJSC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | ||
| from sqlalchemy.future import select | ||
|
|
||
| from syncmaster.backend.middlewares import setup_logging | ||
| from syncmaster.backend.settings import BackendSettings as Settings | ||
| from syncmaster.db.models.user import User | ||
|
|
||
|
|
||
| async def add_superusers(session: AsyncSession, usernames: list[str]) -> None: | ||
| logging.info("Adding superusers:") | ||
| result = await session.execute(select(User).where(User.username.in_(usernames)).order_by(User.username)) | ||
| users = result.scalars().all() | ||
|
|
||
| not_found = set(usernames) | ||
| for user in users: | ||
| user.is_superuser = True | ||
| logging.info(" %r", user.username) | ||
| not_found.discard(user.username) | ||
|
|
||
| if not_found: | ||
| for username in not_found: | ||
| session.add(User(username=username, email=f"{username}@mts.ru", is_active=True, is_superuser=True)) | ||
| logging.info(" %r (new user)", username) | ||
|
|
||
| await session.commit() | ||
| logging.info("Done.") | ||
|
|
||
|
|
||
| async def remove_superusers(session: AsyncSession, usernames: list[str]) -> None: | ||
| logging.info("Removing superusers:") | ||
| result = await session.execute(select(User).where(User.username.in_(usernames)).order_by(User.username)) | ||
| users = result.scalars().all() | ||
|
|
||
| not_found = set(usernames) | ||
| for user in users: | ||
| logging.info(" %r", user.username) | ||
| user.is_superuser = False | ||
| not_found.discard(user.username) | ||
|
|
||
| if not_found: | ||
| logging.info("Not found:") | ||
| for username in not_found: | ||
| logging.info(" %r", username) | ||
|
|
||
| await session.commit() | ||
| logging.info("Done.") | ||
|
|
||
|
|
||
| async def list_superusers(session: AsyncSession) -> None: | ||
| result = await session.execute(select(User).filter_by(is_superuser=True).order_by(User.username)) | ||
| superusers = result.scalars().all() | ||
| logging.info("Listing users with SUPERUSER role:") | ||
| for superuser in superusers: | ||
| logging.info(" %r", superuser.username) | ||
| logging.info("Done.") | ||
|
|
||
|
|
||
| def create_parser() -> argparse.ArgumentParser: | ||
| parser = argparse.ArgumentParser(description="Manage superusers.") | ||
| subparsers = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| parser_add = subparsers.add_parser("add", help="Add superuser privileges to users") | ||
| parser_add.add_argument("usernames", nargs="+", help="Usernames to add as superusers") | ||
| parser_add.set_defaults(func=add_superusers) | ||
|
|
||
| parser_remove = subparsers.add_parser("remove", help="Remove superuser privileges from users") | ||
| parser_remove.add_argument("usernames", nargs="+", help="Usernames to remove from superusers") | ||
| parser_remove.set_defaults(func=remove_superusers) | ||
|
|
||
| parser_list = subparsers.add_parser("list", help="List all superusers") | ||
| parser_list.set_defaults(func=list_superusers) | ||
|
|
||
| return parser | ||
|
|
||
|
|
||
| async def main(args: argparse.Namespace, session: AsyncSession) -> None: | ||
| async with session: | ||
| if args.command == "list": | ||
| # 'list' command does not take additional arguments | ||
| await args.func(session) | ||
| else: | ||
| await args.func(session, args.usernames) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| settings = Settings() | ||
| if settings.logging.setup: | ||
| setup_logging(settings.logging.get_log_config_path()) | ||
|
|
||
| engine = create_async_engine(settings.database.url) | ||
| SessionLocal = async_sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession) | ||
| parser = create_parser() | ||
| args = parser.parse_args() | ||
| session = SessionLocal() | ||
| asyncio.run(main(args, session)) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| import pytest | ||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from syncmaster.backend.scripts.manage_superusers import ( | ||
| add_superusers, | ||
| list_superusers, | ||
| remove_superusers, | ||
| ) | ||
| from syncmaster.db.models.user import User | ||
| from tests.mocks import MockUser | ||
|
|
||
| pytestmark = [pytest.mark.asyncio, pytest.mark.backend] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("simple_users", [10], indirect=True) | ||
| async def test_add_superusers(caplog, session: AsyncSession, simple_users: list[MockUser]): | ||
| expected_superusers = [user.username for user in simple_users[:5]] | ||
| expected_not_superusers = [user.username for user in simple_users[5:]] | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| await add_superusers(session, expected_superusers) | ||
|
|
||
| for username in expected_superusers: | ||
| assert repr(username) in caplog.text | ||
|
|
||
| for username in expected_not_superusers: | ||
| assert repr(username) not in caplog.text | ||
|
|
||
| superusers_query = select(User).where(User.username.in_(expected_superusers)) | ||
| superusers_query_result = await session.execute(superusers_query) | ||
| superusers = superusers_query_result.scalars().all() | ||
|
|
||
| assert set(expected_superusers) == {user.username for user in superusers} | ||
| for superuser in superusers: | ||
| assert superuser.is_superuser | ||
|
|
||
| not_superusers_query = select(User).where(User.username.in_(expected_not_superusers)) | ||
| not_superusers_query_result = await session.execute(not_superusers_query) | ||
| not_superusers = not_superusers_query_result.scalars().all() | ||
|
|
||
| assert set(expected_not_superusers) == {user.username for user in not_superusers} | ||
| for user in not_superusers: | ||
| assert not user.is_superuser | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("simple_users", [10], indirect=True) | ||
| async def test_remove_superusers(caplog, session: AsyncSession, simple_users: list[MockUser]): | ||
| # users 0-4 will be superusers, 5-10 will not | ||
| to_create = [user.username for user in simple_users[:5]] | ||
IlyasDevelopment marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| to_delete = [user.username for user in simple_users[5:]] | ||
|
|
||
| expected_superusers = [user.username for user in simple_users[:5]] | ||
| expected_not_superusers = [user.username for user in simple_users[5:]] | ||
|
|
||
| await add_superusers(session, to_create) | ||
|
|
||
| caplog.clear() | ||
| with caplog.at_level(logging.INFO): | ||
| await remove_superusers(session, to_delete) | ||
|
|
||
| for username in expected_superusers: | ||
| assert repr(username) not in caplog.text | ||
|
|
||
| for username in expected_not_superusers: | ||
| assert repr(username) in caplog.text | ||
|
|
||
| superusers_query = select(User).where(User.username.in_(expected_superusers)) | ||
| superusers_query_result = await session.execute(superusers_query) | ||
| superusers = superusers_query_result.scalars().all() | ||
|
|
||
| assert set(expected_superusers) == {user.username for user in superusers} | ||
| for superuser in superusers: | ||
| assert superuser.is_superuser | ||
|
|
||
| not_superusers_query = select(User).where(User.username.in_(expected_not_superusers)) | ||
| not_superusers_query_result = await session.execute(not_superusers_query) | ||
| not_superusers = not_superusers_query_result.scalars().all() | ||
|
|
||
| assert set(expected_not_superusers) == {user.username for user in not_superusers} | ||
| for user in not_superusers: | ||
| assert not user.is_superuser | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("simple_users", [10], indirect=True) | ||
| async def test_list_superusers(caplog, session: AsyncSession, simple_users: list[MockUser]): | ||
| expected_superusers = [user.username for user in simple_users[:5]] | ||
| expected_not_superusers = [user.username for user in simple_users[5:]] | ||
|
|
||
| await add_superusers(session, expected_superusers) | ||
|
|
||
| caplog.clear() | ||
| with caplog.at_level(logging.INFO): | ||
| await list_superusers(session) | ||
|
|
||
| for username in expected_superusers: | ||
| assert repr(username) in caplog.text | ||
|
|
||
| for username in expected_not_superusers: | ||
| assert repr(username) not in caplog.text | ||
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.
Uh oh!
There was an error while loading. Please reload this page.