-
Notifications
You must be signed in to change notification settings - Fork 123
Add credential handling to UserClient and ToolClient #534
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
Open
dannon
wants to merge
4
commits into
galaxyproject:main
Choose a base branch
from
dannon:credential-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da0efd9
Add credential handling to UserClient and ToolClient
dannon 43d76f7
Fix credential response field names and add tests
dannon a2d7d47
Add select_credential_group and fix review issues
dannon 09796ef
Fix CI: use black-compatible formatting, bump version gate, handle mi…
dannon 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -257,3 +257,199 @@ def update_user(self, user_id: str, user_data: dict | None = None, **kwargs: Any | |||||
| user_data.update(kwargs) | ||||||
| url = self._make_url(user_id) + "/information/inputs" | ||||||
| return self._put(url=url, payload=user_data, id=user_id) | ||||||
|
|
||||||
| def get_credentials( | ||||||
| self, | ||||||
| user_id: str, | ||||||
| source_type: str = "tool", | ||||||
| source_id: str | None = None, | ||||||
| source_version: str | None = None, | ||||||
| ) -> list[dict[str, Any]]: | ||||||
| """ | ||||||
| Get stored credentials for a user, optionally filtered by tool. | ||||||
|
|
||||||
| :type user_id: str | ||||||
| :param user_id: encoded user ID | ||||||
|
|
||||||
| :type source_type: str | ||||||
| :param source_type: credential source type (default: 'tool') | ||||||
|
|
||||||
| :type source_id: str | ||||||
| :param source_id: tool ID to filter by | ||||||
|
|
||||||
| :type source_version: str | ||||||
| :param source_version: tool version to filter by | ||||||
|
|
||||||
| :rtype: list of dicts | ||||||
| :return: list of stored credentials | ||||||
| """ | ||||||
| url = self._make_url(user_id) + "/credentials" | ||||||
| params: dict[str, str] = {"source_type": source_type} | ||||||
| if source_id is not None: | ||||||
| params["source_id"] = source_id | ||||||
| if source_version is not None: | ||||||
| params["source_version"] = source_version | ||||||
| return self._get(url=url, params=params) | ||||||
|
|
||||||
| def create_credentials( | ||||||
| self, | ||||||
| user_id: str, | ||||||
| source_type: str, | ||||||
| source_id: str, | ||||||
| source_version: str, | ||||||
| service_name: str, | ||||||
| service_version: str, | ||||||
| group_name: str, | ||||||
| variables: list[dict[str, str]] | None = None, | ||||||
| secrets: list[dict[str, str]] | None = None, | ||||||
| ) -> dict[str, Any]: | ||||||
| """ | ||||||
| Store credentials for a user (e.g. API keys for external services). | ||||||
|
|
||||||
| :type user_id: str | ||||||
| :param user_id: encoded user ID | ||||||
|
|
||||||
| :type source_type: str | ||||||
| :param source_type: credential source type (e.g. 'tool') | ||||||
|
|
||||||
| :type source_id: str | ||||||
| :param source_id: tool ID | ||||||
|
|
||||||
| :type source_version: str | ||||||
| :param source_version: tool version | ||||||
|
|
||||||
| :type service_name: str | ||||||
| :param service_name: name of the credential service | ||||||
|
|
||||||
| :type service_version: str | ||||||
| :param service_version: version of the credential service | ||||||
|
|
||||||
| :type group_name: str | ||||||
| :param group_name: name for the credential group (minimum 3 characters) | ||||||
|
|
||||||
| :type variables: list of dicts | ||||||
| :param variables: list of variable dicts with 'name' and 'value' keys | ||||||
|
|
||||||
| :type secrets: list of dicts | ||||||
| :param secrets: list of secret dicts with 'name' and 'value' keys | ||||||
|
|
||||||
| :rtype: dict | ||||||
| :return: the created credential group (with ``id``, ``name``, | ||||||
| ``variables``, ``secrets``, and ``update_time``) | ||||||
| """ | ||||||
| url = self._make_url(user_id) + "/credentials" | ||||||
| payload = { | ||||||
| "source_type": source_type, | ||||||
| "source_id": source_id, | ||||||
| "source_version": source_version, | ||||||
| "service_credential": { | ||||||
| "name": service_name, | ||||||
| "version": service_version, | ||||||
| "group": { | ||||||
| "name": group_name, | ||||||
| "variables": variables or [], | ||||||
| "secrets": secrets or [], | ||||||
| }, | ||||||
| }, | ||||||
| } | ||||||
| return self._post(url=url, payload=payload) | ||||||
|
|
||||||
| def select_credential_group( | ||||||
| self, | ||||||
| user_id: str, | ||||||
| source_type: str, | ||||||
| source_id: str, | ||||||
| source_version: str, | ||||||
| user_credentials_id: str, | ||||||
| group_id: str | None, | ||||||
| ) -> None: | ||||||
| """ | ||||||
| Select the active credential group for a set of user credentials. | ||||||
| This must be called after ``create_credentials()`` before the | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| credentials can be used with ``run_tool()``. | ||||||
|
|
||||||
| :type user_id: str | ||||||
| :param user_id: encoded user ID | ||||||
|
|
||||||
| :type source_type: str | ||||||
| :param source_type: credential source type (e.g. 'tool') | ||||||
|
|
||||||
| :type source_id: str | ||||||
| :param source_id: tool ID | ||||||
|
|
||||||
| :type source_version: str | ||||||
| :param source_version: tool version | ||||||
|
|
||||||
| :type user_credentials_id: str | ||||||
| :param user_credentials_id: encoded ID of the user credentials entry | ||||||
|
|
||||||
| :type group_id: str or None | ||||||
| :param group_id: encoded ID of the credential group to activate, | ||||||
| or ``None`` to unset | ||||||
| """ | ||||||
| url = self._make_url(user_id) + "/credentials" | ||||||
| payload = { | ||||||
| "source_type": source_type, | ||||||
| "source_id": source_id, | ||||||
| "source_version": source_version, | ||||||
| "service_credentials": [ | ||||||
| { | ||||||
| "user_credentials_id": user_credentials_id, | ||||||
| "current_group_id": group_id, | ||||||
| }, | ||||||
| ], | ||||||
| } | ||||||
| try: | ||||||
| self._put(url=url, payload=payload) | ||||||
| except ConnectionError as e: | ||||||
| if e.status_code == 204: | ||||||
| return None | ||||||
| raise | ||||||
|
|
||||||
| def get_credentials_for_tool( | ||||||
| self, | ||||||
| user_id: str, | ||||||
| tool_id: str, | ||||||
| tool_version: str | None = None, | ||||||
| ) -> list[dict[str, Any]] | None: | ||||||
| """ | ||||||
| Build a credentials_context list suitable for passing to | ||||||
| ``tools.run_tool()``. Returns None if no credentials are stored. | ||||||
|
|
||||||
| :type user_id: str | ||||||
| :param user_id: encoded user ID | ||||||
|
|
||||||
| :type tool_id: str | ||||||
| :param tool_id: tool ID to look up credentials for | ||||||
|
|
||||||
| :type tool_version: str | ||||||
| :param tool_version: tool version | ||||||
|
|
||||||
| :rtype: list of dicts or None | ||||||
| :return: credentials_context list for run_tool(), or None | ||||||
| """ | ||||||
| creds = self.get_credentials(user_id, source_type="tool", source_id=tool_id, source_version=tool_version) | ||||||
| if not creds: | ||||||
| return None | ||||||
| context = [] | ||||||
| for cred in creds: | ||||||
| current_group_id = cred.get("current_group_id") | ||||||
| if not current_group_id: | ||||||
| continue | ||||||
| group_name = "default" | ||||||
| for group in cred.get("groups", []): | ||||||
| if group["id"] == current_group_id: | ||||||
| group_name = group.get("name", "default") | ||||||
| break | ||||||
| context.append( | ||||||
| { | ||||||
| "user_credentials_id": cred["id"], | ||||||
| "name": cred.get("name", ""), | ||||||
| "version": cred.get("version", ""), | ||||||
| "selected_group": { | ||||||
| "id": current_group_id, | ||||||
| "name": group_name, | ||||||
| }, | ||||||
| } | ||||||
| ) | ||||||
| return context if context else None | ||||||
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.
This and the following tests are always skipped because
random_lines1doesn't require credentials, can we usesecret_toolinstead (which is used in Galaxy'stest/integration/test_credentials.py)?