# bmf-san/poc-opa-access-control-system architecture

> Architecture diagram of the GitHub repository bmf-san/poc-opa-access-control-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bmf-san/poc-opa-access-control-system
- Repository: https://github.com/bmf-san/poc-opa-access-control-system

## Overview

##### Main Components
- External Clients: Representing users or services sending requests.
- Policy Enforcement Point (PEP): Acts as a reverse proxy (see cmd/pep and its corresponding Dockerfile) that intercepts requests from external clients.
- Policy Decision Point (PDP): This is the component that evaluates OPA policies (found in cmd/pdp along with Rego policy files such as main.rego, rbac.rego). It decides whether a request is permitted.
- Application Service (Employee): The business application (cmd/employee) that actually processes requests once authorization is granted.
- Policy Information Point (PIP): Although briefly mentioned through cmd/pip, it can be treated as an additional service for providing or integrating supplemental policy information.
- Databases: There are two primary database components—one for employee data (docs/db/employee, postgres/employee) and one for PRP data (docs/db/prp, postgres/prp)—storing resources like roles, permissions, and user details.

##### Out Relationships & Workflow
- The client initiates a request (HTTP) that first reaches the PEP service.
- The PEP intercepts the request, performs initial validation (e.g., checking for the “X-User-ID” header), and then consults the PDP for an access decision by sending data for policy evaluation.
- The PDP uses the Rego policies to compute whether the request should be allowed.
- If permitted, the request is forwarded from the PEP to the Employee service. Otherwise, an appropriate error (403 or 400) is returned.
- Optionally, PIP may be involved if additional policy data or context is required.
- Both the Employee and PRP databases are referenced: the Employee database supports the business service, while the PRP database likely holds configuration for roles, permissions, and resource access mappings.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Business logic is separated from authorization logic.
- Proxy-based Enforcement: The use of a dedicated PEP that intercepts and enforces policies before reaching the main application.
- Microservices: Each service (employee, pdp, pep, pip) is built, tested, and deployed independently, using Docker containers and managed via Docker Compose.
- Centralized Policy Management: All policy-related concerns are handled by PDP and defined using Rego, aiding versioning and centralized rule management.

##### Final Recommendations
- Client ➔ PEP (validates headers and routes requests)
- PEP ➔ PDP (for policy decision)
- PDP ➔ PEP (returns decision)
- PEP ➔ Employee Service (for valid requests)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External entity
    client["External Client"]:::external

    %% Access Control Services layer
    subgraph "Access Control Services"
        pep["PEP (Reverse Proxy)"]:::service
        pdp["PDP (Policy Evaluator)"]:::service
        pip["PIP (Policy Information Point)"]:::service
        employee["Employee Service"]:::service
    end

    %% PDP Internal Component
    pdp -->|"contains"| pdp_policy["Rego Policies"]:::service

    %% Databases layer
    subgraph "Databases"
        emp_db["Employee Database"]:::database
        prp_db["PRP Database"]:::database
    end

    %% Relationships / Data Flow
    client -->|"HTTP_request"| pep
    pep -->|"policy_query"| pdp
    pdp -->|"decision_response"| pep
    pep -->|"forward_valid_request"| employee
    pep -->|"fetch_policy_info"| pip
    pip -->|"policy_data"| prp_db
    pdp -->|"config_read"| prp_db
    employee -->|"db_query"| emp_db

    %% Click Events for Components
    click pep "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/cmd/pep"
    click pep "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/build/pep/Dockerfile"
    click pdp "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/cmd/pdp"
    click pdp "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/build/pdp/Dockerfile"
    click pdp_policy "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/cmd/pdp/policy"
    click employee "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/cmd/employee"
    click employee "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/build/employee/Dockerfile"
    click pip "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/cmd/pip"
    click pip "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/build/pip/Dockerfile"
    click emp_db "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/docs/db/employee"
    click emp_db "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/postgres/employee"
    click prp_db "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/docs/db/prp"
    click prp_db "https://github.com/bmf-san/poc-opa-access-control-system/tree/main/postgres/prp"

    %% Styles
    classDef external fill:#e0f7fa,stroke:#006064,stroke-width:2px;
    classDef service fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef database fill:#ffe082,stroke:#ff8f00,stroke-width:2px;
```
