Search code examples
pythongnome

Python gnomekeyring get Attributes of key


How can I access the attributes of an key saved in the Gnome keyring with Python using the module gnomekeyring?

I created the key with the following code and want to access database_ip and database_user

import gnomekeyring


attributes = {
    'database_ip'   : "localhost",
    'database_name' : "test",
    'database_user' : "test"
    }

gnomekeyring.item_create_sync('login', gnomekeyring.ITEM_GENERIC_SECRET, "mynewkey", attributes, "mysecretpassword", True)

Solution

  • You can access the attributes with:

    gnomekeyring.item_get_attributes_sync('login', key_id)
    

    Example:

    key_names = {}
    key_ids = gnomekeyring.list_item_ids_sync('login')
    for key_id in key_ids:
        key_info = gnomekeyring.item_get_info_sync('login', key_id)
        key_names[key_info.get_display_name()]=key_id
    
    if "mynewkey" in key_names.keys():
        key_attributes = gnomekeyring.item_get_attributes_sync('login', key_names["mynewkey"])
        print key_attributes["database_ip"]
        print key_attributes["database_user"]