Skip to content
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

Add identity to issued commands through the commander view #278

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Version History
===============

v7.0.4
v7.1.0
------

* Add identity to issued commands through the commander view `<https://github.com/lsst-ts/LOVE-manager/pull/278>`_
* Remove cmd user creation for production deployments `<https://github.com/lsst-ts/LOVE-manager/pull/280>`_

v7.0.3
Expand Down
15 changes: 11 additions & 4 deletions manager/api/tests/test_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@


import os
from unittest.mock import call, patch

from api.models import Token
from django.contrib.auth.models import Permission, User
from django.test import TestCase, override_settings
from django.urls import reverse
from api.models import Token
from rest_framework.test import APIClient
from django.contrib.auth.models import User, Permission
from unittest.mock import patch, call

from manager.utils import UserBasedPermission

# python manage.py test api.tests.test_commander.CommanderTestCase
Expand Down Expand Up @@ -60,6 +62,7 @@ def setUp(self):
os.environ["COMMANDER_PORT"] = "bar"

@patch("requests.post")
@patch.dict(os.environ, {"SERVER_URL": "localhost"})
def test_authorized_commander_data(self, mock_requests):
"""Test authorized user commander data is sent to love-commander"""
# Arrange:
Expand All @@ -73,11 +76,15 @@ def test_authorized_commander_data(self, mock_requests):
"cmd": "cmd_setScalars",
"params": {"a": 1, "b": 2},
}
data_with_identity = data.copy()
data_with_identity["identity"] = f"{self.user.username}@localhost"

with self.assertRaises(ValueError):
self.client.post(url, data, format="json")
expected_url = "http://foo:bar/cmd"
self.assertEqual(mock_requests.call_args, call(expected_url, json=data))
self.assertEqual(
mock_requests.call_args, call(expected_url, json=data_with_identity)
)

@patch("requests.post")
def test_unauthorized_commander(self, mock_requests):
Expand Down
11 changes: 9 additions & 2 deletions manager/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ def validate_config_schema(request):
@permission_classes((IsAuthenticated, CommandPermission))
def commander(request):
"""Sends a command to the LOVE-commander
according to the received parameters
according to the received parameters.

Command identity is arranged here.

Params
------
Expand All @@ -449,8 +451,13 @@ def commander(request):
Response
The response and status code of the request to the LOVE-Commander
"""
# Arrange command indentity
request_data = request.data.copy()
host_fqdn = os.environ.get("SERVER_URL", None)
request_data["identity"] = f"{request.user.username}@{host_fqdn}"

url = f"http://{os.environ.get('COMMANDER_HOSTNAME')}:{os.environ.get('COMMANDER_PORT')}/cmd"
response = requests.post(url, json=request.data)
response = requests.post(url, json=request_data)

return Response(response.json(), status=response.status_code)

Expand Down
Loading