@gugananuvem/aws-local-simulator 1.0.33 → 1.0.34

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 (79) hide show
  1. package/README.md +834 -834
  2. package/aws-config +153 -153
  3. package/bin/aws-local-simulator.js +63 -63
  4. package/package.json +3 -2
  5. package/src/config/config-loader.js +114 -114
  6. package/src/config/default-config.js +79 -79
  7. package/src/config/env-loader.js +68 -68
  8. package/src/index.js +146 -146
  9. package/src/index.mjs +123 -123
  10. package/src/server.js +463 -463
  11. package/src/services/apigateway/index.js +75 -75
  12. package/src/services/apigateway/server.js +607 -607
  13. package/src/services/apigateway/simulator.js +1405 -1405
  14. package/src/services/athena/index.js +75 -75
  15. package/src/services/athena/server.js +101 -101
  16. package/src/services/athena/simulador.js +998 -998
  17. package/src/services/athena/simulator.js +346 -346
  18. package/src/services/cloudformation/index.js +106 -106
  19. package/src/services/cloudformation/server.js +417 -417
  20. package/src/services/cloudformation/simulador.js +1020 -1020
  21. package/src/services/cloudtrail/index.js +84 -84
  22. package/src/services/cloudtrail/server.js +235 -235
  23. package/src/services/cloudtrail/simulador.js +719 -719
  24. package/src/services/cloudwatch/index.js +84 -84
  25. package/src/services/cloudwatch/server.js +366 -366
  26. package/src/services/cloudwatch/simulador.js +1173 -1173
  27. package/src/services/cognito/index.js +79 -79
  28. package/src/services/cognito/server.js +297 -297
  29. package/src/services/cognito/simulator.js +1992 -1761
  30. package/src/services/config/index.js +96 -96
  31. package/src/services/config/server.js +215 -215
  32. package/src/services/config/simulador.js +1260 -1260
  33. package/src/services/dynamodb/index.js +74 -74
  34. package/src/services/dynamodb/server.js +139 -139
  35. package/src/services/dynamodb/simulator.js +1005 -994
  36. package/src/services/dynamodb/sqlite-store.js +722 -0
  37. package/src/services/ecs/index.js +65 -65
  38. package/src/services/ecs/server.js +235 -235
  39. package/src/services/ecs/simulator.js +844 -844
  40. package/src/services/eventbridge/index.js +89 -89
  41. package/src/services/eventbridge/server.js +209 -209
  42. package/src/services/eventbridge/simulator.js +684 -684
  43. package/src/services/index.js +45 -45
  44. package/src/services/kms/index.js +75 -75
  45. package/src/services/kms/server.js +81 -81
  46. package/src/services/kms/simulator.js +344 -344
  47. package/src/services/lambda/handler-loader.js +183 -183
  48. package/src/services/lambda/index.js +81 -81
  49. package/src/services/lambda/route-registry.js +274 -274
  50. package/src/services/lambda/server.js +191 -191
  51. package/src/services/lambda/simulator.js +364 -364
  52. package/src/services/parameter-store/index.js +80 -80
  53. package/src/services/parameter-store/server.js +50 -50
  54. package/src/services/parameter-store/simulator.js +201 -201
  55. package/src/services/s3/index.js +73 -73
  56. package/src/services/s3/server.js +350 -350
  57. package/src/services/s3/simulator.js +568 -568
  58. package/src/services/secret-manager/index.js +80 -80
  59. package/src/services/secret-manager/server.js +51 -51
  60. package/src/services/secret-manager/simulator.js +182 -182
  61. package/src/services/sns/index.js +89 -89
  62. package/src/services/sns/server.js +607 -607
  63. package/src/services/sns/simulator.js +1482 -1482
  64. package/src/services/sqs/index.js +98 -98
  65. package/src/services/sqs/server.js +360 -360
  66. package/src/services/sqs/simulator.js +509 -509
  67. package/src/services/sts/index.js +37 -37
  68. package/src/services/sts/server.js +144 -144
  69. package/src/services/sts/simulator.js +69 -69
  70. package/src/services/xray/index.js +83 -83
  71. package/src/services/xray/server.js +308 -308
  72. package/src/services/xray/simulador.js +994 -994
  73. package/src/template/aws-config-template.js +87 -87
  74. package/src/template/aws-config-template.mjs +90 -90
  75. package/src/template/config-template.json +203 -203
  76. package/src/utils/aws-config.js +91 -91
  77. package/src/utils/cloudtrail-audit.js +129 -129
  78. package/src/utils/local-store.js +83 -83
  79. package/src/utils/logger.js +59 -59
@@ -1,344 +1,344 @@
1
- 'use strict';
2
-
3
- const crypto = require('crypto');
4
- const { v4: uuidv4 } = require('uuid');
5
- const { CloudTrailAudit } = require('../../utils/cloudtrail-audit');
6
-
7
- /**
8
- * KMS Simulator - Criptografia real com crypto nativo
9
- */
10
- class KMSSimulator {
11
- constructor(store, logger, config) {
12
- this.store = store;
13
- this.logger = logger;
14
- this.config = config;
15
- this.keys = new Map();
16
- this.aliases = new Map();
17
- this.keyMaterial = new Map();
18
- this.audit = new CloudTrailAudit('kms.amazonaws.com');
19
- }
20
-
21
- async initialize() {
22
- try {
23
- const keys = await this.store.read('kms/keys');
24
- if (Array.isArray(keys)) {
25
- for (const k of keys) {
26
- this.keys.set(k.KeyId, k);
27
- // Re-gerar material da chave a partir do seed
28
- if (k._keySeed) {
29
- this.keyMaterial.set(k.KeyId, Buffer.from(k._keySeed, 'hex'));
30
- }
31
- }
32
- }
33
- const aliases = await this.store.read('kms/aliases');
34
- if (Array.isArray(aliases)) {
35
- for (const a of aliases) this.aliases.set(a.AliasName, a);
36
- }
37
- this.logger.info('KMS: dados carregados', 'kms');
38
- } catch { this.logger.debug('KMS: sem dados anteriores', 'kms'); }
39
- }
40
-
41
- async _persistKeys() {
42
- await this.store.write('kms/keys', null, Array.from(this.keys.values()));
43
- }
44
-
45
- async _persistAliases() {
46
- await this.store.write('kms/aliases', null, Array.from(this.aliases.values()));
47
- }
48
-
49
- _requireKey(keyId) {
50
- // Resolver alias
51
- if (keyId.startsWith('alias/')) {
52
- const alias = this.aliases.get(keyId);
53
- if (!alias) { const err = new Error(`Alias not found: ${keyId}`); err.code = 'NotFoundException'; throw err; }
54
- keyId = alias.TargetKeyId;
55
- }
56
- // Resolver por ARN
57
- if (keyId.startsWith('arn:')) {
58
- keyId = keyId.split('/').pop();
59
- }
60
- const key = this.keys.get(keyId);
61
- if (!key) { const err = new Error(`Key not found: ${keyId}`); err.code = 'NotFoundException'; throw err; }
62
- if (key.KeyState === 'Disabled') { const err = new Error('Key is disabled'); err.code = 'DisabledException'; throw err; }
63
- if (key.KeyState === 'PendingDeletion') { const err = new Error('Key is pending deletion'); err.code = 'KMSInvalidStateException'; throw err; }
64
- return key;
65
- }
66
-
67
- async createKey(params) {
68
- const Description = params.Description || params.description || '';
69
- const KeyUsage = params.KeyUsage || params.keyUsage || 'ENCRYPT_DECRYPT';
70
- const KeySpec = params.KeySpec || params.keySpec || 'SYMMETRIC_DEFAULT';
71
- const Tags = params.Tags || params.tags || [];
72
- const MultiRegion = params.MultiRegion || params.multiRegion || false;
73
-
74
- const keyId = uuidv4();
75
- const keyArn = `arn:aws:kms:local:000000000000:key/${keyId}`;
76
- let keyMaterial;
77
- let publicKey = null;
78
- let privateKey = null;
79
-
80
- this.logger.debug(`KMS: Criando chave ${keyId} (Spec: ${KeySpec})`, 'kms');
81
-
82
- try {
83
- if (KeySpec === 'SYMMETRIC_DEFAULT') {
84
- keyMaterial = crypto.randomBytes(32);
85
- } else if (KeySpec.startsWith('RSA_')) {
86
- const bits = KeySpec === 'RSA_2048' ? 2048 : KeySpec === 'RSA_3072' ? 3072 : 4096;
87
- const pair = crypto.generateKeyPairSync('rsa', {
88
- modulusLength: bits,
89
- publicKeyEncoding: { type: 'spki', format: 'pem' },
90
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
91
- });
92
- publicKey = pair.publicKey;
93
- privateKey = pair.privateKey;
94
- keyMaterial = Buffer.from(privateKey);
95
- } else if (KeySpec.startsWith('ECC_')) {
96
- const curve = KeySpec.includes('P256') ? 'prime256v1' : KeySpec.includes('P384') ? 'secp384r1' : 'secp521r1';
97
- const pair = crypto.generateKeyPairSync('ec', {
98
- namedCurve: curve,
99
- publicKeyEncoding: { type: 'spki', format: 'pem' },
100
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
101
- });
102
- publicKey = pair.publicKey;
103
- privateKey = pair.privateKey;
104
- keyMaterial = Buffer.from(privateKey);
105
- } else {
106
- keyMaterial = crypto.randomBytes(32);
107
- }
108
- } catch (err) {
109
- this.logger.error(`KMS: Erro ao gerar material da chave: ${err.message}`, 'kms');
110
- throw err;
111
- }
112
-
113
- this.keyMaterial.set(keyId, keyMaterial);
114
- const key = {
115
- KeyId: keyId,
116
- KeyArn: keyArn,
117
- Description,
118
- KeyUsage,
119
- KeySpec,
120
- KeyState: 'Enabled',
121
- Enabled: true,
122
- CreationDate: new Date().toISOString(),
123
- MultiRegion,
124
- Tags,
125
- PublicKey: publicKey,
126
- _keySeed: keyMaterial.toString('hex')
127
- };
128
- this.keys.set(keyId, key);
129
- await this._persistKeys();
130
- this.logger.info(`KMS: chave criada com sucesso: ${keyId}`, 'kms');
131
- this.audit.record({ eventName: 'CreateKey', readOnly: false, resources: [{ ARN: keyArn, type: 'AWS::KMS::Key' }], requestParameters: { description: Description, keyUsage: KeyUsage, keySpec: KeySpec } });
132
- return { KeyMetadata: this._sanitizeKey(key) };
133
- }
134
-
135
- async describeKey(params) {
136
- const keyId = params.KeyId || params.keyId;
137
- const key = this._requireKey(keyId);
138
- return { KeyMetadata: this._sanitizeKey(key) };
139
- }
140
-
141
- async listKeys(params) {
142
- const { Limit = 100 } = params || {};
143
- const keys = Array.from(this.keys.values()).slice(0, Limit);
144
- // Para o Dashboard, retornamos mais detalhes se for solicitado via admin
145
- // Mas para o SDK padrão, retornamos apenas o que o SDK espera
146
- return { Keys: keys.map(k => ({ KeyId: k.KeyId, KeyArn: k.KeyArn })) };
147
- }
148
-
149
- listKeysFull() {
150
- return Array.from(this.keys.values()).map(k => this._sanitizeKey(k));
151
- }
152
-
153
-
154
- async enableKey(params) {
155
- const key = this._requireKey(params.KeyId);
156
- key.KeyState = 'Enabled'; key.Enabled = true;
157
- await this._persistKeys();
158
- return {};
159
- }
160
-
161
- async disableKey(params) {
162
- const key = this._requireKey(params.KeyId);
163
- key.KeyState = 'Disabled'; key.Enabled = false;
164
- await this._persistKeys();
165
- return {};
166
- }
167
-
168
- async scheduleKeyDeletion(params) {
169
- const { KeyId, PendingWindowInDays = 30 } = params;
170
- const key = this._requireKey(KeyId);
171
- key.KeyState = 'PendingDeletion';
172
- key.DeletionDate = new Date(Date.now() + PendingWindowInDays * 86400000).toISOString();
173
- await this._persistKeys();
174
- return { KeyId: key.KeyId, DeletionDate: key.DeletionDate };
175
- }
176
-
177
- async cancelKeyDeletion(params) {
178
- const keyId = params.KeyId.startsWith('arn:') ? params.KeyId.split('/').pop() : params.KeyId;
179
- const key = this.keys.get(keyId);
180
- if (!key) { const err = new Error('Key not found'); err.code = 'NotFoundException'; throw err; }
181
- key.KeyState = 'Disabled'; key.DeletionDate = null;
182
- await this._persistKeys();
183
- return { KeyId: key.KeyId };
184
- }
185
-
186
- async createAlias(params) {
187
- const { AliasName, TargetKeyId } = params;
188
- const key = this._requireKey(TargetKeyId);
189
- if (!AliasName.startsWith('alias/')) {
190
- const err = new Error('Alias must start with alias/'); err.code = 'InvalidAliasNameException'; throw err;
191
- }
192
- const alias = { AliasName, TargetKeyId: key.KeyId, AliasArn: `arn:aws:kms:local:000000000000:${AliasName}`, CreationDate: new Date().toISOString() };
193
- this.aliases.set(AliasName, alias);
194
- await this._persistAliases();
195
- return {};
196
- }
197
-
198
- async deleteAlias(params) {
199
- this.aliases.delete(params.AliasName);
200
- await this._persistAliases();
201
- return {};
202
- }
203
-
204
- async listAliases(params) {
205
- const { KeyId } = params || {};
206
- let aliases = Array.from(this.aliases.values());
207
- if (KeyId) aliases = aliases.filter(a => a.TargetKeyId === KeyId);
208
- return { Aliases: aliases };
209
- }
210
-
211
- // ===================== CRYPTO OPERATIONS =====================
212
-
213
- async encrypt(params) {
214
- const { KeyId, Plaintext, EncryptionContext } = params;
215
- const key = this._requireKey(KeyId);
216
- if (key.KeyUsage !== 'ENCRYPT_DECRYPT') {
217
- const err = new Error('Key not for encryption'); err.code = 'InvalidKeyUsageException'; throw err;
218
- }
219
- const material = this.keyMaterial.get(key.KeyId);
220
- const iv = crypto.randomBytes(12);
221
- const cipher = crypto.createCipheriv('aes-256-gcm', material.slice(0, 32), iv);
222
- const plainBuf = Buffer.isBuffer(Plaintext) ? Plaintext : Buffer.from(Plaintext, 'base64');
223
- const encrypted = Buffer.concat([cipher.update(plainBuf), cipher.final()]);
224
- const tag = cipher.getAuthTag();
225
- // Format: iv(12) + tag(16) + ciphertext
226
- const ciphertext = Buffer.concat([iv, tag, encrypted]);
227
- this.audit.record({ eventName: 'Encrypt', readOnly: false, isDataEvent: true, resources: [{ ARN: key.KeyArn, type: 'AWS::KMS::Key' }], requestParameters: { keyId: key.KeyId } });
228
- return {
229
- KeyId: key.KeyId,
230
- CiphertextBlob: ciphertext.toString('base64'),
231
- EncryptionAlgorithm: 'SYMMETRIC_DEFAULT'
232
- };
233
- }
234
-
235
- async decrypt(params) {
236
- const { KeyId, CiphertextBlob, EncryptionContext } = params;
237
- let key;
238
- if (KeyId) {
239
- key = this._requireKey(KeyId);
240
- } else {
241
- // Tentar todas as chaves simétricas
242
- key = Array.from(this.keys.values()).find(k => k.KeySpec === 'SYMMETRIC_DEFAULT' && k.KeyState === 'Enabled');
243
- if (!key) { const err = new Error('No key available'); err.code = 'NotFoundException'; throw err; }
244
- }
245
- const material = this.keyMaterial.get(key.KeyId);
246
- const buf = Buffer.isBuffer(CiphertextBlob) ? CiphertextBlob : Buffer.from(CiphertextBlob, 'base64');
247
- const iv = buf.slice(0, 12);
248
- const tag = buf.slice(12, 28);
249
- const ciphertext = buf.slice(28);
250
- try {
251
- const decipher = crypto.createDecipheriv('aes-256-gcm', material.slice(0, 32), iv);
252
- decipher.setAuthTag(tag);
253
- const decrypted = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
254
- return { KeyId: key.KeyId, Plaintext: decrypted.toString('base64'), EncryptionAlgorithm: 'SYMMETRIC_DEFAULT' };
255
- } catch (e) {
256
- const err = new Error('Decryption failed - invalid ciphertext or wrong key'); err.code = 'InvalidCiphertextException'; throw err;
257
- }
258
- }
259
-
260
- async generateDataKey(params) {
261
- const { KeyId, KeySpec = 'AES_256', NumberOfBytes } = params;
262
- const key = this._requireKey(KeyId);
263
- const dataKeyBytes = NumberOfBytes || (KeySpec === 'AES_128' ? 16 : 32);
264
- const plaintext = crypto.randomBytes(dataKeyBytes);
265
- const encrypted = await this.encrypt({ KeyId: key.KeyId, Plaintext: plaintext });
266
- return {
267
- KeyId: key.KeyId,
268
- Plaintext: plaintext.toString('base64'),
269
- CiphertextBlob: encrypted.CiphertextBlob
270
- };
271
- }
272
-
273
- async generateDataKeyWithoutPlaintext(params) {
274
- const result = await this.generateDataKey(params);
275
- const { Plaintext, ...rest } = result;
276
- return rest;
277
- }
278
-
279
- async generateDataKeyPair(params) {
280
- const { KeyId, KeyPairSpec } = params;
281
- const key = this._requireKey(KeyId);
282
- const bits = KeyPairSpec === 'RSA_2048' ? 2048 : 4096;
283
- const pair = crypto.generateKeyPairSync('rsa', {
284
- modulusLength: bits,
285
- publicKeyEncoding: { type: 'spki', format: 'pem' },
286
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
287
- });
288
- const encrypted = await this.encrypt({ KeyId: key.KeyId, Plaintext: Buffer.from(pair.privateKey) });
289
- return {
290
- KeyId: key.KeyId,
291
- KeyPairSpec,
292
- PublicKey: Buffer.from(pair.publicKey).toString('base64'),
293
- PrivateKeyPlaintext: Buffer.from(pair.privateKey).toString('base64'),
294
- PrivateKeyCiphertextBlob: encrypted.CiphertextBlob
295
- };
296
- }
297
-
298
- async sign(params) {
299
- const { KeyId, Message, MessageType = 'RAW', SigningAlgorithm } = params;
300
- const key = this._requireKey(KeyId);
301
- if (key.KeyUsage !== 'SIGN_VERIFY') {
302
- const err = new Error('Key not for signing'); err.code = 'InvalidKeyUsageException'; throw err;
303
- }
304
- const material = this.keyMaterial.get(key.KeyId);
305
- const msgBuf = Buffer.isBuffer(Message) ? Message : Buffer.from(Message, 'base64');
306
- const sign = crypto.createSign('SHA256');
307
- sign.update(msgBuf);
308
- const signature = sign.sign(material.toString(), 'base64');
309
- return { KeyId: key.KeyId, Signature: signature, SigningAlgorithm };
310
- }
311
-
312
- async verify(params) {
313
- const { KeyId, Message, Signature, SigningAlgorithm } = params;
314
- const key = this._requireKey(KeyId);
315
- const material = this.keyMaterial.get(key.KeyId);
316
- const msgBuf = Buffer.isBuffer(Message) ? Message : Buffer.from(Message, 'base64');
317
- const verify = crypto.createVerify('SHA256');
318
- verify.update(msgBuf);
319
- try {
320
- const valid = verify.verify(material.toString(), Signature, 'base64');
321
- return { KeyId: key.KeyId, SignatureValid: valid, SigningAlgorithm };
322
- } catch { return { KeyId: key.KeyId, SignatureValid: false, SigningAlgorithm }; }
323
- }
324
-
325
- async generateRandom(params) {
326
- const { NumberOfBytes = 32 } = params;
327
- const bytes = crypto.randomBytes(NumberOfBytes);
328
- return { Plaintext: bytes.toString('base64') };
329
- }
330
-
331
- _sanitizeKey(key) {
332
- const { _keySeed, PublicKey: pk, ...clean } = key;
333
- return clean;
334
- }
335
-
336
- async reset() {
337
- this.keys.clear();
338
- this.aliases.clear();
339
- this.keyMaterial.clear();
340
- await this.store.clear('kms');
341
- }
342
- }
343
-
344
- module.exports = { KMSSimulator };
1
+ 'use strict';
2
+
3
+ const crypto = require('crypto');
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const { CloudTrailAudit } = require('../../utils/cloudtrail-audit');
6
+
7
+ /**
8
+ * KMS Simulator - Criptografia real com crypto nativo
9
+ */
10
+ class KMSSimulator {
11
+ constructor(store, logger, config) {
12
+ this.store = store;
13
+ this.logger = logger;
14
+ this.config = config;
15
+ this.keys = new Map();
16
+ this.aliases = new Map();
17
+ this.keyMaterial = new Map();
18
+ this.audit = new CloudTrailAudit('kms.amazonaws.com');
19
+ }
20
+
21
+ async initialize() {
22
+ try {
23
+ const keys = await this.store.read('kms/keys');
24
+ if (Array.isArray(keys)) {
25
+ for (const k of keys) {
26
+ this.keys.set(k.KeyId, k);
27
+ // Re-gerar material da chave a partir do seed
28
+ if (k._keySeed) {
29
+ this.keyMaterial.set(k.KeyId, Buffer.from(k._keySeed, 'hex'));
30
+ }
31
+ }
32
+ }
33
+ const aliases = await this.store.read('kms/aliases');
34
+ if (Array.isArray(aliases)) {
35
+ for (const a of aliases) this.aliases.set(a.AliasName, a);
36
+ }
37
+ this.logger.info('KMS: dados carregados', 'kms');
38
+ } catch { this.logger.debug('KMS: sem dados anteriores', 'kms'); }
39
+ }
40
+
41
+ async _persistKeys() {
42
+ await this.store.write('kms/keys', null, Array.from(this.keys.values()));
43
+ }
44
+
45
+ async _persistAliases() {
46
+ await this.store.write('kms/aliases', null, Array.from(this.aliases.values()));
47
+ }
48
+
49
+ _requireKey(keyId) {
50
+ // Resolver alias
51
+ if (keyId.startsWith('alias/')) {
52
+ const alias = this.aliases.get(keyId);
53
+ if (!alias) { const err = new Error(`Alias not found: ${keyId}`); err.code = 'NotFoundException'; throw err; }
54
+ keyId = alias.TargetKeyId;
55
+ }
56
+ // Resolver por ARN
57
+ if (keyId.startsWith('arn:')) {
58
+ keyId = keyId.split('/').pop();
59
+ }
60
+ const key = this.keys.get(keyId);
61
+ if (!key) { const err = new Error(`Key not found: ${keyId}`); err.code = 'NotFoundException'; throw err; }
62
+ if (key.KeyState === 'Disabled') { const err = new Error('Key is disabled'); err.code = 'DisabledException'; throw err; }
63
+ if (key.KeyState === 'PendingDeletion') { const err = new Error('Key is pending deletion'); err.code = 'KMSInvalidStateException'; throw err; }
64
+ return key;
65
+ }
66
+
67
+ async createKey(params) {
68
+ const Description = params.Description || params.description || '';
69
+ const KeyUsage = params.KeyUsage || params.keyUsage || 'ENCRYPT_DECRYPT';
70
+ const KeySpec = params.KeySpec || params.keySpec || 'SYMMETRIC_DEFAULT';
71
+ const Tags = params.Tags || params.tags || [];
72
+ const MultiRegion = params.MultiRegion || params.multiRegion || false;
73
+
74
+ const keyId = uuidv4();
75
+ const keyArn = `arn:aws:kms:local:000000000000:key/${keyId}`;
76
+ let keyMaterial;
77
+ let publicKey = null;
78
+ let privateKey = null;
79
+
80
+ this.logger.debug(`KMS: Criando chave ${keyId} (Spec: ${KeySpec})`, 'kms');
81
+
82
+ try {
83
+ if (KeySpec === 'SYMMETRIC_DEFAULT') {
84
+ keyMaterial = crypto.randomBytes(32);
85
+ } else if (KeySpec.startsWith('RSA_')) {
86
+ const bits = KeySpec === 'RSA_2048' ? 2048 : KeySpec === 'RSA_3072' ? 3072 : 4096;
87
+ const pair = crypto.generateKeyPairSync('rsa', {
88
+ modulusLength: bits,
89
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
90
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
91
+ });
92
+ publicKey = pair.publicKey;
93
+ privateKey = pair.privateKey;
94
+ keyMaterial = Buffer.from(privateKey);
95
+ } else if (KeySpec.startsWith('ECC_')) {
96
+ const curve = KeySpec.includes('P256') ? 'prime256v1' : KeySpec.includes('P384') ? 'secp384r1' : 'secp521r1';
97
+ const pair = crypto.generateKeyPairSync('ec', {
98
+ namedCurve: curve,
99
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
100
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
101
+ });
102
+ publicKey = pair.publicKey;
103
+ privateKey = pair.privateKey;
104
+ keyMaterial = Buffer.from(privateKey);
105
+ } else {
106
+ keyMaterial = crypto.randomBytes(32);
107
+ }
108
+ } catch (err) {
109
+ this.logger.error(`KMS: Erro ao gerar material da chave: ${err.message}`, 'kms');
110
+ throw err;
111
+ }
112
+
113
+ this.keyMaterial.set(keyId, keyMaterial);
114
+ const key = {
115
+ KeyId: keyId,
116
+ KeyArn: keyArn,
117
+ Description,
118
+ KeyUsage,
119
+ KeySpec,
120
+ KeyState: 'Enabled',
121
+ Enabled: true,
122
+ CreationDate: new Date().toISOString(),
123
+ MultiRegion,
124
+ Tags,
125
+ PublicKey: publicKey,
126
+ _keySeed: keyMaterial.toString('hex')
127
+ };
128
+ this.keys.set(keyId, key);
129
+ await this._persistKeys();
130
+ this.logger.info(`KMS: chave criada com sucesso: ${keyId}`, 'kms');
131
+ this.audit.record({ eventName: 'CreateKey', readOnly: false, resources: [{ ARN: keyArn, type: 'AWS::KMS::Key' }], requestParameters: { description: Description, keyUsage: KeyUsage, keySpec: KeySpec } });
132
+ return { KeyMetadata: this._sanitizeKey(key) };
133
+ }
134
+
135
+ async describeKey(params) {
136
+ const keyId = params.KeyId || params.keyId;
137
+ const key = this._requireKey(keyId);
138
+ return { KeyMetadata: this._sanitizeKey(key) };
139
+ }
140
+
141
+ async listKeys(params) {
142
+ const { Limit = 100 } = params || {};
143
+ const keys = Array.from(this.keys.values()).slice(0, Limit);
144
+ // Para o Dashboard, retornamos mais detalhes se for solicitado via admin
145
+ // Mas para o SDK padrão, retornamos apenas o que o SDK espera
146
+ return { Keys: keys.map(k => ({ KeyId: k.KeyId, KeyArn: k.KeyArn })) };
147
+ }
148
+
149
+ listKeysFull() {
150
+ return Array.from(this.keys.values()).map(k => this._sanitizeKey(k));
151
+ }
152
+
153
+
154
+ async enableKey(params) {
155
+ const key = this._requireKey(params.KeyId);
156
+ key.KeyState = 'Enabled'; key.Enabled = true;
157
+ await this._persistKeys();
158
+ return {};
159
+ }
160
+
161
+ async disableKey(params) {
162
+ const key = this._requireKey(params.KeyId);
163
+ key.KeyState = 'Disabled'; key.Enabled = false;
164
+ await this._persistKeys();
165
+ return {};
166
+ }
167
+
168
+ async scheduleKeyDeletion(params) {
169
+ const { KeyId, PendingWindowInDays = 30 } = params;
170
+ const key = this._requireKey(KeyId);
171
+ key.KeyState = 'PendingDeletion';
172
+ key.DeletionDate = new Date(Date.now() + PendingWindowInDays * 86400000).toISOString();
173
+ await this._persistKeys();
174
+ return { KeyId: key.KeyId, DeletionDate: key.DeletionDate };
175
+ }
176
+
177
+ async cancelKeyDeletion(params) {
178
+ const keyId = params.KeyId.startsWith('arn:') ? params.KeyId.split('/').pop() : params.KeyId;
179
+ const key = this.keys.get(keyId);
180
+ if (!key) { const err = new Error('Key not found'); err.code = 'NotFoundException'; throw err; }
181
+ key.KeyState = 'Disabled'; key.DeletionDate = null;
182
+ await this._persistKeys();
183
+ return { KeyId: key.KeyId };
184
+ }
185
+
186
+ async createAlias(params) {
187
+ const { AliasName, TargetKeyId } = params;
188
+ const key = this._requireKey(TargetKeyId);
189
+ if (!AliasName.startsWith('alias/')) {
190
+ const err = new Error('Alias must start with alias/'); err.code = 'InvalidAliasNameException'; throw err;
191
+ }
192
+ const alias = { AliasName, TargetKeyId: key.KeyId, AliasArn: `arn:aws:kms:local:000000000000:${AliasName}`, CreationDate: new Date().toISOString() };
193
+ this.aliases.set(AliasName, alias);
194
+ await this._persistAliases();
195
+ return {};
196
+ }
197
+
198
+ async deleteAlias(params) {
199
+ this.aliases.delete(params.AliasName);
200
+ await this._persistAliases();
201
+ return {};
202
+ }
203
+
204
+ async listAliases(params) {
205
+ const { KeyId } = params || {};
206
+ let aliases = Array.from(this.aliases.values());
207
+ if (KeyId) aliases = aliases.filter(a => a.TargetKeyId === KeyId);
208
+ return { Aliases: aliases };
209
+ }
210
+
211
+ // ===================== CRYPTO OPERATIONS =====================
212
+
213
+ async encrypt(params) {
214
+ const { KeyId, Plaintext, EncryptionContext } = params;
215
+ const key = this._requireKey(KeyId);
216
+ if (key.KeyUsage !== 'ENCRYPT_DECRYPT') {
217
+ const err = new Error('Key not for encryption'); err.code = 'InvalidKeyUsageException'; throw err;
218
+ }
219
+ const material = this.keyMaterial.get(key.KeyId);
220
+ const iv = crypto.randomBytes(12);
221
+ const cipher = crypto.createCipheriv('aes-256-gcm', material.slice(0, 32), iv);
222
+ const plainBuf = Buffer.isBuffer(Plaintext) ? Plaintext : Buffer.from(Plaintext, 'base64');
223
+ const encrypted = Buffer.concat([cipher.update(plainBuf), cipher.final()]);
224
+ const tag = cipher.getAuthTag();
225
+ // Format: iv(12) + tag(16) + ciphertext
226
+ const ciphertext = Buffer.concat([iv, tag, encrypted]);
227
+ this.audit.record({ eventName: 'Encrypt', readOnly: false, isDataEvent: true, resources: [{ ARN: key.KeyArn, type: 'AWS::KMS::Key' }], requestParameters: { keyId: key.KeyId } });
228
+ return {
229
+ KeyId: key.KeyId,
230
+ CiphertextBlob: ciphertext.toString('base64'),
231
+ EncryptionAlgorithm: 'SYMMETRIC_DEFAULT'
232
+ };
233
+ }
234
+
235
+ async decrypt(params) {
236
+ const { KeyId, CiphertextBlob, EncryptionContext } = params;
237
+ let key;
238
+ if (KeyId) {
239
+ key = this._requireKey(KeyId);
240
+ } else {
241
+ // Tentar todas as chaves simétricas
242
+ key = Array.from(this.keys.values()).find(k => k.KeySpec === 'SYMMETRIC_DEFAULT' && k.KeyState === 'Enabled');
243
+ if (!key) { const err = new Error('No key available'); err.code = 'NotFoundException'; throw err; }
244
+ }
245
+ const material = this.keyMaterial.get(key.KeyId);
246
+ const buf = Buffer.isBuffer(CiphertextBlob) ? CiphertextBlob : Buffer.from(CiphertextBlob, 'base64');
247
+ const iv = buf.slice(0, 12);
248
+ const tag = buf.slice(12, 28);
249
+ const ciphertext = buf.slice(28);
250
+ try {
251
+ const decipher = crypto.createDecipheriv('aes-256-gcm', material.slice(0, 32), iv);
252
+ decipher.setAuthTag(tag);
253
+ const decrypted = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
254
+ return { KeyId: key.KeyId, Plaintext: decrypted.toString('base64'), EncryptionAlgorithm: 'SYMMETRIC_DEFAULT' };
255
+ } catch (e) {
256
+ const err = new Error('Decryption failed - invalid ciphertext or wrong key'); err.code = 'InvalidCiphertextException'; throw err;
257
+ }
258
+ }
259
+
260
+ async generateDataKey(params) {
261
+ const { KeyId, KeySpec = 'AES_256', NumberOfBytes } = params;
262
+ const key = this._requireKey(KeyId);
263
+ const dataKeyBytes = NumberOfBytes || (KeySpec === 'AES_128' ? 16 : 32);
264
+ const plaintext = crypto.randomBytes(dataKeyBytes);
265
+ const encrypted = await this.encrypt({ KeyId: key.KeyId, Plaintext: plaintext });
266
+ return {
267
+ KeyId: key.KeyId,
268
+ Plaintext: plaintext.toString('base64'),
269
+ CiphertextBlob: encrypted.CiphertextBlob
270
+ };
271
+ }
272
+
273
+ async generateDataKeyWithoutPlaintext(params) {
274
+ const result = await this.generateDataKey(params);
275
+ const { Plaintext, ...rest } = result;
276
+ return rest;
277
+ }
278
+
279
+ async generateDataKeyPair(params) {
280
+ const { KeyId, KeyPairSpec } = params;
281
+ const key = this._requireKey(KeyId);
282
+ const bits = KeyPairSpec === 'RSA_2048' ? 2048 : 4096;
283
+ const pair = crypto.generateKeyPairSync('rsa', {
284
+ modulusLength: bits,
285
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
286
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
287
+ });
288
+ const encrypted = await this.encrypt({ KeyId: key.KeyId, Plaintext: Buffer.from(pair.privateKey) });
289
+ return {
290
+ KeyId: key.KeyId,
291
+ KeyPairSpec,
292
+ PublicKey: Buffer.from(pair.publicKey).toString('base64'),
293
+ PrivateKeyPlaintext: Buffer.from(pair.privateKey).toString('base64'),
294
+ PrivateKeyCiphertextBlob: encrypted.CiphertextBlob
295
+ };
296
+ }
297
+
298
+ async sign(params) {
299
+ const { KeyId, Message, MessageType = 'RAW', SigningAlgorithm } = params;
300
+ const key = this._requireKey(KeyId);
301
+ if (key.KeyUsage !== 'SIGN_VERIFY') {
302
+ const err = new Error('Key not for signing'); err.code = 'InvalidKeyUsageException'; throw err;
303
+ }
304
+ const material = this.keyMaterial.get(key.KeyId);
305
+ const msgBuf = Buffer.isBuffer(Message) ? Message : Buffer.from(Message, 'base64');
306
+ const sign = crypto.createSign('SHA256');
307
+ sign.update(msgBuf);
308
+ const signature = sign.sign(material.toString(), 'base64');
309
+ return { KeyId: key.KeyId, Signature: signature, SigningAlgorithm };
310
+ }
311
+
312
+ async verify(params) {
313
+ const { KeyId, Message, Signature, SigningAlgorithm } = params;
314
+ const key = this._requireKey(KeyId);
315
+ const material = this.keyMaterial.get(key.KeyId);
316
+ const msgBuf = Buffer.isBuffer(Message) ? Message : Buffer.from(Message, 'base64');
317
+ const verify = crypto.createVerify('SHA256');
318
+ verify.update(msgBuf);
319
+ try {
320
+ const valid = verify.verify(material.toString(), Signature, 'base64');
321
+ return { KeyId: key.KeyId, SignatureValid: valid, SigningAlgorithm };
322
+ } catch { return { KeyId: key.KeyId, SignatureValid: false, SigningAlgorithm }; }
323
+ }
324
+
325
+ async generateRandom(params) {
326
+ const { NumberOfBytes = 32 } = params;
327
+ const bytes = crypto.randomBytes(NumberOfBytes);
328
+ return { Plaintext: bytes.toString('base64') };
329
+ }
330
+
331
+ _sanitizeKey(key) {
332
+ const { _keySeed, PublicKey: pk, ...clean } = key;
333
+ return clean;
334
+ }
335
+
336
+ async reset() {
337
+ this.keys.clear();
338
+ this.aliases.clear();
339
+ this.keyMaterial.clear();
340
+ await this.store.clear('kms');
341
+ }
342
+ }
343
+
344
+ module.exports = { KMSSimulator };