secrets-manager icon indicating copy to clipboard operation
secrets-manager copied to clipboard

Python SDK - get a list of records from a shared folder based on folder name

Open heretic098 opened this issue 2 years ago • 7 comments

Hello, please excuse the bonehead question but is there a nice way to get a list of Keeper records based on the name of a shared folder?

e.g. if in my keeper vault I have a shared folder called "myfolder" containing two records, "secret1" and "secret2" is there a way that I can sensibly invoke https://github.com/Keeper-Security/secrets-manager/blob/master/sdk/python/core/keeper_secrets_manager_core/dto/dtos.py#L269-L290 to get it to spit out the folder representation and the secrets so I can go:

myfolder = SecretsManager.folder_by_name("myfolder")
for secret in myfolder.records:
  print(secret.uid)

heretic098 avatar Feb 17 '23 13:02 heretic098

Folder management is on the roadmap but still work in progress. Secrets manager clients and SDKs will start adding new features as soon as back-end release with KSM folder management goes live.

idimov-keeper avatar Feb 17 '23 16:02 idimov-keeper

@heretic098 right now the payload from the backend returns Shared Folder UID, not title yet. Here is an example code that shows how to get records in the Shared Folders, listing Shared Folder UID:

resp = secrets_manager.get_secrets(full_response=True)

for folder in resp.folders:
    print(f'Shared Folder UID: {folder.uid}')
    for record in folder.records:
        print(f"\t{record.uid} - {record.title}")

Example output will look like this:

Shared Folder UID: OWyDV1XnTdpfxnlk-w0fLQ
	KfCMYdaai2ignkAcQfXXfg - QA User 1
	O_zJWnfE7SwVOKUtohRr6A - Office Code
	pHVV9DQdmL0IKGb547Z9Yg - DB Resource
	w7GRHJpKrYIG_IveAfDvgQ - Config Files
Shared Folder UID: 9qVjkUR6-WzA9aGFRKa69w
	Jm1HKxKDCCAVXL8kdjKbMQ - MySQL Regular user
	mm9wuCFQBQK6glG-e1xPmQ - MySQL (root)

I will create a feature request to add Shared Folder title to the response from the backend.

maksimu avatar Feb 17 '23 17:02 maksimu

Thanks, @maksimu , this did work. I'd be interested in whatever the issue number, ticket number whatever is for the feature request for the backend. I'm happy for you to close this issue if it's not useful to keep it open.

heretic098 avatar Feb 20 '23 15:02 heretic098

With the release of KSM CLI 1.1.3 you can now list secrets in any folder or filter records by matching a JSON Path query release notes download

idimov-keeper avatar Apr 16 '24 20:04 idimov-keeper

With the release of KSM CLI 1.1.3 you can now list secrets in any folder or filter records by matching a JSON Path query release notes download

I'm currently trying to use the KSM CLI now and the JSON Path query function may as well not even be there, for all the good it does. The only path that seems to return any data is '$'. If I try to specify a folder (eg: '$.certificates') I get nothing.

Additionally, it should at least be possible to enumerate the folders so we can figure out what UID we need. As it stands, the only way to get a UID is by manually going into the Vault GUI by hand and compiling a list of folders with their associated UIDs. That's just absurd.

ilsaloving avatar Apr 24 '24 01:04 ilsaloving

ksm secret list command supports following options

-f, --folder <folder UID>  List only records in the selected folder UID.
-r, --recursive list recursively all records in the selected folder UID including all subfolders.
-q, --query <JSONPath Query>  List only records matching the JSON Path query.
-v, --show-value print matching value instead of the record title when using JSON Path query.

where -q, --query option is for the path inside the record data - ex. --query '$.fields[?(@.type=="password")].value' will list only secrets that have a value in the password field and you can use -v, --show-value to optionally print the field value instead of record title. You can have all options active -f -r -q -v that means print record UIDs and values of all records matching the JSONPath query inside a folder (and all its sub-folders if -r present)

idimov-keeper avatar Apr 24 '24 02:04 idimov-keeper

That explanation and example needs to be added to the documentation. For anyone new to the system, it's not at all obvious.

ilsaloving avatar Apr 24 '24 13:04 ilsaloving

KSM CLI 1.1.4 added search by title to ksm secret list command and ksm folder ... commands.

idimov-keeper avatar Jun 05 '24 21:06 idimov-keeper