@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
@@ -2,18 +2,20 @@
2
2
  * Lambda Simulator - Simula execução de funções Lambda
3
3
  */
4
4
 
5
- const HandlerLoader = require('./handler-loader');
6
- const logger = require('../../utils/logger');
5
+ const HandlerLoader = require("./handler-loader");
6
+ const logger = require("../../utils/logger");
7
+ const { CloudTrailAudit } = require("../../utils/cloudtrail-audit");
7
8
 
8
9
  class LambdaSimulator {
9
10
  constructor(config) {
10
11
  this.config = config;
11
12
  this.lambdas = new Map(); // functionName -> { handler, env, config }
12
13
  this.environment = { ...process.env };
14
+ this.audit = new CloudTrailAudit("lambda.amazonaws.com");
13
15
  }
14
16
 
15
17
  async initialize() {
16
- logger.debug('Inicializando Lambda Simulator...');
18
+ logger.debug("Inicializando Lambda Simulator...");
17
19
 
18
20
  if (this.config.lambdas && this.config.lambdas.length > 0) {
19
21
  for (const lambdaConfig of this.config.lambdas) {
@@ -26,7 +28,7 @@ class LambdaSimulator {
26
28
 
27
29
  async registerLambda(lambdaConfig) {
28
30
  try {
29
- const { name, handler: handlerPath, env = {}, type = 'auto' } = lambdaConfig;
31
+ const { name, handler: handlerPath, env = {}, type = "auto" } = lambdaConfig;
30
32
 
31
33
  if (!name) {
32
34
  logger.warn(`Lambda sem nome ignorada: ${JSON.stringify(lambdaConfig)}`);
@@ -34,25 +36,29 @@ class LambdaSimulator {
34
36
  }
35
37
 
36
38
  const handler = await HandlerLoader.load(handlerPath, type);
37
-
38
- this.lambdas.set(name, {
39
- name,
40
- handler,
41
- handlerPath,
42
- handlerName: handler.name || 'anonymous',
43
- env,
44
- type,
45
- registeredAt: new Date().toISOString()
46
- });
47
-
39
+ if (handler != undefined) {
40
+ this.lambdas.set(name, {
41
+ name,
42
+ handler,
43
+ handlerPath,
44
+ handlerName: handler.name || "anonymous",
45
+ env,
46
+ type,
47
+ registeredAt: new Date().toISOString(),
48
+ });
49
+ }
48
50
  logger.debug(`✅ Lambda registrada: ${name} -> ${handlerPath}`);
49
- } catch (error) {
50
- logger.error(`❌ Erro ao registrar Lambda ${lambdaConfig.name}:`, error);
51
- throw error;
51
+ } catch (error) {
52
+ if (error.message.indexOf("Handler não encontrado") == -1){
53
+ logger.error(`Erro ao registrar Lambda ${lambdaConfig.name}:`, error);
54
+ throw error;
55
+ }else{
56
+ logger.error(`Erro ao registrar Lambda ${lambdaConfig.name}:`);
57
+ }
52
58
  }
53
59
  }
54
60
 
55
- async invoke(functionName, event, invocationType = 'RequestResponse') {
61
+ async invoke(functionName, event, invocationType = "RequestResponse") {
56
62
  const lambda = this.lambdas.get(functionName);
57
63
 
58
64
  if (!lambda) {
@@ -62,14 +68,18 @@ class LambdaSimulator {
62
68
  this.applyEnvironment(lambda.env);
63
69
  logger.debug(`🎯 Invocando Lambda: ${functionName}`);
64
70
 
65
- if (invocationType === 'Event') {
66
- this.executeHandler(lambda.handler, event).catch(err =>
67
- logger.error(`❌ Async Lambda error (${functionName}):`, err)
68
- );
71
+ if (invocationType === "Event") {
72
+ this.executeHandler(lambda.handler, event).catch((err) => logger.error(`❌ Async Lambda error (${functionName}):`, err));
69
73
  return { StatusCode: 202 };
70
74
  }
71
75
 
72
76
  const result = await this.executeHandler(lambda.handler, event);
77
+ this.audit.record({
78
+ eventName: "Invoke",
79
+ readOnly: false,
80
+ resources: [{ ARN: `arn:aws:lambda:local:000000000000:function:${functionName}`, type: "AWS::Lambda::Function" }],
81
+ requestParameters: { functionName, invocationType },
82
+ });
73
83
  return { StatusCode: result.statusCode || 200, Payload: result };
74
84
  }
75
85
 
@@ -79,10 +89,10 @@ class LambdaSimulator {
79
89
  const result = await handler(event, context);
80
90
  return result;
81
91
  } catch (error) {
82
- logger.error('❌ Erro no handler:', error);
92
+ logger.error("❌ Erro no handler:", error);
83
93
  return {
84
94
  statusCode: 500,
85
- body: JSON.stringify({ error: 'Internal Server Error', message: error.message })
95
+ body: JSON.stringify({ error: "Internal Server Error", message: error.message }),
86
96
  };
87
97
  }
88
98
  }
@@ -90,16 +100,16 @@ class LambdaSimulator {
90
100
  createContext() {
91
101
  return {
92
102
  awsRequestId: Math.random().toString(36).substring(7),
93
- functionName: 'local-lambda',
94
- functionVersion: '$LATEST',
95
- invokedFunctionArn: 'arn:aws:lambda:local:000000000000:function:local-lambda',
96
- memoryLimitInMB: '1024',
97
- logGroupName: '/aws/lambda/local-lambda',
98
- logStreamName: 'local-stream',
103
+ functionName: "local-lambda",
104
+ functionVersion: "$LATEST",
105
+ invokedFunctionArn: "arn:aws:lambda:local:000000000000:function:local-lambda",
106
+ memoryLimitInMB: "1024",
107
+ logGroupName: "/aws/lambda/local-lambda",
108
+ logStreamName: "local-stream",
99
109
  getRemainingTimeInMillis: () => 30000,
100
110
  callbackWaitsForEmptyEventLoop: true,
101
111
  identity: null,
102
- clientContext: null
112
+ clientContext: null,
103
113
  };
104
114
  }
105
115
 
@@ -120,13 +130,13 @@ class LambdaSimulator {
120
130
  }
121
131
 
122
132
  listLambdas() {
123
- return Array.from(this.lambdas.values()).map(l => ({
133
+ return Array.from(this.lambdas.values()).map((l) => ({
124
134
  name: l.name,
125
135
  handlerName: l.handlerName,
126
136
  handlerPath: l.handlerPath,
127
137
  type: l.type,
128
138
  env: l.env,
129
- registeredAt: l.registeredAt
139
+ registeredAt: l.registeredAt,
130
140
  }));
131
141
  }
132
142
 
@@ -139,13 +149,13 @@ class LambdaSimulator {
139
149
  }
140
150
 
141
151
  async reloadLambdas() {
142
- logger.info('🔄 Recarregando Lambdas...');
152
+ logger.info("🔄 Recarregando Lambdas...");
143
153
 
144
154
  for (const [name, lambda] of this.lambdas.entries()) {
145
155
  try {
146
156
  const newHandler = await HandlerLoader.reload(lambda.handlerPath, lambda.type);
147
157
  lambda.handler = newHandler;
148
- lambda.handlerName = newHandler.name || 'anonymous';
158
+ lambda.handlerName = newHandler.name || "anonymous";
149
159
  logger.debug(`✅ Lambda recarregada: ${name}`);
150
160
  } catch (error) {
151
161
  logger.error(`❌ Erro ao recarregar Lambda ${name}:`, error);
@@ -158,14 +168,14 @@ class LambdaSimulator {
158
168
  getStats() {
159
169
  return {
160
170
  totalLambdas: this.lambdas.size,
161
- lambdas: this.listLambdas().map(l => ({ name: l.name, handler: l.handlerName }))
171
+ lambdas: this.listLambdas().map((l) => ({ name: l.name, handler: l.handlerName })),
162
172
  };
163
173
  }
164
174
 
165
175
  async reset() {
166
176
  await this.reloadLambdas();
167
177
  this.environment = { ...process.env };
168
- logger.debug('Lambda: Estado resetado');
178
+ logger.debug("Lambda: Estado resetado");
169
179
  }
170
180
  }
171
181
 
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Parameter Store Service
5
+ * Porta padrão: 4002
6
+ */
7
+
8
+ const http = require('http');
9
+ const path = require('path');
10
+ const { ParameterStoreSimulator } = require('./simulator');
11
+ const { ParameterStoreServer } = require('./server');
12
+ const LocalStore = require('../../utils/local-store');
13
+
14
+ class ParameterStoreService {
15
+ constructor(config) {
16
+ this.config = config;
17
+ this.logger = require('../../utils/logger');
18
+ this.name = 'parameter-store';
19
+ this.port = config?.ports?.parameterStore || config?.services?.parameterStore?.port || 4002;
20
+ this.store = null;
21
+ this.simulator = null;
22
+ this.httpServer = null;
23
+ this.isRunning = false;
24
+ }
25
+
26
+ async initialize() {
27
+ this.logger.debug(`Inicializando Parameter Store Service na porta ${this.port}...`);
28
+ const dataDir = process.env.AWS_LOCAL_SIMULATOR_DATA_DIR;
29
+ this.store = new LocalStore(path.join(dataDir, 'parameter-store'));
30
+ this.simulator = new ParameterStoreSimulator(this.store, this.logger, this.config);
31
+ await this.simulator.initialize();
32
+ this.app = new ParameterStoreServer(this.simulator, this.logger, this.config).getApp();
33
+ this.logger.debug('Parameter Store Service inicializado');
34
+ }
35
+
36
+ injectDependencies(server) {
37
+ const ct = server.getService('cloudtrail');
38
+ if (ct?.simulator) this.simulator.audit.setTrail(ct.simulator);
39
+ }
40
+
41
+ async start() {
42
+ if (this.isRunning) return;
43
+ return new Promise((resolve, reject) => {
44
+ this.httpServer = http.createServer(this.app);
45
+ this.httpServer.listen(this.port, () => {
46
+ this.isRunning = true;
47
+ this.logger.debug(`Parameter Store rodando na porta ${this.port}`);
48
+ resolve();
49
+ });
50
+ this.httpServer.on('error', reject);
51
+ });
52
+ }
53
+
54
+ async stop() {
55
+ if (!this.isRunning || !this.httpServer) return;
56
+ return new Promise((resolve) => {
57
+ this.httpServer.close(() => {
58
+ this.isRunning = false;
59
+ resolve();
60
+ });
61
+ });
62
+ }
63
+
64
+ async reset() {
65
+ await this.simulator.reset();
66
+ }
67
+
68
+ getStatus() {
69
+ return {
70
+ running: this.isRunning,
71
+ port: this.port,
72
+ endpoint: `http://localhost:${this.port}`,
73
+ parameters: this.simulator?.parameters.size || 0,
74
+ };
75
+ }
76
+
77
+ getSimulator() { return this.simulator; }
78
+ }
79
+
80
+ module.exports = { ParameterStoreService };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const express = require('express');
4
+ const cors = require('cors');
5
+
6
+ class ParameterStoreServer {
7
+ constructor(simulator, logger, config) {
8
+ this.simulator = simulator; this.logger = logger; this.config = config;
9
+ this.app = express();
10
+ this._setupMiddleware(); this._setupRoutes();
11
+ }
12
+ _setupMiddleware() {
13
+ if (this.config.cors?.enabled !== false) this.app.use(cors({ origin: this.config.cors?.origin || '*' }));
14
+ this.app.use(express.json({ limit: '5mb', type: ['application/json', 'application/x-amz-json-1.1'] }));
15
+ }
16
+ _getOperation(target) {
17
+ const map = {
18
+ 'AmazonSSM.PutParameter': 'putParameter',
19
+ 'AmazonSSM.GetParameter': 'getParameter',
20
+ 'AmazonSSM.GetParameters': 'getParameters',
21
+ 'AmazonSSM.GetParametersByPath': 'getParametersByPath',
22
+ 'AmazonSSM.DeleteParameter': 'deleteParameter',
23
+ 'AmazonSSM.DeleteParameters': 'deleteParameters',
24
+ 'AmazonSSM.DescribeParameters': 'describeParameters',
25
+ 'AmazonSSM.GetParameterHistory': 'getParameterHistory',
26
+ 'AmazonSSM.AddTagsToResource': 'addTagsToResource',
27
+ 'AmazonSSM.RemoveTagsFromResource': 'removeTagsFromResource',
28
+ };
29
+ return map[target];
30
+ }
31
+ _setupRoutes() {
32
+ this.app.get('/__admin/health', (req, res) => res.json({ status: 'healthy', service: 'parameter-store', timestamp: new Date().toISOString() }));
33
+ this.app.post('/', async (req, res) => {
34
+ const target = req.headers['x-amz-target'];
35
+ const operation = this._getOperation(target);
36
+ if (!operation) return res.status(400).json({ __type: 'InvalidAction', message: `Unknown: ${target}` });
37
+ try {
38
+ const result = await this.simulator[operation](req.body || {});
39
+ res.json(result || {});
40
+ } catch (err) {
41
+ this.logger.error(`ParameterStore ${target}: ${err.message}`, 'parameter-store');
42
+ const statusCodes = { ParameterNotFound: 400, ParameterAlreadyExists: 400, ParameterPatternMismatch: 400 };
43
+ res.status(statusCodes[err.code] || 500).json({ __type: err.code || 'InternalServerError', message: err.message });
44
+ }
45
+ });
46
+ }
47
+ getApp() { return this.app; }
48
+ }
49
+
50
+ module.exports = { ParameterStoreServer };
@@ -0,0 +1,201 @@
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
+ * Parameter Store (SSM) Simulator
9
+ */
10
+ class ParameterStoreSimulator {
11
+ constructor(store, logger, config) {
12
+ this.store = store; this.logger = logger; this.config = config;
13
+ this.parameters = new Map();
14
+ this.history = new Map();
15
+ this.audit = new CloudTrailAudit('ssm.amazonaws.com');
16
+ }
17
+
18
+ async initialize() {
19
+ try {
20
+ const params = await this.store.read('parameter-store/parameters');
21
+ if (Array.isArray(params)) for (const p of params) this.parameters.set(p.Name, p);
22
+ const history = await this.store.read('parameter-store/history');
23
+ if (Array.isArray(history)) for (const h of history) this.history.set(h.name, h.versions);
24
+ this.logger.info('ParameterStore: dados carregados', 'parameter-store');
25
+ } catch { this.logger.debug('ParameterStore: sem dados anteriores', 'parameter-store'); }
26
+ }
27
+
28
+ async _persist() {
29
+ await this.store.write('parameter-store/parameters', null, Array.from(this.parameters.values()));
30
+ const histArr = Array.from(this.history.entries()).map(([name, versions]) => ({ name, versions }));
31
+ await this.store.write('parameter-store/history', null, histArr);
32
+ }
33
+
34
+ _require(name) {
35
+ const p = this.parameters.get(name);
36
+ if (!p) { const err = new Error(`Parameter not found: ${name}`); err.code = 'ParameterNotFound'; throw err; }
37
+ return p;
38
+ }
39
+
40
+ async putParameter(params) {
41
+ const { Name, Value, Type = 'String', Description, Overwrite, Tags = [], KeyId, AllowedPattern, DataType = 'text' } = params;
42
+ if (this.parameters.has(Name) && !Overwrite) {
43
+ const err = new Error(`Parameter already exists: ${Name}`); err.code = 'ParameterAlreadyExists'; throw err;
44
+ }
45
+ if (AllowedPattern && !new RegExp(AllowedPattern).test(Value)) {
46
+ const err = new Error(`Value doesn't match pattern: ${AllowedPattern}`); err.code = 'ParameterPatternMismatch'; throw err;
47
+ }
48
+ const existing = this.parameters.get(Name);
49
+ const version = existing ? existing.Version + 1 : 1;
50
+ const storedValue = Type === 'SecureString' ? this._encrypt(Value) : Value;
51
+ const param = {
52
+ Name, Value: storedValue, Type, Description: Description || '', Version: version,
53
+ LastModifiedDate: new Date().toISOString(),
54
+ LastModifiedUser: 'local',
55
+ ARN: `arn:aws:ssm:local:000000000000:parameter${Name}`,
56
+ DataType, Tags: Tags || [], KeyId: Type === 'SecureString' ? (KeyId || 'aws/ssm') : undefined
57
+ };
58
+ this.parameters.set(Name, param);
59
+ // History
60
+ if (!this.history.has(Name)) this.history.set(Name, []);
61
+ this.history.get(Name).push({ ...param, Value });
62
+ await this._persist();
63
+ this.logger.info(`ParameterStore: parâmetro definido: ${Name}`, 'parameter-store');
64
+ this.audit.record({ eventName: 'PutParameter', readOnly: false, isDataEvent: true, resources: [{ ARN: param.ARN, type: 'AWS::SSM::Parameter' }], requestParameters: { name: Name, type: Type } });
65
+ return { Version: version, Tier: 'Standard' };
66
+ }
67
+
68
+ async getParameter(params) {
69
+ const { Name, WithDecryption } = params;
70
+ const param = this._require(Name);
71
+ const value = (WithDecryption && param.Type === 'SecureString') ? this._decrypt(param.Value) : param.Value;
72
+ this.audit.record({ eventName: 'GetParameter', readOnly: true, isDataEvent: true, resources: [{ ARN: param.ARN, type: 'AWS::SSM::Parameter' }], requestParameters: { name: Name } });
73
+ return { Parameter: { ...param, Value: value } };
74
+ }
75
+
76
+ async getParameters(params) {
77
+ const { Names, WithDecryption } = params;
78
+ const found = []; const invalid = [];
79
+ for (const name of Names) {
80
+ try {
81
+ const param = this._require(name);
82
+ const value = (WithDecryption && param.Type === 'SecureString') ? this._decrypt(param.Value) : param.Value;
83
+ found.push({ ...param, Value: value });
84
+ } catch { invalid.push(name); }
85
+ }
86
+ return { Parameters: found, InvalidParameters: invalid };
87
+ }
88
+
89
+ async getParametersByPath(params) {
90
+ const { Path, Recursive, WithDecryption, MaxResults = 10, NextToken, ParameterFilters = [] } = params;
91
+ let results = Array.from(this.parameters.values()).filter(p => {
92
+ if (Recursive) return p.Name.startsWith(Path);
93
+ const rest = p.Name.slice(Path.length);
94
+ return p.Name.startsWith(Path) && !rest.includes('/');
95
+ });
96
+ for (const filter of ParameterFilters) {
97
+ if (filter.Key === 'Type') results = results.filter(p => filter.Values.includes(p.Type));
98
+ }
99
+ let startIdx = 0;
100
+ if (NextToken) startIdx = parseInt(NextToken);
101
+ const slice = results.slice(startIdx, startIdx + MaxResults);
102
+ return {
103
+ Parameters: slice.map(p => ({
104
+ ...p, Value: (WithDecryption && p.Type === 'SecureString') ? this._decrypt(p.Value) : p.Value
105
+ })),
106
+ NextToken: results.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined
107
+ };
108
+ }
109
+
110
+ async deleteParameter(params) {
111
+ const p = this._require(params.Name);
112
+ this.parameters.delete(params.Name);
113
+ await this._persist();
114
+ this.audit.record({ eventName: 'DeleteParameter', readOnly: false, resources: [{ ARN: p.ARN, type: 'AWS::SSM::Parameter' }], requestParameters: { name: params.Name } });
115
+ return {};
116
+ }
117
+
118
+ async deleteParameters(params) {
119
+ const { Names } = params;
120
+ const deleted = []; const invalid = [];
121
+ for (const name of Names) {
122
+ if (this.parameters.has(name)) { this.parameters.delete(name); deleted.push(name); }
123
+ else invalid.push(name);
124
+ }
125
+ await this._persist();
126
+ return { DeletedParameters: deleted, InvalidParameters: invalid };
127
+ }
128
+
129
+ async describeParameters(params) {
130
+ const { Filters = [], ParameterFilters = [], MaxResults = 50, NextToken } = params || {};
131
+ let results = Array.from(this.parameters.values());
132
+ for (const f of Filters) {
133
+ if (f.Key === 'Name') results = results.filter(p => f.Values.some(v => p.Name.includes(v)));
134
+ if (f.Key === 'Type') results = results.filter(p => f.Values.includes(p.Type));
135
+ }
136
+ let startIdx = 0;
137
+ if (NextToken) startIdx = parseInt(NextToken);
138
+ const slice = results.slice(startIdx, startIdx + MaxResults);
139
+ return {
140
+ Parameters: slice.map(({ Value, ...p }) => p),
141
+ NextToken: results.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined
142
+ };
143
+ }
144
+
145
+ async getParameterHistory(params) {
146
+ const { Name, MaxResults = 50, NextToken } = params;
147
+ this._require(Name);
148
+ const history = this.history.get(Name) || [];
149
+ let startIdx = 0;
150
+ if (NextToken) startIdx = parseInt(NextToken);
151
+ const slice = history.slice(startIdx, startIdx + MaxResults);
152
+ return {
153
+ Parameters: slice,
154
+ NextToken: history.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined
155
+ };
156
+ }
157
+
158
+ async addTagsToResource(params) {
159
+ const { ResourceType, ResourceId, Tags } = params;
160
+ if (ResourceType === 'Parameter') {
161
+ const param = this._require(ResourceId);
162
+ for (const tag of Tags) { const idx = param.Tags.findIndex(t => t.Key === tag.Key); if (idx >= 0) param.Tags[idx] = tag; else param.Tags.push(tag); }
163
+ await this._persist();
164
+ }
165
+ return {};
166
+ }
167
+
168
+ async removeTagsFromResource(params) {
169
+ const { ResourceType, ResourceId, TagKeys } = params;
170
+ if (ResourceType === 'Parameter') {
171
+ const param = this._require(ResourceId);
172
+ param.Tags = param.Tags.filter(t => !TagKeys.includes(t.Key));
173
+ await this._persist();
174
+ }
175
+ return {};
176
+ }
177
+
178
+ _encrypt(value) {
179
+ const key = crypto.createHash('sha256').update('local-ssm-key').digest();
180
+ const iv = crypto.randomBytes(12);
181
+ const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
182
+ const enc = Buffer.concat([cipher.update(value, 'utf8'), cipher.final()]);
183
+ const tag = cipher.getAuthTag();
184
+ return Buffer.concat([iv, tag, enc]).toString('base64');
185
+ }
186
+
187
+ _decrypt(encrypted) {
188
+ try {
189
+ const key = crypto.createHash('sha256').update('local-ssm-key').digest();
190
+ const buf = Buffer.from(encrypted, 'base64');
191
+ const iv = buf.slice(0, 12); const tag = buf.slice(12, 28); const enc = buf.slice(28);
192
+ const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
193
+ decipher.setAuthTag(tag);
194
+ return Buffer.concat([decipher.update(enc), decipher.final()]).toString('utf8');
195
+ } catch { return encrypted; }
196
+ }
197
+
198
+ async reset() { this.parameters.clear(); this.history.clear(); await this.store.clear('parameter-store'); }
199
+ }
200
+
201
+ module.exports = { ParameterStoreSimulator };
@@ -20,10 +20,9 @@ class S3Service {
20
20
  const logger = require('../../utils/logger');
21
21
  logger.debug(`Inicializando S3 Service na porta ${this.port}...`);
22
22
 
23
- // Cria o simulador
24
23
  this.simulator = new S3Simulator(this.config);
25
-
26
- // Cria o servidor HTTP
24
+ await this.simulator.initialize();
25
+
27
26
  this.server = new S3Server(this.port, this.config);
28
27
  this.server.simulator = this.simulator;
29
28
 
@@ -32,6 +31,11 @@ class S3Service {
32
31
  logger.debug('S3 Service inicializado');
33
32
  }
34
33
 
34
+ injectDependencies(server) {
35
+ const ct = server.getService('cloudtrail');
36
+ if (ct?.simulator) this.simulator.audit.setTrail(ct.simulator);
37
+ }
38
+
35
39
  async start() {
36
40
  if (this.isRunning) return;
37
41
  await this.server.start();
@@ -17,8 +17,8 @@ class S3Server {
17
17
  }
18
18
 
19
19
  setupMiddlewares() {
20
- this.app.use(express.json({ limit: '100mb' }));
21
- this.app.use(express.raw({ type: 'application/octet-stream', limit: '100mb' }));
20
+ // Captura raw body como Buffer para qualquer content-type
21
+ this.app.use(express.raw({ type: () => true, limit: '100mb' }));
22
22
  this.app.use(express.text({ limit: '100mb' }));
23
23
 
24
24
  // Logging de requisições
@@ -35,8 +35,10 @@ class S3Server {
35
35
  }
36
36
 
37
37
  async initialize() {
38
- this.simulator = new S3Simulator(this.config);
39
- await this.simulator.initialize();
38
+ if (!this.simulator) {
39
+ this.simulator = new S3Simulator(this.config);
40
+ await this.simulator.initialize();
41
+ }
40
42
  this.setupRoutes();
41
43
  }
42
44
 
@@ -80,15 +82,20 @@ class S3Server {
80
82
 
81
83
  // Upload object
82
84
  this.app.put('/:bucket/*', (req, res) => {
83
- const bucket = req.params.bucket;
84
- const key = req.params[0];
85
- const result = this.simulator.putObject(bucket, key, req.body, req.headers);
86
-
87
- if (result.error) {
88
- res.status(result.status).send(this.simulator.generateErrorResponse(result.error.code, result.error.message));
89
- } else {
90
- res.set('ETag', `"${result.etag}"`);
91
- res.status(200).send();
85
+ try {
86
+ const bucket = req.params.bucket;
87
+ const key = req.params[0];
88
+ const result = this.simulator.putObject(bucket, key, req.body, req.headers);
89
+
90
+ if (result.error) {
91
+ res.status(result.status).send(this.simulator.generateErrorResponse(result.error.code, result.error.message));
92
+ } else {
93
+ res.set('ETag', `"${result.etag}"`);
94
+ res.status(200).send();
95
+ }
96
+ } catch (err) {
97
+ logger.error('S3 putObject error:', err);
98
+ res.status(500).send(this.simulator.generateErrorResponse('InternalError', err.message));
92
99
  }
93
100
  });
94
101