@gugananuvem/aws-local-simulator 1.0.12 → 1.0.14

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 (57) hide show
  1. package/README.md +235 -11
  2. package/package.json +12 -2
  3. package/src/config/default-config.js +1 -0
  4. package/src/index.js +18 -2
  5. package/src/server.js +36 -32
  6. package/src/services/apigateway/index.js +5 -0
  7. package/src/services/apigateway/server.js +20 -0
  8. package/src/services/apigateway/simulator.js +13 -3
  9. package/src/services/athena/index.js +75 -0
  10. package/src/services/athena/server.js +101 -0
  11. package/src/services/athena/simulador.js +998 -0
  12. package/src/services/athena/simulator.js +346 -0
  13. package/src/services/cloudformation/index.js +106 -0
  14. package/src/services/cloudformation/server.js +417 -0
  15. package/src/services/cloudformation/simulador.js +1045 -0
  16. package/src/services/cloudtrail/index.js +84 -0
  17. package/src/services/cloudtrail/server.js +235 -0
  18. package/src/services/cloudtrail/simulador.js +719 -0
  19. package/src/services/cloudwatch/index.js +84 -0
  20. package/src/services/cloudwatch/server.js +366 -0
  21. package/src/services/cloudwatch/simulador.js +1173 -0
  22. package/src/services/cognito/index.js +5 -0
  23. package/src/services/cognito/simulator.js +4 -0
  24. package/src/services/config/index.js +96 -0
  25. package/src/services/config/server.js +215 -0
  26. package/src/services/config/simulador.js +1260 -0
  27. package/src/services/dynamodb/index.js +7 -3
  28. package/src/services/dynamodb/server.js +4 -2
  29. package/src/services/dynamodb/simulator.js +39 -29
  30. package/src/services/eventbridge/index.js +55 -51
  31. package/src/services/eventbridge/server.js +209 -0
  32. package/src/services/eventbridge/simulator.js +684 -0
  33. package/src/services/index.js +30 -4
  34. package/src/services/kms/index.js +75 -0
  35. package/src/services/kms/server.js +67 -0
  36. package/src/services/kms/simulator.js +324 -0
  37. package/src/services/lambda/index.js +5 -0
  38. package/src/services/lambda/simulator.js +48 -38
  39. package/src/services/parameter-store/index.js +80 -0
  40. package/src/services/parameter-store/server.js +50 -0
  41. package/src/services/parameter-store/simulator.js +201 -0
  42. package/src/services/s3/index.js +7 -3
  43. package/src/services/s3/server.js +20 -13
  44. package/src/services/s3/simulator.js +163 -407
  45. package/src/services/secret-manager/index.js +80 -0
  46. package/src/services/secret-manager/server.js +50 -0
  47. package/src/services/secret-manager/simulator.js +171 -0
  48. package/src/services/sns/index.js +55 -42
  49. package/src/services/sns/server.js +580 -0
  50. package/src/services/sns/simulator.js +1482 -0
  51. package/src/services/sqs/index.js +2 -4
  52. package/src/services/sqs/server.js +4 -2
  53. package/src/services/xray/index.js +83 -0
  54. package/src/services/xray/server.js +308 -0
  55. package/src/services/xray/simulador.js +994 -0
  56. package/src/utils/cloudtrail-audit.js +129 -0
  57. package/src/utils/local-store.js +18 -2
@@ -0,0 +1,346 @@
1
+ 'use strict';
2
+
3
+ const { randomUUID } = require('crypto');
4
+ const { CloudTrailAudit } = require('../../utils/cloudtrail-audit');
5
+
6
+ const ACCOUNT = '000000000000';
7
+ const REGION = 'us-east-1';
8
+
9
+ /**
10
+ * Athena Simulator
11
+ * Suporta: StartQueryExecution, GetQueryExecution, GetQueryResults,
12
+ * StopQueryExecution, ListQueryExecutions, CreateNamedQuery,
13
+ * GetNamedQuery, ListNamedQueries, DeleteNamedQuery,
14
+ * CreateWorkGroup, GetWorkGroup, ListWorkGroups, DeleteWorkGroup
15
+ */
16
+ class AthenaSimulator {
17
+ constructor(config, store, logger) {
18
+ this.config = config;
19
+ this.store = store;
20
+ this.logger = logger;
21
+
22
+ /** @type {Map<string, Object>} queryExecutionId → execution */
23
+ this.queryExecutions = new Map();
24
+
25
+ /** @type {Map<string, Object>} namedQueryId → namedQuery */
26
+ this.namedQueries = new Map();
27
+
28
+ /** @type {Map<string, Object>} workGroupName → workGroup */
29
+ this.workGroups = new Map();
30
+
31
+ this.audit = new CloudTrailAudit('athena.amazonaws.com');
32
+ }
33
+
34
+ async initialize() {
35
+ // Cria workgroup padrão
36
+ if (!this.workGroups.has('primary')) {
37
+ this.workGroups.set('primary', {
38
+ Name: 'primary',
39
+ State: 'ENABLED',
40
+ Description: 'Primary workgroup',
41
+ CreationTime: new Date().toISOString(),
42
+ Configuration: {
43
+ ResultConfiguration: { OutputLocation: 's3://aws-athena-query-results-local/' },
44
+ EnforceWorkGroupConfiguration: false,
45
+ PublishCloudWatchMetricsEnabled: false,
46
+ BytesScannedCutoffPerQuery: 0,
47
+ RequesterPaysEnabled: false,
48
+ },
49
+ });
50
+ }
51
+
52
+ try {
53
+ const data = await this.store.load('athena');
54
+ if (data) {
55
+ if (data.queryExecutions) this.queryExecutions = new Map(Object.entries(data.queryExecutions));
56
+ if (data.namedQueries) this.namedQueries = new Map(Object.entries(data.namedQueries));
57
+ if (data.workGroups) this.workGroups = new Map(Object.entries(data.workGroups));
58
+ this.logger.info(`[Athena] Loaded ${this.queryExecutions.size} executions, ${this.namedQueries.size} named queries`);
59
+ }
60
+ } catch {
61
+ this.logger.debug('[Athena] No persisted data, starting fresh');
62
+ }
63
+ }
64
+
65
+ async _persist() {
66
+ try {
67
+ await this.store.save('athena', {
68
+ queryExecutions: Object.fromEntries(this.queryExecutions),
69
+ namedQueries: Object.fromEntries(this.namedQueries),
70
+ workGroups: Object.fromEntries(this.workGroups),
71
+ });
72
+ } catch (err) {
73
+ this.logger.warn(`[Athena] Failed to persist: ${err.message}`);
74
+ }
75
+ }
76
+
77
+ // ── Query Execution ──────────────────────────────────────────────────────
78
+
79
+ async startQueryExecution(params) {
80
+ const {
81
+ QueryString,
82
+ QueryExecutionContext = {},
83
+ ResultConfiguration = {},
84
+ WorkGroup = 'primary',
85
+ ClientRequestToken,
86
+ } = params;
87
+
88
+ if (!QueryString) throw this._error('InvalidRequestException', 'QueryString is required');
89
+
90
+ const wg = this.workGroups.get(WorkGroup);
91
+ if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} does not exist`);
92
+
93
+ const queryExecutionId = randomUUID();
94
+ const outputLocation = ResultConfiguration.OutputLocation ||
95
+ wg.Configuration?.ResultConfiguration?.OutputLocation ||
96
+ `s3://aws-athena-query-results-local/${queryExecutionId}/`;
97
+
98
+ const execution = {
99
+ QueryExecutionId: queryExecutionId,
100
+ Query: QueryString,
101
+ StatementType: this._detectStatementType(QueryString),
102
+ ResultConfiguration: { OutputLocation: outputLocation },
103
+ QueryExecutionContext,
104
+ Status: {
105
+ State: 'SUCCEEDED',
106
+ SubmissionDateTime: new Date().toISOString(),
107
+ CompletionDateTime: new Date().toISOString(),
108
+ },
109
+ Statistics: {
110
+ EngineExecutionTimeInMillis: Math.floor(Math.random() * 500) + 50,
111
+ DataScannedInBytes: Math.floor(Math.random() * 1024 * 1024),
112
+ TotalExecutionTimeInMillis: Math.floor(Math.random() * 600) + 100,
113
+ QueryQueueTimeInMillis: 10,
114
+ ServiceProcessingTimeInMillis: 20,
115
+ },
116
+ WorkGroup,
117
+ _results: this._generateResults(QueryString),
118
+ };
119
+
120
+ this.queryExecutions.set(queryExecutionId, execution);
121
+ await this._persist();
122
+
123
+ this.audit.record({
124
+ eventName: 'StartQueryExecution',
125
+ readOnly: false,
126
+ resources: [{ ARN: `arn:aws:athena:${REGION}:${ACCOUNT}:workgroup/${WorkGroup}`, type: 'AWS::Athena::WorkGroup' }],
127
+ requestParameters: { queryString: QueryString, workGroup: WorkGroup },
128
+ });
129
+
130
+ this.logger.info(`[Athena] Query started: ${queryExecutionId}`);
131
+ return { QueryExecutionId: queryExecutionId };
132
+ }
133
+
134
+ getQueryExecution({ QueryExecutionId }) {
135
+ const exec = this.queryExecutions.get(QueryExecutionId);
136
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
137
+
138
+ const { _results, ...clean } = exec;
139
+ this.audit.record({
140
+ eventName: 'GetQueryExecution',
141
+ readOnly: true,
142
+ requestParameters: { queryExecutionId: QueryExecutionId },
143
+ });
144
+ return { QueryExecution: clean };
145
+ }
146
+
147
+ getQueryResults({ QueryExecutionId, MaxResults = 1000, NextToken }) {
148
+ const exec = this.queryExecutions.get(QueryExecutionId);
149
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
150
+ if (exec.Status.State !== 'SUCCEEDED') {
151
+ throw this._error('InvalidRequestException', `Query is in state ${exec.Status.State}`);
152
+ }
153
+
154
+ const rows = exec._results || [];
155
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
156
+ const slice = rows.slice(startIdx, startIdx + MaxResults);
157
+
158
+ this.audit.record({
159
+ eventName: 'GetQueryResults',
160
+ readOnly: true,
161
+ requestParameters: { queryExecutionId: QueryExecutionId },
162
+ });
163
+
164
+ return {
165
+ ResultSet: {
166
+ Rows: slice,
167
+ ResultSetMetadata: { ColumnInfo: exec._columnInfo || [] },
168
+ },
169
+ NextToken: rows.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
170
+ };
171
+ }
172
+
173
+ async stopQueryExecution({ QueryExecutionId }) {
174
+ const exec = this.queryExecutions.get(QueryExecutionId);
175
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
176
+
177
+ exec.Status.State = 'CANCELLED';
178
+ exec.Status.CompletionDateTime = new Date().toISOString();
179
+ await this._persist();
180
+
181
+ this.audit.record({ eventName: 'StopQueryExecution', readOnly: false, requestParameters: { queryExecutionId: QueryExecutionId } });
182
+ return {};
183
+ }
184
+
185
+ listQueryExecutions({ MaxResults = 50, NextToken, WorkGroup } = {}) {
186
+ let ids = Array.from(this.queryExecutions.keys());
187
+ if (WorkGroup) ids = ids.filter(id => this.queryExecutions.get(id).WorkGroup === WorkGroup);
188
+
189
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
190
+ const slice = ids.slice(startIdx, startIdx + MaxResults);
191
+
192
+ return {
193
+ QueryExecutionIds: slice,
194
+ NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
195
+ };
196
+ }
197
+
198
+ // ── Named Queries ────────────────────────────────────────────────────────
199
+
200
+ async createNamedQuery(params) {
201
+ const { Name, Description = '', Database, QueryString, WorkGroup = 'primary' } = params;
202
+ if (!Name || !QueryString) throw this._error('InvalidRequestException', 'Name and QueryString are required');
203
+
204
+ const namedQueryId = randomUUID();
205
+ const query = { NamedQueryId: namedQueryId, Name, Description, Database, QueryString, WorkGroup };
206
+ this.namedQueries.set(namedQueryId, query);
207
+ await this._persist();
208
+
209
+ this.audit.record({ eventName: 'CreateNamedQuery', readOnly: false, requestParameters: { name: Name } });
210
+ return { NamedQueryId: namedQueryId };
211
+ }
212
+
213
+ getNamedQuery({ NamedQueryId }) {
214
+ const q = this.namedQueries.get(NamedQueryId);
215
+ if (!q) throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
216
+ return { NamedQuery: q };
217
+ }
218
+
219
+ listNamedQueries({ MaxResults = 50, NextToken, WorkGroup } = {}) {
220
+ let ids = Array.from(this.namedQueries.keys());
221
+ if (WorkGroup) ids = ids.filter(id => this.namedQueries.get(id).WorkGroup === WorkGroup);
222
+
223
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
224
+ const slice = ids.slice(startIdx, startIdx + MaxResults);
225
+
226
+ return {
227
+ NamedQueryIds: slice,
228
+ NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
229
+ };
230
+ }
231
+
232
+ async deleteNamedQuery({ NamedQueryId }) {
233
+ if (!this.namedQueries.has(NamedQueryId)) {
234
+ throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
235
+ }
236
+ this.namedQueries.delete(NamedQueryId);
237
+ await this._persist();
238
+ this.audit.record({ eventName: 'DeleteNamedQuery', readOnly: false, requestParameters: { namedQueryId: NamedQueryId } });
239
+ return {};
240
+ }
241
+
242
+ // ── WorkGroups ───────────────────────────────────────────────────────────
243
+
244
+ async createWorkGroup(params) {
245
+ const { Name, Description = '', Configuration = {}, Tags = [] } = params;
246
+ if (!Name) throw this._error('InvalidRequestException', 'Name is required');
247
+ if (this.workGroups.has(Name)) throw this._error('InvalidRequestException', `WorkGroup ${Name} already exists`);
248
+
249
+ const wg = {
250
+ Name, Description, State: 'ENABLED', Tags,
251
+ CreationTime: new Date().toISOString(),
252
+ Configuration: {
253
+ ResultConfiguration: Configuration.ResultConfiguration || { OutputLocation: `s3://aws-athena-query-results-local/${Name}/` },
254
+ EnforceWorkGroupConfiguration: Configuration.EnforceWorkGroupConfiguration || false,
255
+ PublishCloudWatchMetricsEnabled: Configuration.PublishCloudWatchMetricsEnabled || false,
256
+ BytesScannedCutoffPerQuery: Configuration.BytesScannedCutoffPerQuery || 0,
257
+ RequesterPaysEnabled: Configuration.RequesterPaysEnabled || false,
258
+ },
259
+ };
260
+
261
+ this.workGroups.set(Name, wg);
262
+ await this._persist();
263
+ this.audit.record({ eventName: 'CreateWorkGroup', readOnly: false, requestParameters: { name: Name } });
264
+ return {};
265
+ }
266
+
267
+ getWorkGroup({ WorkGroup }) {
268
+ const wg = this.workGroups.get(WorkGroup);
269
+ if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
270
+ return { WorkGroup: wg };
271
+ }
272
+
273
+ listWorkGroups({ MaxResults = 50, NextToken } = {}) {
274
+ const all = Array.from(this.workGroups.values()).map(({ Name, State, Description, CreationTime }) => ({
275
+ Name, State, Description, CreationTime,
276
+ }));
277
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
278
+ const slice = all.slice(startIdx, startIdx + MaxResults);
279
+ return {
280
+ WorkGroups: slice,
281
+ NextToken: all.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
282
+ };
283
+ }
284
+
285
+ async deleteWorkGroup({ WorkGroup, RecursiveDeleteOption = false }) {
286
+ if (WorkGroup === 'primary') throw this._error('InvalidRequestException', 'Cannot delete primary workgroup');
287
+ if (!this.workGroups.has(WorkGroup)) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
288
+
289
+ if (RecursiveDeleteOption) {
290
+ for (const [id, exec] of this.queryExecutions.entries()) {
291
+ if (exec.WorkGroup === WorkGroup) this.queryExecutions.delete(id);
292
+ }
293
+ }
294
+
295
+ this.workGroups.delete(WorkGroup);
296
+ await this._persist();
297
+ this.audit.record({ eventName: 'DeleteWorkGroup', readOnly: false, requestParameters: { workGroup: WorkGroup } });
298
+ return {};
299
+ }
300
+
301
+ // ── Helpers ──────────────────────────────────────────────────────────────
302
+
303
+ _detectStatementType(query) {
304
+ const q = query.trim().toUpperCase();
305
+ if (q.startsWith('SELECT')) return 'DML';
306
+ if (q.startsWith('CREATE') || q.startsWith('DROP') || q.startsWith('ALTER')) return 'DDL';
307
+ if (q.startsWith('INSERT') || q.startsWith('UPDATE') || q.startsWith('DELETE')) return 'DML';
308
+ return 'UTILITY';
309
+ }
310
+
311
+ _generateResults(query) {
312
+ const q = query.trim().toUpperCase();
313
+ if (!q.startsWith('SELECT')) return [{ Data: [{ VarCharValue: 'OK' }] }];
314
+
315
+ // Gera resultado simulado com header + 2 linhas de exemplo
316
+ return [
317
+ { Data: [{ VarCharValue: 'id' }, { VarCharValue: 'value' }, { VarCharValue: 'timestamp' }] },
318
+ { Data: [{ VarCharValue: '1' }, { VarCharValue: 'sample-data-1' }, { VarCharValue: new Date().toISOString() }] },
319
+ { Data: [{ VarCharValue: '2' }, { VarCharValue: 'sample-data-2' }, { VarCharValue: new Date().toISOString() }] },
320
+ ];
321
+ }
322
+
323
+ _error(code, message) {
324
+ const err = new Error(message);
325
+ err.code = code;
326
+ err.statusCode = 400;
327
+ return err;
328
+ }
329
+
330
+ getStats() {
331
+ return {
332
+ queryExecutions: this.queryExecutions.size,
333
+ namedQueries: this.namedQueries.size,
334
+ workGroups: this.workGroups.size,
335
+ };
336
+ }
337
+
338
+ async reset() {
339
+ this.queryExecutions.clear();
340
+ this.namedQueries.clear();
341
+ this.workGroups.clear();
342
+ await this._persist();
343
+ }
344
+ }
345
+
346
+ module.exports = { AthenaSimulator };
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview CloudFormation Service
5
+ * Porta padrão: 4580
6
+ */
7
+
8
+ const http = require('http');
9
+ const path = require('path');
10
+ const { CloudFormationSimulator } = require('./simulador');
11
+ const { createCloudFormationServer } = require('./server');
12
+ const LocalStore = require('../../utils/local-store');
13
+
14
+ class CloudFormationService {
15
+ constructor(config) {
16
+ this.config = config;
17
+ this.logger = require('../../utils/logger');
18
+ this.name = 'cloudformation';
19
+ this.port = config?.ports?.cloudformation || config?.services?.cloudformation?.port || 4580;
20
+ this.store = null;
21
+ this.simulator = null;
22
+ this._server = null;
23
+ this.isRunning = false;
24
+ }
25
+
26
+ async initialize() {
27
+ this.logger.debug(`Inicializando CloudFormation Service na porta ${this.port}...`);
28
+ const dataDir = process.env.AWS_LOCAL_SIMULATOR_DATA_DIR;
29
+ this.store = new LocalStore(path.join(dataDir, 'cloudformation'));
30
+ this.simulator = new CloudFormationSimulator(this.config, this.store, this.logger);
31
+ await this.simulator.load();
32
+ this.logger.debug('CloudFormation Service inicializado');
33
+ }
34
+
35
+ injectDependencies(server) {
36
+ const ct = server.getService('cloudtrail');
37
+ if (ct?.simulator) this.simulator.audit.setTrail(ct.simulator);
38
+
39
+ const s3 = server.getService('s3');
40
+ if (s3?.simulator) this.simulator.s3Simulator = s3.simulator;
41
+
42
+ const sqs = server.getService('sqs');
43
+ if (sqs?.simulator) this.simulator.sqsSimulator = sqs.simulator;
44
+
45
+ const dynamo = server.getService('dynamodb');
46
+ if (dynamo?.simulator) this.simulator.dynamoSimulator = dynamo.simulator;
47
+
48
+ const kms = server.getService('kms');
49
+ if (kms?.simulator) this.simulator.kmsSimulator = kms.simulator;
50
+
51
+ const secrets = server.getService('secret-manager');
52
+ if (secrets?.simulator) this.simulator.secretsSimulator = secrets.simulator;
53
+
54
+ const params = server.getService('parameter-store');
55
+ if (params?.simulator) this.simulator.parameterStoreSimulator = params.simulator;
56
+
57
+ const athena = server.getService('athena');
58
+ if (athena?.simulator) this.simulator.athenaSimulator = athena.simulator;
59
+ }
60
+
61
+ async start() {
62
+ if (this.isRunning) return;
63
+ const app = createCloudFormationServer(this.simulator, this.config, this.logger);
64
+ this._server = http.createServer(app);
65
+ return new Promise((resolve, reject) => {
66
+ this._server.listen(this.port, () => {
67
+ this.isRunning = true;
68
+ this.logger.debug(`CloudFormation rodando na porta ${this.port}`);
69
+ resolve();
70
+ });
71
+ this._server.once('error', reject);
72
+ });
73
+ }
74
+
75
+ async stop() {
76
+ if (!this.isRunning || !this._server) return;
77
+ return new Promise((resolve, reject) => {
78
+ this._server.close((err) => {
79
+ if (err) return reject(err);
80
+ this.isRunning = false;
81
+ resolve();
82
+ });
83
+ });
84
+ }
85
+
86
+ async reset() {
87
+ await this.simulator.reset();
88
+ }
89
+
90
+ getStatus() {
91
+ const stats = this.simulator?.getStats() || {};
92
+ return {
93
+ running: this.isRunning,
94
+ port: this.port,
95
+ endpoint: `http://localhost:${this.port}`,
96
+ ...stats,
97
+ };
98
+ }
99
+
100
+ getSimulator() { return this.simulator; }
101
+
102
+ async createStack(params) { return this.simulator.createStack(params); }
103
+ describeStacks(params) { return this.simulator.describeStacks(params); }
104
+ }
105
+
106
+ module.exports = CloudFormationService;