Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add app_list, app_set_active and webhook_set_active commands (#19969)

Lukasz Ostrowski0e3fa1b02255
diff --git a/saleor/app/management/commands/app_list.py b/saleor/app/management/commands/app_list.py
new file mode 100644
index 0000000000..dc6b222287
--- /dev/null
+++ b/saleor/app/management/commands/app_list.py
@@ -0,0 +1,60 @@
+from typing import Any
+
+import graphene
+from django.db.models import Prefetch
+
+from ....core.utils.url import sanitize_url_for_logging
+from ....webhook.models import Webhook
+from ...models import App
+from .utils import JSONLinesCommand
+
+
+class Command(JSONLinesCommand):
+ help = (
+ "List apps that are not soft-deleted, each followed by its webhooks, "
+ "as JSON Lines. Inactive apps and webhooks are included."
+ )
+
+ def handle(self, *args: Any, **options: Any) -> None:
+ webhooks = Webhook.objects.order_by("pk").only(
+ "pk", "app_id", "name", "target_url", "is_active"
+ )
+ apps = (
+ App.objects.not_removed()
+ .order_by("pk")
+ .only("pk", "identifier", "name", "is_active")
+ .prefetch_related(Prefetch("webhooks", queryset=webhooks))
+ )
+ for app in apps.iterator(chunk_size=1000):
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ self._write(
+ {
+ "kind": "app",
+ "id": app_id,
+ "identifier": app.identifier,
+ "name": app.name,
+ "is_active": app.is_active,
+ }
+ )
+ # .all(), not .iterator(): iterator() bypasses the prefetch cache
+ # and would run one query per app.
+ for webhook in app.webhooks.all():
+ self._write(
+ {
+ "kind": "webhook",
+ "id": graphene.Node.to_global_id("Webhook", webhook.pk),
+ "app_id": app_id,
+ "name": webhook.name,
+ "target_url": sanitize_url_for_logging(
+ webhook.target_url,
+ strip_querystring=True,
+ strip_fragment=True,
+ ),
+ "is_active": webhook.is_active,
+ "app_is_active": app.is_active,
+ # Mirrors the is_active filters in saleor/webhook/utils.py.
+ # App lifecycle events skip the app check, so an inactive
+ # app still gets its own APP_STATUS_CHANGED/APP_DELETED.
+ "delivering": webhook.is_active and app.is_active,
+ }
+ )
diff --git a/saleor/app/management/commands/app_set_active.py b/saleor/app/management/commands/app_set_active.py
new file mode 100644
index 0000000000..199f227686
--- /dev/null
+++ b/saleor/app/management/commands/app_set_active.py
@@ -0,0 +1,49 @@
+from typing import Any
+
+import graphene
+from django.core.management import CommandError
+from django.core.management.base import CommandParser
+
+from ....core.utils.events import call_event
+from ....plugins.manager import get_plugins_manager
+from ...models import App
+from .utils import JSONLinesCommand, parse_bool, parse_id
+
+
+class Command(JSONLinesCommand):
+ help = (
+ "Activate or deactivate an app, like the appActivate/appDeactivate "
+ "mutations. Does nothing when the app is already in the target state."
+ )
+
+ def add_arguments(self, parser: CommandParser) -> None:
+ parser.add_argument(
+ "--id", required=True, help="App database ID or GraphQL ID."
+ )
+ parser.add_argument(
+ "--active", required=True, type=parse_bool, help="true or false."
+ )
+
+ def handle(self, *args: Any, **options: Any) -> None:
+ target = options["active"]
+ pk = parse_id(options["id"], "App")
+ apps = App.objects.not_removed().filter(pk=pk)
+ changed = apps.filter(is_active=not target).update(is_active=target)
+ app = apps.first()
+ if app is None:
+ raise CommandError(f"App not found: {options['id']}.")
+ if changed:
+ manager = get_plugins_manager(allow_replica=False)
+ call_event(manager.app_status_changed, app)
+ self._write(
+ {
+ "kind": "app",
+ "id": graphene.Node.to_global_id("App", app.pk),
+ "identifier": app.identifier,
+ "name": app.name,
+ "is_active": {
+ "before": not target if changed else app.is_active,
+ "after": app.is_active,
+ },
+ }
+ )
diff --git a/saleor/app/management/commands/utils.py b/saleor/app/management/commands/utils.py
index 76363614e9..316a0d8fee 100644
--- a/saleor/app/management/commands/utils.py
+++ b/saleor/app/management/commands/utils.py
@@ -1,5 +1,14 @@
-from django.core.management import CommandError
+import json
+from argparse import ArgumentTypeError
+from typing import Any
+from django.core.management import BaseCommand, CommandError
+from graphql import GraphQLError
+
+# Special case: non-GraphQL code must not import from the graphql layer, but
+# these commands accept GraphQL global IDs as input, so they reuse the same
+# decoder the API uses.
+from ....graphql.core.utils import from_global_id_or_error
from ....permission.enums import (
AppPermission,
get_permissions,
@@ -26,3 +35,36 @@ def clean_permissions(required_permissions: list[str]):
[all_permissions[perm] for perm in required_permissions]
)
return permissions
+
+
+def parse_bool(value: str) -> bool:
+ """Parse a strict `true`/`false` command-line value."""
+ match value.lower():
+ case "true":
+ return True
+ case "false":
+ return False
+ raise ArgumentTypeError(f"expected 'true' or 'false', got {value!r}")
+
+
+def parse_id(value: str, expected_type: str) -> int:
+ """Parse a database ID or a GraphQL global ID of `expected_type`."""
+ try:
+ return int(value)
+ except ValueError:
+ pass
+ try:
+ _, pk = from_global_id_or_error(value, expected_type, raise_error=True)
+ except GraphQLError as e:
+ raise CommandError(e.message) from e
+ try:
+ return int(pk)
+ except ValueError as e:
+ raise CommandError(f"Invalid ID: {value}. Expected: {expected_type}.") from e
+
+
+class JSONLinesCommand(BaseCommand):
+ """Command that writes its output as JSON Lines for machine consumption."""
+
+ def _write(self, record: dict[str, Any]) -> None:
+ self.stdout.write(json.dumps(record))
diff --git a/saleor/app/management/commands/webhook_set_active.py b/saleor/app/management/commands/webhook_set_active.py
new file mode 100644
index 0000000000..3d256977cb
--- /dev/null
+++ b/saleor/app/management/commands/webhook_set_active.py
@@ -0,0 +1,44 @@
+from typing import Any
+
+import graphene
+from django.core.management import CommandError
+from django.core.management.base import CommandParser
+
+from ....webhook.models import Webhook
+from .utils import JSONLinesCommand, parse_bool, parse_id
+
+
+class Command(JSONLinesCommand):
+ help = (
+ "Activate or deactivate a webhook, like webhookUpdate(input: {isActive}). "
+ "Does nothing when the webhook is already in the target state."
+ )
+
+ def add_arguments(self, parser: CommandParser) -> None:
+ parser.add_argument(
+ "--id", required=True, help="Webhook database ID or GraphQL ID."
+ )
+ parser.add_argument(
+ "--active", required=True, type=parse_bool, help="true or false."
+ )
+
+ def handle(self, *args: Any, **options: Any) -> None:
+ target = options["active"]
+ pk = parse_id(options["id"], "Webhook")
+ webhooks = Webhook.objects.filter(pk=pk, app__removed_at__isnull=True)
+ changed = webhooks.filter(is_active=not target).update(is_active=target)
+ webhook = webhooks.first()
+ if webhook is None:
+ raise CommandError(f"Webhook not found: {options['id']}.")
+ self._write(
+ {
+ "kind": "webhook",
+ "id": graphene.Node.to_global_id("Webhook", webhook.pk),
+ "app_id": graphene.Node.to_global_id("App", webhook.app_id),
+ "name": webhook.name,
+ "is_active": {
+ "before": not target if changed else webhook.is_active,
+ "after": webhook.is_active,
+ },
+ }
+ )
diff --git a/saleor/app/tests/test_app_commands.py b/saleor/app/tests/test_app_commands.py
index c1b0448355..c083479a20 100644
--- a/saleor/app/tests/test_app_commands.py
+++ b/saleor/app/tests/test_app_commands.py
@@ -1,3 +1,5 @@
+import json
+from io import StringIO
from unittest.mock import ANY, Mock, call, patch
import graphene
@@ -426,3 +428,364 @@ def test_app_delete_all_with_force_sync_dispatches_sync_webhooks(
for call in mocked_apply.call_args_list
}
assert called_pks == delivery_pks
+
+
+def _call_command_json_lines(name: str, *args: str) -> list[dict]:
+ out = StringIO()
+ call_command(name, *args, stdout=out)
+ return [json.loads(line) for line in out.getvalue().splitlines()]
+
+
+def _create_webhook(app: App, name: str, is_active: bool = True) -> Webhook:
+ return Webhook.objects.create(
+ name=name,
+ app=app,
+ target_url=f"https://example.com/{app.identifier}/{name}",
+ is_active=is_active,
+ secret_key="secret-key",
+ custom_headers={"X-Secret": "secret-header"},
+ subscription_query="subscription { event { ... on OrderCreated { order { id } } } }",
+ )
+
+
+def _app_record(app: App) -> dict:
+ return {
+ "kind": "app",
+ "id": graphene.Node.to_global_id("App", app.pk),
+ "identifier": app.identifier,
+ "name": app.name,
+ "is_active": app.is_active,
+ }
+
+
+def _webhook_record(webhook: Webhook, delivering: bool) -> dict:
+ return {
+ "kind": "webhook",
+ "id": graphene.Node.to_global_id("Webhook", webhook.pk),
+ "app_id": graphene.Node.to_global_id("App", webhook.app_id),
+ "name": webhook.name,
+ "target_url": webhook.target_url,
+ "is_active": webhook.is_active,
+ "app_is_active": webhook.app.is_active,
+ "delivering": delivering,
+ }
+
+
+def test_app_list_outputs_not_removed_apps_with_their_webhooks(db):
+ # given
+ active_app = App.objects.create(name="Active", identifier="active", is_active=True)
+ active_webhook = _create_webhook(active_app, "on")
+ inactive_webhook = _create_webhook(active_app, "off", is_active=False)
+ inactive_app = App.objects.create(
+ name="Inactive", identifier="inactive", is_active=False
+ )
+ webhook_of_inactive_app = _create_webhook(inactive_app, "on")
+ removed_app = App.objects.create(
+ name="Removed", identifier="removed", removed_at=timezone.now()
+ )
+ _create_webhook(removed_app, "on")
+
+ # when
+ records = _call_command_json_lines("app_list")
+
+ # then
+ assert records == [
+ _app_record(active_app),
+ _webhook_record(active_webhook, delivering=True),
+ _webhook_record(inactive_webhook, delivering=False),
+ _app_record(inactive_app),
+ _webhook_record(webhook_of_inactive_app, delivering=False),
+ ]
+
+
+def test_app_list_with_no_apps_outputs_nothing(db):
+ # when
+ records = _call_command_json_lines("app_list")
+
+ # then
+ assert records == []
+
+
+ ("_case", "target_url", "expected"),
+ [
+ (
+ "credentials",
+ "https://user:[email protected]/hook",
+ "https://***:***@example.com/hook",
+ ),
+ (
+ "query_and_fragment",
+ "https://example.com/hook?token=secret#frag",
+ "https://example.com/hook",
+ ),
+ (
+ "credentials_port_and_query",
+ "awssqs://key:[email protected]:443/queue?region=x",
+ "awssqs://***:***@sqs.example.com:443/queue",
+ ),
+ ],
+)
+def test_app_list_redacts_webhook_target_url(_case, target_url, expected, db):
+ # given
+ app = App.objects.create(name="App", identifier="app")
+ webhook = _create_webhook(app, "hook")
+ webhook.target_url = target_url
+ webhook.save(update_fields=["target_url"])
+
+ # when
+ records = _call_command_json_lines("app_list")
+
+ # then
+ assert records[1]["target_url"] == expected
+
+
[email protected]("count", [1, 3])
+def test_app_list_query_count_does_not_depend_on_number_of_objects(
+ count, db, django_assert_num_queries
+):
+ # given
+ for i in range(count):
+ app = App.objects.create(name=f"App {i}", identifier=f"app-{i}")
+ for j in range(count):
+ _create_webhook(app, f"hook-{j}")
+
+ # when
+ with django_assert_num_queries(2):
+ records = _call_command_json_lines("app_list")
+
+ # then
+ assert len(records) == count + count * count
+
+
+APP_SET_ACTIVE_COMMAND_MODULE = "saleor.app.management.commands.app_set_active"
+
+
+def _patched_app_set_active_manager():
+ with patch(
+ f"{APP_SET_ACTIVE_COMMAND_MODULE}.get_plugins_manager"
+ ) as mocked_get_manager:
+ mocked_manager = Mock()
+ mocked_get_manager.return_value = mocked_manager
+ yield mocked_manager
+
+
[email protected]("id_format", ["graphql", "database"])
[email protected](("before", "target"), [(True, False), (False, True)])
+def test_app_set_active_changes_state_and_emits_status_changed(
+ before,
+ target,
+ id_format,
+ db,
+ _patched_app_set_active_manager,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ app = App.objects.create(name="App", identifier="app", is_active=before)
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ command_id = app_id if id_format == "graphql" else str(app.pk)
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ records = _call_command_json_lines(
+ "app_set_active", "--id", command_id, "--active", str(target).lower()
+ )
+
+ # then
+ app.refresh_from_db()
+ assert app.is_active is target
+ _patched_app_set_active_manager.app_status_changed.assert_called_once_with(app)
+ assert records == [
+ {
+ "kind": "app",
+ "id": app_id,
+ "identifier": app.identifier,
+ "name": app.name,
+ "is_active": {"before": before, "after": target},
+ }
+ ]
+
+
[email protected]("state", [True, False])
+def test_app_set_active_same_state_does_not_emit_event(
+ state, db, _patched_app_set_active_manager, django_capture_on_commit_callbacks
+):
+ # given
+ app = App.objects.create(name="App", identifier="app", is_active=state)
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ records = _call_command_json_lines(
+ "app_set_active", "--id", app_id, "--active", str(state).upper()
+ )
+
+ # then
+ _patched_app_set_active_manager.app_status_changed.assert_not_called()
+ assert records[0]["is_active"] == {"before": state, "after": state}
+
+
+ ("_case", "global_id", "message"),
+ [
+ ("not_base64", "garbage", "Invalid ID: garbage. Expected: App."),
+ (
+ "wrong_type",
+ graphene.Node.to_global_id("Webhook", 1),
+ "Invalid ID: V2ViaG9vazox. Expected: App, received: Webhook.",
+ ),
+ (
+ "non_int_pk",
+ graphene.Node.to_global_id("App", "x"),
+ "Invalid ID: QXBwOng=. Expected: App.",
+ ),
+ (
+ "uuid_pk",
+ graphene.Node.to_global_id("App", "9f1c0e9e-3b1a-4a5e-9a8d-1c2b3d4e5f60"),
+ "Invalid ID: QXBwOjlmMWMwZTllLTNiMWEtNGE1ZS05YThkLTFjMmIzZDRlNWY2MA==. "
+ "Expected: App.",
+ ),
+ (
+ "unknown_pk",
+ graphene.Node.to_global_id("App", -1),
+ "App not found: QXBwOi0x.",
+ ),
+ ("unknown_database_id", "-1", "App not found: -1."),
+ ],
+)
+def test_app_set_active_rejects_invalid_id(
+ _case, global_id, message, db, _patched_app_set_active_manager
+):
+ # when
+ with pytest.raises(CommandError) as exc_info:
+ call_command("app_set_active", "--id", global_id, "--active", "false")
+
+ # then
+ assert str(exc_info.value) == message
+ _patched_app_set_active_manager.app_status_changed.assert_not_called()
+
+
+def test_app_set_active_rejects_removed_app(db, _patched_app_set_active_manager):
+ # given
+ app = App.objects.create(
+ name="Removed", identifier="removed", is_active=True, removed_at=timezone.now()
+ )
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ with pytest.raises(CommandError) as exc_info:
+ call_command("app_set_active", "--id", app_id, "--active", "false")
+
+ # then
+ assert str(exc_info.value) == f"App not found: {app_id}."
+ app.refresh_from_db()
+ assert app.is_active is True
+ _patched_app_set_active_manager.app_status_changed.assert_not_called()
+
+
[email protected]("command", ["app_set_active", "webhook_set_active"])
[email protected]("active", ["yes", "1", ""])
+def test_set_active_rejects_non_boolean_active(command, active, db):
+ # when
+ with pytest.raises(CommandError) as exc_info:
+ call_command(command, "--id", "unused", "--active", active)
+
+ # then
+ assert str(exc_info.value) == (
+ f"Error: argument --active: expected 'true' or 'false', got {active!r}"
+ )
+
+
[email protected]("id_format", ["graphql", "database"])
[email protected](("before", "target"), [(True, False), (False, True)])
[email protected]("app_is_active", [True, False])
+def test_webhook_set_active_changes_state(before, target, app_is_active, id_format, db):
+ # given
+ app = App.objects.create(name="App", identifier="app", is_active=app_is_active)
+ webhook = _create_webhook(app, "hook", is_active=before)
+ other_webhook = _create_webhook(app, "other", is_active=before)
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+ command_id = webhook_id if id_format == "graphql" else str(webhook.pk)
+
+ # when
+ records = _call_command_json_lines(
+ "webhook_set_active", "--id", command_id, "--active", str(target)
+ )
+
+ # then
+ webhook.refresh_from_db()
+ other_webhook.refresh_from_db()
+ assert webhook.is_active is target
+ assert other_webhook.is_active is before
+ assert records == [
+ {
+ "kind": "webhook",
+ "id": webhook_id,
+ "app_id": graphene.Node.to_global_id("App", app.pk),
+ "name": webhook.name,
+ "is_active": {"before": before, "after": target},
+ }
+ ]
+
+
[email protected]("state", [True, False])
+def test_webhook_set_active_same_state_reports_no_change(state, db):
+ # given
+ app = App.objects.create(name="App", identifier="app", is_active=True)
+ webhook = _create_webhook(app, "hook", is_active=state)
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+
+ # when
+ records = _call_command_json_lines(
+ "webhook_set_active", "--id", webhook_id, "--active", str(state)
+ )
+
+ # then
+ webhook.refresh_from_db()
+ assert webhook.is_active is state
+ assert records[0]["is_active"] == {"before": state, "after": state}
+
+
+ ("_case", "global_id", "message"),
+ [
+ ("not_base64", "garbage", "Invalid ID: garbage. Expected: Webhook."),
+ (
+ "wrong_type",
+ graphene.Node.to_global_id("App", 1),
+ "Invalid ID: QXBwOjE=. Expected: Webhook, received: App.",
+ ),
+ (
+ "unknown_pk",
+ graphene.Node.to_global_id("Webhook", -1),
+ "Webhook not found: V2ViaG9vazotMQ==.",
+ ),
+ ("unknown_database_id", "-1", "Webhook not found: -1."),
+ ],
+)
+def test_webhook_set_active_rejects_invalid_id(_case, global_id, message, db):
+ # when
+ with pytest.raises(CommandError) as exc_info:
+ call_command("webhook_set_active", "--id", global_id, "--active", "false")
+
+ # then
+ assert str(exc_info.value) == message
+
+
+def test_webhook_set_active_rejects_webhook_of_removed_app(db):
+ # given
+ app = App.objects.create(
+ name="Removed", identifier="removed", removed_at=timezone.now()
+ )
+ webhook = _create_webhook(app, "hook", is_active=True)
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+
+ # when
+ with pytest.raises(CommandError) as exc_info:
+ call_command("webhook_set_active", "--id", webhook_id, "--active", "false")
+
+ # then
+ assert str(exc_info.value) == f"Webhook not found: {webhook_id}."
+ webhook.refresh_from_db()
+ assert webhook.is_active is True
diff --git a/saleor/core/utils/tests/test_url.py b/saleor/core/utils/tests/test_url.py
index 13a5b85da2..261d2291ba 100644
--- a/saleor/core/utils/tests/test_url.py
+++ b/saleor/core/utils/tests/test_url.py
@@ -51,3 +51,27 @@ def test_get_default_storage_root_url():
)
def test_sanitize_url_for_logging(url, expected):
assert sanitize_url_for_logging(url) == expected
+
+
+ ("strip_querystring", "strip_fragment", "expected"),
+ [
+ (False, False, "https://***:***@example.com/test?token=secret#frag"),
+ (True, False, "https://***:***@example.com/test#frag"),
+ (False, True, "https://***:***@example.com/test?token=secret"),
+ (True, True, "https://***:***@example.com/test"),
+ ],
+)
+def test_sanitize_url_for_logging_strips_querystring_and_fragment(
+ strip_querystring, strip_fragment, expected
+):
+ # given
+ url = "https://user:[email protected]/test?token=secret#frag"
+
+ # when
+ result = sanitize_url_for_logging(
+ url, strip_querystring=strip_querystring, strip_fragment=strip_fragment
+ )
+
+ # then
+ assert result == expected
diff --git a/saleor/core/utils/url.py b/saleor/core/utils/url.py
index a36dccb4c8..8bc13ea530 100644
--- a/saleor/core/utils/url.py
+++ b/saleor/core/utils/url.py
@@ -51,7 +51,9 @@ def get_default_storage_root_url():
return build_absolute_uri(default_storage.url(tmp_path)).rstrip(tmp_path)
-def sanitize_url_for_logging(url: str) -> str:
+def sanitize_url_for_logging(
+ url: str, *, strip_querystring: bool = False, strip_fragment: bool = False
+) -> str:
"""Remove sensitive data from a URL to make it safe for logging."""
url_parts = urlparse(url)
if url_parts.username or url_parts.password:
@@ -60,4 +62,8 @@ def sanitize_url_for_logging(url: str) -> str:
if url_parts.port
else f"***:***@{url_parts.hostname}"
)
+ if strip_querystring:
+ url_parts = url_parts._replace(query="")
+ if strip_fragment:
+ url_parts = url_parts._replace(fragment="")
return url_parts.geturl()