@gugananuvem/aws-local-simulator 1.0.12 → 1.0.15

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 (77) hide show
  1. package/README.md +594 -257
  2. package/bin/aws-local-simulator.js +63 -63
  3. package/package.json +21 -10
  4. package/src/config/config-loader.js +114 -114
  5. package/src/config/default-config.js +68 -67
  6. package/src/config/env-loader.js +68 -68
  7. package/src/index.js +146 -130
  8. package/src/index.mjs +123 -123
  9. package/src/server.js +227 -223
  10. package/src/services/apigateway/index.js +73 -68
  11. package/src/services/apigateway/server.js +507 -487
  12. package/src/services/apigateway/simulator.js +1261 -1251
  13. package/src/services/athena/index.js +75 -0
  14. package/src/services/athena/server.js +101 -0
  15. package/src/services/athena/simulador.js +998 -0
  16. package/src/services/athena/simulator.js +346 -0
  17. package/src/services/cloudformation/index.js +106 -0
  18. package/src/services/cloudformation/server.js +417 -0
  19. package/src/services/cloudformation/simulador.js +1045 -0
  20. package/src/services/cloudtrail/index.js +84 -0
  21. package/src/services/cloudtrail/server.js +235 -0
  22. package/src/services/cloudtrail/simulador.js +719 -0
  23. package/src/services/cloudwatch/index.js +84 -0
  24. package/src/services/cloudwatch/server.js +366 -0
  25. package/src/services/cloudwatch/simulador.js +1173 -0
  26. package/src/services/cognito/index.js +79 -65
  27. package/src/services/cognito/server.js +301 -279
  28. package/src/services/cognito/simulator.js +1655 -1115
  29. package/src/services/config/index.js +96 -0
  30. package/src/services/config/server.js +215 -0
  31. package/src/services/config/simulador.js +1260 -0
  32. package/src/services/dynamodb/index.js +74 -70
  33. package/src/services/dynamodb/server.js +125 -121
  34. package/src/services/dynamodb/simulator.js +630 -620
  35. package/src/services/ecs/index.js +65 -65
  36. package/src/services/ecs/server.js +235 -233
  37. package/src/services/ecs/simulator.js +844 -844
  38. package/src/services/eventbridge/index.js +89 -85
  39. package/src/services/eventbridge/server.js +209 -0
  40. package/src/services/eventbridge/simulator.js +684 -0
  41. package/src/services/index.js +45 -19
  42. package/src/services/kms/index.js +75 -0
  43. package/src/services/kms/server.js +67 -0
  44. package/src/services/kms/simulator.js +324 -0
  45. package/src/services/lambda/handler-loader.js +183 -183
  46. package/src/services/lambda/index.js +78 -73
  47. package/src/services/lambda/route-registry.js +274 -274
  48. package/src/services/lambda/server.js +145 -145
  49. package/src/services/lambda/simulator.js +199 -172
  50. package/src/services/parameter-store/index.js +80 -0
  51. package/src/services/parameter-store/server.js +50 -0
  52. package/src/services/parameter-store/simulator.js +201 -0
  53. package/src/services/s3/index.js +73 -69
  54. package/src/services/s3/server.js +329 -238
  55. package/src/services/s3/simulator.js +565 -740
  56. package/src/services/secret-manager/index.js +80 -0
  57. package/src/services/secret-manager/server.js +50 -0
  58. package/src/services/secret-manager/simulator.js +171 -0
  59. package/src/services/sns/index.js +89 -76
  60. package/src/services/sns/server.js +580 -0
  61. package/src/services/sns/simulator.js +1482 -0
  62. package/src/services/sqs/index.js +93 -95
  63. package/src/services/sqs/server.js +349 -345
  64. package/src/services/sqs/simulator.js +441 -441
  65. package/src/services/sts/index.js +37 -37
  66. package/src/services/sts/server.js +144 -142
  67. package/src/services/sts/simulator.js +69 -69
  68. package/src/services/xray/index.js +83 -0
  69. package/src/services/xray/server.js +308 -0
  70. package/src/services/xray/simulador.js +994 -0
  71. package/src/template/aws-config-template.js +87 -87
  72. package/src/template/aws-config-template.mjs +90 -90
  73. package/src/template/config-template.json +203 -203
  74. package/src/utils/aws-config.js +91 -91
  75. package/src/utils/cloudtrail-audit.js +129 -0
  76. package/src/utils/local-store.js +83 -67
  77. package/src/utils/logger.js +59 -59
@@ -0,0 +1,129 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * CloudTrail Audit Helper
5
+ *
6
+ * Utilitário para registrar eventos de API nos simuladores.
7
+ * Cada simulador recebe uma instância via injectDependencies.
8
+ *
9
+ * Uso:
10
+ * this.audit = new CloudTrailAudit('s3.amazonaws.com');
11
+ * // após injeção:
12
+ * this.audit.setTrail(cloudtrailSimulator);
13
+ * // ao executar operação:
14
+ * this.audit.record({ eventName: 'PutObject', readOnly: false, ... });
15
+ */
16
+
17
+ class CloudTrailAudit {
18
+ /**
19
+ * @param {string} eventSource - ex: 's3.amazonaws.com'
20
+ */
21
+ constructor(eventSource) {
22
+ this.eventSource = eventSource;
23
+ this.trail = null;
24
+ }
25
+
26
+ /** @param {Object|null} cloudtrailSimulator */
27
+ setTrail(cloudtrailSimulator) {
28
+ this.trail = cloudtrailSimulator || null;
29
+ }
30
+
31
+ /**
32
+ * Registra um evento se o CloudTrail estiver ativo e o tipo de evento
33
+ * estiver habilitado nos event selectors do trail.
34
+ *
35
+ * @param {Object} params
36
+ * @param {string} params.eventName
37
+ * @param {boolean} [params.readOnly=false]
38
+ * @param {Array} [params.resources=[]]
39
+ * @param {Object} [params.requestParameters=null]
40
+ * @param {Object} [params.responseElements=null]
41
+ * @param {string} [params.username='local-user']
42
+ * @param {string} [params.sourceIPAddress='127.0.0.1']
43
+ */
44
+ record(params = {}) {
45
+ if (!this.trail) return;
46
+
47
+ try {
48
+ // Verifica se há algum trail com logging ativo
49
+ const hasActiveTrail = this._hasActiveTrail();
50
+ if (!hasActiveTrail) return;
51
+
52
+ // Verifica se o evento deve ser gravado (management vs data event)
53
+ if (!this._shouldRecord(params)) return;
54
+
55
+ this.trail.recordEvent({
56
+ eventSource: this.eventSource,
57
+ eventName: params.eventName,
58
+ readOnly: params.readOnly !== undefined ? params.readOnly : false,
59
+ resources: params.resources || [],
60
+ requestParameters: params.requestParameters || null,
61
+ responseElements: params.responseElements || null,
62
+ username: params.username || 'local-user',
63
+ sourceIPAddress: params.sourceIPAddress || '127.0.0.1',
64
+ });
65
+ } catch (_) {
66
+ // Nunca deixa o audit quebrar o fluxo principal
67
+ }
68
+ }
69
+
70
+ _hasActiveTrail() {
71
+ if (!this.trail || !this.trail.trailStatus) return false;
72
+ for (const status of this.trail.trailStatus.values()) {
73
+ if (status.isLogging) return true;
74
+ }
75
+ return false;
76
+ }
77
+
78
+ /**
79
+ * Verifica event selectors para decidir se o evento deve ser gravado.
80
+ * Management events são sempre gravados quando logging está ativo.
81
+ * Data events só são gravados se houver um selector configurado para o recurso.
82
+ */
83
+ _shouldRecord(params) {
84
+ if (!this.trail || !this.trail.eventSelectors) return true;
85
+
86
+ const isDataEvent = params.isDataEvent === true;
87
+
88
+ // Management events: gravados por padrão
89
+ if (!isDataEvent) return true;
90
+
91
+ // Data events: verifica se algum trail tem selector para este eventSource
92
+ for (const [trailName, selectors] of this.trail.eventSelectors.entries()) {
93
+ const status = this.trail.trailStatus.get(trailName);
94
+ if (!status || !status.isLogging) continue;
95
+
96
+ for (const selector of selectors) {
97
+ const dataResources = selector.DataResources || [];
98
+ for (const dr of dataResources) {
99
+ if (this._matchesDataResource(dr)) return true;
100
+ }
101
+ }
102
+ }
103
+
104
+ return false;
105
+ }
106
+
107
+ _matchesDataResource(dataResource) {
108
+ const typeMap = {
109
+ 's3.amazonaws.com': 'AWS::S3::Object',
110
+ 'dynamodb.amazonaws.com': 'AWS::DynamoDB::Table',
111
+ 'kms.amazonaws.com': 'AWS::KMS::Key',
112
+ 'secretsmanager.amazonaws.com': 'AWS::SecretsManager::Secret',
113
+ 'ssm.amazonaws.com': 'AWS::SSM::Parameter',
114
+ 'cognito-idp.amazonaws.com': 'AWS::Cognito::UserPool',
115
+ 'execute-api.amazonaws.com': 'AWS::APIGateway::Stage',
116
+ };
117
+
118
+ const expectedType = typeMap[this.eventSource];
119
+ if (!expectedType) return false;
120
+
121
+ if (dataResource.Type !== expectedType) return false;
122
+
123
+ // Values: wildcard ou ARN específico
124
+ const values = dataResource.Values || [];
125
+ return values.some((v) => v.endsWith('*') || v === expectedType);
126
+ }
127
+ }
128
+
129
+ module.exports = { CloudTrailAudit };
@@ -1,68 +1,84 @@
1
- /**
2
- * Local Store - Persistência em arquivos JSON
3
- */
4
-
5
- const fs = require('fs');
6
- const path = require('path');
7
- const mkdirp = require('mkdirp');
8
-
9
- class LocalStore {
10
- constructor(dataDir) {
11
- this.dataDir = dataDir;
12
- this.ensureDir();
13
- }
14
-
15
- ensureDir() {
16
- if (!fs.existsSync(this.dataDir)) {
17
- mkdirp.sync(this.dataDir);
18
- }
19
- }
20
-
21
- getFilePath(entity) {
22
- return path.join(this.dataDir, `${entity}.json`);
23
- }
24
-
25
- read(entity) {
26
- const filePath = this.getFilePath(entity);
27
- if (!fs.existsSync(filePath)) return [];
28
-
29
- try {
30
- const content = fs.readFileSync(filePath, 'utf8');
31
- return JSON.parse(content);
32
- } catch (error) {
33
- console.error(`Erro ao ler ${entity}:`, error);
34
- return [];
35
- }
36
- }
37
-
38
- write(entity, data) {
39
- const filePath = this.getFilePath(entity);
40
- try {
41
- fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
42
- } catch (error) {
43
- console.error(`Erro ao escrever ${entity}:`, error);
44
- throw error;
45
- }
46
- }
47
-
48
- delete(entity) {
49
- const filePath = this.getFilePath(entity);
50
- if (fs.existsSync(filePath)) {
51
- fs.unlinkSync(filePath);
52
- }
53
- }
54
-
55
- exists(entity) {
56
- const filePath = this.getFilePath(entity);
57
- return fs.existsSync(filePath);
58
- }
59
-
60
- list() {
61
- if (!fs.existsSync(this.dataDir)) return [];
62
- return fs.readdirSync(this.dataDir)
63
- .filter(f => f.endsWith('.json'))
64
- .map(f => f.replace('.json', ''));
65
- }
66
- }
67
-
1
+ /**
2
+ * Local Store - Persistência em arquivos JSON
3
+ */
4
+
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const mkdirp = require('mkdirp');
8
+
9
+ class LocalStore {
10
+ constructor(dataDir) {
11
+ this.dataDir = dataDir;
12
+ this.ensureDir();
13
+ }
14
+
15
+ ensureDir() {
16
+ if (!fs.existsSync(this.dataDir)) {
17
+ mkdirp.sync(this.dataDir);
18
+ }
19
+ }
20
+
21
+ getFilePath(entity) {
22
+ // Suporta subpaths como 'parameter-store/parameters'
23
+ const parts = entity.split('/');
24
+ const dir = parts.length > 1 ? path.join(this.dataDir, ...parts.slice(0, -1)) : this.dataDir;
25
+ if (!fs.existsSync(dir)) mkdirp.sync(dir);
26
+ return path.join(this.dataDir, `${entity}.json`);
27
+ }
28
+
29
+ read(entity) {
30
+ const filePath = this.getFilePath(entity);
31
+ if (!fs.existsSync(filePath)) return [];
32
+
33
+ try {
34
+ const content = fs.readFileSync(filePath, 'utf8');
35
+ return JSON.parse(content);
36
+ } catch (error) {
37
+ console.error(`Erro ao ler ${entity}:`, error);
38
+ return [];
39
+ }
40
+ }
41
+
42
+ write(entity, data, _data2) {
43
+ // Suporta chamada com 3 args: write(entity, null, data) usado por alguns simuladores
44
+ const payload = (data === null || data === undefined) ? _data2 : data;
45
+ const filePath = this.getFilePath(entity);
46
+ try {
47
+ fs.writeFileSync(filePath, JSON.stringify(payload, null, 2));
48
+ } catch (error) {
49
+ console.error(`Erro ao escrever ${entity}:`, error);
50
+ throw error;
51
+ }
52
+ }
53
+
54
+ delete(entity) {
55
+ const filePath = this.getFilePath(entity);
56
+ if (fs.existsSync(filePath)) {
57
+ fs.unlinkSync(filePath);
58
+ }
59
+ }
60
+
61
+ exists(entity) {
62
+ const filePath = this.getFilePath(entity);
63
+ return fs.existsSync(filePath);
64
+ }
65
+
66
+ list() {
67
+ if (!fs.existsSync(this.dataDir)) return [];
68
+ return fs.readdirSync(this.dataDir)
69
+ .filter(f => f.endsWith('.json'))
70
+ .map(f => f.replace('.json', ''));
71
+ }
72
+
73
+ // Aliases async para compatibilidade com simuladores que usam store.load/store.save
74
+ async load(entity) {
75
+ const data = this.read(entity);
76
+ return Array.isArray(data) && data.length === 0 ? null : data;
77
+ }
78
+
79
+ async save(entity, data) {
80
+ this.write(entity, data);
81
+ }
82
+ }
83
+
68
84
  module.exports = LocalStore;
@@ -1,60 +1,60 @@
1
- /**
2
- * Logger configurável
3
- */
4
-
5
- let currentLogLevel = 'info';
6
-
7
- const LOG_LEVELS = {
8
- silent: 0,
9
- error: 1,
10
- warn: 2,
11
- info: 3,
12
- debug: 4,
13
- verboso: 5
14
- };
15
-
16
- class Logger {
17
- static setLevel(level) {
18
- if (LOG_LEVELS[level] !== undefined) {
19
- currentLogLevel = level;
20
- }
21
- }
22
-
23
- static error(...args) {
24
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.error) {
25
- console.error('\x1b[31m%s\x1b[0m', '❌', ...args);
26
- }
27
- }
28
-
29
- static warn(...args) {
30
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.warn) {
31
- console.warn('\x1b[33m%s\x1b[0m', '⚠️', ...args);
32
- }
33
- }
34
-
35
- static info(...args) {
36
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.info) {
37
- console.log('\x1b[36m%s\x1b[0m', 'ℹ️', ...args);
38
- }
39
- }
40
-
41
- static debug(...args) {
42
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.debug) {
43
- console.debug('\x1b[90m%s\x1b[0m', '🔍', ...args);
44
- }
45
- }
46
-
47
- static verboso(...args) {
48
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.verboso) {
49
- console.log('\x1b[35m%s\x1b[0m', '📝', ...args);
50
- }
51
- }
52
-
53
- static success(...args) {
54
- if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.info) {
55
- console.log('\x1b[32m%s\x1b[0m', '✅', ...args);
56
- }
57
- }
58
- }
59
-
1
+ /**
2
+ * Logger configurável
3
+ */
4
+
5
+ let currentLogLevel = 'info';
6
+
7
+ const LOG_LEVELS = {
8
+ silent: 0,
9
+ error: 1,
10
+ warn: 2,
11
+ info: 3,
12
+ debug: 4,
13
+ verboso: 5
14
+ };
15
+
16
+ class Logger {
17
+ static setLevel(level) {
18
+ if (LOG_LEVELS[level] !== undefined) {
19
+ currentLogLevel = level;
20
+ }
21
+ }
22
+
23
+ static error(...args) {
24
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.error) {
25
+ console.error('\x1b[31m%s\x1b[0m', '❌', ...args);
26
+ }
27
+ }
28
+
29
+ static warn(...args) {
30
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.warn) {
31
+ console.warn('\x1b[33m%s\x1b[0m', '⚠️', ...args);
32
+ }
33
+ }
34
+
35
+ static info(...args) {
36
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.info) {
37
+ console.log('\x1b[36m%s\x1b[0m', 'ℹ️', ...args);
38
+ }
39
+ }
40
+
41
+ static debug(...args) {
42
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.debug) {
43
+ console.debug('\x1b[90m%s\x1b[0m', '🔍', ...args);
44
+ }
45
+ }
46
+
47
+ static verboso(...args) {
48
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.verboso) {
49
+ console.log('\x1b[35m%s\x1b[0m', '📝', ...args);
50
+ }
51
+ }
52
+
53
+ static success(...args) {
54
+ if (LOG_LEVELS[currentLogLevel] >= LOG_LEVELS.info) {
55
+ console.log('\x1b[32m%s\x1b[0m', '✅', ...args);
56
+ }
57
+ }
58
+ }
59
+
60
60
  module.exports = Logger;