# bhatti/plexauthz architecture

> Architecture diagram of the GitHub repository bhatti/plexauthz, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bhatti/plexauthz
- Repository: https://github.com/bhatti/plexauthz

## Overview

##### High-Level Architecture Components

a) Client Layer:
- gRPC Clients
- REST API Clients
- Direct Database Clients

b) API Layer:
- gRPC Services (shown in api/v1/services/)
- REST Controllers
- Protocol Buffer Definitions

c) Core Services:
- Authorization Service
- Principal Service
- Resource Service
- Permission Service
- Relationship Service
- Service
- Role Service
- Organization Service

d) Domain Layer:
- Authorization Logic
- Policy Evaluation
- Template Processing
- Dynamic Condition Evaluation

e) Data Layer:
- Redis Store
- DynamoDB Store
- Repository Implementations

##### Component Relationships and Data Flow

a) External Interactions:
- Client requests → API Layer
- API Layer → Core Services
- Core Services → Domain Layer
- Domain Layer → Data Layer

b) Internal Flows:
- Authorization flow paths
- Data persistence paths
- Policy evaluation paths

##### Detailed Component Breakdown

a) Authorization Flow:
**Client → AuthZService → Authorizer → PolicyEvaluator → RepositoryLayer**

b) Data Model Relationships:
**Organization → Principal → Resources**
- ↓           ↓          ↓
- Groups     Permissions  Instances
- ↓           ↓
- Roles    Relationships

##### Implementation Details

Technical aspects:

a) Service Implementations:
- GRPC Services (internal/server/)
- HTTP Services (internal/service/http/)
- Database Services (internal/service/db/)

b) Repository Layer:
- Base Repository
- Specific Entity Repositories
- Data Store Implementations

##### Cross-Cutting Concerns

Aspects:

- Metrics Collection
- Logging
- Error Handling
- Configuration Management

##### Deployment View

How the system can be deployed:

- Containerized deployment (Dockerfile present)
- Service boundaries
- Database instances
- Load balancers

##### Integration points

Remember to maintain a balance between detail and clarity, focusing on the architectural decisions that make PlexAuthZ unique, such as its hybrid approach to authorization and its flexible policy evaluation system.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph Clients
        GC["gRPC Clients"]:::client
        RC["REST Clients"]:::client
        DBC["DB Clients"]:::client
    end

    %% API Layer
    subgraph API
        GRPCS["gRPC Services"]:::api
        RESTC["REST Controllers"]:::api
        PROTO["Protocol Buffers"]:::api
    end
    click GRPCS "https://github.com/bhatti/PlexAuthZ/tree/main/api/v1/services/"
    click PROTO "https://github.com/bhatti/PlexAuthZ/tree/main/api/v1/types/"

    %% Core Services Layer
    subgraph CoreServices
        AS["Authorization Service"]:::core
        PS["Principal Service"]:::core
        RS["Resource Service"]:::core
        PMS["Permission Service"]:::core
        RLS["Relationship Service"]:::core
        GS["Group Service"]:::core
        ROS["Role Service"]:::core
        OS["Organization Service"]:::core
    end
    click AS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/auth_service.go"
    click PS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/principal_service.go"
    click RS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/resource_service.go"
    click PMS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/permission_service.go"
    click RLS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/relationship_service.go"
    click GS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/group_service.go"
    click ROS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/role_service.go"
    click OS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/service/organization_service.go"

    %% Domain Layer
    subgraph Domain
        AL["Authorization Logic"]:::domain
        PE["Policy Evaluation"]:::domain
        TP["Template Processing"]:::domain
        DCE["Dynamic Condition Evaluation"]:::domain
    end
    click AL "https://github.com/bhatti/PlexAuthZ/tree/main/internal/authz/"
    click TP "https://github.com/bhatti/PlexAuthZ/blob/main/internal/domain/template_helper.go"

    %% Data Layer
    subgraph DataLayer
        RDS[("Redis Store")]:::data
        DDB[("DynamoDB")]:::data
        REPO["Repositories"]:::data
    end
    click RDS "https://github.com/bhatti/PlexAuthZ/blob/main/internal/repository/redis/redis_store.go"
    click DDB "https://github.com/bhatti/PlexAuthZ/blob/main/internal/repository/ddb/ddb_store.go"
    click REPO "https://github.com/bhatti/PlexAuthZ/tree/main/internal/repository/"

    %% Cross-cutting Concerns
    subgraph CrossCutting
        MC["Metrics"]:::cross
        LOG["Logging"]:::cross
        CFG["Configuration"]:::cross
        ERR["Error Handling"]:::cross
    end
    click MC "https://github.com/bhatti/PlexAuthZ/tree/main/internal/metrics/"
    click CFG "https://github.com/bhatti/PlexAuthZ/blob/main/internal/domain/config.go"
    click ERR "https://github.com/bhatti/PlexAuthZ/blob/main/internal/domain/errors.go"

    %% Relationships
    GC --> GRPCS
    RC --> RESTC
    DBC --> REPO
    
    GRPCS --> AS
    RESTC --> AS
    
    AS --> AL
    AL --> PE
    PE --> TP
    PE --> DCE
    
    AL --> REPO
    REPO --> RDS
    REPO --> DDB
    
    AS --> PS
    AS --> RS
    AS --> PMS
    AS --> RLS
    AS --> GS
    AS --> ROS
    AS --> OS

    %% Styles
    classDef client fill:#gray,stroke:#333,stroke-width:2px
    classDef api fill:#3498db,stroke:#333,stroke-width:2px
    classDef core fill:#2ecc71,stroke:#333,stroke-width:2px
    classDef domain fill:#f1c40f,stroke:#333,stroke-width:2px
    classDef data fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef cross fill:#9b59b6,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        C1["Client Component"]:::client
        A1["API Component"]:::api
        S1["Core Service"]:::core
        D1["Domain Component"]:::domain
        DA1["Data Store"]:::data
        CC1["Cross-cutting"]:::cross
    end
```
