PyKMIP - Key Management Interoperability Protocol

0.10.0 · active · verified Fri Apr 17

PyKMIP (Python Key Management Interoperability Protocol) is a client library for interacting with KMIP servers, enabling operations such as creating, retrieving, deleting, and managing cryptographic keys and objects. The current version is 0.10.0, and it follows a somewhat irregular but active release cadence, typically with bug fixes and minor features between major functional updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a KMIP server using `KmipClient`, create a new symmetric key, and then destroy it. It emphasizes secure handling of sensitive information via environment variables and includes basic error handling for common connection and file issues. Ensure you have client and CA certificates (e.g., `client.pem`, `client.key`, `ca.pem`) configured for TLS.

import os
from kmip.pie.client import KmipClient
from kmip.pie import enums, objects

# Configure KMIP server details from environment variables for security
KMIP_HOST = os.environ.get("KMIP_HOST", "localhost")
KMIP_PORT = int(os.environ.get("KMIP_PORT", "5696"))
CLIENT_CERT_PATH = os.environ.get("CLIENT_CERT_PATH", "./client.pem")
CLIENT_KEY_PATH = os.environ.get("CLIENT_KEY_PATH", "./client.key")
CA_CERT_PATH = os.environ.get("CA_CERT_PATH", "./ca.pem")

try:
    # Initialize the KMIP client with TLS configuration
    with KmipClient(
        host=KMIP_HOST,
        port=KMIP_PORT,
        cert=CLIENT_CERT_PATH,
        key=CLIENT_KEY_PATH,
        ca=CA_CERT_PATH,
        ssl_version="PROTOCOL_TLSv1_2" # Explicit TLSv1.2, or let system negotiate (PROTOCOL_TLS)
    ) as client:
        client.open()
        print(f"Successfully connected to KMIP server at {KMIP_HOST}:{KMIP_PORT}")

        # Example 1: Create a new symmetric key
        print("\nCreating a 256-bit AES symmetric key...")
        create_result = client.create(
            enums.ObjectType.SYMMETRIC_KEY,
            enums.CryptographicAlgorithm.AES,
            256,
            enums.CryptographicUsageMask.ENCRYPT
        )

        if create_result.result_status == enums.ResultStatus.SUCCESS:
            key_uuid = create_result.uuid
            print(f"Key created successfully. UUID: {key_uuid}")

            # Example 2: Destroy the created key
            print(f"\nDestroying key with UUID: {key_uuid}...")
            destroy_result = client.destroy(key_uuid)

            if destroy_result.result_status == enums.ResultStatus.SUCCESS:
                print(f"Key {key_uuid} destroyed successfully.")
            else:
                print(f"Failed to destroy key: {destroy_result.result_reason.name}")
        else:
            print(f"Failed to create key: {create_result.result_reason.name} ({create_result.result_status.name})")

except ConnectionRefusedError:
    print(f"Error: Connection refused. Is the KMIP server running on {KMIP_HOST}:{KMIP_PORT}?")
except FileNotFoundError as e:
    print(f"Error: Certificate or key file not found: {e}. Check paths: {CLIENT_CERT_PATH}, {CLIENT_KEY_PATH}, {CA_CERT_PATH}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    # The 'with' statement handles client closing automatically
    print("\nKMIP client operations completed.")

view raw JSON →