feat: Add SentinelOne integration

- Add SentinelOne API client (lib/services/sentinelone-client.ts)
  - Paginated fetching for sites, agents, threats
  - JWT token auth via S1_API_URL / S1_API_TOKEN env vars

- Add SentinelOne sync service (lib/services/sentinelone-sync-service.ts)
  - Full sync: sites, agents, threats into s1_* tables
  - Sync history tracking with per-entity results

- Add DB migration 038: s1_sites, s1_agents, s1_threats,
  s1_company_mappings, s1_sync_history tables

- Add API routes:
  - POST/GET /api/sentinelone/sync
  - GET/POST/DELETE /api/sentinelone/company-mappings
  - GET /api/sentinelone/coverage (fixed Cartesian product bug)

- Add UI pages:
  - /admin/sync/sentinelone — sync admin with history + stats
  - /sentinelone/coverage — AV coverage report per site
  - /sentinelone/mappings — map S1 sites to Autotask companies

- Wire SentinelOne into admin sync overview card grid
- Add SentinelOne Sync to app navigation
- Fix docker-compose: remove explicit S1 env var entries that
  were overwriting env_file values with empty strings
This commit is contained in:
lorentz 2026-02-27 05:31:31 -05:00
parent d7c3dc7168
commit ed6c4a8b65
12 changed files with 1637 additions and 7 deletions

View file

@ -16,17 +16,20 @@ interface IntegrationCard {
}
const INTEGRATIONS: IntegrationCard[] = [
{ id: 'autotask', category: 'PSA', product: 'Autotask', description: 'Tickets, companies, contacts, time entries, configuration items', href: '/admin/sync/autotask', logo: '/logos/autotask.ico', color: 'red' },
{ id: 'veeam', category: 'Backup', product: 'Veeam VSPC', description: 'Agent jobs, backup jobs, protected workloads, compliance', href: '/admin/sync/veeam', logo: '/logos/veeam.ico', color: 'green' },
{ id: 'datto-rmm', category: 'RMM', product: 'Datto RMM', description: 'Device monitoring, alerts, patch management, remote access', href: '/admin/sync/datto-rmm', logo: '/logos/datto-rmm.ico', color: 'blue' },
{ id: 'auvik', category: 'NMS', product: 'Auvik', description: 'Network devices, topology, tenant mappings', href: '/admin/sync/auvik', logo: '/logos/auvik.ico', color: 'purple' },
{ id: 'addigy', category: 'Apple RMM', product: 'Addigy', description: 'macOS/iOS device management, policies, compliance', href: '/admin/sync/addigy', logo: '/logos/addigy.ico', color: 'gray' },
{ id: 'autotask', category: 'PSA', product: 'Autotask', description: 'Tickets, companies, contacts, time entries, configuration items', href: '/admin/sync/autotask', logo: '/logos/autotask.ico', color: 'red' },
{ id: 'itglue', category: 'Documentation', product: 'IT Glue', description: 'Organizations, configurations, contacts, passwords, flexible assets, documents, domains', href: '/admin/sync/itglue', logo: '/logos/itglue.ico', color: 'blue' },
{ id: 'veeam', category: 'Backup', product: 'Veeam VSPC', description: 'Agent jobs, backup jobs, protected workloads, compliance', href: '/admin/sync/veeam', logo: '/logos/veeam.ico', color: 'green' },
{ id: 'datto-rmm', category: 'RMM', product: 'Datto RMM', description: 'Device monitoring, alerts, patch management, remote access', href: '/admin/sync/datto-rmm', logo: '/logos/datto-rmm.ico', color: 'orange' },
{ id: 'auvik', category: 'NMS', product: 'Auvik', description: 'Network devices, topology, tenant mappings', href: '/admin/sync/auvik', logo: '/logos/auvik.ico', color: 'purple' },
{ id: 'addigy', category: 'Apple RMM', product: 'Addigy', description: 'macOS/iOS device management, policies, compliance', href: '/admin/sync/addigy', logo: '/logos/addigy.ico', color: 'gray' },
{ id: 'sentinelone', category: 'EDR/AV', product: 'SentinelOne', description: 'Endpoint agents, threat detections, site coverage, AV health', href: '/admin/sync/sentinelone', logo: '/logos/sentinelone.ico', color: 'purple' },
];
const COLOR_MAP: Record<string, { bg: string; border: string }> = {
red: { bg: 'bg-red-500/5', border: 'border-red-500/20' },
green: { bg: 'bg-green-500/5', border: 'border-green-500/20' },
blue: { bg: 'bg-blue-500/5', border: 'border-blue-500/20' },
orange: { bg: 'bg-orange-500/5', border: 'border-orange-500/20' },
purple: { bg: 'bg-purple-500/5', border: 'border-purple-500/20' },
gray: { bg: 'bg-muted/20', border: 'border-border' },
};
@ -48,17 +51,24 @@ export default function SyncOverviewPage() {
const [autotaskSync, setAutotaskSync] = useState<any>(null);
const [loading, setLoading] = useState(true);
const [itglueSyncData, setItglueSyncData] = useState<any>(null);
const [s1SyncData, setS1SyncData] = useState<any>(null);
const fetchAll = async () => {
try {
const [intRes, atRes] = await Promise.all([
const [intRes, atRes, itgRes, s1Res] = await Promise.all([
fetch('/api/integrations/status'),
fetch('/api/sync/last-sync'),
fetch('/api/itglue/sync'),
fetch('/api/sentinelone/sync'),
]);
if (intRes.ok) setStatus(await intRes.json());
if (atRes.ok) {
const d = await atRes.json();
setAutotaskSync(d.lastSync || {});
}
if (itgRes.ok) setItglueSyncData(await itgRes.json());
if (s1Res.ok) setS1SyncData(await s1Res.json());
} catch (e) {
console.error(e);
} finally {
@ -110,6 +120,31 @@ export default function SyncOverviewPage() {
critical: d.openAlerts?.critical ?? 0,
};
}
if (id === 'itglue') {
if (!itglueSyncData) return null;
const h = itglueSyncData.history?.[0];
const c = itglueSyncData.counts ?? {};
return {
lastSync: h?.completed_at ?? null,
organizations: Number(c.organizations ?? 0),
configurations: Number(c.configurations ?? 0),
totalUpserted: h?.total_upserted ?? 0,
status: h?.status ?? null,
};
}
if (id === 'sentinelone') {
if (!s1SyncData) return null;
const h = s1SyncData.history?.[0];
const c = s1SyncData.counts ?? {};
return {
lastSync: h?.completed_at ?? null,
status: h?.status ?? null,
sites: Number(c.sites ?? 0),
agents: Number(c.agents ?? 0),
infected: Number(c.infected ?? 0),
threats: Number(c.threats ?? 0),
};
}
if (id === 'auvik') return { lastSync: status.auvik?.lastSync, configured: status.auvik?.configured };
if (id === 'addigy') return { lastSync: status.addigy?.lastSync, configured: status.addigy?.configured };
return null;
@ -131,6 +166,16 @@ export default function SyncOverviewPage() {
if (summary.openAlerts > 0) return <AlertTriangle className="w-4 h-4 text-yellow-500" />;
return <CheckCircle2 className="w-4 h-4 text-green-500" />;
}
if (id === 'itglue') {
if (!summary.lastSync) return <Clock className="w-4 h-4 text-muted-foreground" />;
if (summary.status === 'failed') return <XCircle className="w-4 h-4 text-red-500" />;
return <CheckCircle2 className="w-4 h-4 text-green-500" />;
}
if (id === 'sentinelone') {
if (!summary.lastSync) return <Clock className="w-4 h-4 text-muted-foreground" />;
if (summary.infected > 0) return <AlertTriangle className="w-4 h-4 text-red-500" />;
return <CheckCircle2 className="w-4 h-4 text-green-500" />;
}
return <CheckCircle2 className="w-4 h-4 text-green-500" />;
};
@ -243,6 +288,40 @@ export default function SyncOverviewPage() {
)}
</>
)}
{intg.id === 'itglue' && summary && (
<>
<div className="flex justify-between">
<span>Last sync</span>
<span className="font-medium text-foreground">{fmtDate(summary.lastSync)}</span>
</div>
<div className="flex justify-between">
<span>Organizations</span>
<span className="font-medium text-foreground">{(summary as any).organizations?.toLocaleString()}</span>
</div>
<div className="flex justify-between">
<span>Configurations</span>
<span className="font-medium text-foreground">{(summary as any).configurations?.toLocaleString()}</span>
</div>
</>
)}
{intg.id === 'sentinelone' && summary && (
<>
<div className="flex justify-between">
<span>Last sync</span>
<span className="font-medium text-foreground">{fmtDate(summary.lastSync)}</span>
</div>
<div className="flex justify-between">
<span>Sites / Agents</span>
<span className="font-medium text-foreground">{(summary as any).sites} / {(summary as any).agents?.toLocaleString()}</span>
</div>
{(summary as any).infected > 0 && (
<div className="flex justify-between text-red-600">
<span>Infected</span>
<span className="font-medium">{(summary as any).infected}</span>
</div>
)}
</>
)}
{(intg.id === 'auvik' || intg.id === 'addigy') && (
<div className="flex justify-between">
<span>Status</span>

View file

@ -0,0 +1,188 @@
'use client';
import { useState, useEffect, useCallback } from 'react';
import Link from 'next/link';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import {
ArrowLeft, RefreshCw, Play, CheckCircle2, XCircle, Clock,
Shield, Monitor, AlertTriangle, Activity,
} from 'lucide-react';
function fmtDate(d: string | null) {
if (!d) return '—';
return new Date(d).toLocaleString();
}
function fmtDuration(ms: number | null) {
if (!ms) return '—';
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
return `${(ms / 60000).toFixed(1)}m`;
}
export default function SentinelOneSyncPage() {
const [data, setData] = useState<any>(null);
const [loading, setLoading] = useState(true);
const [syncing, setSyncing] = useState(false);
const fetchData = useCallback(async () => {
try {
const res = await fetch('/api/sentinelone/sync');
if (res.ok) setData(await res.json());
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
const interval = setInterval(fetchData, 10000);
return () => clearInterval(interval);
}, [fetchData]);
const triggerSync = async () => {
setSyncing(true);
try {
await fetch('/api/sentinelone/sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ triggeredBy: 'manual' }),
});
setTimeout(fetchData, 2000);
} finally {
setSyncing(false);
}
};
const lastSync = data?.history?.[0];
const counts = data?.counts ?? {};
const inProgress = data?.inProgress ?? false;
return (
<div className="container mx-auto py-8 space-y-6 max-w-5xl">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Link href="/admin/sync">
<Button variant="ghost" size="sm"><ArrowLeft className="w-4 h-4 mr-1" />Back</Button>
</Link>
<div>
<h1 className="text-2xl font-bold flex items-center gap-2">
<Shield className="w-6 h-6 text-purple-500" />
SentinelOne Sync
</h1>
<p className="text-sm text-muted-foreground">Sites, agents, and threats synced to s1_* tables</p>
</div>
</div>
<Button onClick={triggerSync} disabled={syncing || inProgress}>
{syncing || inProgress
? <><RefreshCw className="w-4 h-4 mr-2 animate-spin" />Syncing...</>
: <><Play className="w-4 h-4 mr-2" />Sync Now</>}
</Button>
</div>
{/* Stats */}
<div className="grid grid-cols-2 md:grid-cols-5 gap-4">
{[
{ label: 'Sites', value: counts.sites, icon: Shield, color: 'text-purple-500' },
{ label: 'Agents', value: counts.agents, icon: Monitor, color: 'text-blue-500' },
{ label: 'Active', value: counts.active_agents, icon: Activity, color: 'text-green-500' },
{ label: 'Infected', value: counts.infected, icon: AlertTriangle, color: 'text-red-500' },
{ label: 'Threats', value: counts.threats, icon: XCircle, color: 'text-orange-500' },
].map(({ label, value, icon: Icon, color }) => (
<Card key={label}>
<CardHeader className="pb-2">
<CardTitle className="text-xs text-muted-foreground flex items-center gap-1">
<Icon className={`w-3 h-3 ${color}`} />{label}
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{loading ? '—' : (Number(value ?? 0)).toLocaleString()}
</div>
</CardContent>
</Card>
))}
</div>
{/* Last sync status */}
{lastSync && (
<Card>
<CardHeader>
<CardTitle className="text-sm">Last Sync</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex items-center gap-3">
{lastSync.status === 'completed'
? <CheckCircle2 className="w-5 h-5 text-green-500" />
: lastSync.status === 'running'
? <RefreshCw className="w-5 h-5 text-blue-500 animate-spin" />
: <XCircle className="w-5 h-5 text-red-500" />}
<div>
<div className="font-medium capitalize">{lastSync.status}</div>
<div className="text-xs text-muted-foreground">
{fmtDate(lastSync.completed_at || lastSync.started_at)} · {fmtDuration(lastSync.duration_ms)} · {lastSync.total_upserted?.toLocaleString()} records
</div>
</div>
</div>
{lastSync.entity_results && (
<div className="grid grid-cols-3 gap-2">
{(Array.isArray(lastSync.entity_results)
? lastSync.entity_results
: JSON.parse(lastSync.entity_results)
).map((e: any) => (
<div key={e.entity} className="flex items-center justify-between text-sm border rounded p-2">
<span className="capitalize">{e.entity}</span>
<div className="flex items-center gap-1">
{e.success
? <Badge variant="secondary">{e.recordsUpserted.toLocaleString()}</Badge>
: <Badge variant="destructive">failed</Badge>}
</div>
</div>
))}
</div>
)}
{lastSync.error_message && (
<div className="text-xs text-red-500 bg-red-500/10 rounded p-2">{lastSync.error_message}</div>
)}
</CardContent>
</Card>
)}
{/* History */}
<Card>
<CardHeader><CardTitle className="text-sm">Sync History</CardTitle></CardHeader>
<CardContent>
<div className="space-y-2">
{(data?.history ?? []).map((h: any) => (
<div key={h.id} className="flex items-center justify-between text-sm border-b pb-2 last:border-0">
<div className="flex items-center gap-2">
{h.status === 'completed' ? <CheckCircle2 className="w-4 h-4 text-green-500" />
: h.status === 'running' ? <RefreshCw className="w-4 h-4 text-blue-500 animate-spin" />
: <XCircle className="w-4 h-4 text-red-500" />}
<span className="text-muted-foreground">{fmtDate(h.started_at)}</span>
</div>
<div className="flex items-center gap-3 text-muted-foreground">
<span>{h.total_upserted?.toLocaleString() ?? 0} records</span>
<span>{fmtDuration(h.duration_ms)}</span>
<Badge variant="outline" className="text-xs">{h.triggered_by}</Badge>
</div>
</div>
))}
{!loading && (data?.history ?? []).length === 0 && (
<p className="text-sm text-muted-foreground text-center py-4">No sync history yet run a sync to get started</p>
)}
</div>
</CardContent>
</Card>
<div className="flex gap-3">
<Link href="/sentinelone/coverage">
<Button variant="outline"><Shield className="w-4 h-4 mr-2" />Coverage Report</Button>
</Link>
<Link href="/sentinelone/mappings">
<Button variant="outline"><Monitor className="w-4 h-4 mr-2" />Company Mappings</Button>
</Link>
</div>
</div>
);
}