diff --git a/HISTORY.rst b/HISTORY.rst index e61fa66..670b1af 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -7,6 +7,7 @@ History * Added methods for the instance secret fingerprint and for linking the instance secret with another DataMasque instance. * Added ``rotate_instance_secret``, which replaces the instance secret with a newly generated one. +* Added ``get_current_instance_secret_links``, which returns the links that rotating the instance secret would end. Requires server version TBD diff --git a/datamasque/client/instance_secret.py b/datamasque/client/instance_secret.py index 8ab7bc4..69ec276 100644 --- a/datamasque/client/instance_secret.py +++ b/datamasque/client/instance_secret.py @@ -6,6 +6,7 @@ from datamasque.client.exceptions import InstanceSecretLinkError, InstanceSecretLinkErrorCode from datamasque.client.models.instance_secret import ( InFlightSyncResult, + InstanceSecretEvent, InstanceSecretLinkRequest, InstanceSecretLinkRequestVerification, InstanceSecretLinkResponse, @@ -57,6 +58,22 @@ def get_instance_secret_fingerprint(self) -> str: fingerprint: str = response.json()["fingerprint"] return fingerprint + def get_current_instance_secret_links(self) -> list[InstanceSecretEvent]: + """ + Returns the shares and adoptions that link this DataMasque instance with others, newest first. + + These are the links that `rotate_instance_secret` would end, and the list is empty after a rotation. + Another DataMasque instance can rotate or restore its own instance secret without telling this one, + so a link listed here may have ended already. + Requires an admin user, and works on every license type. + + Raises: + DataMasqueApiError: the user is not an admin user. + """ + + response = self.make_request("GET", "/api/instance-secret/current-links/") + return [InstanceSecretEvent.model_validate(link) for link in response.json()["links"]] + def rotate_instance_secret(self) -> InFlightSyncResult: """ Replaces the instance secret of this DataMasque instance with a newly generated one. diff --git a/tests/test_instance_secret.py b/tests/test_instance_secret.py index 878c8e0..dc8384b 100644 --- a/tests/test_instance_secret.py +++ b/tests/test_instance_secret.py @@ -42,6 +42,53 @@ def test_get_instance_secret_fingerprint_returns_the_fingerprint(client): assert client.get_instance_secret_fingerprint() == "7KQ2-9MXA-3HDP-T0WZ" +def test_get_current_instance_secret_links_returns_the_links_in_order(client): + shared = event_json( + "shared", + peer_hostname="joining.example.com", + request_fingerprint="7KQ2-9MXA-3HDP-T0WZ", + response_fingerprint="0F2A-9QXH-7RZW-B4ND", + ) + adopted = { + **event_json( + "adopted", + peer_hostname="sharing.example.com", + request_fingerprint="EBDV-ECVC-8QJ4-9XT5", + response_fingerprint="9GGM-DR1H-ETXX-41Q6", + ), + "id": "8a1c2d3e-4f5a-4b6c-8d7e-9f0a1b2c3d4e", + } + with requests_mock.Mocker() as m: + m.get("http://test-server/api/instance-secret/current-links/", json={"links": [shared, adopted]}) + links = client.get_current_instance_secret_links() + + assert [(link.kind, link.peer_hostname) for link in links] == [ + (InstanceSecretEventKind.shared, "joining.example.com"), + (InstanceSecretEventKind.adopted, "sharing.example.com"), + ] + assert links[0].created_at == datetime(2026, 10, 30, 11, 0, tzinfo=timezone.utc) + assert links[1].response_fingerprint == "9GGM-DR1H-ETXX-41Q6" + + +def test_get_current_instance_secret_links_returns_an_empty_list_when_there_are_none(client): + with requests_mock.Mocker() as m: + m.get("http://test-server/api/instance-secret/current-links/", json={"links": []}) + assert client.get_current_instance_secret_links() == [] + + +def test_get_current_instance_secret_links_by_a_user_who_is_not_an_admin_raises_data_masque_api_error(client): + with requests_mock.Mocker() as m: + m.get( + "http://test-server/api/instance-secret/current-links/", + json={"detail": "You do not have permission to perform this action."}, + status_code=403, + ) + with pytest.raises(DataMasqueApiError) as error_info: + client.get_current_instance_secret_links() + + assert error_info.value.response.status_code == 403 + + @pytest.mark.parametrize("in_flight_sync", list(InFlightSyncResult)) def test_rotate_instance_secret_returns_the_in_flight_sync_result(client, in_flight_sync): with requests_mock.Mocker() as m: