guardrail-compliance 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/dist/audit/emitter.d.ts +97 -0
  2. package/dist/audit/emitter.d.ts.map +1 -0
  3. package/dist/audit/emitter.js +197 -0
  4. package/dist/audit/events.d.ts +304 -0
  5. package/dist/audit/events.d.ts.map +1 -0
  6. package/dist/audit/events.js +267 -0
  7. package/dist/audit/index.d.ts +11 -0
  8. package/dist/audit/index.d.ts.map +1 -0
  9. package/dist/audit/index.js +51 -0
  10. package/dist/audit/storage.d.ts +93 -0
  11. package/dist/audit/storage.d.ts.map +1 -0
  12. package/dist/audit/storage.js +337 -0
  13. package/dist/automation/__tests__/compliance-scheduler.test.d.ts +2 -0
  14. package/dist/automation/__tests__/compliance-scheduler.test.d.ts.map +1 -0
  15. package/dist/automation/__tests__/compliance-scheduler.test.js +140 -0
  16. package/dist/automation/audit-logger.d.ts +129 -0
  17. package/dist/automation/audit-logger.d.ts.map +1 -0
  18. package/dist/automation/audit-logger.js +473 -0
  19. package/dist/automation/compliance-scheduler-fixed.d.ts +1 -0
  20. package/dist/automation/compliance-scheduler-fixed.d.ts.map +1 -0
  21. package/dist/automation/compliance-scheduler-fixed.js +1 -0
  22. package/dist/automation/compliance-scheduler.d.ts +83 -0
  23. package/dist/automation/compliance-scheduler.d.ts.map +1 -0
  24. package/dist/automation/compliance-scheduler.js +414 -0
  25. package/dist/automation/dashboard.d.ts +194 -0
  26. package/dist/automation/dashboard.d.ts.map +1 -0
  27. package/dist/automation/dashboard.js +768 -0
  28. package/dist/automation/email-service.d.ts +69 -0
  29. package/dist/automation/email-service.d.ts.map +1 -0
  30. package/dist/automation/email-service.js +218 -0
  31. package/dist/automation/evidence-collector.d.ts +140 -0
  32. package/dist/automation/evidence-collector.d.ts.map +1 -0
  33. package/dist/automation/evidence-collector.js +682 -0
  34. package/dist/automation/index.d.ts +8 -0
  35. package/dist/automation/index.d.ts.map +1 -0
  36. package/dist/automation/index.js +24 -0
  37. package/dist/automation/pdf-exporter.d.ts +90 -0
  38. package/dist/automation/pdf-exporter.d.ts.map +1 -0
  39. package/dist/automation/pdf-exporter.js +381 -0
  40. package/dist/automation/reporting-engine.d.ts +116 -0
  41. package/dist/automation/reporting-engine.d.ts.map +1 -0
  42. package/dist/automation/reporting-engine.js +329 -0
  43. package/dist/container/index.d.ts +4 -0
  44. package/dist/container/index.d.ts.map +1 -0
  45. package/dist/container/index.js +19 -0
  46. package/dist/container/kubernetes.d.ts +94 -0
  47. package/dist/container/kubernetes.d.ts.map +1 -0
  48. package/dist/container/kubernetes.js +268 -0
  49. package/dist/container/rules.d.ts +27 -0
  50. package/dist/container/rules.d.ts.map +1 -0
  51. package/dist/container/rules.js +216 -0
  52. package/dist/container/scanner.d.ts +50 -0
  53. package/dist/container/scanner.d.ts.map +1 -0
  54. package/dist/container/scanner.js +143 -0
  55. package/dist/frameworks/engine.d.ts +108 -0
  56. package/dist/frameworks/engine.d.ts.map +1 -0
  57. package/dist/frameworks/engine.js +206 -0
  58. package/dist/frameworks/gdpr.d.ts +6 -0
  59. package/dist/frameworks/gdpr.d.ts.map +1 -0
  60. package/dist/frameworks/gdpr.js +198 -0
  61. package/dist/frameworks/hipaa.d.ts +6 -0
  62. package/dist/frameworks/hipaa.d.ts.map +1 -0
  63. package/dist/frameworks/hipaa.js +183 -0
  64. package/dist/frameworks/index.d.ts +8 -0
  65. package/dist/frameworks/index.d.ts.map +1 -0
  66. package/dist/frameworks/index.js +30 -0
  67. package/dist/frameworks/iso27001.d.ts +63 -0
  68. package/dist/frameworks/iso27001.d.ts.map +1 -0
  69. package/dist/frameworks/iso27001.js +331 -0
  70. package/dist/frameworks/nist.d.ts +62 -0
  71. package/dist/frameworks/nist.d.ts.map +1 -0
  72. package/dist/frameworks/nist.js +424 -0
  73. package/dist/frameworks/pci.d.ts +6 -0
  74. package/dist/frameworks/pci.d.ts.map +1 -0
  75. package/dist/frameworks/pci.js +201 -0
  76. package/dist/frameworks/soc2.d.ts +7 -0
  77. package/dist/frameworks/soc2.d.ts.map +1 -0
  78. package/dist/frameworks/soc2.js +248 -0
  79. package/dist/iac/drift-detector.d.ts +64 -0
  80. package/dist/iac/drift-detector.d.ts.map +1 -0
  81. package/dist/iac/drift-detector.js +134 -0
  82. package/dist/iac/index.d.ts +4 -0
  83. package/dist/iac/index.d.ts.map +1 -0
  84. package/dist/iac/index.js +19 -0
  85. package/dist/iac/rules.d.ts +17 -0
  86. package/dist/iac/rules.d.ts.map +1 -0
  87. package/dist/iac/rules.js +385 -0
  88. package/dist/iac/scanner.d.ts +104 -0
  89. package/dist/iac/scanner.d.ts.map +1 -0
  90. package/dist/iac/scanner.js +343 -0
  91. package/dist/index.d.ts +7 -0
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +28 -0
  94. package/dist/pii/data-flow.d.ts +58 -0
  95. package/dist/pii/data-flow.d.ts.map +1 -0
  96. package/dist/pii/data-flow.js +154 -0
  97. package/dist/pii/detector.d.ts +60 -0
  98. package/dist/pii/detector.d.ts.map +1 -0
  99. package/dist/pii/detector.js +267 -0
  100. package/dist/pii/index.d.ts +4 -0
  101. package/dist/pii/index.d.ts.map +1 -0
  102. package/dist/pii/index.js +19 -0
  103. package/dist/pii/patterns.d.ts +36 -0
  104. package/dist/pii/patterns.d.ts.map +1 -0
  105. package/dist/pii/patterns.js +108 -0
  106. package/dist/policy/index.d.ts +5 -0
  107. package/dist/policy/index.d.ts.map +1 -0
  108. package/dist/policy/index.js +20 -0
  109. package/dist/policy/opa-engine.d.ts +121 -0
  110. package/dist/policy/opa-engine.d.ts.map +1 -0
  111. package/dist/policy/opa-engine.js +423 -0
  112. package/package.json +31 -0
  113. package/src/audit/emitter.ts +383 -0
  114. package/src/audit/events.ts +351 -0
  115. package/src/audit/index.ts +35 -0
  116. package/src/audit/storage.ts +394 -0
  117. package/src/automation/__tests__/compliance-scheduler.test.ts +183 -0
  118. package/src/automation/audit-logger.ts +629 -0
  119. package/src/automation/compliance-scheduler-fixed.ts +0 -0
  120. package/src/automation/compliance-scheduler.ts +516 -0
  121. package/src/automation/dashboard.ts +947 -0
  122. package/src/automation/email-service.ts +230 -0
  123. package/src/automation/evidence-collector.ts +866 -0
  124. package/src/automation/index.ts +8 -0
  125. package/src/automation/pdf-exporter.ts +434 -0
  126. package/src/automation/reporting-engine.ts +462 -0
  127. package/src/container/index.ts +3 -0
  128. package/src/container/kubernetes.ts +379 -0
  129. package/src/container/rules.ts +244 -0
  130. package/src/container/scanner.ts +202 -0
  131. package/src/frameworks/engine.ts +298 -0
  132. package/src/frameworks/gdpr.ts +204 -0
  133. package/src/frameworks/hipaa.ts +209 -0
  134. package/src/frameworks/index.ts +23 -0
  135. package/src/frameworks/iso27001.ts +398 -0
  136. package/src/frameworks/nist.ts +518 -0
  137. package/src/frameworks/pci.ts +226 -0
  138. package/src/frameworks/soc2.ts +281 -0
  139. package/src/iac/drift-detector.ts +197 -0
  140. package/src/iac/index.ts +3 -0
  141. package/src/iac/rules.ts +420 -0
  142. package/src/iac/scanner.ts +445 -0
  143. package/src/index.ts +17 -0
  144. package/src/pii/data-flow.ts +216 -0
  145. package/src/pii/detector.ts +327 -0
  146. package/src/pii/index.ts +3 -0
  147. package/src/pii/patterns.ts +128 -0
  148. package/src/policy/index.ts +5 -0
  149. package/src/policy/opa-engine.ts +504 -0
@@ -0,0 +1,383 @@
1
+ /**
2
+ * Audit Trail Emitter
3
+ *
4
+ * Central audit event emission with tier-gating.
5
+ * Usage: audit.emit(eventInput) from CLI, MCP, VS Code, etc.
6
+ */
7
+
8
+ import {
9
+ AuditEvent,
10
+ AuditEventInput,
11
+ AuditSurface,
12
+ AuditActionCategory,
13
+ AuditResult,
14
+ AuditTier,
15
+ AuditEventMetadata,
16
+ createAuditEvent,
17
+ AuditActions,
18
+ } from './events';
19
+ import {
20
+ AuditStorageAdapter,
21
+ createStorageAdapter,
22
+ } from './storage';
23
+
24
+ // Singleton storage instance
25
+ let storageInstance: AuditStorageAdapter | null = null;
26
+
27
+ // Configuration
28
+ interface AuditConfig {
29
+ enabled: boolean;
30
+ tier: AuditTier;
31
+ basePath: string;
32
+ storageType: 'local' | 'server';
33
+ serverUrl?: string;
34
+ serverApiKey?: string;
35
+ actor?: {
36
+ id: string;
37
+ type: 'user' | 'system' | 'ci' | 'api';
38
+ name?: string;
39
+ email?: string;
40
+ };
41
+ }
42
+
43
+ const defaultConfig: AuditConfig = {
44
+ enabled: true,
45
+ tier: 'free',
46
+ basePath: process.cwd(),
47
+ storageType: 'local',
48
+ };
49
+
50
+ let currentConfig: AuditConfig = { ...defaultConfig };
51
+
52
+ /**
53
+ * Configure the audit system
54
+ */
55
+ export function configureAudit(config: Partial<AuditConfig>): void {
56
+ currentConfig = { ...currentConfig, ...config };
57
+
58
+ // Reset storage instance if config changed
59
+ storageInstance = null;
60
+ }
61
+
62
+ /**
63
+ * Get or create storage instance
64
+ */
65
+ function getStorage(): AuditStorageAdapter {
66
+ if (!storageInstance) {
67
+ storageInstance = createStorageAdapter({
68
+ type: currentConfig.storageType,
69
+ basePath: currentConfig.basePath,
70
+ apiUrl: currentConfig.serverUrl,
71
+ apiKey: currentConfig.serverApiKey,
72
+ });
73
+ }
74
+ return storageInstance;
75
+ }
76
+
77
+ /**
78
+ * Check if audit is enabled for the current tier
79
+ */
80
+ function isAuditEnabled(): boolean {
81
+ if (!currentConfig.enabled) return false;
82
+
83
+ // Minimal logging for free/starter (just basic events, no full trail)
84
+ // Full audit trail requires compliance+ tier
85
+ return true; // Always log something, tier controls detail level
86
+ }
87
+
88
+ /**
89
+ * Check if full audit trail is available (Compliance+ tier)
90
+ */
91
+ export function hasFullAuditAccess(): boolean {
92
+ return ['compliance', 'enterprise', 'unlimited'].includes(currentConfig.tier);
93
+ }
94
+
95
+ /**
96
+ * Get current actor from config or environment
97
+ */
98
+ function getCurrentActor(): AuditEvent['actor'] {
99
+ if (currentConfig.actor) {
100
+ return currentConfig.actor;
101
+ }
102
+
103
+ const env = process.env as Record<string, string | undefined>;
104
+
105
+ // Try to detect from environment
106
+ const userId = env['GUARDRAIL_USER_ID'] || env['USER'] || 'anonymous';
107
+ const userName = env['GUARDRAIL_USER_NAME'] || env['USERNAME'];
108
+ const userEmail = env['GUARDRAIL_USER_EMAIL'];
109
+
110
+ // Detect CI environment
111
+ if (env['CI'] || env['GITHUB_ACTIONS'] || env['GITLAB_CI']) {
112
+ return {
113
+ id: env['GITHUB_ACTOR'] || env['GITLAB_USER_LOGIN'] || 'ci-system',
114
+ type: 'ci',
115
+ name: env['GITHUB_ACTOR'] || env['GITLAB_USER_NAME'],
116
+ };
117
+ }
118
+
119
+ return {
120
+ id: userId,
121
+ type: 'user',
122
+ name: userName,
123
+ email: userEmail,
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Emit an audit event
129
+ */
130
+ export async function emit(input: AuditEventInput): Promise<AuditEvent | null> {
131
+ if (!isAuditEnabled()) {
132
+ return null;
133
+ }
134
+
135
+ const storage = getStorage();
136
+ const prevHash = await storage.getLastHash();
137
+
138
+ // Override tier with current config tier (for proper redaction)
139
+ const event = createAuditEvent({
140
+ ...input,
141
+ tier: currentConfig.tier,
142
+ }, prevHash);
143
+
144
+ await storage.append(event);
145
+
146
+ return event;
147
+ }
148
+
149
+ /**
150
+ * Helper to emit with common defaults
151
+ */
152
+ export async function emitAction(
153
+ surface: AuditSurface,
154
+ action: string,
155
+ category: AuditActionCategory,
156
+ target: AuditEvent['target'],
157
+ result: AuditResult,
158
+ metadata?: AuditEventMetadata
159
+ ): Promise<AuditEvent | null> {
160
+ return emit({
161
+ actor: getCurrentActor(),
162
+ surface,
163
+ action,
164
+ category,
165
+ target,
166
+ tier: currentConfig.tier,
167
+ result,
168
+ metadata,
169
+ });
170
+ }
171
+
172
+ // Convenience methods for common CLI actions
173
+
174
+ export async function emitScanStart(
175
+ surface: AuditSurface,
176
+ projectPath: string,
177
+ args?: string[]
178
+ ): Promise<AuditEvent | null> {
179
+ return emitAction(
180
+ surface,
181
+ AuditActions.SCAN_START,
182
+ 'scan',
183
+ { type: 'project', path: projectPath },
184
+ 'success',
185
+ { command: 'scan', args, projectPath }
186
+ );
187
+ }
188
+
189
+ export async function emitScanComplete(
190
+ surface: AuditSurface,
191
+ projectPath: string,
192
+ result: AuditResult,
193
+ metadata?: { score?: number; grade?: string; issueCount?: number; durationMs?: number }
194
+ ): Promise<AuditEvent | null> {
195
+ return emitAction(
196
+ surface,
197
+ AuditActions.SCAN_COMPLETE,
198
+ 'scan',
199
+ { type: 'project', path: projectPath },
200
+ result,
201
+ { command: 'scan', projectPath, ...metadata }
202
+ );
203
+ }
204
+
205
+ export async function emitShipCheck(
206
+ surface: AuditSurface,
207
+ projectPath: string,
208
+ result: AuditResult,
209
+ metadata?: { score?: number; grade?: string; canShip?: boolean }
210
+ ): Promise<AuditEvent | null> {
211
+ return emitAction(
212
+ surface,
213
+ AuditActions.SHIP_CHECK,
214
+ 'ship',
215
+ { type: 'project', path: projectPath },
216
+ result,
217
+ { command: 'ship', projectPath, custom: metadata }
218
+ );
219
+ }
220
+
221
+ export async function emitRealityStart(
222
+ surface: AuditSurface,
223
+ url: string,
224
+ flows?: string[]
225
+ ): Promise<AuditEvent | null> {
226
+ return emitAction(
227
+ surface,
228
+ AuditActions.REALITY_START,
229
+ 'reality',
230
+ { type: 'url', path: url },
231
+ 'success',
232
+ { command: 'reality', custom: { url, flows } }
233
+ );
234
+ }
235
+
236
+ export async function emitRealityComplete(
237
+ surface: AuditSurface,
238
+ url: string,
239
+ result: AuditResult,
240
+ metadata?: { durationMs?: number; testsPassed?: number; testsFailed?: number }
241
+ ): Promise<AuditEvent | null> {
242
+ return emitAction(
243
+ surface,
244
+ AuditActions.REALITY_COMPLETE,
245
+ 'reality',
246
+ { type: 'url', path: url },
247
+ result,
248
+ { command: 'reality', ...metadata }
249
+ );
250
+ }
251
+
252
+ export async function emitAutopilotAction(
253
+ surface: AuditSurface,
254
+ action: 'enable' | 'disable' | 'run' | 'report',
255
+ projectPath: string,
256
+ result: AuditResult,
257
+ metadata?: AuditEventMetadata
258
+ ): Promise<AuditEvent | null> {
259
+ const actionMap = {
260
+ enable: AuditActions.AUTOPILOT_ENABLE,
261
+ disable: AuditActions.AUTOPILOT_DISABLE,
262
+ run: AuditActions.AUTOPILOT_RUN,
263
+ report: AuditActions.AUTOPILOT_REPORT,
264
+ };
265
+
266
+ return emitAction(
267
+ surface,
268
+ actionMap[action],
269
+ 'autopilot',
270
+ { type: 'project', path: projectPath },
271
+ result,
272
+ { command: 'autopilot', projectPath, ...metadata }
273
+ );
274
+ }
275
+
276
+ export async function emitFixPlan(
277
+ surface: AuditSurface,
278
+ projectPath: string,
279
+ result: AuditResult,
280
+ metadata?: { fixCount?: number; scope?: string }
281
+ ): Promise<AuditEvent | null> {
282
+ return emitAction(
283
+ surface,
284
+ AuditActions.FIX_PLAN,
285
+ 'fix',
286
+ { type: 'project', path: projectPath },
287
+ result,
288
+ { command: 'fix', projectPath, ...metadata }
289
+ );
290
+ }
291
+
292
+ export async function emitFixApply(
293
+ surface: AuditSurface,
294
+ projectPath: string,
295
+ result: AuditResult,
296
+ metadata?: { fixCount?: number; filesModified?: number }
297
+ ): Promise<AuditEvent | null> {
298
+ return emitAction(
299
+ surface,
300
+ AuditActions.FIX_APPLY,
301
+ 'fix',
302
+ { type: 'project', path: projectPath },
303
+ result,
304
+ { command: 'fix', projectPath, ...metadata }
305
+ );
306
+ }
307
+
308
+ export async function emitGateCheck(
309
+ surface: AuditSurface,
310
+ projectPath: string,
311
+ passed: boolean,
312
+ metadata?: { policy?: string; score?: number }
313
+ ): Promise<AuditEvent | null> {
314
+ return emitAction(
315
+ surface,
316
+ passed ? AuditActions.GATE_PASS : AuditActions.GATE_FAIL,
317
+ 'gate',
318
+ { type: 'project', path: projectPath },
319
+ passed ? 'success' : 'failure',
320
+ { command: 'gate', projectPath, ...metadata }
321
+ );
322
+ }
323
+
324
+ export async function emitToolInvoke(
325
+ surface: AuditSurface,
326
+ toolName: string,
327
+ args: Record<string, unknown>,
328
+ result: AuditResult,
329
+ metadata?: AuditEventMetadata
330
+ ): Promise<AuditEvent | null> {
331
+ return emitAction(
332
+ surface,
333
+ AuditActions.TOOL_INVOKE,
334
+ 'tool',
335
+ { type: 'tool', name: toolName },
336
+ result,
337
+ { command: toolName, custom: { args }, ...metadata }
338
+ );
339
+ }
340
+
341
+ export async function emitAuth(
342
+ surface: AuditSurface,
343
+ action: 'login' | 'logout' | 'token_refresh',
344
+ result: AuditResult,
345
+ metadata?: { method?: string }
346
+ ): Promise<AuditEvent | null> {
347
+ const actionMap = {
348
+ login: AuditActions.AUTH_LOGIN,
349
+ logout: AuditActions.AUTH_LOGOUT,
350
+ token_refresh: AuditActions.AUTH_TOKEN_REFRESH,
351
+ };
352
+
353
+ return emitAction(
354
+ surface,
355
+ actionMap[action],
356
+ 'auth',
357
+ { type: 'auth' },
358
+ result,
359
+ metadata
360
+ );
361
+ }
362
+
363
+ // Export the audit object for convenient usage
364
+ export const audit = {
365
+ configure: configureAudit,
366
+ emit,
367
+ emitAction,
368
+ emitScanStart,
369
+ emitScanComplete,
370
+ emitShipCheck,
371
+ emitRealityStart,
372
+ emitRealityComplete,
373
+ emitAutopilotAction,
374
+ emitFixPlan,
375
+ emitFixApply,
376
+ emitGateCheck,
377
+ emitToolInvoke,
378
+ emitAuth,
379
+ hasFullAccess: hasFullAuditAccess,
380
+ getStorage,
381
+ };
382
+
383
+ export default audit;