How do I list and access the secrets stored in Ubuntu's keyring from the command line?

3.6k views Asked by At

I see this in the help

    keyring -h
usage: keyring [-h] [-p KEYRING_PATH] [-b KEYRING_BACKEND] [--list-backends] [--disable]
               [operation] [service] [username]

positional arguments:
  operation             get|set|del
  service
  username

optional arguments:
  -h, --help            show this help message and exit
  -p KEYRING_PATH, --keyring-path KEYRING_PATH
                        Path to the keyring backend
  -b KEYRING_BACKEND, --keyring-backend KEYRING_BACKEND
                        Name of the keyring backend
  --list-backends       List keyring backends and exit
  --disable             Disable keyring and exit

However without remembering what I stored there a long time ago I can't figure out how to list the secrets stored there. What is the right command line for this ?

2

There are 2 answers

0
Mads Skjern On BEST ANSWER

The keyring command line tool does not support that.

A feature request has been discussed in this thread (link) , but it has not been implemented.

The 'keyring' tool is not a keychain service in itself, but a wrapper for other keychain services, called backends.

It can get and set secrets in a variety of keychain services, including: KWallet, SecretService, Windows Credential Locker, etc. These are called backends.

Use the underlying keychain service for getting a list.

In my case, on Ubuntu 22.04, the underlying default keychain tool is SecretService. I was able to get a list with the following Python code.

import secretstorage
conn = secretstorage.dbus_init()
collection = secretstorage.get_default_collection(conn)
for item in collection.get_all_items():
    print('='*30)
    print('label:', item.get_label())
    print('attributes:')
    for k,v in item.get_attributes().items():
        print('\t%-12s: %s' % (k,v))
    print('secret:',item.get_secret())

The underlying Python library does partly support this

The keyring command line tool is just the command line part of a Python library https://github.com/jaraco/keyring.

With some Python knowledge you are able to use keyring to get such a list.

for item in keyring.get_keyring().get_preferred_collection().get_all_items():
     print(item.get_label(), item.get_attributes())

However, I suspect it only lists the secrets for one backend (the default/preferred one), in my case SecretService. I assume most people only have one keychain service in use, but there might be cases where there is more than one.

2
MiniMe On

In my case I am using a Python Keyring utility https://github.com/jaraco/keyring

The above file indicated by the output of the below contain the key/value you stored there, encrypted

python -c "import keyring.util.platform_; print(keyring.util.platform_.data_root())"