Skip to content
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
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion datamasque/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion datamasque/client/instance_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -148,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(
Expand Down
2 changes: 1 addition & 1 deletion datamasque/client/models/instance_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
23 changes: 23 additions & 0 deletions tests/test_instance_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down