OpenStack Keystone Client Library
python-keystoneclient is the official Python binding to the OpenStack Identity API (Keystone). It provides a Python API for interacting with the Keystone service to manage authentication, authorization, and service catalog discovery within an OpenStack environment. The current version is 5.8.0, and its release cadence is tied to the broader OpenStack development cycle.
Warnings
- deprecated The `keystone` command-line interface (CLI) is deprecated in favor of the `openstack` CLI provided by `python-openstackclient`.
- deprecated Non-session based authentication (passing username, password directly to `keystoneclient.Client` without a `keystoneauth1.session.Session` object) is deprecated.
- deprecated `keystoneclient` authentication plugins are deprecated in favor of `keystoneauth1` plugins.
- breaking The exception classes previously found under `keystoneclient.apiclient.exceptions` were removed and mapped to `keystoneauth1.exceptions`. Direct imports from `keystoneclient.apiclient.exceptions` will fail.
- gotcha When bundling applications with PyInstaller, `python-keystoneclient` (and its dependency `keystoneauth1`) may encounter `PackageNotFoundError` or issues related to `pbr` versioning.
Install
-
pip install python-keystoneclient
Imports
- v3.Password
from keystoneauth1.identity import v3
- session.Session
from keystoneauth1 import session
- client.Client
from keystoneclient.v3 import client
- keystoneclient.exceptions.ClientException
from keystoneclient import exceptions
Quickstart
import os
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client
# Environment variables for authentication (recommended practice)
AUTH_URL = os.environ.get('OS_AUTH_URL', 'http://localhost:5000/v3')
USERNAME = os.environ.get('OS_USERNAME', 'admin')
PASSWORD = os.environ.get('OS_PASSWORD', 'password')
PROJECT_NAME = os.environ.get('OS_PROJECT_NAME', 'admin')
USER_DOMAIN_ID = os.environ.get('OS_USER_DOMAIN_ID', 'default')
PROJECT_DOMAIN_ID = os.environ.get('OS_PROJECT_DOMAIN_ID', 'default')
# 1. Authenticate using a session (V3 API example)
auth = v3.Password(
auth_url=AUTH_URL,
username=USERNAME,
password=PASSWORD,
project_name=PROJECT_NAME,
user_domain_id=USER_DOMAIN_ID,
project_domain_id=PROJECT_DOMAIN_ID
)
sess = session.Session(auth=auth)
# 2. Initialize the Keystone client
keystone = client.Client(session=sess)
# 3. Perform an operation (e.g., list projects)
try:
projects = keystone.projects.list()
print(f"Successfully connected to Keystone. Found {len(projects)} projects.")
for project in projects:
print(f" - {project.name} (ID: {project.id})")
except exceptions.ClientException as e:
print(f"Error connecting to Keystone: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")