# yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker architecture

> Architecture diagram of the GitHub repository yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker
- Repository: https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker

## Overview

##### Project type and purpose
- This is a Dockerized, demo full-stack API solution showcasing secure data access.
- Keycloak is used as an OAuth2/OIDC Identity Provider.
- Data API Builder (DAB) sits in front of a SQL Server database to expose REST endpoints.
- The goal is to demonstrate multiple authentication flows (anonymous, password, client-credentials, authorization code) and RBAC via DAB.

##### File structure
- Top level:
- .env – environment variables (e.g., DB credentials, Keycloak admin creds)
- dab-config.json – DAB’s data model and authorization rules
- docker-compose.yml – orchestration for all containers
- SQL/create-sql-env.sql – schema + seed for SQL Server
- DAB/ directory:
- Dockerfile – builds the DAB container
- keycloak.crt – Keycloak’s self-signed cert for trust
- keycloak/ directory:
- certs/keycloak.crt & keycloak.key – SSL certificates for Keycloak HTTPS
- Dockerfile – builds Keycloak server image
- Implicit component: SQL Server container defined in docker-compose.

##### Extract architectural insights from the README
- Services launched via Docker Compose: Keycloak (port 8443), DAB (port 5000), SQL Server.
- Workflow: clients obtain tokens from Keycloak, then call DAB endpoints.
- DAB validates JWTs using Keycloak’s cert, enforces RBAC, and proxies queries to SQL Server.
- Demonstrates HTTPS between services, use of self-signed certificates for dev.

##### Main components
a. Clients (Postman/browser/app)
b. Keycloak Identity Provider
c. Data API Builder (DAB)
d. SQL Server database
e. Docker network connecting all services

##### Relationships and data flows
1. Clients → Keycloak: OAuth2/OIDC token requests (anonymous, password, client-credentials, auth-code flows)
2. Keycloak → Clients: JWT or access tokens over HTTPS
3. Clients → DAB endpoints: attach JWT in Authorization header
4. DAB → Keycloak: fetch/verify JWKS or cert to validate JWT signature
5. DAB → SQL Server: execute SELECT/INSERT/UPDATE based on dab-config.json, enforce row-level and role-level security
6. SQL Server → DAB → Clients: return JSON payloads

##### Architectural patterns and design principles
- Clear service separation (Auth vs Data).
- Principle of least privilege: DAB applies RBAC rules defined in dab-config.json.
- Deployment as containers: immutable infrastructure via Dockerfiles + compose.
- Certificate trust chain: self-signed cert distribution into DAB.

##### Technologies and frameworks
- Keycloak (Java, WildFly) for OAuth2/OIDC
- Data API Builder (.NET Core / Azure Data API Builder)
- SQL Server (docker image)
- Docker Compose networking
- OpenSSL for cert generation
- JSON-based DAB configuration for data model & auth rules

3. Ports, protocols (HTTPS, TCP), and security cert exchange.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Clients"
        Client["Clients\n(Postman/Browser/App)"]:::clients
    end

    subgraph "Auth Service"
        Keycloak["Keycloak (IDP)\nPort 8443"]:::auth
    end

    subgraph "Data Service"
        DAB["Data API Builder\nPort 5000"]:::data
        Config["dab-config.json"]:::config
    end

    subgraph "Database"
        SQL["SQL Server\nPort 1433"]:::data
        SQLScript["create-sql-env.sql"]:::config
    end

    subgraph "Docker Setup"
        Env[".env"]:::config
        Compose["docker-compose.yml"]:::config
    end

    Client -->|"Token Request\n(anonymous, password,\nclient-credentials,\nauth-code)"| Keycloak
    Keycloak -->|"Token Response\n(JWT)"| Client

    Client -->|"API Call + JWT"| DAB
    DAB -->|"Fetch JWKS / Validate JWT"| Keycloak
    Keycloak -->|"keycloak.crt"| DAB

    DAB -->|"DB Query (SELECT/INSERT/UPDATE)"| SQL
    SQL -->|"Data Response (JSON)"| DAB
    DAB -->|"JSON Response"| Client

    Env --- Compose
    Compose --- Keycloak
    Compose --- DAB
    Compose --- SQL

    click Env "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/blob/main/.env"
    click Compose "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/blob/main/docker-compose.yml"
    click DAB "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/tree/main/DAB/Dockerfile"
    click Config "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/blob/main/dab-config.json"
    click Keycloak "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/tree/main/keycloak/Dockerfile"
    click SQLScript "https://github.com/yougnoli/api-authentication-authorization-keycloak-data-api-builder-docker/blob/main/SQL/create-sql-env.sql"

    classDef clients fill:#AEDFF7,stroke:#036B8F,color:#036B8F;
    classDef auth fill:#D4F5C4,stroke:#4CAF50,color:#4CAF50;
    classDef data fill:#FFF2CC,stroke:#F1C232,color:#BF8F00;
    classDef config fill:#E0E0E0,stroke:#888888,color:#333333;
```
