# googlecloudplatform/docker-clamav-malware-scanner architecture

> Architecture diagram of the GitHub repository googlecloudplatform/docker-clamav-malware-scanner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/googlecloudplatform/docker-clamav-malware-scanner
- Repository: https://github.com/googlecloudplatform/docker-clamav-malware-scanner

## Overview

##### Main Components
- Cloud Storage Buckets – Represent the various buckets that hold files pending scan (“unscanned”), clean files, and quarantined files. There is also a bucket used for ClamAV content distribution (CVD mirror).
- Cloud Run Service – This containerized service (defined by files like server.ts, scanner.ts, and gcs-proxy-server.ts along with Dockerfile and bootstrap.sh) implements the central scanning logic using ClamAV.
- Eventarc – Illustrate how events (such as file uploads) trigger the service by sending notifications or HTTP events to Cloud Run.
- Infrastructure as Code – Include a segment for Terraform (with directories terraform/infra and terraform/service) that provisions infrastructure resources such as APIs, buckets, and Cloud Run services.
- CI/CD & Code Quality Tools – Optionally show the GitHub Actions workflows (in .github/workflows) that manage code health, release processes, and security scanning.
- Logging & Metrics – Specify that the service uses logging (logger.ts) and metrics (metrics.ts) components, with a specific note of the migration to OpenTelemetry for monitoring.

##### Relationships and Interactions
- Data Flow: An incoming file upload to the “unscanned” bucket triggers an event (via Eventarc) that invokes the Cloud Run service.
- Processing Flow: Once triggered, the service (via server.ts and scanner.ts) retrieves the file, runs a scan using ClamAV (the ClamAV configuration being modulated by config settings), and then routes the file to either the “clean” bucket or “quarantined” bucket depending on scan results.
- Environment & Deployment Control: The configuration management process (via environment variables and config files) ensures the service is flexible. The Terraform scripts provision and manage the cloud resources, tying the infrastructure into the overall architecture.
- Infrastructure CI/CD: The testing (located in the spec folder) and code quality checks (through GitHub Actions and linting/prettier configurations) ensure robust deployments.

##### Architectural Patterns and Design Principles
- Event-Driven Architecture: The system relies on events (from GCS via Eventarc) to trigger the Cloud Run service.
- Separation of Concerns: The scanner logic, server handling, and proxy components are modularized—each playing a distinct role.
- Infrastructure as Code: Terraform is used for provisioning, making the system portable and reproducible.
- Containerization: Docker is used to create a consistent runtime environment for the Cloud Run service.
- Configuration Flexibility: Use of environmental variables (and techniques like shell variable expansion) allows a single container image to be re-used across different deployments.

##### Relevant Technologies, Frameworks, and Libraries
- Google Cloud Technologies: Cloud Run, Eventarc, Cloud Storage (GCS).
- Containerization & CI/CD: Docker, GitHub Actions (workflows in .github), Cloud Build.
- Infrastructure Provisioning: Terraform scripts deploy and manage the necessary cloud resources.
- Static Analysis & Testing: ESLint (eslint.config.mjs), Prettier configurations, and specification tests (in the spec/ directory).
- Monitoring & Telemetry: OpenTelemetry and custom metrics (via metrics.ts).

##### Specific Elements
- “GCS Buckets” for unscanned, clean, quarantined, and ClamAV mirror.
- “Cloud Run Malware Scanner” with subcomponents like the scanner, server, and GCS proxy.
- “Eventarc” indicating the event trigger flow.
- “Terraform Provisioning” that shows infrastructure deployment.
- The trigger event from GCS to Eventarc.
- The flow from Eventarc to Cloud Run.
- The output path from Cloud Run back to specific GCS buckets.
- The deployment/management interactions flowing from Terraform.
- Optionally differentiate components by type:
- Annotations describing key functionality like “File Scan”, “Config Injection”, and “Metric Reporting” for clarity.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Google Cloud Services
    subgraph "Google Cloud Services"
        A["GCS Bucket: Unscanned"]:::cloud
        B["GCS Bucket: Clean"]:::cloud
        C["GCS Bucket: Quarantined"]:::cloud
        D["GCS Bucket: ClamAV Mirror"]:::cloud
        E["Eventarc"]:::cloud
    end

    %% Cloud Run Service
    F["Cloud Run Malware Scanner Service"]:::backend

    %% Configuration & Environment Management
    G["Configuration & Environment Injection"]:::config
    subgraph "Configuration Files"
        K1["config.json.tmpl"]:::config
        K2["config.ts"]:::config
        K3["config-env.yaml"]:::config
    end

    %% Logging & Metrics
    H["Logging & Metrics (Runtime)"]:::metrics
    subgraph "Logging & Monitoring Files"
        L1["logger.ts"]:::metrics
        L2["metrics.ts"]:::metrics
    end

    %% Terraform Provisioning
    subgraph "Terraform Provisioning"
        I1["Terraform Infra"]:::infra
        I2["Terraform Service"]:::infra
    end

    %% CI/CD & Code Quality Tools
    subgraph "CI/CD & Code Quality"
        J1["GitHub Actions Workflows"]:::ci
        J2["Linting (ESLint)"]:::ci
        J3["Prettier Config"]:::ci
    end

    %% Data Flows
    A -->|"upload_event"| E
    E -->|"trigger_event"| F
    F -->|"classifies_as_clean"| B
    F -->|"classifies_as_quarantine"| C
    D ---|"provides ClamAV content"| F

    %% Configuration Injection
    F -->|"injects_config"| G
    G --> K1
    G --> K2
    G --> K3

    %% Logging and Metrics
    F -->|"emits_logs/metrics"| H
    H --> L1
    H --> L2

    %% Infrastructure Provisioning Relationships
    I1 ---|"provisions"| A
    I1 ---|"provisions"| E
    I1 ---|"provisions"| F
    I2 ---|"configures"| A
    I2 ---|"configures"| E
    I2 ---|"configures"| F

    %% CI/CD Relationships
    J1 ---|"deploys"| F
    J1 ---|"deploys_infra"| I1
    J2 ---|"ensures_code_quality"| F
    J3 ---|"formats_code"| F

    %% Styling Classes
    classDef cloud fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef backend fill:#FFD700,stroke:#333,stroke-width:1px;
    classDef config fill:#E6E6FA,stroke:#333,stroke-width:1px;
    classDef metrics fill:#FFB6C1,stroke:#333,stroke-width:1px;
    classDef infra fill:#98FB98,stroke:#333,stroke-width:1px;
    classDef ci fill:#FCF6BD,stroke:#333,stroke-width:1px;

    %% Click Events
    click F "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/tree/main/cloudrun-malware-scanner/"
    click I1 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/tree/main/terraform/infra"
    click I2 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/tree/main/terraform/service"
    click J1 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/tree/main/.github/workflows/"
    click J2 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/eslint.config.mjs"
    click J3 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/.prettierrc.js"
    click K1 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/config.json.tmpl"
    click K2 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/config.ts"
    click K3 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/config-env.yaml"
    click L1 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/logger.ts"
    click L2 "https://github.com/googlecloudplatform/docker-clamav-malware-scanner/blob/main/cloudrun-malware-scanner/metrics.ts"
```
