Compare commits
20
Commits
f4da65dbc2
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eaf197f86e | ||
|
|
7b98fab2c8 | ||
|
|
d7ab571bbe | ||
|
|
87fcd522c4 | ||
|
|
5884d840e7 | ||
|
|
a631b8cd1e | ||
|
|
8bc5200f77 | ||
|
|
8878dbb454 | ||
|
|
ecf275dedd | ||
|
|
a6b9ee5b09 | ||
|
|
2e81132f2d | ||
|
|
c534299c8e | ||
|
|
4b428ec32a | ||
|
|
752373c7b7 | ||
|
|
d7ddc087a6 | ||
|
|
7f77bfef4e | ||
|
|
4e64aab0a5 | ||
|
|
4596dfe28b | ||
|
|
57a3d04625 | ||
|
|
81129d6c9c |
@@ -1,3 +1,4 @@
|
|||||||
JWKS_URL=https://auth.iceberg.black/realms/iceberg/protocol/openid-connect/certs
|
JWKS_URL=https://auth.iceberg.black/realms/iceberg/protocol/openid-connect/certs
|
||||||
ISSUER=https://auth.iceberg.black/realms/iceberg
|
ISSUER=https://auth.iceberg.black/realms/iceberg
|
||||||
OLLAMA_URL=...
|
OLLAMA_URL=...
|
||||||
|
CORS_ORIGIN=
|
||||||
@@ -33,6 +33,8 @@ jobs:
|
|||||||
${{ runner.os }}-cargo-
|
${{ runner.os }}-cargo-
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
|
env:
|
||||||
|
SQLX_OFFLINE: true
|
||||||
run: cargo test --workspace --verbose --all
|
run: cargo test --workspace --verbose --all
|
||||||
|
|
||||||
- name: Run Clippy (linter)
|
- name: Run Clippy (linter)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ name: Publish & Deploy
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- 'v*'
|
- "v*"
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -34,6 +34,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Build and Push
|
- name: Build and Push
|
||||||
uses: https://github.com/docker/build-push-action@v5
|
uses: https://github.com/docker/build-push-action@v5
|
||||||
|
env:
|
||||||
|
SQLX_OFFLINE: true
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
@@ -86,4 +88,3 @@ jobs:
|
|||||||
REPO_LOWER=$REPO_LOWER TAG=$TAG docker compose up -d
|
REPO_LOWER=$REPO_LOWER TAG=$TAG docker compose up -d
|
||||||
|
|
||||||
docker image prune -af
|
docker image prune -af
|
||||||
|
|
||||||
|
|||||||
+11
-4
@@ -12,30 +12,37 @@ repos:
|
|||||||
|
|
||||||
- id: clippy
|
- id: clippy
|
||||||
name: clippy
|
name: clippy
|
||||||
entry: cargo clippy -- -D warnings
|
entry: bash -c 'SQLX_OFFLINE=true cargo clippy -- -D warnings'
|
||||||
language: system
|
language: system
|
||||||
types: [rust]
|
types: [rust]
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit]
|
stages: [pre-commit]
|
||||||
|
|
||||||
# ─────────────── PRE-PUSH (heavy checks) ───────────────
|
# ─────────────── PRE-PUSH (heavy checks) ───────────────
|
||||||
|
- id: sqlx-prepare-check
|
||||||
|
name: sqlx prepare check
|
||||||
|
entry: cargo sqlx prepare --check
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
stages: [pre-push]
|
||||||
|
|
||||||
- id: test
|
- id: test
|
||||||
name: cargo test
|
name: cargo test
|
||||||
entry: cargo test --all
|
entry: bash -c 'SQLX_OFFLINE=true cargo test --all'
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-push]
|
stages: [pre-push]
|
||||||
|
|
||||||
- id: build
|
- id: build
|
||||||
name: cargo build release
|
name: cargo build release
|
||||||
entry: cargo build --release
|
entry: bash -c 'SQLX_OFFLINE=true cargo build --release'
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-push]
|
stages: [pre-push]
|
||||||
|
|
||||||
- id: audit
|
- id: audit
|
||||||
name: cargo audit
|
name: cargo audit
|
||||||
entry: cargo audit
|
entry: bash -c 'cargo audit || echo "cargo audit failed (non-blocking)"'
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-push]
|
stages: [pre-push]
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO auth.api_key (key_hash, name, created_by, scopes)\n VALUES ($1, $2, $3, $4)\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"TextArray"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "14b38b0f3fca61893dc6e036dc68f643a724b729f73f8308ae0b3a70d87dc5de"
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO auth.app_user (id)\n VALUES ($1)\n ON CONFLICT (id) DO NOTHING\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "70117c16fc3efeebbcb40838d13dd427bf246f4eabd3e335077f47a7aa7882e4"
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n UPDATE auth.api_key\n SET last_used_at = now()\n WHERE id = $1\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "ab03067777ea2a8f12c3dec5cf99caf4ad700008d892a33c25408c34bebab83c"
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n SELECT u.id AS user_id, ak.id as key_id\n FROM auth.api_key ak\n JOIN auth.app_user u ON u.id = ak.created_by\n WHERE ak.key_hash = $1\n AND ak.revoked_at IS NULL\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "user_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "key_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "c0816078de6c25d441752500b9307900ff3d6c7781ebd9c0d8b47031539d8bdf"
|
||||||
|
}
|
||||||
Generated
+969
-31
File diff suppressed because it is too large
Load Diff
+12
-5
@@ -5,20 +5,27 @@ edition = "2024"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wiremock = "0.6"
|
wiremock = "0.6"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.52.2", features = ["macros", "rt-multi-thread"] }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.8.8"
|
axum = "0.8.9"
|
||||||
utoipa = { version = "5.4.0", features = ["axum_extras"] }
|
utoipa = { version = "5.5.0", features = ["axum_extras"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
jsonwebtoken = { version = "10.3.0", features = ["aws_lc_rs"] }
|
jsonwebtoken = { version = "10.3.0", features = ["aws_lc_rs"] }
|
||||||
reqwest = { version = "0.13.2", features = ["json", "stream"] }
|
reqwest = { version = "0.13.3", features = ["json", "stream"] }
|
||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
dotenvy = "0.15"
|
dotenvy = "0.15"
|
||||||
thiserror = "2.0.18"
|
thiserror = "2.0.18"
|
||||||
tokio-stream = "0.1"
|
tokio-stream = "0.1"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
chrono = { version = "0.4.44", features = ["serde"] }
|
chrono = { version = "0.4.44", features = ["serde"] }
|
||||||
uuid = { version = "1", features = ["v4", "serde"] }
|
uuid = { version = "1.23.1", features = ["v4", "serde"] }
|
||||||
|
tower-http = { version = "0.6.10", features = ["cors"] }
|
||||||
|
tracing = "0.1.44"
|
||||||
|
tracing-subscriber = { version = "0.3", features = ["env-filter"]}
|
||||||
|
sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "postgres", "uuid", "chrono"] }
|
||||||
|
rand = "0.8"
|
||||||
|
base64 = "0.22.1"
|
||||||
|
sha2 = "0.11.0"
|
||||||
@@ -17,6 +17,7 @@ RUN rm -rf src
|
|||||||
|
|
||||||
# Build actual app
|
# Build actual app
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
|
COPY .sqlx ./.sqlx
|
||||||
RUN cargo build --release
|
RUN cargo build --release
|
||||||
|
|
||||||
# ── Runtime Stage ─────────────────────────────────────────────────────────────
|
# ── Runtime Stage ─────────────────────────────────────────────────────────────
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ services:
|
|||||||
- .env
|
- .env
|
||||||
|
|
||||||
ports:
|
ports:
|
||||||
- "6066:3000"
|
- "6066:3001"
|
||||||
|
|
||||||
restart: always
|
restart: always
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,269 @@
|
|||||||
|
# TODO
|
||||||
|
|
||||||
|
- Race condition on jwks token refresh
|
||||||
|
- Rate Limiting
|
||||||
|
|
||||||
|
git tag -d v1.0.0; git push origin :refs/tags/v1.0.0; git tag -a v1.0.0 -m "Release v1.0.0"; git push origin v1.0.0
|
||||||
|
|
||||||
|
curl https://chat.iceberg.black/api/v1/models -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJublpLek04TkZHVmpWbGFPRXZpMUtFSTVHQWRwaGlsYjh3RHRLeG5JOENZIn0.eyJleHAiOjE3NzU4MTUyODksImlhdCI6MTc3NTgxNDk4OSwianRpIjoiMTgwYzA2NDUtYzZiMC00MDRmLTgyYjEtMzA3YmY4ZmJlNmJlIiwiaXNzIjoiaHR0cHM6Ly9hdXRoLmljZWJlcmcuYmxhY2svcmVhbG1zL2ljZWJlcmciLCJzdWIiOiJmZGRiN2FjZC1kMmE5LTRmMTctOWIxNi1kZjVlN2EzNDI4YjciLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJjaGF0LWFwaSIsInNjb3BlIjoiIiwiY2xpZW50SG9zdCI6Ijg2LjIxMi44NC4xOTEiLCJjbGllbnRBZGRyZXNzIjoiODYuMjEyLjg0LjE5MSIsImNsaWVudF9pZCI6ImNoYXQtYXBpIn0.abjHABcjCJNiB6vriRw60nfzabEfD7CXwyRhkahFkC8ATgfy4fn0T8PnFfsaRpsXuhamIhWwNskrA7L9V3mbWWKW-JEOvImDhTc8sIX0E5fDTbk8O5wa_2yzNdLpRxdSjLqgL544rB8I-LZ8bl5SxtdN3gHfrnWr5ef8bbLgPRzZIylT3QUpah0uywDM_cfrrve9SMHHOUUItyzmOHLw0Igit1EzyFNyjbWf6OAU6TMjOF_eFTc5sakyBwsdJnGy0nhj5R-wxLpr1ug3iEd3Y-jDzHO4m6jariXVJ7Vvbz71i7sadDEKdSyOKcCeQbU4T0Tf7IxqW4c2DNnk3BFFuw"
|
||||||
|
|
||||||
|
curl -X POST "https://auth.iceberg.black/realms/iceberg/protocol/openid-connect/token" -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials" -d "client_id=chat-api" -d "client_secret=5fHUp8Z5GoNM70MVOGuQTfKFkaAE48Za"
|
||||||
|
|
||||||
|
curl -s -X POST https://chat.iceberg.black/api/v1/chat/completions \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJublpLek04TkZHVmpWbGFPRXZpMUtFSTVHQWRwaGlsYjh3RHRLeG5JOENZIn0.eyJleHAiOjE3NzU4MTU0NTEsImlhdCI6MTc3NTgxNTE1MSwianRpIjoiOGNjMmM5MjUtZmMzNy00MDFhLWE1ZjUtODFlOGJhNjcxNzcwIiwiaXNzIjoiaHR0cHM6Ly9hdXRoLmljZWJlcmcuYmxhY2svcmVhbG1zL2ljZWJlcmciLCJzdWIiOiJmZGRiN2FjZC1kMmE5LTRmMTctOWIxNi1kZjVlN2EzNDI4YjciLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJjaGF0LWFwaSIsInNjb3BlIjoiIiwiY2xpZW50SG9zdCI6Ijg2LjIxMi44NC4xOTEiLCJjbGllbnRBZGRyZXNzIjoiODYuMjEyLjg0LjE5MSIsImNsaWVudF9pZCI6ImNoYXQtYXBpIn0.Exv34aoAqwzSqQziZH3zScAnK_xiNCXqpOQl54x7NFMdO0gVgacJgMxoW82Ym8aCNfBRMFtWclZJ9RFh1b9uSEUgsdVtqvMX-2kCAhiMSjmIBPU-L0gr5N63c3bScOoAYa37baR4mXQ5LxHjfkLo_rHDJ74adg2JMo359Wbuu1_OR708_q8yjgO_4fQeYbIffxADwRDvPSIwQ8Y2qRjaAIOZs0xmA9p128CUxlUyvhxwfFquYKRaDs5QE9pIAtvm_KuoBydYopm8j8cbm4ixDhUwYjkzniIIapY77NxpmMosfh8BhK0W3ieK2gTKfmiFDhYgkGybU6b1BnJMy1_Kxg" \
|
||||||
|
-d '{
|
||||||
|
"model": "llama3:latest",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "What is Rust?"}
|
||||||
|
]
|
||||||
|
}' | jq .
|
||||||
|
|
||||||
|
# 🦙 Ollama Rust API Wrapper
|
||||||
|
|
||||||
|
A high-performance Rust API wrapper around Ollama, providing an OpenAI-compatible interface, model lifecycle management, and advanced runtime features.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🚀 Features
|
||||||
|
|
||||||
|
- ✅ OpenAI-compatible API (`/v1/...`)
|
||||||
|
- ⚡ Streaming (Server-Sent Events)
|
||||||
|
- 🧠 Model lifecycle management (load/unload)
|
||||||
|
- 🔐 API key authentication (optional)
|
||||||
|
- 📊 Usage tracking & observability
|
||||||
|
- 🔀 Model routing & abstraction
|
||||||
|
- 🧩 Extensible architecture (multi-provider ready)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 📡 API Endpoints
|
||||||
|
|
||||||
|
## 1. Core LLM API (OpenAI-compatible)
|
||||||
|
|
||||||
|
- [x] `POST /v1/chat/completions` + streaming
|
||||||
|
- [x] `POST /v1/completions` + streaming
|
||||||
|
- [ ] `POST /v1/embeddings`
|
||||||
|
- [x] `GET /v1/models`
|
||||||
|
|
||||||
|
## 2. Model Lifecycle Management
|
||||||
|
|
||||||
|
- [x] `POST /v1/models/{model}/load`
|
||||||
|
- [x] `POST /v1/models/{model}/unload`
|
||||||
|
|
||||||
|
## 3. Model Management
|
||||||
|
|
||||||
|
- [ ] `POST /v1/models/pull`
|
||||||
|
- [ ] `DELETE /v1/models/{model}`
|
||||||
|
|
||||||
|
## 4. Runtime & Observability
|
||||||
|
|
||||||
|
### Model Status
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v1/models/{model}/status
|
||||||
|
```
|
||||||
|
|
||||||
|
### List Loaded Models
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v1/runtime/models
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Health Checks
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /health
|
||||||
|
GET /ready
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🧠 Internal Mapping (Ollama)
|
||||||
|
|
||||||
|
| Wrapper Endpoint | Ollama Endpoint |
|
||||||
|
| ------------------------- | --------------- |
|
||||||
|
| /v1/chat/completions | /api/chat |
|
||||||
|
| /v1/completions | /api/generate |
|
||||||
|
| /v1/embeddings | /api/embeddings |
|
||||||
|
| /v1/models | /api/tags |
|
||||||
|
| /v1/models/pull | /api/pull |
|
||||||
|
| DELETE /v1/models/{model} | /api/delete |
|
||||||
|
| load/unload | /api/generate |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🔧 Advanced Features
|
||||||
|
|
||||||
|
## 🔀 Model Routing
|
||||||
|
|
||||||
|
Use abstract model names:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"model": "fast"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Example mapping:
|
||||||
|
|
||||||
|
```
|
||||||
|
fast → llama3:8b
|
||||||
|
smart → llama3:70b
|
||||||
|
code → deepseek-coder
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📊 Usage Tracking
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v1/usage
|
||||||
|
```
|
||||||
|
|
||||||
|
Tracks:
|
||||||
|
|
||||||
|
- request count
|
||||||
|
- latency
|
||||||
|
- per-model usage
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚦 Rate Limiting
|
||||||
|
|
||||||
|
- Requests per minute
|
||||||
|
- Tokens per minute
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
```
|
||||||
|
429 Too Many Requests
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧠 Sessions (Context Management)
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /v1/sessions
|
||||||
|
POST /v1/sessions/{id}/chat
|
||||||
|
```
|
||||||
|
|
||||||
|
Stores conversation history server-side.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ⚡ Caching
|
||||||
|
|
||||||
|
- Embeddings
|
||||||
|
- Deterministic prompts (temperature = 0)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧩 Tool / Function Calling
|
||||||
|
|
||||||
|
Supports structured tool execution:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tools": [
|
||||||
|
{
|
||||||
|
"name": "function_name",
|
||||||
|
"parameters": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📦 Batch Requests
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /v1/batch
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧠 Auto Eviction
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /v1/runtime/evict
|
||||||
|
```
|
||||||
|
|
||||||
|
Strategies:
|
||||||
|
|
||||||
|
- LRU
|
||||||
|
- memory threshold
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧾 Logs
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v1/logs
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔔 Async Jobs / Webhooks
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /v1/jobs
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🏗️ Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
Client → Rust API → Ollama → Response
|
||||||
|
```
|
||||||
|
|
||||||
|
### Layers:
|
||||||
|
|
||||||
|
- HTTP (Axum)
|
||||||
|
- Service layer (business logic)
|
||||||
|
- Provider abstraction
|
||||||
|
- Ollama client
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🔌 Provider Abstraction (Future-Proof)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
trait LlmProvider {
|
||||||
|
async fn chat(...);
|
||||||
|
async fn embeddings(...);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Supports:
|
||||||
|
|
||||||
|
- Ollama (current)
|
||||||
|
- OpenAI (future)
|
||||||
|
- Others
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🎯 Roadmap
|
||||||
|
|
||||||
|
- [ ] Full OpenAI compatibility
|
||||||
|
- [ ] Multi-node routing
|
||||||
|
- [ ] GPU-aware scheduling
|
||||||
|
- [ ] Web UI dashboard
|
||||||
|
- [ ] Distributed inference
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 🧠 Summary
|
||||||
|
|
||||||
|
This project turns Ollama into:
|
||||||
|
|
||||||
|
👉 A local OpenAI-compatible API
|
||||||
|
👉 A controllable model runtime
|
||||||
|
👉 A foundation for a full LLM gateway
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
- open api doc for bearer token
|
||||||
|
- db
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod postgres;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
use sqlx::PgPool;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub async fn update_last_access(pool: &PgPool, api_key_id: Uuid) -> Result<(), sqlx::Error> {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"
|
||||||
|
UPDATE auth.api_key
|
||||||
|
SET last_used_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
|
api_key_id
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod api_key;
|
||||||
|
pub mod pool;
|
||||||
|
pub mod user_repository;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub async fn create_pool(database_url: &str) -> Result<PgPool, sqlx::Error> {
|
||||||
|
PgPoolOptions::new()
|
||||||
|
.max_connections(10)
|
||||||
|
.acquire_timeout(Duration::from_secs(5))
|
||||||
|
.connect(database_url)
|
||||||
|
.await
|
||||||
|
.map_err(|err| {
|
||||||
|
tracing::error!("Postgres connection error: {:?}", err);
|
||||||
|
err
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
use sqlx::PgPool;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub async fn ensure_user_exists(pool: &PgPool, user_id: Uuid) -> Result<(), sqlx::Error> {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO auth.app_user (id)
|
||||||
|
VALUES ($1)
|
||||||
|
ON CONFLICT (id) DO NOTHING
|
||||||
|
"#,
|
||||||
|
user_id
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+12
-3
@@ -5,12 +5,21 @@ use crate::routes;
|
|||||||
|
|
||||||
#[derive(OpenApi)]
|
#[derive(OpenApi)]
|
||||||
#[openapi(
|
#[openapi(
|
||||||
|
info(
|
||||||
|
title = "Ollama Proxy",
|
||||||
|
description = "OpenAI-compatible proxy for local Ollama models",
|
||||||
|
version = "0.1.0",
|
||||||
|
license(
|
||||||
|
name = "MIT",
|
||||||
|
url = "https://opensource.org/licenses/MIT"
|
||||||
|
),
|
||||||
|
),
|
||||||
paths(
|
paths(
|
||||||
routes::v1::chat::completions,
|
routes::v1::chat::completions,
|
||||||
routes::v1::chat::chat_completions,
|
routes::v1::chat::chat_completions,
|
||||||
// routes::v1::models::list_models,
|
routes::v1::models::list_models,
|
||||||
// routes::v1::models::load_model,
|
routes::v1::models::load_model,
|
||||||
// routes::v1::models::unload_model,
|
routes::v1::models::unload_model,
|
||||||
),
|
),
|
||||||
components(
|
components(
|
||||||
schemas(
|
schemas(
|
||||||
|
|||||||
@@ -178,3 +178,14 @@ pub struct ChatDelta {
|
|||||||
pub role: Option<Role>,
|
pub role: Option<Role>,
|
||||||
pub content: Option<String>,
|
pub content: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
pub struct CreateApiKeyRequest {
|
||||||
|
pub name: String,
|
||||||
|
pub scopes: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
pub struct CreateApiKeyResponse {
|
||||||
|
pub api_key: String, // ONLY returned once
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
pub mod databases;
|
||||||
pub mod dto;
|
pub mod dto;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod middlewares;
|
pub mod middlewares;
|
||||||
pub mod providers;
|
pub mod providers;
|
||||||
|
pub mod state;
|
||||||
|
pub mod utils;
|
||||||
|
|||||||
+46
-3
@@ -1,3 +1,4 @@
|
|||||||
|
mod databases;
|
||||||
mod docs;
|
mod docs;
|
||||||
mod dto;
|
mod dto;
|
||||||
mod errors;
|
mod errors;
|
||||||
@@ -5,18 +6,29 @@ mod middlewares;
|
|||||||
mod providers;
|
mod providers;
|
||||||
mod routes;
|
mod routes;
|
||||||
mod state;
|
mod state;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use crate::databases::postgres;
|
||||||
use crate::providers::ollama::client::OllamaProvider;
|
use crate::providers::ollama::client::OllamaProvider;
|
||||||
use crate::state::app_state::AppState;
|
use crate::state::app_state::AppState;
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
use axum::http::{HeaderName, HeaderValue, Method, header};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tower_http::cors::CorsLayer;
|
||||||
|
use tracing_subscriber::{EnvFilter, fmt};
|
||||||
|
|
||||||
static OLLAMA_URL: Lazy<String> = Lazy::new(|| env::var("OLLAMA_URL").expect("OLLAMA_URL not set"));
|
static OLLAMA_URL: Lazy<String> = Lazy::new(|| env::var("OLLAMA_URL").expect("OLLAMA_URL not set"));
|
||||||
|
|
||||||
|
pub fn init_tracing() {
|
||||||
|
let filter = env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
|
||||||
|
|
||||||
|
fmt().with_env_filter(EnvFilter::new(filter)).init();
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
@@ -24,16 +36,47 @@ async fn main() {
|
|||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init_tracing();
|
||||||
|
|
||||||
|
// DB Connection
|
||||||
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
|
|
||||||
|
let pool = postgres::pool::create_pool(&database_url)
|
||||||
|
.await
|
||||||
|
.expect("Fatal error");
|
||||||
|
|
||||||
let state = AppState {
|
let state = AppState {
|
||||||
ollama: Arc::new(OllamaProvider::new(OLLAMA_URL.as_str())),
|
ollama: Arc::new(OllamaProvider::new(OLLAMA_URL.as_str())),
|
||||||
|
postgres: pool,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let cors_origin =
|
||||||
|
env::var("CORS_ORIGIN").unwrap_or_else(|_| "http://localhost:3000".to_string());
|
||||||
|
|
||||||
|
let cors = CorsLayer::new()
|
||||||
|
.allow_origin(cors_origin.parse::<HeaderValue>().unwrap())
|
||||||
|
.allow_methods([
|
||||||
|
Method::GET,
|
||||||
|
Method::POST,
|
||||||
|
Method::PUT,
|
||||||
|
Method::DELETE,
|
||||||
|
Method::OPTIONS,
|
||||||
|
])
|
||||||
|
.allow_headers([
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
header::ACCEPT,
|
||||||
|
HeaderName::from_static("x-api-key"),
|
||||||
|
])
|
||||||
|
.allow_credentials(true);
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.nest("/v1", routes::v1::router())
|
.nest("/v1", routes::v1::router(state.clone()))
|
||||||
|
.layer(cors)
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
let addr = SocketAddr::from(([0, 0, 0, 0], 3001));
|
||||||
println!("Server running on {}", addr);
|
tracing::debug!("Server running on {}", addr);
|
||||||
|
|
||||||
axum::serve(tokio::net::TcpListener::bind(addr).await.unwrap(), app)
|
axum::serve(tokio::net::TcpListener::bind(addr).await.unwrap(), app)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct ApiKeyClaims {
|
||||||
|
pub sub: Uuid,
|
||||||
|
pub api_key_id: Uuid,
|
||||||
|
}
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode, decode_header};
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_json::Value;
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
pub struct Claims {
|
|
||||||
pub sub: String,
|
|
||||||
pub preferred_username: Option<String>,
|
|
||||||
pub exp: usize,
|
|
||||||
pub iss: String,
|
|
||||||
pub aud: Option<String>,
|
|
||||||
pub realm_access: Option<RealmAccess>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
pub struct RealmAccess {
|
|
||||||
pub roles: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
static ISSUER: Lazy<String> = Lazy::new(|| env::var("ISSUER").expect("ISSUER not set"));
|
|
||||||
|
|
||||||
pub fn validate_token(token: &str, jwks: &Value) -> Result<Claims, String> {
|
|
||||||
// 1. Decode header
|
|
||||||
let header = decode_header(token).map_err(|_| "Invalid header")?;
|
|
||||||
|
|
||||||
let kid = header.kid.ok_or("Missing kid")?;
|
|
||||||
|
|
||||||
// 2. Find matching key
|
|
||||||
let keys = jwks["keys"].as_array().ok_or("Invalid JWKS")?;
|
|
||||||
|
|
||||||
let key = keys
|
|
||||||
.iter()
|
|
||||||
.find(|k| k["kid"] == kid)
|
|
||||||
.ok_or("Matching key not found")?;
|
|
||||||
|
|
||||||
// 3. Extract RSA components
|
|
||||||
let n = key["n"].as_str().ok_or("Missing n")?;
|
|
||||||
let e = key["e"].as_str().ok_or("Missing e")?;
|
|
||||||
|
|
||||||
let decoding_key =
|
|
||||||
DecodingKey::from_rsa_components(n, e).map_err(|_| "Invalid decoding key")?;
|
|
||||||
|
|
||||||
// 4. Setup validation rules
|
|
||||||
let mut validation = Validation::new(Algorithm::RS256);
|
|
||||||
|
|
||||||
validation.set_issuer(&[ISSUER.as_str()]);
|
|
||||||
|
|
||||||
// Optional but recommended:
|
|
||||||
validation.validate_exp = true;
|
|
||||||
validation.validate_aud = false; // depends on your Keycloak config
|
|
||||||
|
|
||||||
// 5. Decode & verify
|
|
||||||
let token_data = decode::<Claims>(token, &decoding_key, &validation)
|
|
||||||
.map_err(|_| "Token validation failed")?;
|
|
||||||
|
|
||||||
Ok(token_data.claims)
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
|
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode, decode_header};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
// ------ JWKS ------
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct JwksCache {
|
struct JwksCache {
|
||||||
jwks: Value,
|
jwks: Value,
|
||||||
@@ -55,3 +60,89 @@ pub async fn get_jwks() -> Result<Value, reqwest::Error> {
|
|||||||
// Expired or empty → refresh
|
// Expired or empty → refresh
|
||||||
refresh_jwks().await
|
refresh_jwks().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------ Claims ------
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||||
|
pub struct KeycloakClaims {
|
||||||
|
pub sub: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub exp: usize,
|
||||||
|
pub iss: String,
|
||||||
|
pub aud: Option<Vec<String>>,
|
||||||
|
pub realm_access: Option<RealmAccess>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub resource_access: HashMap<String, ResourceAccess>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct RealmAccess {
|
||||||
|
pub roles: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct ResourceAccess {
|
||||||
|
pub roles: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeycloakClaims {
|
||||||
|
pub fn realm_roles(&self) -> &[String] {
|
||||||
|
self.realm_access
|
||||||
|
.as_ref()
|
||||||
|
.map_or(&[], |r| r.roles.as_slice())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_realm_role(&self, role: &str) -> bool {
|
||||||
|
self.realm_roles().iter().any(|r| r == role)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn client_roles(&self, client: &str) -> &[String] {
|
||||||
|
self.resource_access
|
||||||
|
.get(client)
|
||||||
|
.map_or(&[], |r| r.roles.as_slice())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_client_role(&self, client: &str, role: &str) -> bool {
|
||||||
|
self.client_roles(client).iter().any(|r| r == role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------ Validation ------
|
||||||
|
|
||||||
|
static ISSUER: Lazy<String> = Lazy::new(|| env::var("ISSUER").expect("ISSUER not set"));
|
||||||
|
|
||||||
|
pub fn validate_token(token: &str, jwks: &Value) -> Result<KeycloakClaims, String> {
|
||||||
|
// 1. Decode header
|
||||||
|
let header = decode_header(token).map_err(|_| "Invalid header")?;
|
||||||
|
|
||||||
|
let kid = header.kid.ok_or("Missing kid")?;
|
||||||
|
|
||||||
|
// 2. Find matching key
|
||||||
|
let keys = jwks["keys"].as_array().ok_or("Invalid JWKS")?;
|
||||||
|
|
||||||
|
let key = keys
|
||||||
|
.iter()
|
||||||
|
.find(|k| k["kid"] == kid)
|
||||||
|
.ok_or("Matching key not found")?;
|
||||||
|
|
||||||
|
// 3. Extract RSA components
|
||||||
|
let n = key["n"].as_str().ok_or("Missing n")?;
|
||||||
|
let e = key["e"].as_str().ok_or("Missing e")?;
|
||||||
|
|
||||||
|
let decoding_key =
|
||||||
|
DecodingKey::from_rsa_components(n, e).map_err(|_| "Invalid decoding key")?;
|
||||||
|
|
||||||
|
// 4. Setup validation rules
|
||||||
|
let mut validation = Validation::new(Algorithm::RS256);
|
||||||
|
|
||||||
|
validation.set_issuer(&[ISSUER.as_str()]);
|
||||||
|
|
||||||
|
validation.validate_exp = true;
|
||||||
|
validation.validate_aud = false;
|
||||||
|
|
||||||
|
// 5. Decode & verify
|
||||||
|
let token_data = decode::<KeycloakClaims>(token, &decoding_key, &validation)
|
||||||
|
.map_err(|_| "Token validation failed")?;
|
||||||
|
|
||||||
|
Ok(token_data.claims)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,51 +1,215 @@
|
|||||||
use axum::{extract::Request, http::StatusCode, middleware::Next, response::Response};
|
use axum::{
|
||||||
|
extract::{Request, State},
|
||||||
use crate::middlewares::auth::{
|
http::StatusCode,
|
||||||
jwt::validate_token,
|
middleware::Next,
|
||||||
keycloak::{get_jwks, refresh_jwks},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn auth_middleware(mut request: Request, next: Next) -> Result<Response, StatusCode> {
|
use crate::databases::postgres::{
|
||||||
#[cfg(debug_assertions)]
|
api_key::update_last_access, user_repository::ensure_user_exists,
|
||||||
println!("Middleware hit");
|
};
|
||||||
|
use crate::middlewares::auth::apikey::ApiKeyClaims;
|
||||||
|
use crate::middlewares::auth::keycloak::{KeycloakClaims, get_jwks, refresh_jwks, validate_token};
|
||||||
|
use crate::state::app_state::AppState;
|
||||||
|
use crate::utils::crypto::hash_key;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
let headers = request.headers();
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Auth {
|
||||||
|
Jwt(KeycloakClaims),
|
||||||
|
ApiKey(ApiKeyClaims),
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
impl Auth {
|
||||||
println!("Headers extracted");
|
pub fn user_id(&self) -> Uuid {
|
||||||
|
match self {
|
||||||
|
Auth::Jwt(c) => c.sub.parse().expect("sub is a valid UUID"),
|
||||||
|
Auth::ApiKey(c) => c.sub,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let auth_header = headers.get("authorization").and_then(|v| v.to_str().ok());
|
pub fn has_realm_role(&self, role: &str) -> bool {
|
||||||
|
match self {
|
||||||
|
Auth::Jwt(c) => c.has_realm_role(role),
|
||||||
|
Auth::ApiKey(_) => false, // API keys carry no roles
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
pub fn has_client_role(&self, client: &str, role: &str) -> bool {
|
||||||
println!("Auth header: {:?}", auth_header);
|
match self {
|
||||||
|
Auth::Jwt(c) => c.has_client_role(client, role),
|
||||||
|
Auth::ApiKey(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let auth_header = auth_header.ok_or(StatusCode::UNAUTHORIZED)?;
|
pub async fn auth_middleware(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
request: Request,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
|
match try_jwt(&state, request, next).await {
|
||||||
|
Ok(response) => Ok(response),
|
||||||
|
Err((request, next)) => try_api_key(&state, request, next).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let token = auth_header
|
/// Returns Ok(Response) if JWT was valid and request handled.
|
||||||
.strip_prefix("Bearer ")
|
/// Returns Err((request, next)) if no JWT was present (caller should try next method).
|
||||||
|
/// Returns a 401/500 response directly if JWT was present but invalid.
|
||||||
|
async fn try_jwt(
|
||||||
|
state: &AppState,
|
||||||
|
request: Request,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, (Request, Next)> {
|
||||||
|
let token = request
|
||||||
|
.headers()
|
||||||
|
.get("authorization")
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.and_then(|v| v.strip_prefix("Bearer "))
|
||||||
|
.map(str::to_owned);
|
||||||
|
|
||||||
|
let Some(token) = token else {
|
||||||
|
// No Authorization header at all → let API key branch try
|
||||||
|
return Err((request, next));
|
||||||
|
};
|
||||||
|
|
||||||
|
let jwks = match get_jwks().await {
|
||||||
|
Ok(j) => j,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to fetch JWKS: {e}");
|
||||||
|
// Token was present but we can't validate → hard 500
|
||||||
|
return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let claims = match validate_token(&token, &jwks) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(_) => {
|
||||||
|
// Try refreshing JWKS once
|
||||||
|
match refresh_jwks().await {
|
||||||
|
Ok(fresh_jwks) => match validate_token(&token, &fresh_jwks) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(_) => {
|
||||||
|
tracing::warn!("JWT validation failed after JWKS refresh");
|
||||||
|
return Ok(StatusCode::UNAUTHORIZED.into_response());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to refresh JWKS: {e}");
|
||||||
|
return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::debug!("JWT valid, sub={}", claims.sub);
|
||||||
|
handle_auth(state, request, next, Auth::Jwt(claims))
|
||||||
|
.await
|
||||||
|
.map_err(|_| unreachable!())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns Ok(Response) if API key was valid.
|
||||||
|
/// Returns Err(StatusCode) otherwise (UNAUTHORIZED or INTERNAL_SERVER_ERROR).
|
||||||
|
async fn try_api_key(
|
||||||
|
state: &AppState,
|
||||||
|
request: Request,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
|
let key = request
|
||||||
|
.headers()
|
||||||
|
.get("x-api-key")
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.ok_or(StatusCode::UNAUTHORIZED)?
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
let key_hash = hash_key(&key);
|
||||||
|
|
||||||
|
let row = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT u.id AS user_id, ak.id as key_id
|
||||||
|
FROM auth.api_key ak
|
||||||
|
JOIN auth.app_user u ON u.id = ak.created_by
|
||||||
|
WHERE ak.key_hash = $1
|
||||||
|
AND ak.revoked_at IS NULL
|
||||||
|
"#,
|
||||||
|
key_hash
|
||||||
|
)
|
||||||
|
.fetch_optional(&state.postgres)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tracing::error!("DB error during API key lookup: {e}");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
.ok_or(StatusCode::UNAUTHORIZED)?;
|
.ok_or(StatusCode::UNAUTHORIZED)?;
|
||||||
|
|
||||||
let jwks = get_jwks().await.map_err(|_| StatusCode::UNAUTHORIZED)?;
|
tracing::debug!("API key valid, user_id={}", row.user_id);
|
||||||
|
|
||||||
match validate_token(token, &jwks) {
|
handle_auth(
|
||||||
Ok(claims) => {
|
state,
|
||||||
#[cfg(debug_assertions)]
|
request,
|
||||||
println!("Token valid");
|
next,
|
||||||
|
Auth::ApiKey(ApiKeyClaims {
|
||||||
|
sub: row.user_id,
|
||||||
|
api_key_id: row.key_id,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
request.extensions_mut().insert(claims);
|
// ── Shared post-auth logic ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Ensures the user exists in the DB, inserts `Auth` into extensions, runs the next handler.
|
||||||
|
async fn handle_auth(
|
||||||
|
state: &AppState,
|
||||||
|
mut request: Request,
|
||||||
|
next: Next,
|
||||||
|
auth: Auth,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
|
match &auth {
|
||||||
|
Auth::Jwt(_) => {
|
||||||
|
ensure_user_exists(&state.postgres, auth.user_id())
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tracing::error!("ensure_user_exists failed: {e}");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
Auth::ApiKey(key) => {
|
||||||
|
update_last_access(&state.postgres, key.api_key_id)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tracing::error!("update_last_access failed: {e}");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request.extensions_mut().insert(auth);
|
||||||
|
Ok(next.run(request).await)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Role guard ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Layer-level middleware that checks roles *after* `auth_middleware` has run.
|
||||||
|
pub async fn require_roles(
|
||||||
|
request: Request,
|
||||||
|
next: Next,
|
||||||
|
realm_role: Option<&'static str>,
|
||||||
|
client_role: Option<&'static str>,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
|
let auth = request
|
||||||
|
.extensions()
|
||||||
|
.get::<Auth>()
|
||||||
|
.ok_or(StatusCode::UNAUTHORIZED)?;
|
||||||
|
|
||||||
|
if realm_role.is_some_and(|role| !auth.has_realm_role(role)) {
|
||||||
|
return Err(StatusCode::FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
if client_role.is_some_and(|role| !auth.has_client_role("chat-api", role)) {
|
||||||
|
return Err(StatusCode::FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(next.run(request).await)
|
Ok(next.run(request).await)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
|
||||||
let jwks = refresh_jwks().await.map_err(|_| StatusCode::UNAUTHORIZED)?;
|
|
||||||
|
|
||||||
match validate_token(token, &jwks) {
|
|
||||||
Ok(claims) => {
|
|
||||||
request.extensions_mut().insert(claims);
|
|
||||||
Ok(next.run(request).await)
|
|
||||||
}
|
|
||||||
Err(_) => Err(StatusCode::UNAUTHORIZED),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
pub mod jwt;
|
pub mod apikey;
|
||||||
pub mod keycloak;
|
pub mod keycloak;
|
||||||
pub mod middleware;
|
pub mod middleware;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
use axum::{
|
||||||
|
Json,
|
||||||
|
extract::{Extension, State},
|
||||||
|
http::StatusCode,
|
||||||
|
};
|
||||||
|
use base64::{Engine as _, engine::general_purpose};
|
||||||
|
use rand::RngCore;
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
|
||||||
|
use crate::dto::api::{CreateApiKeyRequest, CreateApiKeyResponse};
|
||||||
|
use crate::middlewares::auth::middleware::Auth;
|
||||||
|
use crate::state::app_state::AppState;
|
||||||
|
use crate::utils::crypto::hash_key;
|
||||||
|
|
||||||
|
fn generate_api_key() -> String {
|
||||||
|
let mut bytes = [0u8; 32];
|
||||||
|
OsRng.fill_bytes(&mut bytes);
|
||||||
|
general_purpose::URL_SAFE_NO_PAD.encode(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_api_key(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Extension(claims): Extension<Auth>,
|
||||||
|
Json(body): Json<CreateApiKeyRequest>,
|
||||||
|
) -> Result<Json<CreateApiKeyResponse>, StatusCode> {
|
||||||
|
if matches!(claims, Auth::ApiKey(_)) {
|
||||||
|
return Err(StatusCode::FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
let raw_key = generate_api_key();
|
||||||
|
let key_hash = hash_key(&raw_key);
|
||||||
|
|
||||||
|
dbg!(&claims);
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO auth.api_key (key_hash, name, created_by, scopes)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
"#,
|
||||||
|
key_hash,
|
||||||
|
body.name,
|
||||||
|
claims.user_id(),
|
||||||
|
&body.scopes
|
||||||
|
)
|
||||||
|
.execute(&state.postgres)
|
||||||
|
.await
|
||||||
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||||
|
|
||||||
|
Ok(Json(CreateApiKeyResponse { api_key: raw_key }))
|
||||||
|
}
|
||||||
@@ -51,6 +51,7 @@ pub async fn completions(
|
|||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(body): Json<api::CompletionRequest>,
|
Json(body): Json<api::CompletionRequest>,
|
||||||
) -> Result<Response, (axum::http::StatusCode, Json<api::ErrorResponse>)> {
|
) -> Result<Response, (axum::http::StatusCode, Json<api::ErrorResponse>)> {
|
||||||
|
tracing::debug!("Received /completion with body {:?}", body);
|
||||||
if body.base.stream {
|
if body.base.stream {
|
||||||
let stream = state.ollama.completions_stream(&body).await.map_err(|e| {
|
let stream = state.ollama.completions_stream(&body).await.map_err(|e| {
|
||||||
let (code, msg) = ollama_err(e);
|
let (code, msg) = ollama_err(e);
|
||||||
@@ -116,6 +117,7 @@ pub async fn chat_completions(
|
|||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(body): Json<api::ChatRequest>,
|
Json(body): Json<api::ChatRequest>,
|
||||||
) -> Result<Response, (axum::http::StatusCode, String)> {
|
) -> Result<Response, (axum::http::StatusCode, String)> {
|
||||||
|
tracing::debug!("Received /completion with body {:?}", body);
|
||||||
if body.base.stream {
|
if body.base.stream {
|
||||||
let stream = state
|
let stream = state
|
||||||
.ollama
|
.ollama
|
||||||
|
|||||||
+12
-4
@@ -1,9 +1,11 @@
|
|||||||
|
pub mod apikey;
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
use crate::docs::ApiDoc;
|
use crate::docs::ApiDoc;
|
||||||
use crate::middlewares::auth::auth_middleware;
|
use crate::middlewares::auth::{auth_middleware, middleware::require_roles};
|
||||||
use crate::state::app_state::AppState;
|
use crate::state::app_state::AppState;
|
||||||
|
|
||||||
use axum::{Json, Router, middleware, routing::get, routing::post};
|
use axum::{Json, Router, middleware, routing::get, routing::post};
|
||||||
use utoipa::OpenApi;
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
@@ -12,7 +14,7 @@ async fn openapi_json() -> Json<utoipa::openapi::OpenApi> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn public_router() -> Router<AppState> {
|
fn public_router() -> Router<AppState> {
|
||||||
Router::new().route("/openapi.json", get(openapi_json))
|
Router::new().route("/docs.json", get(openapi_json))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn protected_router() -> Router<AppState> {
|
pub fn protected_router() -> Router<AppState> {
|
||||||
@@ -22,10 +24,16 @@ pub fn protected_router() -> Router<AppState> {
|
|||||||
.route("/chat/completions", post(chat::chat_completions))
|
.route("/chat/completions", post(chat::chat_completions))
|
||||||
.route("/models/{model}/load", post(models::load_model))
|
.route("/models/{model}/load", post(models::load_model))
|
||||||
.route("/models/{model}/unload", post(models::unload_model))
|
.route("/models/{model}/unload", post(models::unload_model))
|
||||||
|
.route(
|
||||||
|
"/keys/generate",
|
||||||
|
post(apikey::create_api_key).route_layer(middleware::from_fn(|req, next| {
|
||||||
|
require_roles(req, next, None, Some("admin"))
|
||||||
|
})),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn router() -> Router<AppState> {
|
pub fn router(state: AppState) -> Router<AppState> {
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(public_router())
|
.merge(public_router())
|
||||||
.merge(protected_router().layer(middleware::from_fn(auth_middleware)))
|
.merge(protected_router().layer(middleware::from_fn_with_state(state, auth_middleware)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
use crate::providers::ollama::client::OllamaProvider;
|
use crate::providers::ollama::client::OllamaProvider;
|
||||||
|
use sqlx::PgPool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub ollama: Arc<OllamaProvider>,
|
pub ollama: Arc<OllamaProvider>,
|
||||||
|
pub postgres: PgPool,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
pub fn hash_key(key: &str) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(key.as_bytes());
|
||||||
|
hasher
|
||||||
|
.finalize()
|
||||||
|
.iter()
|
||||||
|
.map(|b| format!("{:02x}", b))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod crypto;
|
||||||
Reference in New Issue
Block a user