1Password/connect-sdk-python

List by tag

mafrosis opened this issue · 2 comments

Summary

I have existing CLI scripts which list secrets as k:v pairs by tag, using the v1 version of op. I'd like to be able to do this with v2 and Connect.

Use cases

I don't want to list ALL secrets. I don't want to fetch many by name. I want to list by a specific tag.

Proposed solution

I'm interested in contributing a solution, but I see a bunch of the code is autogenerated somehow. If you can advise then maybe I can implement what I need!

Hey @mafrosis,

Thank you for raising this. This is an interesting use case and I find it quite handy.

Indeed, a bunch of code has been generated based on the API spec at the beginning to ease the development, but now we're no longer relying on the autogenerated tool. So you don't need to worry too much about it.

We really appreciate your interest in contributing a solution for this. You can fork this repo and try to make a function that achieves your desired result. Then you can open a PR and we will take a look at it.

The API call for filtering the items by tag should have the following endpoint:

v1/vaults/<vault-id>/items?filter=tag eq <tag-name>

Here's an example function I've come up with that may achieve what you want:

    def get_items_by_tags(self, vault_id: str, tag: str):
        """Returns a list of item summaries for the specified vault
           filtered by the provided tags

        Args:
            vault_id (str): The id of the vault in which to get the items from
            tag (str): The list of tags to filter the items returned

        Raises:
            FailedToRetrieveItemException: Thrown when a HTTP error is returned
            from the 1Password Connect API

        Returns:
            List[SummaryItem]: A list of summarized items
        """
        filter_query = f'tag eq "{tag}"'
        url = f"/v1/vaults/{vault_id}/items?filter={filter_query}"

        response = self.build_request("GET", url)
        try:
            response.raise_for_status()
        except HTTPError:
            raise FailedToRetrieveItemException(
                f"Unable to retrieve items. Received {response.status_code} \
                     for {url} with message: {response.json().get('message')}"
            )

        return self.deserialize(response.content, "list[SummaryItem]")

In the meantime, I've also raised this with my team and will let you know when we same something in progress (like a PR) that may tackle this enhancement.

Was there any progress on this item?