secubox-openwrt/package/secubox/secubox-ai-gateway/files/usr/lib/ai-gateway/providers/gemini.sh
CyberMind-FR f3cea01792 feat(ai-gateway): Add Data Classifier (Sovereignty Engine) for ANSSI CSPN
Implement secubox-ai-gateway package with intelligent AI request routing
based on data sensitivity classification for GDPR/ANSSI compliance.

Features:
- 3-tier data classification: LOCAL_ONLY, SANITIZED, CLOUD_DIRECT
- Provider hierarchy: LocalAI > Mistral (EU) > Claude > GPT > Gemini > xAI
- PII sanitizer: IPv4/IPv6, MAC, credentials, private keys scrubbing
- OpenAI-compatible API proxy on port 4050
- aigatewayctl CLI: status, classify, sanitize, provider, audit commands
- RPCD backend with 11 ubus methods for LuCI integration
- ANSSI CSPN audit logging in JSONL format

Classification patterns detect:
- IP addresses, MAC addresses, private keys
- Credentials (password, secret, token, api_key)
- System paths, security tool references
- WireGuard configuration data

All cloud providers are opt-in. Default LOCAL_ONLY ensures data
sovereignty - sensitive data never leaves the device.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:55:22 +01:00

63 lines
1.7 KiB
Bash

#!/bin/sh
# SecuBox AI Gateway - Google Gemini Provider Adapter
CONFIG="ai-gateway"
provider_request() {
local request_json="$1"
local endpoint=$(uci -q get ${CONFIG}.gemini.endpoint || echo "https://generativelanguage.googleapis.com/v1beta")
local api_key=$(uci -q get ${CONFIG}.gemini.api_key)
local model=$(uci -q get ${CONFIG}.gemini.model || echo "gemini-pro")
if [ -z "$api_key" ]; then
printf '{"error":{"message":"Gemini API key not configured","type":"auth_error","code":"missing_api_key"}}'
return 1
fi
# Convert OpenAI format to Gemini format
local user_content=$(echo "$request_json" | jsonfilter -e '@.messages[-1].content' 2>/dev/null)
# Build Gemini request
local gemini_request=$(cat <<EOF
{
"contents": [{"parts":[{"text":"$user_content"}]}]
}
EOF
)
# Send to Gemini API
local response=$(echo "$gemini_request" | wget -q -O - \
--post-data=- \
--header="Content-Type: application/json" \
"${endpoint}/models/${model}:generateContent?key=${api_key}" 2>/dev/null)
if [ -z "$response" ]; then
printf '{"error":{"message":"Gemini API request failed","type":"provider_error","code":"gemini_error"}}'
return 1
fi
# Convert Gemini response to OpenAI format
local content=$(echo "$response" | jsonfilter -e '@.candidates[0].content.parts[0].text' 2>/dev/null)
if [ -n "$content" ]; then
printf '{"choices":[{"message":{"role":"assistant","content":"%s"},"finish_reason":"stop"}]}' "$content"
else
echo "$response"
fi
}
provider_test() {
local api_key=$(uci -q get ${CONFIG}.gemini.api_key)
echo "Testing Google Gemini..."
if [ -z "$api_key" ]; then
echo "Status: NOT CONFIGURED (no API key)"
return 1
fi
echo "Status: CONFIGURED"
echo "API Key: ***"
return 0
}