@zintrust/workers 0.1.27
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.
- package/README.md +861 -0
- package/dist/AnomalyDetection.d.ts +102 -0
- package/dist/AnomalyDetection.js +321 -0
- package/dist/AutoScaler.d.ts +127 -0
- package/dist/AutoScaler.js +425 -0
- package/dist/BroadcastWorker.d.ts +21 -0
- package/dist/BroadcastWorker.js +24 -0
- package/dist/CanaryController.d.ts +103 -0
- package/dist/CanaryController.js +380 -0
- package/dist/ChaosEngineering.d.ts +79 -0
- package/dist/ChaosEngineering.js +216 -0
- package/dist/CircuitBreaker.d.ts +106 -0
- package/dist/CircuitBreaker.js +374 -0
- package/dist/ClusterLock.d.ts +90 -0
- package/dist/ClusterLock.js +385 -0
- package/dist/ComplianceManager.d.ts +177 -0
- package/dist/ComplianceManager.js +556 -0
- package/dist/DatacenterOrchestrator.d.ts +133 -0
- package/dist/DatacenterOrchestrator.js +404 -0
- package/dist/DeadLetterQueue.d.ts +122 -0
- package/dist/DeadLetterQueue.js +539 -0
- package/dist/HealthMonitor.d.ts +42 -0
- package/dist/HealthMonitor.js +301 -0
- package/dist/MultiQueueWorker.d.ts +89 -0
- package/dist/MultiQueueWorker.js +277 -0
- package/dist/NotificationWorker.d.ts +21 -0
- package/dist/NotificationWorker.js +23 -0
- package/dist/Observability.d.ts +153 -0
- package/dist/Observability.js +530 -0
- package/dist/PluginManager.d.ts +123 -0
- package/dist/PluginManager.js +392 -0
- package/dist/PriorityQueue.d.ts +117 -0
- package/dist/PriorityQueue.js +244 -0
- package/dist/ResourceMonitor.d.ts +164 -0
- package/dist/ResourceMonitor.js +605 -0
- package/dist/SLAMonitor.d.ts +110 -0
- package/dist/SLAMonitor.js +274 -0
- package/dist/WorkerFactory.d.ts +193 -0
- package/dist/WorkerFactory.js +1507 -0
- package/dist/WorkerInit.d.ts +85 -0
- package/dist/WorkerInit.js +223 -0
- package/dist/WorkerMetrics.d.ts +114 -0
- package/dist/WorkerMetrics.js +509 -0
- package/dist/WorkerRegistry.d.ts +145 -0
- package/dist/WorkerRegistry.js +319 -0
- package/dist/WorkerShutdown.d.ts +61 -0
- package/dist/WorkerShutdown.js +159 -0
- package/dist/WorkerVersioning.d.ts +107 -0
- package/dist/WorkerVersioning.js +300 -0
- package/dist/build-manifest.json +462 -0
- package/dist/config/workerConfig.d.ts +3 -0
- package/dist/config/workerConfig.js +19 -0
- package/dist/createQueueWorker.d.ts +23 -0
- package/dist/createQueueWorker.js +113 -0
- package/dist/dashboard/index.d.ts +1 -0
- package/dist/dashboard/index.js +1 -0
- package/dist/dashboard/types.d.ts +117 -0
- package/dist/dashboard/types.js +1 -0
- package/dist/dashboard/workers-api.d.ts +4 -0
- package/dist/dashboard/workers-api.js +638 -0
- package/dist/dashboard/workers-dashboard-ui.d.ts +3 -0
- package/dist/dashboard/workers-dashboard-ui.js +1026 -0
- package/dist/dashboard/workers-dashboard.d.ts +4 -0
- package/dist/dashboard/workers-dashboard.js +904 -0
- package/dist/helper/index.d.ts +5 -0
- package/dist/helper/index.js +10 -0
- package/dist/http/WorkerApiController.d.ts +38 -0
- package/dist/http/WorkerApiController.js +312 -0
- package/dist/http/WorkerController.d.ts +374 -0
- package/dist/http/WorkerController.js +1351 -0
- package/dist/http/middleware/CustomValidation.d.ts +92 -0
- package/dist/http/middleware/CustomValidation.js +270 -0
- package/dist/http/middleware/DatacenterValidator.d.ts +3 -0
- package/dist/http/middleware/DatacenterValidator.js +94 -0
- package/dist/http/middleware/EditWorkerValidation.d.ts +7 -0
- package/dist/http/middleware/EditWorkerValidation.js +55 -0
- package/dist/http/middleware/FeaturesValidator.d.ts +3 -0
- package/dist/http/middleware/FeaturesValidator.js +60 -0
- package/dist/http/middleware/InfrastructureValidator.d.ts +31 -0
- package/dist/http/middleware/InfrastructureValidator.js +226 -0
- package/dist/http/middleware/OptionsValidator.d.ts +3 -0
- package/dist/http/middleware/OptionsValidator.js +112 -0
- package/dist/http/middleware/PayloadSanitizer.d.ts +7 -0
- package/dist/http/middleware/PayloadSanitizer.js +42 -0
- package/dist/http/middleware/ProcessorPathSanitizer.d.ts +3 -0
- package/dist/http/middleware/ProcessorPathSanitizer.js +74 -0
- package/dist/http/middleware/QueueNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/QueueNameSanitizer.js +45 -0
- package/dist/http/middleware/ValidateDriver.d.ts +7 -0
- package/dist/http/middleware/ValidateDriver.js +20 -0
- package/dist/http/middleware/VersionSanitizer.d.ts +3 -0
- package/dist/http/middleware/VersionSanitizer.js +25 -0
- package/dist/http/middleware/WorkerNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/WorkerNameSanitizer.js +46 -0
- package/dist/http/middleware/WorkerValidationChain.d.ts +27 -0
- package/dist/http/middleware/WorkerValidationChain.js +185 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +48 -0
- package/dist/routes/workers.d.ts +12 -0
- package/dist/routes/workers.js +81 -0
- package/dist/storage/WorkerStore.d.ts +45 -0
- package/dist/storage/WorkerStore.js +195 -0
- package/dist/type.d.ts +76 -0
- package/dist/type.js +1 -0
- package/dist/ui/router/ui.d.ts +3 -0
- package/dist/ui/router/ui.js +83 -0
- package/dist/ui/types/worker-ui.d.ts +229 -0
- package/dist/ui/types/worker-ui.js +5 -0
- package/package.json +53 -0
- package/src/AnomalyDetection.ts +434 -0
- package/src/AutoScaler.ts +654 -0
- package/src/BroadcastWorker.ts +34 -0
- package/src/CanaryController.ts +531 -0
- package/src/ChaosEngineering.ts +301 -0
- package/src/CircuitBreaker.ts +495 -0
- package/src/ClusterLock.ts +499 -0
- package/src/ComplianceManager.ts +815 -0
- package/src/DatacenterOrchestrator.ts +561 -0
- package/src/DeadLetterQueue.ts +733 -0
- package/src/HealthMonitor.ts +390 -0
- package/src/MultiQueueWorker.ts +431 -0
- package/src/NotificationWorker.ts +33 -0
- package/src/Observability.ts +696 -0
- package/src/PluginManager.ts +551 -0
- package/src/PriorityQueue.ts +351 -0
- package/src/ResourceMonitor.ts +769 -0
- package/src/SLAMonitor.ts +408 -0
- package/src/WorkerFactory.ts +2108 -0
- package/src/WorkerInit.ts +313 -0
- package/src/WorkerMetrics.ts +709 -0
- package/src/WorkerRegistry.ts +443 -0
- package/src/WorkerShutdown.ts +210 -0
- package/src/WorkerVersioning.ts +422 -0
- package/src/config/workerConfig.ts +25 -0
- package/src/createQueueWorker.ts +174 -0
- package/src/dashboard/index.ts +6 -0
- package/src/dashboard/types.ts +141 -0
- package/src/dashboard/workers-api.ts +785 -0
- package/src/dashboard/zintrust.svg +30 -0
- package/src/helper/index.ts +11 -0
- package/src/http/WorkerApiController.ts +369 -0
- package/src/http/WorkerController.ts +1512 -0
- package/src/http/middleware/CustomValidation.ts +360 -0
- package/src/http/middleware/DatacenterValidator.ts +124 -0
- package/src/http/middleware/EditWorkerValidation.ts +74 -0
- package/src/http/middleware/FeaturesValidator.ts +82 -0
- package/src/http/middleware/InfrastructureValidator.ts +295 -0
- package/src/http/middleware/OptionsValidator.ts +144 -0
- package/src/http/middleware/PayloadSanitizer.ts +52 -0
- package/src/http/middleware/ProcessorPathSanitizer.ts +86 -0
- package/src/http/middleware/QueueNameSanitizer.ts +55 -0
- package/src/http/middleware/ValidateDriver.ts +29 -0
- package/src/http/middleware/VersionSanitizer.ts +30 -0
- package/src/http/middleware/WorkerNameSanitizer.ts +56 -0
- package/src/http/middleware/WorkerValidationChain.ts +230 -0
- package/src/index.ts +98 -0
- package/src/routes/workers.ts +154 -0
- package/src/storage/WorkerStore.ts +240 -0
- package/src/type.ts +89 -0
- package/src/types/queue-monitor.d.ts +38 -0
- package/src/types/queue-redis.d.ts +38 -0
- package/src/ui/README.md +13 -0
- package/src/ui/components/JsonEditor.js +670 -0
- package/src/ui/components/JsonViewer.js +387 -0
- package/src/ui/components/WorkerCard.js +178 -0
- package/src/ui/components/WorkerExpandPanel.js +257 -0
- package/src/ui/components/fetcher.js +42 -0
- package/src/ui/components/sla-scorecard.js +32 -0
- package/src/ui/components/styles.css +30 -0
- package/src/ui/components/table-expander.js +34 -0
- package/src/ui/integration/worker-ui-integration.js +565 -0
- package/src/ui/router/ui.ts +99 -0
- package/src/ui/services/workerApi.js +240 -0
- package/src/ui/types/worker-ui.ts +283 -0
- package/src/ui/utils/jsonValidator.js +444 -0
- package/src/ui/workers/index.html +202 -0
- package/src/ui/workers/main.js +1781 -0
- package/src/ui/workers/styles.css +1350 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compliance Manager
|
|
3
|
+
* GDPR, HIPAA, and SOC2 compliance enforcement
|
|
4
|
+
* Sealed namespace for immutability
|
|
5
|
+
*/
|
|
6
|
+
import { ErrorFactory, Logger, NodeSingletons, createRedisConnection, } from '@zintrust/core';
|
|
7
|
+
// Access NodeSingletons lazily to avoid initialization errors in test environments
|
|
8
|
+
const getCrypto = () => {
|
|
9
|
+
if (!NodeSingletons) {
|
|
10
|
+
throw ErrorFactory.createWorkerError('NodeSingletons not available');
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
createCipheriv: NodeSingletons.createCipheriv,
|
|
14
|
+
createDecipheriv: NodeSingletons.createDecipheriv,
|
|
15
|
+
pbkdf2Sync: NodeSingletons.pbkdf2Sync,
|
|
16
|
+
randomBytes: NodeSingletons.randomBytes,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
// Redis key prefixes
|
|
20
|
+
const AUDIT_LOG_PREFIX = 'compliance:audit:';
|
|
21
|
+
const DATA_SUBJECT_PREFIX = 'compliance:subject:';
|
|
22
|
+
const ACCESS_REQUEST_PREFIX = 'compliance:request:';
|
|
23
|
+
const VIOLATION_PREFIX = 'compliance:violation:';
|
|
24
|
+
const CONSENT_PREFIX = 'compliance:consent:';
|
|
25
|
+
// Internal state
|
|
26
|
+
let redisClient = null;
|
|
27
|
+
let complianceConfig = null;
|
|
28
|
+
// Default configuration
|
|
29
|
+
const DEFAULT_CONFIG = {
|
|
30
|
+
gdpr: {
|
|
31
|
+
enabled: true,
|
|
32
|
+
dataRetentionDays: 365,
|
|
33
|
+
requireConsent: true,
|
|
34
|
+
enableRightToForgotten: true,
|
|
35
|
+
enableDataPortability: true,
|
|
36
|
+
enableAccessRequest: true,
|
|
37
|
+
},
|
|
38
|
+
hipaa: {
|
|
39
|
+
enabled: false,
|
|
40
|
+
requireEncryptionAtRest: true,
|
|
41
|
+
requireEncryptionInTransit: true,
|
|
42
|
+
auditRetentionYears: 6,
|
|
43
|
+
requireAccessControls: true,
|
|
44
|
+
enableBreachNotification: true,
|
|
45
|
+
},
|
|
46
|
+
soc2: {
|
|
47
|
+
enabled: true,
|
|
48
|
+
requireChangeLogging: true,
|
|
49
|
+
requireAccessReviews: true,
|
|
50
|
+
accessReviewIntervalDays: 90,
|
|
51
|
+
requireIncidentResponse: true,
|
|
52
|
+
requireDisasterRecovery: true,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Helper: Generate unique ID
|
|
57
|
+
*/
|
|
58
|
+
const generateId = () => {
|
|
59
|
+
const { randomBytes } = getCrypto();
|
|
60
|
+
return randomBytes(16).toString('hex');
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Helper: Encrypt data (AES-256-GCM)
|
|
64
|
+
*/
|
|
65
|
+
const encryptData = (data, keyId) => {
|
|
66
|
+
const { createCipheriv, pbkdf2Sync, randomBytes } = getCrypto();
|
|
67
|
+
// In production, retrieve key from secure key management service (AWS KMS, HashiCorp Vault, etc.)
|
|
68
|
+
const key = pbkdf2Sync(keyId, 'salt', 100_000, 32, 'sha256');
|
|
69
|
+
const iv = randomBytes(16);
|
|
70
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
71
|
+
let encrypted = cipher.update(data, 'utf8', 'hex');
|
|
72
|
+
encrypted += cipher.final('hex');
|
|
73
|
+
const authTag = cipher.getAuthTag();
|
|
74
|
+
return {
|
|
75
|
+
encrypted,
|
|
76
|
+
iv: iv.toString('hex'),
|
|
77
|
+
authTag: authTag.toString('hex'),
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Helper: Decrypt data (AES-256-GCM)
|
|
82
|
+
*/
|
|
83
|
+
const decryptData = (encrypted, iv, authTag, keyId) => {
|
|
84
|
+
const { createDecipheriv, pbkdf2Sync } = getCrypto();
|
|
85
|
+
const key = pbkdf2Sync(keyId, 'salt', 100_000, 32, 'sha256');
|
|
86
|
+
const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(iv, 'hex'));
|
|
87
|
+
decipher.setAuthTag(Buffer.from(authTag, 'hex'));
|
|
88
|
+
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
89
|
+
decrypted += decipher.final('utf8');
|
|
90
|
+
return decrypted;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Helper: Check if consent is required and valid
|
|
94
|
+
*/
|
|
95
|
+
const checkConsent = async (dataSubjectId) => {
|
|
96
|
+
const gdprConfig = complianceConfig?.gdpr;
|
|
97
|
+
if (gdprConfig?.enabled !== true || gdprConfig?.requireConsent !== true) {
|
|
98
|
+
return { valid: true };
|
|
99
|
+
}
|
|
100
|
+
if (!redisClient) {
|
|
101
|
+
return { valid: false, reason: 'Compliance system not initialized' };
|
|
102
|
+
}
|
|
103
|
+
const key = `${DATA_SUBJECT_PREFIX}${dataSubjectId}`;
|
|
104
|
+
const subjectJson = await redisClient.get(key);
|
|
105
|
+
if (subjectJson === null) {
|
|
106
|
+
return { valid: false, reason: 'Data subject not found' };
|
|
107
|
+
}
|
|
108
|
+
const subject = JSON.parse(subjectJson);
|
|
109
|
+
if (!subject.consentGiven) {
|
|
110
|
+
return { valid: false, reason: 'Consent not given' };
|
|
111
|
+
}
|
|
112
|
+
if (subject.consentWithdrawnDate) {
|
|
113
|
+
return { valid: false, reason: 'Consent withdrawn' };
|
|
114
|
+
}
|
|
115
|
+
return { valid: true };
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Helper: Store audit log
|
|
119
|
+
*/
|
|
120
|
+
const storeAuditLog = async (log) => {
|
|
121
|
+
if (!redisClient)
|
|
122
|
+
return;
|
|
123
|
+
try {
|
|
124
|
+
const key = `${AUDIT_LOG_PREFIX}${log.standard}`;
|
|
125
|
+
const score = log.timestamp.getTime();
|
|
126
|
+
const data = JSON.stringify(log);
|
|
127
|
+
await redisClient.zadd(key, score, data);
|
|
128
|
+
// Set retention based on standard
|
|
129
|
+
let retentionDays = 365; // Default 1 year
|
|
130
|
+
if (log.standard === 'hipaa' && complianceConfig?.hipaa.enabled === true) {
|
|
131
|
+
retentionDays = complianceConfig.hipaa.auditRetentionYears * 365;
|
|
132
|
+
}
|
|
133
|
+
await redisClient.expire(key, retentionDays * 24 * 60 * 60);
|
|
134
|
+
Logger.debug('Compliance audit log stored', {
|
|
135
|
+
standard: log.standard,
|
|
136
|
+
action: log.action,
|
|
137
|
+
userId: log.userId,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
Logger.error('Failed to store compliance audit log', error);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const hasNonEmptyString = (value) => {
|
|
145
|
+
return typeof value === 'string' && value.length > 0;
|
|
146
|
+
};
|
|
147
|
+
const runGdprChecks = async (dataSubjectId) => {
|
|
148
|
+
if (complianceConfig?.gdpr.enabled !== true) {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
if (!hasNonEmptyString(dataSubjectId)) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
const consentCheck = await checkConsent(dataSubjectId);
|
|
155
|
+
if (consentCheck.valid) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
return [`GDPR: ${consentCheck.reason}`];
|
|
159
|
+
};
|
|
160
|
+
const runHipaaChecks = (action, userId, resourceId) => {
|
|
161
|
+
if (complianceConfig?.hipaa.enabled !== true) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
if (complianceConfig.hipaa.requireAccessControls !== true) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
Logger.debug('HIPAA access control check', { action, userId, resourceId });
|
|
168
|
+
return [];
|
|
169
|
+
};
|
|
170
|
+
const runSoc2Checks = (action) => {
|
|
171
|
+
if (complianceConfig?.soc2.enabled !== true) {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
if (complianceConfig.soc2.requireChangeLogging !== true) {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
if (action.includes('modify') || action.includes('delete') || action.includes('update')) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
return [];
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Compliance Manager - Sealed namespace
|
|
184
|
+
*/
|
|
185
|
+
export const ComplianceManager = Object.freeze({
|
|
186
|
+
/**
|
|
187
|
+
* Initialize compliance manager
|
|
188
|
+
*/
|
|
189
|
+
initialize(redisConfig, config) {
|
|
190
|
+
if (redisClient) {
|
|
191
|
+
Logger.warn('ComplianceManager already initialized');
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
redisClient = createRedisConnection(redisConfig);
|
|
195
|
+
complianceConfig = {
|
|
196
|
+
gdpr: { ...DEFAULT_CONFIG.gdpr, ...config?.gdpr },
|
|
197
|
+
hipaa: { ...DEFAULT_CONFIG.hipaa, ...config?.hipaa },
|
|
198
|
+
soc2: { ...DEFAULT_CONFIG.soc2, ...config?.soc2 },
|
|
199
|
+
};
|
|
200
|
+
Logger.info('ComplianceManager initialized', {
|
|
201
|
+
gdpr: complianceConfig.gdpr.enabled,
|
|
202
|
+
hipaa: complianceConfig.hipaa.enabled,
|
|
203
|
+
soc2: complianceConfig.soc2.enabled,
|
|
204
|
+
});
|
|
205
|
+
},
|
|
206
|
+
/**
|
|
207
|
+
* Register data subject
|
|
208
|
+
*/
|
|
209
|
+
async registerDataSubject(subject) {
|
|
210
|
+
if (!redisClient) {
|
|
211
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
212
|
+
}
|
|
213
|
+
const key = `${DATA_SUBJECT_PREFIX}${subject.id}`;
|
|
214
|
+
await redisClient.set(key, JSON.stringify(subject));
|
|
215
|
+
// Set expiry based on retention period
|
|
216
|
+
if (subject.retentionPeriod !== undefined) {
|
|
217
|
+
await redisClient.expire(key, subject.retentionPeriod * 24 * 60 * 60);
|
|
218
|
+
}
|
|
219
|
+
await storeAuditLog({
|
|
220
|
+
id: generateId(),
|
|
221
|
+
timestamp: new Date(),
|
|
222
|
+
standard: 'gdpr',
|
|
223
|
+
action: 'data-subject-registered',
|
|
224
|
+
userId: 'system',
|
|
225
|
+
dataSubjectId: subject.id,
|
|
226
|
+
resourceId: subject.id,
|
|
227
|
+
resourceType: 'data-subject',
|
|
228
|
+
result: 'success',
|
|
229
|
+
severity: 'info',
|
|
230
|
+
});
|
|
231
|
+
Logger.info('Data subject registered', { id: subject.id });
|
|
232
|
+
},
|
|
233
|
+
/**
|
|
234
|
+
* Record consent
|
|
235
|
+
*/
|
|
236
|
+
async recordConsent(dataSubjectId, consentGiven, userId) {
|
|
237
|
+
const client = redisClient;
|
|
238
|
+
const gdprConfig = complianceConfig?.gdpr;
|
|
239
|
+
if (!client || gdprConfig?.enabled !== true) {
|
|
240
|
+
throw ErrorFactory.createConfigError('GDPR compliance not enabled');
|
|
241
|
+
}
|
|
242
|
+
const subjectKey = `${DATA_SUBJECT_PREFIX}${dataSubjectId}`;
|
|
243
|
+
const subjectJson = await client.get(subjectKey);
|
|
244
|
+
if (subjectJson === null) {
|
|
245
|
+
throw ErrorFactory.createNotFoundError(`Data subject not found: ${dataSubjectId}`);
|
|
246
|
+
}
|
|
247
|
+
const subject = JSON.parse(subjectJson);
|
|
248
|
+
subject.consentGiven = consentGiven;
|
|
249
|
+
if (consentGiven) {
|
|
250
|
+
subject.consentDate = new Date();
|
|
251
|
+
subject.consentWithdrawnDate = undefined;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
subject.consentWithdrawnDate = new Date();
|
|
255
|
+
}
|
|
256
|
+
await client.set(subjectKey, JSON.stringify(subject));
|
|
257
|
+
// Store consent in separate key for auditing
|
|
258
|
+
const consentKey = `${CONSENT_PREFIX}${dataSubjectId}`;
|
|
259
|
+
const consentRecord = {
|
|
260
|
+
dataSubjectId,
|
|
261
|
+
consentGiven,
|
|
262
|
+
timestamp: new Date(),
|
|
263
|
+
recordedBy: userId,
|
|
264
|
+
};
|
|
265
|
+
await client.zadd(consentKey, Date.now(), JSON.stringify(consentRecord));
|
|
266
|
+
await storeAuditLog({
|
|
267
|
+
id: generateId(),
|
|
268
|
+
timestamp: new Date(),
|
|
269
|
+
standard: 'gdpr',
|
|
270
|
+
action: consentGiven ? 'consent-given' : 'consent-withdrawn',
|
|
271
|
+
userId,
|
|
272
|
+
dataSubjectId,
|
|
273
|
+
resourceId: dataSubjectId,
|
|
274
|
+
resourceType: 'consent',
|
|
275
|
+
result: 'success',
|
|
276
|
+
severity: 'info',
|
|
277
|
+
});
|
|
278
|
+
Logger.info('Consent recorded', { dataSubjectId, consentGiven });
|
|
279
|
+
},
|
|
280
|
+
/**
|
|
281
|
+
* Check if action is compliant
|
|
282
|
+
*/
|
|
283
|
+
async checkCompliance(action, userId, dataSubjectId, resourceId) {
|
|
284
|
+
const violations = [];
|
|
285
|
+
const gdprViolations = await runGdprChecks(dataSubjectId);
|
|
286
|
+
violations.push(...gdprViolations);
|
|
287
|
+
const hipaaViolations = runHipaaChecks(action, userId, resourceId);
|
|
288
|
+
violations.push(...hipaaViolations);
|
|
289
|
+
const soc2Violations = runSoc2Checks(action);
|
|
290
|
+
violations.push(...soc2Violations);
|
|
291
|
+
const compliant = violations.length === 0;
|
|
292
|
+
if (!compliant) {
|
|
293
|
+
await storeAuditLog({
|
|
294
|
+
id: generateId(),
|
|
295
|
+
timestamp: new Date(),
|
|
296
|
+
standard: 'gdpr', // Primary standard for consent
|
|
297
|
+
action,
|
|
298
|
+
userId,
|
|
299
|
+
dataSubjectId,
|
|
300
|
+
resourceId: resourceId ?? 'unknown',
|
|
301
|
+
resourceType: 'compliance-check',
|
|
302
|
+
result: 'blocked',
|
|
303
|
+
reason: violations.join('; '),
|
|
304
|
+
severity: 'warning',
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
return { compliant, violations };
|
|
308
|
+
},
|
|
309
|
+
/**
|
|
310
|
+
* Create access request (GDPR Right to Access, Deletion, etc.)
|
|
311
|
+
*/
|
|
312
|
+
async createAccessRequest(request) {
|
|
313
|
+
if (!redisClient) {
|
|
314
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
315
|
+
}
|
|
316
|
+
const requestId = generateId();
|
|
317
|
+
const fullRequest = {
|
|
318
|
+
id: requestId,
|
|
319
|
+
requestDate: new Date(),
|
|
320
|
+
status: 'pending',
|
|
321
|
+
...request,
|
|
322
|
+
};
|
|
323
|
+
const key = `${ACCESS_REQUEST_PREFIX}${requestId}`;
|
|
324
|
+
await redisClient.set(key, JSON.stringify(fullRequest));
|
|
325
|
+
await storeAuditLog({
|
|
326
|
+
id: generateId(),
|
|
327
|
+
timestamp: new Date(),
|
|
328
|
+
standard: 'gdpr',
|
|
329
|
+
action: `access-request-${request.requestType}`,
|
|
330
|
+
userId: request.requestedBy,
|
|
331
|
+
dataSubjectId: request.dataSubjectId,
|
|
332
|
+
resourceId: requestId,
|
|
333
|
+
resourceType: 'access-request',
|
|
334
|
+
result: 'success',
|
|
335
|
+
severity: 'info',
|
|
336
|
+
});
|
|
337
|
+
Logger.info('Access request created', {
|
|
338
|
+
id: requestId,
|
|
339
|
+
type: request.requestType,
|
|
340
|
+
dataSubjectId: request.dataSubjectId,
|
|
341
|
+
});
|
|
342
|
+
return requestId;
|
|
343
|
+
},
|
|
344
|
+
/**
|
|
345
|
+
* Process access request
|
|
346
|
+
*/
|
|
347
|
+
async processAccessRequest(requestId, status, processedBy) {
|
|
348
|
+
if (!redisClient) {
|
|
349
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
350
|
+
}
|
|
351
|
+
const key = `${ACCESS_REQUEST_PREFIX}${requestId}`;
|
|
352
|
+
const requestJson = await redisClient.get(key);
|
|
353
|
+
if (requestJson === null) {
|
|
354
|
+
throw ErrorFactory.createNotFoundError(`Access request not found: ${requestId}`);
|
|
355
|
+
}
|
|
356
|
+
const request = JSON.parse(requestJson);
|
|
357
|
+
request.status = status;
|
|
358
|
+
if (status === 'approved') {
|
|
359
|
+
request.approvedBy = processedBy;
|
|
360
|
+
}
|
|
361
|
+
else if (status === 'completed') {
|
|
362
|
+
request.completedBy = processedBy;
|
|
363
|
+
request.completedDate = new Date();
|
|
364
|
+
}
|
|
365
|
+
await redisClient.set(key, JSON.stringify(request));
|
|
366
|
+
await storeAuditLog({
|
|
367
|
+
id: generateId(),
|
|
368
|
+
timestamp: new Date(),
|
|
369
|
+
standard: 'gdpr',
|
|
370
|
+
action: `access-request-${status}`,
|
|
371
|
+
userId: processedBy,
|
|
372
|
+
dataSubjectId: request.dataSubjectId,
|
|
373
|
+
resourceId: requestId,
|
|
374
|
+
resourceType: 'access-request',
|
|
375
|
+
result: 'success',
|
|
376
|
+
severity: status === 'rejected' ? 'warning' : 'info',
|
|
377
|
+
});
|
|
378
|
+
Logger.info('Access request processed', { id: requestId, status });
|
|
379
|
+
},
|
|
380
|
+
/**
|
|
381
|
+
* Encrypt sensitive data (HIPAA compliance)
|
|
382
|
+
*/
|
|
383
|
+
encryptSensitiveData(data, userId, keyId = 'default-key') {
|
|
384
|
+
if (complianceConfig?.hipaa.enabled === true &&
|
|
385
|
+
complianceConfig.hipaa.requireEncryptionAtRest !== true) {
|
|
386
|
+
Logger.warn('HIPAA encryption not enforced by configuration');
|
|
387
|
+
}
|
|
388
|
+
const { encrypted, iv, authTag } = encryptData(data, keyId);
|
|
389
|
+
const metadata = {
|
|
390
|
+
algorithm: 'aes-256-gcm',
|
|
391
|
+
keyId,
|
|
392
|
+
encryptedAt: new Date(),
|
|
393
|
+
encryptedBy: userId,
|
|
394
|
+
};
|
|
395
|
+
// Store IV and authTag with encrypted data
|
|
396
|
+
const encryptedPackage = JSON.stringify({ encrypted, iv, authTag, metadata });
|
|
397
|
+
return {
|
|
398
|
+
encrypted: encryptedPackage,
|
|
399
|
+
metadata,
|
|
400
|
+
};
|
|
401
|
+
},
|
|
402
|
+
/**
|
|
403
|
+
* Decrypt sensitive data
|
|
404
|
+
*/
|
|
405
|
+
decryptSensitiveData(encryptedPackage, userId) {
|
|
406
|
+
const parsed = JSON.parse(encryptedPackage);
|
|
407
|
+
if (typeof parsed.encrypted !== 'string' ||
|
|
408
|
+
typeof parsed.iv !== 'string' ||
|
|
409
|
+
typeof parsed.authTag !== 'string' ||
|
|
410
|
+
typeof parsed.metadata?.keyId !== 'string') {
|
|
411
|
+
throw ErrorFactory.createValidationError('Invalid encrypted payload');
|
|
412
|
+
}
|
|
413
|
+
const { encrypted, iv, authTag, metadata } = parsed;
|
|
414
|
+
storeAuditLog({
|
|
415
|
+
id: generateId(),
|
|
416
|
+
timestamp: new Date(),
|
|
417
|
+
standard: 'hipaa',
|
|
418
|
+
action: 'data-decrypted',
|
|
419
|
+
userId,
|
|
420
|
+
resourceId: metadata.keyId,
|
|
421
|
+
resourceType: 'encrypted-data',
|
|
422
|
+
result: 'success',
|
|
423
|
+
severity: 'info',
|
|
424
|
+
});
|
|
425
|
+
return decryptData(encrypted, iv, authTag, metadata.keyId);
|
|
426
|
+
},
|
|
427
|
+
/**
|
|
428
|
+
* Record compliance violation
|
|
429
|
+
*/
|
|
430
|
+
async recordViolation(violation) {
|
|
431
|
+
if (!redisClient) {
|
|
432
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
433
|
+
}
|
|
434
|
+
const violationId = generateId();
|
|
435
|
+
const fullViolation = {
|
|
436
|
+
id: violationId,
|
|
437
|
+
timestamp: new Date(),
|
|
438
|
+
...violation,
|
|
439
|
+
};
|
|
440
|
+
const key = `${VIOLATION_PREFIX}${violationId}`;
|
|
441
|
+
await redisClient.set(key, JSON.stringify(fullViolation));
|
|
442
|
+
await storeAuditLog({
|
|
443
|
+
id: generateId(),
|
|
444
|
+
timestamp: new Date(),
|
|
445
|
+
standard: violation.standard,
|
|
446
|
+
action: 'violation-recorded',
|
|
447
|
+
userId: 'system',
|
|
448
|
+
resourceId: violationId,
|
|
449
|
+
resourceType: 'compliance-violation',
|
|
450
|
+
result: 'success',
|
|
451
|
+
severity: 'critical',
|
|
452
|
+
reason: violation.description,
|
|
453
|
+
});
|
|
454
|
+
Logger.error('Compliance violation recorded', {
|
|
455
|
+
id: violationId,
|
|
456
|
+
standard: violation.standard,
|
|
457
|
+
type: violation.violationType,
|
|
458
|
+
severity: violation.severity,
|
|
459
|
+
});
|
|
460
|
+
return violationId;
|
|
461
|
+
},
|
|
462
|
+
/**
|
|
463
|
+
* Get audit logs
|
|
464
|
+
*/
|
|
465
|
+
async getAuditLogs(standard, startDate, endDate, limit = 1000) {
|
|
466
|
+
if (!redisClient) {
|
|
467
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
const key = `${AUDIT_LOG_PREFIX}${standard}`;
|
|
471
|
+
const minScore = startDate ? startDate.getTime() : '-inf';
|
|
472
|
+
const maxScore = endDate ? endDate.getTime() : '+inf';
|
|
473
|
+
const logs = await redisClient.zrangebyscore(key, minScore, maxScore, 'LIMIT', 0, limit);
|
|
474
|
+
return logs.map((log) => JSON.parse(log));
|
|
475
|
+
}
|
|
476
|
+
catch (error) {
|
|
477
|
+
Logger.error('Failed to retrieve audit logs', error);
|
|
478
|
+
return [];
|
|
479
|
+
}
|
|
480
|
+
},
|
|
481
|
+
/**
|
|
482
|
+
* Get compliance summary
|
|
483
|
+
*/
|
|
484
|
+
async getComplianceSummary() {
|
|
485
|
+
if (!redisClient || !complianceConfig) {
|
|
486
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
487
|
+
}
|
|
488
|
+
const client = redisClient;
|
|
489
|
+
const config = complianceConfig;
|
|
490
|
+
// Count data subjects
|
|
491
|
+
const subjectKeys = await client.keys(`${DATA_SUBJECT_PREFIX}*`);
|
|
492
|
+
// Count pending access requests
|
|
493
|
+
const requestKeys = await client.keys(`${ACCESS_REQUEST_PREFIX}*`);
|
|
494
|
+
let pendingRequests = 0;
|
|
495
|
+
const requestEntries = await Promise.all(requestKeys.map(async (key) => client.get(key)));
|
|
496
|
+
requestEntries.forEach((requestJson) => {
|
|
497
|
+
if (requestJson !== null) {
|
|
498
|
+
const request = JSON.parse(requestJson);
|
|
499
|
+
if (request.status === 'pending') {
|
|
500
|
+
pendingRequests++;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
// Count violations
|
|
505
|
+
const violationKeys = await client.keys(`${VIOLATION_PREFIX}*`);
|
|
506
|
+
return {
|
|
507
|
+
gdpr: {
|
|
508
|
+
enabled: config.gdpr.enabled,
|
|
509
|
+
dataSubjects: subjectKeys.length,
|
|
510
|
+
pendingRequests,
|
|
511
|
+
},
|
|
512
|
+
hipaa: {
|
|
513
|
+
enabled: config.hipaa.enabled,
|
|
514
|
+
encryptedResources: 0, // Would need to track this separately
|
|
515
|
+
auditLogRetention: `${config.hipaa.auditRetentionYears} years`,
|
|
516
|
+
},
|
|
517
|
+
soc2: {
|
|
518
|
+
enabled: config.soc2.enabled,
|
|
519
|
+
violations: violationKeys.length,
|
|
520
|
+
},
|
|
521
|
+
};
|
|
522
|
+
},
|
|
523
|
+
/**
|
|
524
|
+
* Get configuration
|
|
525
|
+
*/
|
|
526
|
+
getConfig() {
|
|
527
|
+
return complianceConfig ? { ...complianceConfig } : null;
|
|
528
|
+
},
|
|
529
|
+
/**
|
|
530
|
+
* Update configuration
|
|
531
|
+
*/
|
|
532
|
+
updateConfig(config) {
|
|
533
|
+
if (!complianceConfig) {
|
|
534
|
+
throw ErrorFactory.createConfigError('ComplianceManager not initialized');
|
|
535
|
+
}
|
|
536
|
+
complianceConfig = {
|
|
537
|
+
gdpr: { ...complianceConfig.gdpr, ...config.gdpr },
|
|
538
|
+
hipaa: { ...complianceConfig.hipaa, ...config.hipaa },
|
|
539
|
+
soc2: { ...complianceConfig.soc2, ...config.soc2 },
|
|
540
|
+
};
|
|
541
|
+
Logger.info('Compliance configuration updated', { config });
|
|
542
|
+
},
|
|
543
|
+
/**
|
|
544
|
+
* Shutdown
|
|
545
|
+
*/
|
|
546
|
+
async shutdown() {
|
|
547
|
+
Logger.info('ComplianceManager shutting down...');
|
|
548
|
+
if (redisClient) {
|
|
549
|
+
await redisClient.quit();
|
|
550
|
+
redisClient = null;
|
|
551
|
+
}
|
|
552
|
+
complianceConfig = null;
|
|
553
|
+
Logger.info('ComplianceManager shutdown complete');
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
// Graceful shutdown handled by WorkerShutdown
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Datacenter Orchestration
|
|
3
|
+
* Multi-datacenter worker coordination with region affinity and failover
|
|
4
|
+
* Sealed namespace for immutability
|
|
5
|
+
*/
|
|
6
|
+
export type DatacenterRegion = {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
location: {
|
|
10
|
+
continent: string;
|
|
11
|
+
country: string;
|
|
12
|
+
city: string;
|
|
13
|
+
coordinates?: {
|
|
14
|
+
lat: number;
|
|
15
|
+
lng: number;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
priority: number;
|
|
19
|
+
capacity: number;
|
|
20
|
+
currentLoad: number;
|
|
21
|
+
healthStatus: 'healthy' | 'degraded' | 'offline';
|
|
22
|
+
latency: number;
|
|
23
|
+
costMultiplier: number;
|
|
24
|
+
};
|
|
25
|
+
export type ReplicationStrategy = 'none' | 'active-passive' | 'active-active' | 'multi-master';
|
|
26
|
+
export type FailoverPolicy = {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
autoFailover: boolean;
|
|
29
|
+
failoverThreshold: number;
|
|
30
|
+
healthCheckInterval: number;
|
|
31
|
+
minHealthyRegions: number;
|
|
32
|
+
preferredRegions: string[];
|
|
33
|
+
};
|
|
34
|
+
export type WorkerPlacement = {
|
|
35
|
+
workerName: string;
|
|
36
|
+
primaryRegion: string;
|
|
37
|
+
secondaryRegions: string[];
|
|
38
|
+
replicationStrategy: ReplicationStrategy;
|
|
39
|
+
affinityRules: {
|
|
40
|
+
preferLocal: boolean;
|
|
41
|
+
maxLatency?: number;
|
|
42
|
+
avoidRegions?: string[];
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export type DatacenterTopology = {
|
|
46
|
+
regions: DatacenterRegion[];
|
|
47
|
+
connections: Array<{
|
|
48
|
+
from: string;
|
|
49
|
+
to: string;
|
|
50
|
+
latency: number;
|
|
51
|
+
bandwidth: number;
|
|
52
|
+
}>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Datacenter Orchestrator - Sealed namespace
|
|
56
|
+
*/
|
|
57
|
+
export declare const DatacenterOrchestrator: Readonly<{
|
|
58
|
+
/**
|
|
59
|
+
* Register datacenter region
|
|
60
|
+
*/
|
|
61
|
+
registerRegion(region: DatacenterRegion): void;
|
|
62
|
+
/**
|
|
63
|
+
* Unregister datacenter region
|
|
64
|
+
*/
|
|
65
|
+
unregisterRegion(regionId: string): void;
|
|
66
|
+
/**
|
|
67
|
+
* Get region information
|
|
68
|
+
*/
|
|
69
|
+
getRegion(regionId: string): DatacenterRegion | null;
|
|
70
|
+
/**
|
|
71
|
+
* List all regions
|
|
72
|
+
*/
|
|
73
|
+
listRegions(healthStatus?: DatacenterRegion["healthStatus"]): ReadonlyArray<DatacenterRegion>;
|
|
74
|
+
/**
|
|
75
|
+
* Update region health status
|
|
76
|
+
*/
|
|
77
|
+
updateRegionHealth(regionId: string, healthStatus: DatacenterRegion["healthStatus"]): void;
|
|
78
|
+
/**
|
|
79
|
+
* Update region load
|
|
80
|
+
*/
|
|
81
|
+
updateRegionLoad(regionId: string, currentLoad: number): void;
|
|
82
|
+
/**
|
|
83
|
+
* Place worker in datacenter
|
|
84
|
+
*/
|
|
85
|
+
placeWorker(placement: WorkerPlacement): void;
|
|
86
|
+
/**
|
|
87
|
+
* Get worker placement
|
|
88
|
+
*/
|
|
89
|
+
getPlacement(workerName: string): WorkerPlacement | null;
|
|
90
|
+
/**
|
|
91
|
+
* Update worker placement
|
|
92
|
+
*/
|
|
93
|
+
updatePlacement(workerName: string, updates: Partial<WorkerPlacement>): void;
|
|
94
|
+
/**
|
|
95
|
+
* Remove worker placement
|
|
96
|
+
*/
|
|
97
|
+
removeWorker(workerName: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* Find optimal region for job execution
|
|
100
|
+
*/
|
|
101
|
+
findOptimalRegion(workerName: string, clientRegion?: string): string | null;
|
|
102
|
+
/**
|
|
103
|
+
* Set failover policy for region
|
|
104
|
+
*/
|
|
105
|
+
setFailoverPolicy(regionId: string, policy: FailoverPolicy): void;
|
|
106
|
+
/**
|
|
107
|
+
* Get failover policy
|
|
108
|
+
*/
|
|
109
|
+
getFailoverPolicy(regionId: string): FailoverPolicy | null;
|
|
110
|
+
/**
|
|
111
|
+
* Start health checks for region
|
|
112
|
+
*/
|
|
113
|
+
startHealthChecks(regionId: string, intervalSeconds: number): void;
|
|
114
|
+
/**
|
|
115
|
+
* Stop health checks for region
|
|
116
|
+
*/
|
|
117
|
+
stopHealthChecks(regionId: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* Get datacenter topology
|
|
120
|
+
*/
|
|
121
|
+
getTopology(): DatacenterTopology;
|
|
122
|
+
/**
|
|
123
|
+
* Get load balancing recommendation
|
|
124
|
+
*/
|
|
125
|
+
getLoadBalancingRecommendation(): Array<{
|
|
126
|
+
regionId: string;
|
|
127
|
+
recommendedLoad: number;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Shutdown datacenter orchestrator
|
|
131
|
+
*/
|
|
132
|
+
shutdown(): void;
|
|
133
|
+
}>;
|