Kerberos 5 Test Environment (k5test)

0.10.4 · active · verified Fri Apr 17

k5test is a Python library designed for testing applications that interact with Kerberos 5. It allows for the creation of self-contained, temporary Kerberos environments, including a KDC (Key Distribution Center) and Kadmin server, simplifying integration and unit testing. The current version is 0.10.4, and it maintains an active release cadence with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a temporary Kerberos 5 environment using `k5test.K5Context`. It initializes a KDC, provides a credential cache and keytab, and configures `krb5.conf`. The example then uses `subprocess` to run `kinit` to authenticate 'testuser' and `kadmin.local` to list principals, showcasing how to interact with the managed Kerberos services within your tests. Note that system Kerberos binaries must be installed.

import subprocess
from k5test import K5Context
import os

def run_k5test_example():
    # K5Context creates a temporary Kerberos environment.
    # It provides paths to a credential cache (ccache) and keytab,
    # and sets up a krb5.conf to point to its KDC.
    try:
        with K5Context() as c:
            print(f"Kerberos context created. KDC Port: {c.kdc_port}")
            print(f"Credential Cache: {c.ccache}")
            print(f"Keytab: {c.keytab}")

            # Example: Authenticate as 'testuser' using the generated ccache
            # kinit requires the KRB5CCNAME environment variable to be set.
            kinit_env = os.environ.copy()
            kinit_env['KRB5CCNAME'] = c.ccache
            # k5test often manages KRB5_CONFIG and other Kerberos env vars internally
            # within the context manager, or provides an 'env' dict for subprocesses.
            # For kinit, we usually only need to override KRB5CCNAME.

            print("\nRunning kinit...")
            subprocess.run(['kinit', 'testuser'], env=kinit_env, check=True, capture_output=True)
            print("kinit successful for testuser.")

            # Example: List principals using kadmin.local
            # kadmin.local might need more environment variables set by K5Context
            # to locate its configuration and daemons. Use c.env.
            print("\nListing principals via kadmin.local...")
            kadmin_output = subprocess.check_output(['kadmin.local', '-q', 'list_principals'], env=c.env, text=True)
            print(kadmin_output)

            # Your test code that uses Kerberos can go here
            print("\nKerberos environment is ready for testing.")

    except FileNotFoundError as e:
        print(f"Error: Kerberos binary not found. Please ensure krb5-kdc and krb5-admin-server are installed. {e}")
    except subprocess.CalledProcessError as e:
        print(f"Error during Kerberos command execution: {e}")
        print(f"Stderr: {e.stderr.decode()}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    run_k5test_example()

view raw JSON →