Fix credential profile overwrite in config.toml#474
Open
vivienhenz24 wants to merge 2 commits intorunpod:mainfrom
Open
Fix credential profile overwrite in config.toml#474vivienhenz24 wants to merge 2 commits intorunpod:mainfrom
vivienhenz24 wants to merge 2 commits intorunpod:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a data-loss issue in the CLI config credential writer by preserving existing profiles in ~/.runpod/config.toml instead of overwriting the entire file when adding a profile.
Changes:
- Updated
set_credentials()to read/parse the existing TOML, update/add the target profile, and write the full config back. - Switched the credential-writing implementation to use
tomlkitfor parsing/writing. - Updated the unit test to reflect the new read-then-write flow.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
runpod/cli/groups/config/functions.py |
Loads existing TOML config, updates/creates the requested profile, and dumps the full document back to disk. |
tests/test_cli/test_cli_groups/test_config_functions.py |
Adjusts set_credentials() test to expect a read + write cycle and tomlkit.dump() usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+43
to
+45
| raise ValueError( | ||
| "Profile already exists. Use `update_credentials` instead." | ||
| ) |
Comment on lines
49
to
+50
| with open(CREDENTIAL_FILE, "w", encoding="UTF-8") as cred_file: | ||
| cred_file.write("[" + profile + "]\n") | ||
| cred_file.write('api_key = "' + api_key + '"\n') | ||
| tomlkit.dump(config, cred_file) |
Comment on lines
+24
to
26
| mock_document.side_effect = [{}, {"default": True}] | ||
| functions.set_credentials("RUNPOD_API_KEY") | ||
|
|
Comment on lines
+19
to
+39
| @patch("builtins.open", new_callable=mock_open, read_data="") | ||
| def test_set_credentials(self, mock_file, mock_document, mock_dump): | ||
| """ | ||
| Tests the set_credentials function. | ||
| """ | ||
| mock_toml_load.return_value = "" | ||
| mock_document.side_effect = [{}, {"default": True}] | ||
| functions.set_credentials("RUNPOD_API_KEY") | ||
|
|
||
| mock_file.assert_called_with(functions.CREDENTIAL_FILE, "w", encoding="UTF-8") | ||
| assert any( | ||
| call.args[0] == functions.CREDENTIAL_FILE | ||
| and call.args[1] == "r" | ||
| and call.kwargs.get("encoding") == "UTF-8" | ||
| for call in mock_file.call_args_list | ||
| ) | ||
| assert any( | ||
| call.args[0] == functions.CREDENTIAL_FILE | ||
| and call.args[1] == "w" | ||
| and call.kwargs.get("encoding") == "UTF-8" | ||
| for call in mock_file.call_args_list | ||
| ) | ||
| assert mock_dump.called |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Found a data‑loss bug in set_credentials() where it opens config.toml in "w" mode and writes only the current profile, which truncates the file and deletes all other profiles. That means if you already have [default] and [profile1], calling set_credentials("new-key", "profile2") leaves you with only [profile2] and no warning.
I switched it to read the existing TOML, update/add the requested profile in memory, and write the whole thing back so nothing else is lost, and adjusted the test to match.