Keystone Middleware

12.0.0 · active · verified Thu Apr 16

Keystonemiddleware provides WSGI middleware components for OpenStack Identity (Keystone) integration. It enables services to authenticate requests against Keystone, handle tokens, and authorize access based on user roles and projects. The current stable version is 12.0.0, and it follows the OpenStack release cycle, typically releasing new versions with each OpenStack cycle, leading to several major releases per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to wrap a simple WSGI application with the `AuthToken` middleware. It uses environment variables for configuration to make it runnable without hardcoding credentials. For `AuthToken` to function, a running Keystone instance and optionally Memcached (for token caching) are required. The example shows how to access user and project IDs populated by the middleware from the WSGI `environ`.

import os
from wsgiref.simple_server import make_server
from keystonemiddleware.auth_token import AuthToken

def simple_app(environ, start_response):
    """Simplest possible WSGI application"""
    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]
    start_response(status, headers)
    # AuthToken populates these if a valid token is provided
    user_id = environ.get('HTTP_X_USER_ID', 'Unknown User ID')
    project_id = environ.get('HTTP_X_PROJECT_ID', 'Unknown Project ID')
    return [f"Hello, user '{user_id}' from project '{project_id}' via keystonemiddleware!\n".encode('utf-8')]

# Configuration for AuthToken (simplified, typically from paste.ini or configuration files)
# IMPORTANT: Replace with your actual Keystone endpoint and user/project details.
# For production, avoid 'insecure=True' and set 'memcached_servers'.
auth_config = {
    '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', 'secret'),
    'project_name': os.environ.get('OS_PROJECT_NAME', 'admin'),
    'user_domain_name': os.environ.get('OS_USER_DOMAIN_NAME', 'Default'),
    'project_domain_name': os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default'),
    'memcached_servers': os.environ.get('MEMCACHED_SERVERS', '127.0.0.1:11211'), # Required for caching
    'insecure': 'True' if os.environ.get('OS_INSECURE') else 'False', # Use only for testing/development
    'delay_auth_decision': 'True' # Allows app to handle unauthenticated requests if needed
}

# Wrap the application with AuthToken middleware
application = AuthToken(simple_app, auth_config)

if __name__ == '__main__':
    httpd = make_server('', 8000, application)
    print("Serving on port 8000...")
    print("Access with a valid X-Auth-Token header to see user info:")
    print("  curl -H \"X-Auth-Token: <your-keystone-token>\" http://localhost:8000/")
    print("Or with no token (if 'delay_auth_decision' is True):")
    print("  curl http://localhost:8000/")
    print("Ensure memcached is running if configured, e.g., 'sudo apt install memcached' and 'systemctl start memcached'.")
    print("Configure environment variables like OS_AUTH_URL, OS_USERNAME, OS_PASSWORD, etc. for actual Keystone integration.")
    httpd.serve_forever()

view raw JSON →