Search code examples
pythonsecuritypassword-protectionpassword-manager

Access entry from Vaultwarden with Python


I want to use my vaultwarden docker instance on the server to handle multiple passwords securely. Within my python project I want to access one of the stored entries.

To access the vaultwarden I want to use https://github.com/numberly/python-vaultwarden

In my current code I can see all the ciphers in the collection. But I struggle to access the password for later use.

from uuid import UUID
from vaultwarden.clients.bitwarden import BitwardenAPIClient
from vaultwarden.models.bitwarden import OrganizationCollection, get_organization
from vaultwarden.utils.crypto import decode_cipher_string
import os

from dotenv import load_dotenv

load_dotenv()

bitwarden_client = BitwardenAPIClient(os.getenv("VW_URL"),
                                      os.getenv("VW_USER_MAIL"),
                                      os.getenv("VW_USER_PW"),
                                      os.getenv("VW_CLIENT_ID"),
                                      os.getenv("VW_CLIENT_SECRET"),
                                      os.getenv("VW_DEVICE_ID"))

org_uuid = UUID('...')
collection_uuid = UUID('...')

orga = get_organization(bitwarden_client, org_uuid)
collection_elements = orga.ciphers(collection_uuid)

for element in collection_elements:
    if element.Name == "Grafana":
        password_encrypted = element.model_extra["data"]["password"]
        password_decrypted = decode_cipher_string(password_encrypted)
        print(password_decrypted)

Any suggestion for accessing the password for later use inside of the program?


Solution

  • I solved the issue:

    for element in collection_elements:
    if element.Name == "Grafana":
        password_encrypted = element.model_extra["data"]["password"]
        password_decrypted = decrypt(password_encrypted, orga.key())
        print(password_decrypted)
    

    I needed to get the key of the organization to decrypt the encrypted password with the decrypt() methode.