# keylime/rust-keylime architecture

> Architecture diagram of the GitHub repository keylime/rust-keylime, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/keylime/rust-keylime
- Repository: https://github.com/keylime/rust-keylime

## Overview

##### Main Components
- Keylime Agent HTTP Server: This is the centerpiece (the entry point is in keylime-agent/src/main.rs), which handles HTTP requests and orchestrates keylime operations.
- TPM/TPM Emulator Module: Represent the TPM interface used by the agent. This module (spread across files like keys_handler.rs, quotes_handler.rs, and secure_mount.rs) interacts with the physical TPM or a TPM emulator.
- Key Management & Cryptographic Functions: Highlight modules in the keylime directory (e.g., algorithms.rs, crypto/) that deal with cryptographic computations, x509 handling and certificate management.
- IMA Emulator: There is a separate component (keylime-ima-emulator) simulating IMA (Integrity Measurement Architecture) functionalities, which may interface with the agent.
- Configuration & Logging: Modules such as config.rs and integration with logging (with ‘pretty-env-logger’ per documentation) that manage service configuration and log tracing.
- Packaging and Deployment: Include build and deployment scripts (GNUmakefile, debian/, rpm/, docker/, systemd unit files in dist/systemd) that show how the agent is packaged as a systemd-managed service and distributed via various package managers.
- Testing and CI: Note the tests and GitHub workflows which show the continuous integration process, such as build scripts and security scanning (Cargo.lock, rustfmt.toml, codecov.yml, etc.).

##### Relationships and Interactions
- The cryptographic and key management library (located in the keylime module) can be shown as a dependency, interacting with both the HTTP server and the TPM interface.
- The IMA emulator acts as a supplementary module that might provide measurement data, thereby linking to the agent’s decision-making process.
- Configuration modules feed settings into the HTTP server and TPM handlers.
- The testing/CI modules (GitHub workflows, cargo test scripts) are out-of-band but should be noted as part of the development environment rather than the runtime architecture.

##### Architectural Patterns & Design Principles
- Modular Design: The project is organized into well-delineated modules (e.g., agent, IMA emulator, keycrypto) which interact via clearly defined interfaces.
- Separation of Concerns: Splitting components such as the HTTP server, TPM interface, and IMA functionality supports clarity and maintainability.
- Deployment Abstraction: Packaging directories, systemd unit files, and Docker configurations indicate that the project emphasizes ease of deployment and integration with host system environments.
- Logging & Error Handling: Key modules (error.rs, errors_handler.rs) suggest a robust approach to error management across the service.

##### Relevant Technologies and Frameworks
- Rust and Cargo (Cargo.toml, Cargo.lock) as the building system.
- TPM libraries and cryptographic toolkits (e.g., openssl, tpm2-tss) as integral for providing trust.
- Linux system integration via systemd and packaging scripts (debian/, rpm/).
- Docker for containerized deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runtime Components
    subgraph "Runtime Components"
        subgraph "Agent Core"
            A["Keylime Agent (HTTP Server)"]:::service
            click A "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/main.rs"
        end

        subgraph "TPM/TPM Emulator"
            B1["keys_handler.rs"]:::module
            click B1 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/keys_handler.rs"
            B2["quotes_handler.rs"]:::module
            click B2 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/quotes_handler.rs"
            B3["secure_mount.rs"]:::module
            click B3 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/secure_mount.rs"
        end

        subgraph "Crypto & Key Management"
            C1["algorithms.rs"]:::crypto
            click C1 "https://github.com/keylime/rust-keylime/blob/master/keylime/src/algorithms.rs"
            C2["crypto/ Directory"]:::crypto
            click C2 "https://github.com/keylime/rust-keylime/tree/master/keylime/src/crypto/"
            C3["x509.rs"]:::crypto
            click C3 "https://github.com/keylime/rust-keylime/blob/master/keylime/src/crypto/x509.rs"
        end

        subgraph "IMA Emulator"
            D["IMA Emulator"]:::module
            click D "https://github.com/keylime/rust-keylime/blob/master/keylime-ima-emulator/src/main.rs"
        end

        subgraph "Configuration & Logging"
            E1["config.rs"]:::config
            click E1 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/config.rs"
            E2["error.rs"]:::config
            click E2 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/error.rs"
            E3["errors_handler.rs"]:::config
            click E3 "https://github.com/keylime/rust-keylime/blob/master/keylime-agent/src/errors_handler.rs"
        end
    end

    %% Deployment & Packaging Components
    subgraph "Deployment & Packaging"
        F1["debian scripts"]:::deployment
        click F1 "https://github.com/keylime/rust-keylime/tree/master/debian/"
        F2["rpm scripts"]:::deployment
        click F2 "https://github.com/keylime/rust-keylime/blob/master/rpm/centos/keylime-agent-rust.spec"
        F3["systemd files"]:::deployment
        click F3 "https://github.com/keylime/rust-keylime/blob/master/dist/systemd/system/keylime_agent.service"
        F4["Docker Configs"]:::deployment
        click F4 "https://github.com/keylime/rust-keylime/tree/master/docker/"
        F5["docker-compose"]:::deployment
        click F5 "https://github.com/keylime/rust-keylime/blob/master/docker-compose.yml"
    end

    %% Testing and CI Components
    subgraph "Testing & CI"
        G1["CI Workflows"]:::testing
        click G1 "https://github.com/keylime/rust-keylime/tree/master/.github/workflows/"
        G2["Agent Tests"]:::testing
        click G2 "https://github.com/keylime/rust-keylime/tree/master/keylime-agent/tests/"
        G3["Root Tests"]:::testing
        click G3 "https://github.com/keylime/rust-keylime/tree/master/tests/"
    end

    %% Relationships
    A -->|"TPM_commands"| B1
    A -->|"TPM_commands"| B2
    A -->|"TPM_commands"| B3

    A -->|"Crypto_ops"| C1
    A -->|"Crypto_ops"| C2
    A -->|"Crypto_ops"| C3

    D -->|"IMA_data"| A

    E1 -->|"configures"| A
    E2 -->|"error_handling"| A
    E3 -->|"error_handling"| A

    F3 -->|"hosts"| A
    F4 -->|"hosts"| A
    F5 -->|"orchestrates"| A

    G1 -->|"builds"| A
    G2 -->|"tests"| A
    G3 -->|"tests"| A

    %% Styles
    classDef service fill:#f9c,stroke:#333,stroke-width:2px
    classDef module fill:#bbf,stroke:#333,stroke-width:2px
    classDef crypto fill:#9f9,stroke:#333,stroke-width:2px
    classDef config fill:#ff9,stroke:#333,stroke-width:2px
    classDef deployment fill:#fc9,stroke:#333,stroke-width:2px
    classDef testing fill:#cfc,stroke:#333,stroke-width:2px
```
