# cossacklabs/acra architecture

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

- Interactive diagram: https://gitdiagram.com/cossacklabs/acra
- Repository: https://github.com/cossacklabs/acra

## Overview

##### Project Type and Purpose
- Acra is a distributed, microservice-based database security suite written in Go. It provides application-level field encryption, masking, tokenization, SQL firewall, and intrusion detection for relational and NoSQL datastores.
- Key use cases: transparent SQL proxy (AcraServer), encryption-as-a-service API (AcraTranslator), optional transport layer (AcraConnector), client-side SDKs (AcraWriter/AcraReader), and key management utilities.

##### File Structure Insights
- cmd/… each “cmd/acra-*” directory is a standalone Go service or CLI (acra-server, acra-translator, acra-keys, acra-rotate, etc.).
- encryptor/ and decryptor/ subdirectories: core encryption/decryption logic for MySQL and PostgreSQL protocols.
- keystore/: pluggable storage backends (filesystem, Redis, Vault, AWS KMS).
- network/: wrappers for TLS, Prometheus metrics, GRPC/HTTP servers.
- wrappers/ and examples/: client-side SDKs and sample integrations in various languages.
- configs/ and docker/: deployment manifests, Dockerfiles, and compose files.
- tests/: end‐to‐end integration scenarios with real databases, Consul/Vault, Redis, SSL stacks.

a. Client Applications (Web, Mobile, CLI)
- May embed AcraWriter/AcraReader SDKs for client-side encryption/decryption.
- Or speak plain SQL/HTTP/gRPC to AcraServer/AcraTranslator.
b. AcraConnector (optional)
- Authenticates and terminates TLS for client-to-Acra traffic.
c. AcraServer (SQL Proxy)
- Listens on SQL port, parses queries, applies encryption/masking/tokenization via decryptor/ encryptor modules, enforces SQL firewall, logs events.
- Pulls keys from Key Storage and Master Key Storage when needed.
- Forwards modified queries to the target database (MySQL/PostgreSQL).
d. AcraTranslator (HTTP/gRPC API)
- Exposes REST/gRPC endpoints for encrypt/decrypt/tokenize/detokenize.
- Uses the same core encryptor/decryptor libraries.
e. Datastores
- Primary: SQL databases (MySQL, PostgreSQL), NoSQL, S3, etc.
- Key Storage: Redis or a table in the database, stores encrypted data keys and configuration.
- Master Key Storage: AWS KMS, Google KMS, HashiCorp Vault, HSM (external service).
f. Supporting Services
- Prometheus: metrics from network/, common/prometheus.go.
- Jaeger: tracing via network/tracing.go.
- Logging: structured audit and integrity logs (logging/).
- CI/CD: CircleCI scripts in .circleci/, Docker build scripts.
- Key Management Utilities: cmd/acra-keymaker, cmd/acra-rotate, cmd/acra-backup, cmd/acra-rollback.

##### Component Interactions and Data Flows
- Client → (TLS) → AcraConnector → (plain SQL/TLS-wrapped) → AcraServer → Database → AcraServer → Client
- Client → (HTTPS/gRPC) → AcraTranslator → Key Storage + Master Key Storage → AcraTranslator → Client → Database
- CLI utilities interact with Master Key Storage and Key Storage to provision and rotate encryption keys.
- Throughout, network/ modules handle TLS, connection pools, and metrics.
- Prometheus scrapes endpoints on AcraServer, AcraTranslator, and network wrappers; logs flow to ELK or output files.

##### Architectural Patterns and Principles
- Microservices: each “cmd/acra-*” is a separate process with a single responsibility.
- Proxy Pattern: AcraServer intercepts and transforms SQL traffic transparently.
- Adapter/Plugin: encryptor/ and decryptor/ have protocol-specific adapters for MySQL/PostgreSQL.
- Separation of Concerns: key storage, crypto logic, network I/O, and business logic are in distinct packages.
- Configuration-Driven: configs/ and acra-*.yaml define encryption rules, firewall policies, and storage backends.
- Defense in Depth: layered encryption, SQL firewall, intrusion detection via poison records.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        ClientApps["Client Applications\n(Web, Mobile, CLI)"]:::client
        AcraWriter["AcraWriter SDK"]:::client
        Wrappers["Client-side SDK Wrappers"]:::client
    end

    %% Transport Layer
    subgraph "Transport Layer"
        direction TB
        AcraConnector["AcraConnector"]:::core
    end

    %% Core Services
    subgraph "Core Services"
        direction TB
        AcraServer["acra-server"]:::core
        AcraTranslator["acra-translator"]:::core
    end

    %% Utilities
    subgraph "Utilities & CI/CD"
        direction TB
        AcraKeymaker["acra-keymaker"]:::utils
        AcraKeys["acra-keys"]:::utils
        AcraRotate["acra-rotate"]:::utils
        AcraBackup["acra-backup"]:::utils
        AcraRollback["acra-rollback"]:::utils
        CICD["CI/CD Pipeline\n(.circleci, docker)"]:::utils
        Configs["Deployment Configs"]:::utils
    end

    %% Data & Storage
    subgraph "Data & Key Stores"
        direction TB
        TargetDB["Target Database\n(MySQL/PostgreSQL, NoSQL)"]:::storage
        RedisKeystore["Redis Key Storage"]:::storage
        PluggableKeystore["Pluggable Keystore"]:::storage
        KeystoreV2["Keystore v2 API"]:::storage
        MasterKMS["External KMS/Vault/HSM"]:::storage
    end

    %% Monitoring & Logging
    subgraph "Monitoring & Logging"
        direction TB
        Prometheus["Prometheus"]:::monitor
        Jaeger["Jaeger"]:::monitor
        LoggingService["Structured Logging\n(logging/)"]:::monitor
    end

    %% Core Modules
    Encryptor["encryptor/"]:::core
    Decryptor["decryptor/"]:::core
    SQLParser["sqlparser/"]:::core
    Network["network/"]:::core

    %% Flows
    ClientApps -->|"TLS"| AcraConnector
    AcraWriter --> AcraConnector
    Wrappers --> AcraConnector

    AcraConnector -->|"SQL Traffic"| AcraServer
    AcraServer -->|"Queries"| SQLParser
    SQLParser --> Encryptor
    SQLParser --> Decryptor
    AcraServer --> Network
    AcraServer --> RedisKeystore
    AcraServer --> PluggableKeystore
    AcraServer --> TargetDB
    TargetDB --> AcraServer
    AcraServer --> ClientApps

    ClientApps -->|"HTTPS/gRPC"| AcraTranslator
    AcraTranslator --> Encryptor
    AcraTranslator --> Decryptor
    AcraTranslator --> RedisKeystore
    AcraTranslator --> MasterKMS
    AcraTranslator --> TargetDB
    TargetDB --> AcraTranslator
    AcraTranslator --> ClientApps

    AcraKeymaker --> RedisKeystore
    AcraKeys --> RedisKeystore
    AcraRotate --> RedisKeystore
    AcraBackup --> RedisKeystore
    AcraRollback --> RedisKeystore

    %% Monitoring & Logging Flows
    AcraServer --> Prometheus
    AcraTranslator --> Prometheus
    Network --> Prometheus
    AcraServer --> Jaeger
    AcraTranslator --> Jaeger
    AcraServer --> LoggingService
    AcraTranslator --> LoggingService

    %% Click Events
    click AcraServer "https://github.com/cossacklabs/acra/blob/master/cmd/acra-server/acra-server.go"
    click AcraTranslator "https://github.com/cossacklabs/acra/blob/master/cmd/acra-translator/acra-translator.go"
    click AcraKeymaker "https://github.com/cossacklabs/acra/blob/master/cmd/acra-keymaker/acra-keymaker.go"
    click AcraKeys "https://github.com/cossacklabs/acra/blob/master/cmd/acra-keys/acra-keys.go"
    click AcraRotate "https://github.com/cossacklabs/acra/blob/master/cmd/acra-rotate/acra-rotate.go"
    click AcraBackup "https://github.com/cossacklabs/acra/blob/master/cmd/acra-backup/acra-backup.go"
    click AcraRollback "https://github.com/cossacklabs/acra/blob/master/cmd/acra-rollback/acra-rollback.go"
    click AcraWriter "https://github.com/cossacklabs/acra/blob/master/acra-writer/acra-writer.go"
    click Wrappers "https://github.com/cossacklabs/acra/tree/master/wrappers/"
    click Encryptor "https://github.com/cossacklabs/acra/tree/master/encryptor/"
    click Decryptor "https://github.com/cossacklabs/acra/tree/master/decryptor/"
    click RedisKeystore "https://github.com/cossacklabs/acra/tree/master/keystore/"
    click KeystoreV2 "https://github.com/cossacklabs/acra/tree/master/keystore/v2/keystore"
    click Network "https://github.com/cossacklabs/acra/tree/master/network/"
    click LoggingService "https://github.com/cossacklabs/acra/tree/master/logging/"
    click SQLParser "https://github.com/cossacklabs/acra/tree/master/sqlparser/"
    click CICD "https://github.com/cossacklabs/acra/tree/master/.circleci/"
    click Configs "https://github.com/cossacklabs/acra/tree/master/configs/"
    click Tests "https://github.com/cossacklabs/acra/tree/master/tests/"

    %% Styles
    classDef client fill:#D0E8FF,stroke:#0B75C9,stroke-width:1px
    classDef core fill:#E0F8E0,stroke:#2E8B57,stroke-width:1px
    classDef storage fill:#FFE8C0,stroke:#FF8C00,stroke-width:1px
    classDef monitor fill:#F0E0FF,stroke:#7B1FA2,stroke-width:1px
    classDef utils fill:#E8E8E8,stroke:#6E6E6E,stroke-width:1px
```
