From b84f908ba1ae1bbdc5d069a9035c463fb5774cd9 Mon Sep 17 00:00:00 2001 From: Andrew Chester Date: Tue, 17 Nov 2026 11:27:49 +1300 Subject: [PATCH 1/2] feat: DM-5075 add rotate_instance_secret --- HISTORY.rst | 1 + datamasque/client/instance_secret.py | 19 +++++++++++++++++++ tests/test_instance_secret.py | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 9204831..e61fa66 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -6,6 +6,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. Requires server version TBD diff --git a/datamasque/client/instance_secret.py b/datamasque/client/instance_secret.py index e720334..6dc3784 100644 --- a/datamasque/client/instance_secret.py +++ b/datamasque/client/instance_secret.py @@ -57,6 +57,25 @@ def get_instance_secret_fingerprint(self) -> str: fingerprint: str = response.json()["fingerprint"] return fingerprint + def rotate_instance_secret(self) -> InFlightSyncResult: + """ + Replaces the instance secret of this DataMasque instance with a newly generated one. + + Requires an admin user. + Hashed output of masking runs and in-flight masking changes differs from before this call, + and the instance no longer has the same instance secret as any DataMasque instance it was linked with. + Once this succeeds, masking runs use the new instance secret. + Returns an `InFlightSyncResult` stating whether the IFM service successfully received the new instance secret. + If DataMasque fails to notify the IFM service, + a DataMasque restart is required for in-flight masking to use the new instance secret. + + Raises: + DataMasqueApiError: the user is not an admin user. + """ + + response = self.make_request("POST", "/api/instance-secret/rotate/") + return InFlightSyncResult(response.json()["in_flight_sync"]) + def create_instance_secret_link_request(self) -> InstanceSecretLinkRequest: """ Creates a request code for joining the instance secret of another DataMasque instance. diff --git a/tests/test_instance_secret.py b/tests/test_instance_secret.py index a73c784..878c8e0 100644 --- a/tests/test_instance_secret.py +++ b/tests/test_instance_secret.py @@ -42,6 +42,29 @@ def test_get_instance_secret_fingerprint_returns_the_fingerprint(client): assert client.get_instance_secret_fingerprint() == "7KQ2-9MXA-3HDP-T0WZ" +@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: + m.post("http://test-server/api/instance-secret/rotate/", json={"in_flight_sync": in_flight_sync.value}) + result = client.rotate_instance_secret() + + assert m.call_count == 1 + assert result is in_flight_sync + + +def test_rotate_instance_secret_by_a_user_who_is_not_an_admin_raises_data_masque_api_error(client): + with requests_mock.Mocker() as m: + m.post( + "http://test-server/api/instance-secret/rotate/", + json={"detail": "You do not have permission to perform this action."}, + status_code=403, + ) + with pytest.raises(DataMasqueApiError) as error_info: + client.rotate_instance_secret() + + assert error_info.value.response.status_code == 403 + + def test_create_instance_secret_link_request_posts_without_a_body(client): with requests_mock.Mocker() as m: m.post(f"{LINK_URL}/request/", json=LINK_REQUEST_JSON, status_code=201) From 1274b77cd14755500e99e32f6d7351ef35b73b76 Mon Sep 17 00:00:00 2001 From: Andrew Chester Date: Wed, 18 Nov 2026 16:06:45 +1300 Subject: [PATCH 2/2] docs: DM-4856 reword the stale verification text now the link history is removed --- datamasque/client/exceptions.py | 2 +- datamasque/client/instance_secret.py | 2 +- datamasque/client/models/instance_secret.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/datamasque/client/exceptions.py b/datamasque/client/exceptions.py index 6c392eb..b1f1e38 100644 --- a/datamasque/client/exceptions.py +++ b/datamasque/client/exceptions.py @@ -130,7 +130,7 @@ class InstanceSecretLinkErrorCode(enum.Enum): active_runs = "active_runs" """Runs are in progress. Cancel them or wait for them to finish.""" verification_stale = "verification_stale" - """The instance secret or its history changed after the response code was verified. Verify the response again.""" + """The instance secret changed or was shared since the response code was verified. Verify the response again.""" host_header_too_long = "host_header_too_long" """The hostname used to access DataMasque is longer than 253 characters, the longest that DNS allows.""" insecure_instance_secret = "insecure_instance_secret" diff --git a/datamasque/client/instance_secret.py b/datamasque/client/instance_secret.py index 6dc3784..8ab7bc4 100644 --- a/datamasque/client/instance_secret.py +++ b/datamasque/client/instance_secret.py @@ -167,7 +167,7 @@ def accept_instance_secret_link_response(self, response_code: str, verification_ Raises: ValueError: `response_code` or `verification_token` is empty. InstanceSecretLinkError: the response code is not valid, runs are in progress, - or the instance secret or its history changed after the response code was verified. + or the instance secret changed or was shared since the response code was verified. """ response = self._post_link_step( diff --git a/datamasque/client/models/instance_secret.py b/datamasque/client/models/instance_secret.py index 3939313..cb809c0 100644 --- a/datamasque/client/models/instance_secret.py +++ b/datamasque/client/models/instance_secret.py @@ -103,4 +103,4 @@ class InstanceSecretLinkResponseVerification(BaseModel): links_ending: list[InstanceSecretEvent] """The current links that accepting the response code will end.""" verification_token: str - """Pass this when accepting the response code, which then fails if the instance secret or its history has changed.""" + """Pass this when accepting the response code, which then fails if the instance secret changed or was shared since."""