Repository navigation
NMV-1073: S1 — Migrate Python SDK's update_secrets_collection onto the partial-update endpoint #375
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
Merged
racquesta
merged 4 commits into
develop
from
feature/NMV-1073/s1-migrate-python-sdks-update-secrets-collection
Oct 19, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7ee1204
feat(secrets): route secrets collection updates through the partial-u…
racquesta 5796050
refactor(secrets): keep secrets collection update as a full replace […
racquesta 445c0ed
feat(secrets): add --keep-existing to secrets collection update [NMV-…
racquesta c155759
fix(secrets): reject duplicate secrets in secrets collection update […
racquesta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from nextmv.cloud import Application, Client | ||
| from nextmv.cloud.secrets import Secret, SecretType | ||
|
|
||
| MASKED_VALUE = "***************" | ||
|
|
||
| COLLECTION_SUMMARY = { | ||
| "id": "sc-1", | ||
| "application_id": "my-app", | ||
| "name": "API Keys", | ||
| "description": "Keys for external services", | ||
| "created_at": "2026-08-12T15:04:05Z", | ||
| "updated_at": "2026-09-10T09:21:33Z", | ||
| } | ||
|
|
||
| MASKED_COLLECTION = { | ||
| **COLLECTION_SUMMARY, | ||
| "secrets": [ | ||
| {"type": "env", "location": "API_KEY", "value": MASKED_VALUE}, | ||
| {"type": "env", "location": "DATABASE_URL", "value": MASKED_VALUE}, | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def _response(data: dict) -> MagicMock: | ||
| response = MagicMock() | ||
| response.json.return_value = data | ||
| return response | ||
|
|
||
|
|
||
| class TestUpdateSecretsCollection(unittest.TestCase): | ||
| """Test that update_secrets_collection never resends fetched secret values.""" | ||
|
|
||
| def setUp(self): | ||
| self.app = Application(id="my-app", client=Client(api_key="test-key")) | ||
|
|
||
| def test_metadata_only_uses_patch_without_secrets(self): | ||
| for secrets in (None, []): | ||
| with self.subTest(secrets=secrets): | ||
| with patch.object(Client, "request", return_value=_response(MASKED_COLLECTION)) as mock_request: | ||
| self.app.update_secrets_collection("sc-1", name="New Name", secrets=secrets) | ||
|
|
||
| mock_request.assert_called_once_with( | ||
| method="PATCH", | ||
| endpoint="v1/applications/my-app/secrets/sc-1", | ||
| payload={"name": "New Name"}, | ||
| ) | ||
|
|
||
| def test_nothing_to_update_raises(self): | ||
| with patch.object(Client, "request") as mock_request: | ||
| with self.assertRaises(ValueError): | ||
| self.app.update_secrets_collection("sc-1") | ||
|
|
||
| mock_request.assert_not_called() | ||
|
|
||
| def test_with_secrets_replaces_via_patch_operations(self): | ||
| responses = [_response(MASKED_COLLECTION), _response(MASKED_COLLECTION)] | ||
| with patch.object(Client, "request", side_effect=responses) as mock_request: | ||
| self.app.update_secrets_collection( | ||
| "sc-1", | ||
| description="New Description", | ||
| secrets=[ | ||
| Secret(secret_type=SecretType.ENV, location="API_KEY", value="new-api-key"), | ||
| {"type": "file", "location": "/cert.pem", "value": "cert"}, | ||
| ], | ||
| ) | ||
|
|
||
| self.assertEqual(mock_request.call_count, 2) | ||
| get_call, patch_call = mock_request.call_args_list | ||
| self.assertEqual(get_call.kwargs["method"], "GET") | ||
| self.assertEqual(patch_call.kwargs["method"], "PATCH") | ||
| self.assertEqual(patch_call.kwargs["endpoint"], "v1/applications/my-app/secrets/sc-1") | ||
|
|
||
| payload = patch_call.kwargs["payload"] | ||
| self.assertEqual( | ||
| payload, | ||
| { | ||
| "description": "New Description", | ||
| "operations": [ | ||
| {"op": "update", "type": "env", "location": "API_KEY", "value": "new-api-key"}, | ||
| {"op": "create", "type": "file", "location": "/cert.pem", "value": "cert"}, | ||
| {"op": "delete", "type": "env", "location": "DATABASE_URL"}, | ||
| ], | ||
| }, | ||
| ) | ||
| self.assertNotIn(MASKED_VALUE, str(payload)) | ||
|
|
||
| def test_with_secrets_keep_existing_skips_deletes(self): | ||
| responses = [_response(MASKED_COLLECTION), _response(MASKED_COLLECTION)] | ||
| with patch.object(Client, "request", side_effect=responses) as mock_request: | ||
| self.app.update_secrets_collection( | ||
| "sc-1", | ||
| secrets=[ | ||
| Secret(secret_type=SecretType.ENV, location="API_KEY", value="new-api-key"), | ||
| {"type": "file", "location": "/cert.pem", "value": "cert"}, | ||
| ], | ||
| keep_existing=True, | ||
| ) | ||
|
|
||
| _, patch_call = mock_request.call_args_list | ||
| payload = patch_call.kwargs["payload"] | ||
| self.assertEqual( | ||
| payload, | ||
| { | ||
| "operations": [ | ||
| {"op": "update", "type": "env", "location": "API_KEY", "value": "new-api-key"}, | ||
| {"op": "create", "type": "file", "location": "/cert.pem", "value": "cert"}, | ||
| ], | ||
| }, | ||
| ) | ||
| self.assertNotIn(MASKED_VALUE, str(payload)) | ||
|
|
||
| def test_never_uses_put(self): | ||
| responses = [_response(MASKED_COLLECTION), _response(MASKED_COLLECTION), _response(MASKED_COLLECTION)] | ||
| with patch.object(Client, "request", side_effect=responses) as mock_request: | ||
| self.app.update_secrets_collection("sc-1", name="New Name") | ||
| self.app.update_secrets_collection( | ||
| "sc-1", | ||
| secrets=[Secret(secret_type=SecretType.ENV, location="API_KEY", value="v")], | ||
| ) | ||
|
|
||
| methods = [call.kwargs["method"] for call in mock_request.call_args_list] | ||
| self.assertNotIn("PUT", methods) | ||
|
|
||
| def test_invalid_secret_raises_before_any_request(self): | ||
| with patch.object(Client, "request") as mock_request: | ||
| with self.assertRaises(ValueError): | ||
| self.app.update_secrets_collection("sc-1", secrets=["not-a-secret"]) | ||
|
|
||
| mock_request.assert_not_called() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Minor · non-blocking
If the caller passes the same
(type, location)twice (e.g. a copy-paste in--secrets '[...]'), this emits twoupdate/createops for the same key. With the old PUT the server just got the list. Now the result depends on how C2 handles duplicate ops in one PATCH: an error, last-one-wins, or a partially applied request if it isn't atomic. A cheap client-side check would make this deterministic, e.g. raisingValueError("duplicate secret ...")whenlen(new_keys) != len(new_secrets), before the GET. Non-blocking: fine to skip if C2 already rejects duplicates atomically.