-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring-security.lua
More file actions
460 lines (392 loc) · 14.4 KB
/
Copy pathmonitoring-security.lua
File metadata and controls
460 lines (392 loc) · 14.4 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
448
449
450
451
452
453
454
455
456
457
458
459
460
-- Profile: research (recommended)
-- Run with: llmspell -p research run monitoring-security.lua
-- Full stack with trace logging for complex patterns
-- ============================================================
-- LLMSPELL ADVANCED PATTERNS SHOWCASE
-- ============================================================
-- Phase: 13c.5.6 - Example Header Standardization
-- Category: advanced-patterns
-- Pattern ID: 04 - Monitoring & Security Patterns v0.7.0
-- Complexity: ADVANCED
-- Real-World Use Case: Production system monitoring and security
-- Pattern Category: Operations & Security
--
-- Purpose: Demonstrates production monitoring and security patterns
-- Architecture: Agent-based monitoring, security controls, audit logging
-- Key Capabilities:
-- • System health monitoring with agents
-- • File system security and sandboxing
-- • Process execution controls
-- • Anomaly detection patterns
-- • Security audit logging
-- • Threshold-based alerting
-- • Encryption and hashing operations
-- • Access control patterns
--
-- Prerequisites:
-- • Optional: API keys for agent-based monitoring
-- • Understanding of security principles
--
-- HOW TO RUN:
-- ./target/debug/llmspell run examples/script-users/advanced-patterns/monitoring-security.lua
--
-- EXPECTED OUTPUT:
-- Monitoring and security patterns with audit trails
-- Execution time: 2-5 seconds (with optional API calls)
--
-- Time to Complete: 5 seconds
-- Next Steps: See cookbook/webapp-creator.lua for production application
-- ============================================================
print("=== Monitoring & Security Patterns ===\n")
-- Helper for safe tool execution
local function use_tool(tool_name, params)
local success, result = pcall(function()
return Tool.execute(tool_name, params)
end)
if success and result then
return result
elseif success then
return {success = false, error = "Tool returned no result"}
else
return {success = false, error = tostring(result)}
end
end
-- Helper for security test results
local function print_security_test(label, result, should_fail)
if result.error or result.success == false then
if should_fail then
print(" ✓ " .. label .. ": Correctly blocked")
else
print(" ✗ " .. label .. ": Unexpected failure")
end
else
if should_fail then
print(" ✗ " .. label .. ": SECURITY FAILURE - should have been blocked")
else
print(" ✓ " .. label .. ": Allowed as expected")
end
end
end
-- 1. SYSTEM MONITORING PATTERN
print("1. System Health Monitoring")
print("-" .. string.rep("-", 27))
-- Create monitoring agent if API key available
local monitor_agent = nil
local success, agent = pcall(function()
return Agent.builder()
:name("system_monitor")
:model("openai/gpt-3.5-turbo")
:temperature(0.2)
:max_tokens(200)
:system_prompt([[You are a system monitor. Analyze metrics and identify:
1. Health status (Good/Warning/Critical)
2. Potential issues
3. Optimization recommendations
Be concise and actionable.]])
:build()
end)
if success and agent then
monitor_agent = agent
print(" ✓ Monitor agent created")
else
print(" ⚠ Monitor agent unavailable (no API key)")
end
-- Simulate system metrics
local metrics = {
cpu_usage = math.random(10, 90),
memory_usage = math.random(40, 85),
disk_usage = math.random(30, 80),
active_connections = math.random(10, 100),
error_rate = math.random(0, 5) / 100
}
print(string.format(" Metrics: CPU=%d%% MEM=%d%% DISK=%d%%",
metrics.cpu_usage, metrics.memory_usage, metrics.disk_usage))
-- Threshold-based alerting
if metrics.cpu_usage > 80 then
print(" 🚨 ALERT: High CPU usage detected!")
end
if metrics.memory_usage > 75 then
print(" ⚠️ WARNING: Memory usage approaching limit")
end
if metrics.error_rate > 0.02 then
print(" 🚨 ALERT: Error rate above threshold")
end
-- Agent-based analysis if available
if monitor_agent then
local analysis = monitor_agent:execute({
text = string.format("Analyze: CPU=%d%%, Memory=%d%%, Disk=%d%%, Errors=%.2f%%",
metrics.cpu_usage, metrics.memory_usage, metrics.disk_usage, metrics.error_rate * 100)
})
if analysis and analysis.text then
print(" Agent analysis: " .. analysis.text:sub(1, 100) .. "...")
end
end
-- 2. FILE SYSTEM SECURITY
print("\n2. File System Security Controls")
print("-" .. string.rep("-", 31))
print(" Testing path traversal prevention...")
-- Test 1: Path traversal attempt
local traversal = use_tool("file_operations", {
operation = "read",
path = "/tmp/../../../etc/passwd"
})
print_security_test("Path traversal", traversal, true)
-- Test 2: Restricted directory access
local restricted = use_tool("file_operations", {
operation = "list_dir",
path = "/etc"
})
print_security_test("Access /etc", restricted, true)
-- Test 3: Safe sandbox operation
local safe_write = use_tool("file_operations", {
operation = "write",
path = "/tmp/security_test.txt",
input = "Safe content in sandbox"
})
print_security_test("Write to /tmp", safe_write, false)
-- Test 4: System directory write attempt
local system_write = use_tool("file_operations", {
operation = "write",
path = "/usr/bin/malicious",
input = "malicious content"
})
print_security_test("Write to /usr/bin", system_write, true)
-- 3. PROCESS EXECUTION SECURITY
print("\n3. Process Execution Controls")
print("-" .. string.rep("-", 29))
print(" Testing command whitelisting...")
-- Test 1: Whitelisted command (echo)
local safe_cmd = use_tool("process_executor", {
executable = "echo",
arguments = {"Security test"}
})
print_security_test("Echo command", safe_cmd, false)
-- Test 2: Dangerous command (rm)
local dangerous_cmd = use_tool("process_executor", {
executable = "rm",
arguments = {"-rf", "/"},
timeout_ms = 1000
})
print_security_test("rm -rf command", dangerous_cmd, true)
-- Test 3: Network command (curl)
local network_cmd = use_tool("process_executor", {
executable = "curl",
arguments = {"http://malicious.site"},
timeout_ms = 1000
})
print_security_test("curl command", network_cmd, true)
-- Test 4: Script execution (sh)
local script_cmd = use_tool("process_executor", {
executable = "sh",
arguments = {"-c", "echo 'arbitrary code'"},
timeout_ms = 1000
})
print_security_test("Shell script", script_cmd, true)
-- 4. ANOMALY DETECTION PATTERN
print("\n4. Anomaly Detection Pattern")
print("-" .. string.rep("-", 28))
-- Track file changes for anomaly detection
local baseline_files = {}
-- Create baseline
print(" Creating baseline...")
for i = 1, 3 do
local filename = "/tmp/monitor_" .. i .. ".txt"
use_tool("file_operations", {
operation = "write",
path = filename,
input = "Baseline content " .. i
})
-- Hash file content for integrity checking
local hash_result = use_tool("hash_calculator", {
operation = "hash",
algorithm = "sha256",
input = "Baseline content " .. i
})
if hash_result and hash_result.result and hash_result.result.hash then
baseline_files[filename] = hash_result.result.hash
print(" Baseline: " .. filename .. " -> " .. hash_result.result.hash:sub(1, 16) .. "...")
end
end
-- Simulate file modification (anomaly)
print(" Simulating file modification...")
use_tool("file_operations", {
operation = "write",
path = "/tmp/monitor_2.txt",
input = "Modified content - ANOMALY!"
})
-- Check for anomalies
print(" Checking for anomalies...")
for filename, original_hash in pairs(baseline_files) do
local read_result = use_tool("file_operations", {
operation = "read",
path = filename
})
if read_result and read_result.result and read_result.result.text then
local new_hash_result = use_tool("hash_calculator", {
operation = "hash",
algorithm = "sha256",
input = read_result.result.text
})
if new_hash_result and new_hash_result.result and new_hash_result.result.hash then
if new_hash_result.result.hash ~= original_hash then
print(" 🚨 ANOMALY: " .. filename .. " has been modified!")
else
print(" ✓ " .. filename .. " unchanged")
end
end
end
end
-- 5. AUDIT LOGGING PATTERN
print("\n5. Security Audit Logging")
print("-" .. string.rep("-", 24))
-- Create audit log
local audit_log = {}
local function log_security_event(event_type, details, severity)
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
local entry = {
timestamp = timestamp,
event_type = event_type,
severity = severity,
details = details
}
table.insert(audit_log, entry)
-- Also write to file for persistence
local log_entry = string.format("[%s] %s: %s - %s\n",
timestamp, severity, event_type, details)
use_tool("file_operations", {
operation = "append",
path = "/tmp/security_audit.log",
input = log_entry
})
-- Display based on severity
if severity == "CRITICAL" then
print(" 🚨 " .. event_type .. ": " .. details)
elseif severity == "WARNING" then
print(" ⚠️ " .. event_type .. ": " .. details)
else
print(" ℹ️ " .. event_type .. ": " .. details)
end
end
-- Log various security events
log_security_event("ACCESS_ATTEMPT", "Unauthorized /etc access blocked", "WARNING")
log_security_event("FILE_MODIFIED", "/tmp/monitor_2.txt changed", "WARNING")
log_security_event("PROCESS_BLOCKED", "rm command execution denied", "CRITICAL")
log_security_event("LOGIN_SUCCESS", "User authenticated successfully", "INFO")
-- 6. ENCRYPTION PATTERN
print("\n6. Data Encryption Pattern")
print("-" .. string.rep("-", 25))
-- Sensitive data to protect
local sensitive_data = "SSN: 123-45-6789, Credit Card: 4111-1111-1111-1111"
print(" Original: " .. sensitive_data:sub(1, 30) .. "...")
-- Hash sensitive data (one-way)
local hash_result = use_tool("hash_calculator", {
operation = "hash",
algorithm = "sha256",
input = sensitive_data
})
if hash_result and hash_result.result and hash_result.result.hash then
print(" SHA256: " .. hash_result.result.hash:sub(1, 32) .. "...")
end
-- Base64 encode (not encryption, but obfuscation)
local encode_result = use_tool("base64_encoder", {
operation = "encode",
input = sensitive_data
})
if encode_result and encode_result.result and encode_result.result.encoded then
print(" Encoded: " .. encode_result.result.encoded:sub(1, 32) .. "...")
-- Decode to verify
local decode_result = use_tool("base64_encoder", {
operation = "decode",
input = encode_result.result.encoded
})
if decode_result and decode_result.result and decode_result.result.decoded then
print(" ✓ Decode verified")
end
end
-- 7. RATE LIMITING FOR SECURITY
print("\n7. Rate Limiting Security")
print("-" .. string.rep("-", 24))
print(" Implementing login rate limiting...")
-- Simulate login attempts
local login_attempts = 0
local blocked_until = 0
for i = 1, 8 do
local current_time = os.time()
if current_time < blocked_until then
print(" Attempt " .. i .. ": ✗ Blocked (cooldown)")
else
login_attempts = login_attempts + 1
if login_attempts > 3 then
blocked_until = current_time + 30 -- 30 second cooldown
print(" Attempt " .. i .. ": ✗ Too many attempts - blocking for 30s")
login_attempts = 0
else
print(" Attempt " .. i .. ": ✓ Login attempt allowed")
end
end
end
-- 8. ENVIRONMENT SECURITY
print("\n8. Environment Variable Security")
print("-" .. string.rep("-", 31))
print(" Checking for sensitive variables...")
-- Check for sensitive environment variables
local sensitive_vars = {"API_KEY", "PASSWORD", "SECRET", "TOKEN", "PRIVATE"}
for _, var_pattern in ipairs(sensitive_vars) do
local env_result = use_tool("environment_reader", {
operation = "list",
pattern = var_pattern .. "*"
})
if env_result and env_result.result then
-- In production, would mask or redact these
print(" ⚠️ Found sensitive pattern: " .. var_pattern .. "*")
end
end
-- Safe environment variables
local safe_result = use_tool("environment_reader", {
operation = "get",
variable_name = "PATH"
})
if safe_result and safe_result.result and safe_result.result.value then
print(" ✓ PATH variable accessible (safe)")
end
-- 9. SECURITY METRICS
print("\n9. Security Metrics Dashboard")
print("-" .. string.rep("-", 28))
local security_metrics = {
blocked_attempts = 5,
successful_auth = 12,
file_violations = 3,
process_blocks = 4,
anomalies_detected = 1,
audit_entries = #audit_log
}
print(" Security Dashboard:")
print(" • Blocked attempts: " .. security_metrics.blocked_attempts)
print(" • Successful auth: " .. security_metrics.successful_auth)
print(" • File violations: " .. security_metrics.file_violations)
print(" • Process blocks: " .. security_metrics.process_blocks)
print(" • Anomalies: " .. security_metrics.anomalies_detected)
print(" • Audit entries: " .. security_metrics.audit_entries)
-- Calculate security score
local security_score = 100
security_score = security_score - (security_metrics.file_violations * 5)
security_score = security_score - (security_metrics.anomalies_detected * 10)
security_score = math.max(0, security_score)
print(string.format(" Overall Security Score: %d/100", security_score))
-- 10. BEST PRACTICES
print("\n10. Monitoring & Security Best Practices")
print("-" .. string.rep("-", 39))
print(" • Implement defense in depth")
print(" • Use threshold-based alerting")
print(" • Maintain comprehensive audit logs")
print(" • Monitor for anomalies continuously")
print(" • Enforce strict sandboxing")
print(" • Whitelist allowed operations")
print(" • Rate limit sensitive operations")
print(" • Encrypt sensitive data at rest")
print(" • Regular security metric reviews")
print(" • Automate incident response")
print("\n=== Monitoring & Security Patterns Complete ===")
print("Demonstrated: Monitoring, Sandboxing, Auditing, Anomaly Detection, Encryption")
print("Security Score: " .. security_score .. "/100")
print("Next: See cookbook examples for production applications")