-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdeploy-ai-soc.ps1
More file actions
447 lines (391 loc) · 15.9 KB
/
deploy-ai-soc.ps1
File metadata and controls
447 lines (391 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# =============================================================================
# AI-SOC Master Deployment Script (Windows PowerShell)
# =============================================================================
# Single-command deploy for the entire AI-SOC stack.
#
# Usage:
# .\deploy-ai-soc.ps1 Deploy all services
# .\deploy-ai-soc.ps1 -Stop Tear down all services
# .\deploy-ai-soc.ps1 -Status Show service status
#
# Phases:
# 1. SIEM Core (Wazuh indexer, manager, dashboard)
# 2. AI Services (Ollama, ML Inference, Alert Triage, RAG, Wazuh Integration)
# 3. Monitoring (Prometheus, Grafana, Alertmanager)
# =============================================================================
[CmdletBinding()]
param(
[switch]$Stop,
[switch]$Status,
[switch]$Help
)
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
$ScriptDir = $PSScriptRoot
$ComposeDir = Join-Path $ScriptDir "docker-compose"
$ScriptsDir = Join-Path $ScriptDir "scripts"
$SiemCompose = Join-Path $ComposeDir "phase1-siem-core-windows.yml"
$AiCompose = Join-Path $ComposeDir "ai-services.yml"
$MonCompose = Join-Path $ComposeDir "monitoring-stack.yml"
# ---------------------------------------------------------------------------
# Colour helpers
# ---------------------------------------------------------------------------
function Write-Log { param($Msg) Write-Host "[AI-SOC] $Msg" -ForegroundColor Cyan }
function Write-Ok { param($Msg) Write-Host "[ OK ] $Msg" -ForegroundColor Green }
function Write-Warn { param($Msg) Write-Host "[ WARN ] $Msg" -ForegroundColor Yellow }
function Write-Err { param($Msg) Write-Host "[ERROR ] $Msg" -ForegroundColor Red }
function Write-Banner { param($Msg) Write-Host "`n=== $Msg ===`n" -ForegroundColor Blue }
# ---------------------------------------------------------------------------
# Help
# ---------------------------------------------------------------------------
if ($Help) {
Write-Host "Usage: .\deploy-ai-soc.ps1 [-Stop] [-Status] [-Help]"
Write-Host " (no flags) Deploy full AI-SOC stack"
Write-Host " -Stop Tear down all services"
Write-Host " -Status Show running containers"
exit 0
}
# ---------------------------------------------------------------------------
# Tear down
# ---------------------------------------------------------------------------
function Invoke-Teardown {
Write-Banner "Stopping AI-SOC"
Write-Log "Stopping monitoring stack..."
docker compose -f $MonCompose down 2>$null
Write-Log "Stopping AI services..."
docker compose -f $AiCompose down 2>$null
Write-Log "Stopping SIEM core..."
docker compose -f $SiemCompose down 2>$null
Write-Ok "All services stopped."
}
# ---------------------------------------------------------------------------
# Status
# ---------------------------------------------------------------------------
function Show-Status {
Write-Banner "AI-SOC Service Status"
docker ps --format "table {{.Names}}`t{{.Status}}`t{{.Ports}}"
}
# ---------------------------------------------------------------------------
# Prerequisite checks
# ---------------------------------------------------------------------------
function Test-Prerequisites {
Write-Banner "Checking Prerequisites"
# Docker
try {
$dockerVer = (docker --version) -replace "Docker version ", ""
Write-Ok "Docker: $dockerVer"
} catch {
Write-Err "Docker is not installed. Install from https://docs.docker.com/desktop/windows/"
exit 1
}
# Docker Compose v2
try {
$composeVer = docker compose version --short 2>$null
Write-Ok "Docker Compose: $composeVer"
} catch {
Write-Err "Docker Compose v2 not available. Update Docker Desktop."
exit 1
}
# Docker daemon
try {
docker info 2>$null | Out-Null
Write-Ok "Docker daemon: running"
} catch {
Write-Err "Docker daemon is not running. Start Docker Desktop and retry."
exit 1
}
# Disk space (warn if <20 GB)
$drive = (Get-PSDrive -Name ($ScriptDir.Substring(0,1)))[0]
if ($drive) {
$freeGb = [math]::Round($drive.Free / 1GB, 1)
if ($freeGb -lt 20) {
Write-Warn "Low disk space: ${freeGb}GB free. Recommend at least 20GB."
} else {
Write-Ok "Disk space: ${freeGb}GB free"
}
}
# Memory (warn if <8 GB)
$totalMem = (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory
$totalGb = [math]::Round($totalMem / 1GB, 1)
if ($totalGb -lt 8) {
Write-Warn "Low memory: ${totalGb}GB detected. Recommend at least 8GB."
} else {
Write-Ok "Memory: ${totalGb}GB"
}
}
# ---------------------------------------------------------------------------
# SSL certificates
# ---------------------------------------------------------------------------
function Ensure-Certs {
Write-Banner "SSL Certificates"
$caCert = Join-Path $ScriptDir "config\root-ca\root-ca.pem"
if (Test-Path $caCert) {
Write-Ok "SSL certificates already exist, skipping generation."
return
}
$certScript = Join-Path $ScriptsDir "generate-certs.ps1"
if (Test-Path $certScript) {
Write-Log "Generating SSL certificates..."
& $certScript
Write-Ok "SSL certificates generated."
} else {
Write-Warn "generate-certs.ps1 not found. Skipping certificate generation."
Write-Warn "Wazuh TLS may fail without certificates."
}
}
# ---------------------------------------------------------------------------
# .env file
# ---------------------------------------------------------------------------
function Ensure-Env {
Write-Banner "Environment Configuration"
$envFile = Join-Path $ScriptDir ".env"
$exampleFile = Join-Path $ScriptDir ".env.example"
if (Test-Path $envFile) {
Write-Ok ".env file exists."
} elseif (Test-Path $exampleFile) {
Write-Log "Creating .env from .env.example..."
Copy-Item $exampleFile $envFile
Write-Ok ".env created. Review and update credentials if needed."
} else {
Write-Warn ".env.example not found. Creating minimal .env..."
@"
# Auto-generated by deploy-ai-soc.ps1
# Update passwords before production use
INDEXER_USERNAME=admin
INDEXER_PASSWORD=SecurePassword1!
API_PASSWORD=SecurePassword1!
WAZUH_API_PASSWORD=SecurePassword1!
KIBANA_PASSWORD=SecurePassword1!
"@ | Set-Content $envFile
Write-Ok "Minimal .env created."
}
}
# ---------------------------------------------------------------------------
# Wait for container health
# ---------------------------------------------------------------------------
function Wait-ForHealthy {
param(
[string]$ContainerName,
[int]$MaxWaitSecs = 120,
[int]$IntervalSecs = 10
)
Write-Log "Waiting for $ContainerName to become healthy (max ${MaxWaitSecs}s)..."
$elapsed = 0
while ($elapsed -lt $MaxWaitSecs) {
$state = ""
try {
$state = (docker inspect --format="{{.State.Health.Status}}" $ContainerName 2>$null).Trim()
} catch { }
switch ($state) {
"healthy" {
Write-Ok "$ContainerName is healthy."
return $true
}
"unhealthy" {
Write-Warn "$ContainerName is unhealthy after ${elapsed}s."
return $false
}
"starting" {
Write-Log " $ContainerName is starting... (${elapsed}s)"
}
}
Start-Sleep -Seconds $IntervalSecs
$elapsed += $IntervalSecs
}
Write-Warn "$ContainerName did not become healthy within ${MaxWaitSecs}s. Continuing anyway."
return $true
}
# ---------------------------------------------------------------------------
# Phase 1: SIEM Core
# ---------------------------------------------------------------------------
function Deploy-Siem {
Write-Banner "Phase 1: SIEM Core"
if (-not (Test-Path $SiemCompose)) {
Write-Warn "SIEM compose file not found: $SiemCompose"
Write-Warn "Skipping SIEM phase."
return
}
Write-Log "Starting SIEM core services..."
docker compose -f $SiemCompose up -d --remove-orphans
Wait-ForHealthy -ContainerName "wazuh-indexer" -MaxWaitSecs 180
Write-Ok "SIEM core started."
}
# ---------------------------------------------------------------------------
# Phase 2: AI Services
# ---------------------------------------------------------------------------
function Deploy-AIServices {
Write-Banner "Phase 2: AI Services"
if (-not (Test-Path $AiCompose)) {
Write-Err "AI services compose file not found: $AiCompose"
exit 1
}
Write-Log "Building AI service images..."
docker compose -f $AiCompose build --parallel
Write-Log "Starting AI services..."
docker compose -f $AiCompose up -d --remove-orphans
Wait-ForHealthy -ContainerName "ollama" -MaxWaitSecs 120
Pull-OllamaModel
foreach ($svc in @("chromadb", "ml-inference", "alert-triage", "rag-service")) {
Wait-ForHealthy -ContainerName $svc -MaxWaitSecs 120
}
Write-Ok "AI services started."
}
# ---------------------------------------------------------------------------
# Pull Ollama model
# ---------------------------------------------------------------------------
function Pull-OllamaModel {
$model = "llama3.2:3b"
Write-Log "Pulling Ollama model: $model ..."
$existingModels = docker exec ollama ollama list 2>$null
if ($existingModels -match "llama3.2") {
Write-Ok "Ollama model $model already present."
return
}
try {
docker exec ollama ollama pull $model
Write-Ok "Ollama model $model pulled successfully."
} catch {
Write-Warn "Failed to pull Ollama model $model. Alert Triage will use fallback mode."
}
}
# ---------------------------------------------------------------------------
# Phase 3: Monitoring
# ---------------------------------------------------------------------------
function Deploy-Monitoring {
Write-Banner "Phase 3: Monitoring Stack"
if (-not (Test-Path $MonCompose)) {
Write-Warn "Monitoring compose file not found: $MonCompose"
Write-Warn "Skipping monitoring phase."
return
}
Write-Log "Starting monitoring stack..."
docker compose -f $MonCompose up -d --remove-orphans
Wait-ForHealthy -ContainerName "monitoring-prometheus" -MaxWaitSecs 60
Write-Ok "Monitoring stack started."
}
# ---------------------------------------------------------------------------
# Knowledge base ingestion
# ---------------------------------------------------------------------------
function Invoke-KBIngestion {
Write-Banner "Knowledge Base Ingestion"
$ragUrl = "http://localhost:8300"
$maxWait = 60
$elapsed = 0
Write-Log "Waiting for RAG service to be ready..."
while ($elapsed -lt $maxWait) {
try {
$r = Invoke-WebRequest -Uri "$ragUrl/health" -TimeoutSec 3 -UseBasicParsing
if ($r.StatusCode -eq 200) { break }
} catch { }
Start-Sleep -Seconds 5
$elapsed += 5
}
try {
Invoke-RestMethod -Uri "$ragUrl/health" -TimeoutSec 3 -UseBasicParsing | Out-Null
Write-Log "Triggering MITRE ATT&CK ingestion..."
try {
Invoke-RestMethod -Method Post -Uri "$ragUrl/ingest/mitre" -TimeoutSec 10 -UseBasicParsing | Out-Null
Write-Ok "MITRE ATT&CK ingestion started (runs in background)."
} catch {
Write-Warn "MITRE ingestion trigger failed. Retry: Invoke-RestMethod -Method Post -Uri '$ragUrl/ingest/mitre'"
}
Write-Log "Triggering security runbook ingestion..."
try {
Invoke-RestMethod -Method Post -Uri "$ragUrl/ingest/runbooks" -TimeoutSec 10 -UseBasicParsing | Out-Null
Write-Ok "Security runbook ingestion started."
} catch {
Write-Warn "Runbook ingestion trigger failed."
}
} catch {
Write-Warn "RAG service not reachable after ${maxWait}s. Skipping knowledge base ingestion."
Write-Warn "Trigger manually: Invoke-RestMethod -Method Post -Uri 'http://localhost:8300/ingest/mitre'"
}
}
# ---------------------------------------------------------------------------
# Health check
# ---------------------------------------------------------------------------
function Invoke-HealthCheck {
Write-Banner "Health Check Summary"
$endpoints = @{
"ML Inference" = "http://localhost:8500/health"
"Alert Triage" = "http://localhost:8100/health"
"RAG Service" = "http://localhost:8300/health"
"Wazuh Integration"= "http://localhost:8002/health"
"Prometheus" = "http://localhost:9090/-/healthy"
"Grafana" = "http://localhost:3001/api/health"
}
$allOk = $true
foreach ($name in $endpoints.Keys) {
$url = $endpoints[$name]
try {
$r = Invoke-WebRequest -Uri $url -TimeoutSec 5 -UseBasicParsing
if ($r.StatusCode -eq 200) {
Write-Ok "${name}: reachable"
} else {
Write-Warn "${name}: HTTP $($r.StatusCode)"
$allOk = $false
}
} catch {
Write-Warn "${name}: not reachable ($url)"
$allOk = $false
}
}
if ($allOk) {
Write-Ok "All services healthy."
} else {
Write-Warn "Some services not yet reachable. Run '.\deploy-ai-soc.ps1 -Status' to check container states."
}
}
# ---------------------------------------------------------------------------
# Print access URLs
# ---------------------------------------------------------------------------
function Print-AccessUrls {
Write-Banner "Access URLs"
Write-Host ""
Write-Host "AI Services:" -ForegroundColor White
Write-Host " Alert Triage API: http://localhost:8100/docs" -ForegroundColor Cyan
Write-Host " RAG Service API: http://localhost:8300/docs" -ForegroundColor Cyan
Write-Host " ML Inference API: http://localhost:8500/docs" -ForegroundColor Cyan
Write-Host " Wazuh Integration: http://localhost:8002/docs" -ForegroundColor Cyan
Write-Host ""
Write-Host "Monitoring:" -ForegroundColor White
Write-Host " Grafana: http://localhost:3001 (admin/admin)" -ForegroundColor Cyan
Write-Host " Prometheus: http://localhost:9090" -ForegroundColor Cyan
Write-Host " Alertmanager: http://localhost:9093" -ForegroundColor Cyan
Write-Host ""
Write-Host "SIEM:" -ForegroundColor White
Write-Host " Wazuh Dashboard: https://localhost:443 (admin/SecurePassword1!)" -ForegroundColor Cyan
Write-Host ""
Write-Host "Infrastructure:" -ForegroundColor White
Write-Host " Ollama LLM: http://localhost:11434" -ForegroundColor Cyan
Write-Host " ChromaDB: http://localhost:8200" -ForegroundColor Cyan
Write-Host ""
Write-Host "AI-SOC deployment complete." -ForegroundColor Green
Write-Host "Run '.\deploy-ai-soc.ps1 -Stop' to tear down all services." -ForegroundColor Gray
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "=============================================" -ForegroundColor Blue
Write-Host " AI Security Operations Center" -ForegroundColor Blue
Write-Host " Master Deployment Script (Windows)" -ForegroundColor Blue
Write-Host "=============================================" -ForegroundColor Blue
Write-Host ""
if ($Stop) {
Invoke-Teardown
} elseif ($Status) {
Show-Status
} else {
Test-Prerequisites
Ensure-Certs
Ensure-Env
Deploy-Siem
Deploy-AIServices
Deploy-Monitoring
Invoke-KBIngestion
Invoke-HealthCheck
Print-AccessUrls
}