-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauditService.js
More file actions
195 lines (162 loc) Β· 4.27 KB
/
auditService.js
File metadata and controls
195 lines (162 loc) Β· 4.27 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
const { supabase } = require("./db");
// π find single record
async function findByRequestId(requestId) {
const { data, error } = await supabase
.from("audit_logs")
.select("*")
.eq("request_id", requestId)
.single();
if (error) {
console.error("AUDIT READ ERROR:", {
requestId,
error: error.message
});
return null;
}
return data;
}
// π get logs with RANGE FILTER (DETERMINISTIC READY)
async function getAll(range = "7d", now = new Date()) {
let fromDate = new Date(now);
if (range === "1d") {
fromDate.setDate(fromDate.getDate() - 1);
} else if (range === "7d") {
fromDate.setDate(fromDate.getDate() - 7);
} else if (range === "30d") {
fromDate.setDate(fromDate.getDate() - 30);
} else if (range === "365d") {
fromDate.setDate(fromDate.getDate() - 365);
} else if (range === "5y") {
fromDate.setDate(fromDate.getDate() - (365 * 5));
} else if (range === "all") {
fromDate = new Date(0);
}
const { data, error } = await supabase
.from("audit_logs")
.select("*")
.gte("timestamp", fromDate.toISOString())
.order("timestamp", { ascending: false });
if (error) {
console.error("AUDIT READ ERROR:", error.message);
return [];
}
return data || [];
}
// π STRICT SUMMARY (LOCKED CONTRACT)
async function getAuditSummary(auditLogs) {
const summary = {
total_requests: 0,
status_breakdown: {
ALLOW: 0,
BLOCK: 0,
REQUIRE_OVERRIDE: 0
},
rule_stats: {}
};
const VALID = ["ALLOW", "BLOCK", "REQUIRE_OVERRIDE"];
for (const log of auditLogs) {
const status = log.decision?.status;
if (VALID.includes(status)) {
summary.status_breakdown[status]++;
summary.total_requests++;
}
const trace = log.decision?.meta?.trace || [];
for (const t of trace) {
const ruleId = t.rule_id || "UNKNOWN_RULE"; // β
FIX
if (!summary.rule_stats[ruleId]) {
summary.rule_stats[ruleId] = {
violations: 0,
passes: 0,
skipped: 0
};
}
if (t.result === "VIOLATION") {
summary.rule_stats[ruleId].violations++;
} else if (t.result === "PASSED") {
summary.rule_stats[ruleId].passes++;
} else {
summary.rule_stats[ruleId].skipped++;
}
}
}
return summary;
}
// π TREND CALCULATION
function calculateTrend(current, previous) {
return {
ALLOW: current.ALLOW - previous.ALLOW,
BLOCK: current.BLOCK - previous.BLOCK,
REQUIRE_OVERRIDE:
current.REQUIRE_OVERRIDE - previous.REQUIRE_OVERRIDE
};
}
// π SUMMARY WITH TREND (DETERMINISTIC READY)
async function getAuditSummaryWithTrend(range = "7d", now = new Date()) {
const mapDays = {
"1d": 1,
"7d": 7,
"30d": 30,
"365d": 365,
"5y": 365 * 5
};
let currentFrom = new Date(now);
let previousFrom = new Date(now);
if (range === "all") {
currentFrom = new Date(0);
previousFrom = new Date(0);
} else {
const days = mapDays[range] || 7;
currentFrom.setDate(currentFrom.getDate() - days);
previousFrom.setDate(previousFrom.getDate() - (days * 2));
}
const { data, error } = await supabase
.from("audit_logs")
.select("*")
.gte("timestamp", previousFrom.toISOString())
.order("timestamp", { ascending: false });
if (error) {
console.error("AUDIT READ ERROR:", error.message);
return {
total_requests: 0,
status_breakdown: {
ALLOW: 0,
BLOCK: 0,
REQUIRE_OVERRIDE: 0
},
rule_stats: {},
trend: {
ALLOW: 0,
BLOCK: 0,
REQUIRE_OVERRIDE: 0
}
};
}
const currentLogs = [];
const previousLogs = [];
for (const log of data || []) {
const ts = new Date(log.timestamp);
// β
FIX: invalid timestamp guard
if (isNaN(ts)) continue;
if (ts >= currentFrom) {
currentLogs.push(log);
} else {
previousLogs.push(log);
}
}
const currentSummary = await getAuditSummary(currentLogs);
const previousSummary = await getAuditSummary(previousLogs);
const trend = calculateTrend(
currentSummary.status_breakdown,
previousSummary.status_breakdown
);
return {
...currentSummary,
trend
};
}
module.exports = {
findByRequestId,
getAll,
getAuditSummary,
getAuditSummaryWithTrend
};