Repository navigation
Expand file tree
/
Copy pathaudit-violation.cjs
More file actions
303 lines (265 loc) · 5.51 KB
/
Copy pathaudit-violation.cjs
File metadata and controls
303 lines (265 loc) · 5.51 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
/**
* AuditViolation Builder (T006)
* Creates and manages governance audit violations
* Provides violation metadata, remediation guidance, and affected system tracking
*/
const { randomUUID } = require('crypto');
class AuditViolation {
constructor(options = {}) {
this.id = this._generateId(options);
this.ruleId = options.ruleId || null;
this.ruleName = options.ruleName || null;
this.severity = options.severity || 'MEDIUM';
this.file = options.file || null;
this.line = options.line || null;
this.column = options.column || null;
this.message = options.message || null;
this.remediation = options.remediation || null;
this.affectedSystems = options.affectedSystems || [];
this.context = options.context || null;
this.timestamp = new Date().toISOString();
this.metadata = options.metadata || {};
}
/**
* Generate unique violation ID
*/
_generateId(options) {
const ruleId = options.ruleId || 'unknown';
return `${ruleId}-${randomUUID()}`;
}
/**
* Set location information
*/
setLocation(file, line = null, column = null) {
this.file = file;
if (line !== null) this.line = line;
if (column !== null) this.column = column;
return this;
}
/**
* Set violation message
*/
setMessage(message) {
this.message = message;
return this;
}
/**
* Set remediation guidance
*/
setRemediation(remediation) {
this.remediation = remediation;
return this;
}
/**
* Add affected system
*/
addAffectedSystem(system) {
if (!this.affectedSystems.includes(system)) {
this.affectedSystems.push(system);
}
return this;
}
/**
* Add multiple affected systems
*/
addAffectedSystems(systems) {
for (const system of systems) {
this.addAffectedSystem(system);
}
return this;
}
/**
* Set context (additional details)
*/
setContext(context) {
this.context = context;
return this;
}
/**
* Add metadata
*/
addMetadata(key, value) {
this.metadata[key] = value;
return this;
}
/**
* Get location string
*/
getLocationString() {
let location = this.file || 'unknown';
if (this.line) {
location += `:${this.line}`;
if (this.column) {
location += `:${this.column}`;
}
}
return location;
}
/**
* Check if violation is critical
*/
isCritical() {
return this.severity === 'CRITICAL';
}
/**
* Check if violation is high priority
*/
isHighPriority() {
return ['CRITICAL', 'HIGH'].includes(this.severity);
}
/**
* Serialize to JSON
*/
toJSON() {
return {
id: this.id,
ruleId: this.ruleId,
ruleName: this.ruleName,
severity: this.severity,
location: this.getLocationString(),
file: this.file,
line: this.line,
column: this.column,
message: this.message,
remediation: this.remediation,
affectedSystems: this.affectedSystems,
context: this.context,
timestamp: this.timestamp,
metadata: this.metadata,
};
}
/**
* Serialize to Markdown
*/
toMarkdown() {
let md = `### ${this.ruleName || 'Unnamed Rule'}\n\n`;
md += `**Severity**: ${this.severity}\n\n`;
md += `**Location**: ${this.getLocationString()}\n\n`;
md += `**Message**: ${this.message || 'No message provided'}\n\n`;
if (this.remediation) {
md += `**Remediation**: ${this.remediation}\n\n`;
}
if (this.affectedSystems.length > 0) {
md += `**Affected Systems**: ${this.affectedSystems.join(', ')}\n\n`;
}
if (this.context) {
md += `**Context**: ${this.context}\n\n`;
}
return md;
}
}
/**
* Create a violation builder
*/
function createViolation(ruleId, ruleName, severity, options = {}) {
return new AuditViolation({
ruleId,
ruleName,
severity,
...options,
});
}
/**
* Builder pattern for creating violations
*/
class ViolationBuilder {
constructor() {
this.violations = [];
}
/**
* Start building a new violation
*/
add(ruleId, ruleName, severity) {
const violation = new AuditViolation({
ruleId,
ruleName,
severity,
});
return new ViolationBuilderStep(violation, this);
}
/**
* Get all violations
*/
build() {
return this.violations;
}
/**
* Clear all violations
*/
clear() {
this.violations = [];
return this;
}
/**
* Get violations by severity
*/
getBySeverity(severity) {
return this.violations.filter((v) => v.severity === severity);
}
/**
* Get violations by file
*/
getByFile(file) {
return this.violations.filter((v) => v.file === file);
}
/**
* Get total violation count
*/
count() {
return this.violations.length;
}
/**
* Get critical violation count
*/
criticalCount() {
return this.violations.filter((v) => v.isCritical()).length;
}
/**
* Get high priority violation count
*/
highPriorityCount() {
return this.violations.filter((v) => v.isHighPriority()).length;
}
}
/**
* Helper for fluent violation building
*/
class ViolationBuilderStep {
constructor(violation, parent) {
this.violation = violation;
this.parent = parent;
}
location(file, line, column) {
this.violation.setLocation(file, line, column);
return this;
}
message(message) {
this.violation.setMessage(message);
return this;
}
remediation(remediation) {
this.violation.setRemediation(remediation);
return this;
}
affectedSystems(systems) {
this.violation.addAffectedSystems(systems);
return this;
}
context(context) {
this.violation.setContext(context);
return this;
}
metadata(key, value) {
this.violation.addMetadata(key, value);
return this;
}
end() {
this.parent.violations.push(this.violation);
return this.parent;
}
}
module.exports = {
AuditViolation,
ViolationBuilder,
createViolation,
};