OpenStack Barbican Client Library

7.3.0 · active · verified Thu Apr 16

python-barbicanclient is the official Python client library for interacting with the OpenStack Barbican Key Management API. It provides programmatic access to store, manage, and retrieve secrets, and also includes a command-line interface (`barbican`). The library is actively maintained as part of the OpenStack ecosystem, with releases generally aligning with OpenStack's development cadence. The current version is 7.3.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with OpenStack Keystone and then use the `python-barbicanclient` to create and store a simple secret. It assumes environment variables are set for OpenStack authentication, which is a common practice.

import os
from keystoneclient.auth import identity
from keystoneauth1 import session
from barbicanclient import client

# Configure Keystone authentication using environment variables
auth_url = os.environ.get('OS_AUTH_URL', 'http://localhost:5000/v3')
username = os.environ.get('OS_USERNAME', 'admin')
user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
password = os.environ.get('OS_PASSWORD', 'password')
project_name = os.environ.get('OS_PROJECT_NAME', 'demo')
project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')
barbican_endpoint = os.environ.get('OS_BARBICAN_ENDPOINT', 'http://localhost:9311/v1')

# Create a Keystone authentication plugin
auth = identity.v3.Password(
    auth_url=auth_url,
    username=username,
    user_domain_name=user_domain_name,
    password=password,
    project_name=project_name,
    project_domain_name=project_domain_name
)

# Create a Keystone session
sess = session.Session(auth=auth)

# Create a Barbican client instance
# Pass the Barbican endpoint directly if not discoverable via Keystone catalog
barbican = client.Client(session=sess, endpoint=barbican_endpoint, version='v1')

# Example: Create and store a secret
try:
    secret_name = "my-test-secret"
    payload = "my_sensitive_data_123"
    secret = barbican.secrets.create(name=secret_name, payload=payload)
    secret.store()
    print(f"Secret '{secret_name}' stored with URI: {secret.secret_ref}")

    # Example: Retrieve the secret
    retrieved_secret = barbican.secrets.get(secret.secret_ref)
    print(f"Retrieved secret name: {retrieved_secret.name}")
    # Note: To retrieve the actual payload, you would typically call .payload on the retrieved secret,
    # but direct payload retrieval for security reasons is often handled carefully and might require specific permissions/methods.
    # For this example, we just show retrieval of metadata.

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure Barbican and Keystone services are running and accessible.")
    print("Check environment variables like OS_AUTH_URL, OS_USERNAME, OS_PASSWORD, OS_PROJECT_NAME, OS_BARBICAN_ENDPOINT.")

view raw JSON →