@gugananuvem/aws-local-simulator 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -192
- package/bin/aws-local-simulator.js +62 -62
- package/package.json +8 -5
- package/src/config/config-loader.js +113 -0
- package/src/config/default-config.js +65 -0
- package/src/config/env-loader.js +67 -0
- package/src/index.js +130 -130
- package/src/index.mjs +124 -0
- package/src/server.js +219 -0
- package/src/services/apigateway/index.js +67 -0
- package/src/services/apigateway/server.js +435 -0
- package/src/services/apigateway/simulator.js +1252 -0
- package/src/services/cognito/index.js +66 -0
- package/src/services/cognito/server.js +229 -0
- package/src/services/cognito/simulator.js +848 -0
- package/src/services/dynamodb/index.js +71 -0
- package/src/services/dynamodb/server.js +122 -0
- package/src/services/dynamodb/simulator.js +614 -0
- package/src/services/eventbridge/index.js +85 -0
- package/src/services/index.js +19 -0
- package/src/services/lambda/handler-loader.js +173 -0
- package/src/services/lambda/index.js +73 -0
- package/src/services/lambda/route-registry.js +275 -0
- package/src/services/lambda/server.js +153 -0
- package/src/services/lambda/simulator.js +278 -0
- package/src/services/s3/index.js +70 -0
- package/src/services/s3/server.js +239 -0
- package/src/services/s3/simulator.js +740 -0
- package/src/services/sns/index.js +76 -0
- package/src/services/sqs/index.js +96 -0
- package/src/services/sqs/server.js +274 -0
- package/src/services/sqs/simulator.js +660 -0
- package/src/template/aws-config-template.js +88 -0
- package/src/template/aws-config-template.mjs +91 -0
- package/src/template/config-template.json +165 -0
- package/src/utils/aws-config.js +92 -0
- package/src/utils/local-store.js +68 -0
- package/src/utils/logger.js +60 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognito Service - Simulador de Amazon Cognito
|
|
3
|
+
* Suporta: User Pools, Identity Pools, Authentication, User Management
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const CognitoServer = require('./server');
|
|
7
|
+
const CognitoSimulator = require('./simulator');
|
|
8
|
+
|
|
9
|
+
class CognitoService {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.name = 'cognito';
|
|
13
|
+
this.port = config.ports.cognito || 9229;
|
|
14
|
+
this.server = null;
|
|
15
|
+
this.simulator = null;
|
|
16
|
+
this.isRunning = false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async initialize() {
|
|
20
|
+
const logger = require('../../utils/logger');
|
|
21
|
+
logger.debug(`Inicializando Cognito Service na porta ${this.port}...`);
|
|
22
|
+
|
|
23
|
+
this.simulator = new CognitoSimulator(this.config);
|
|
24
|
+
await this.simulator.initialize();
|
|
25
|
+
|
|
26
|
+
this.server = new CognitoServer(this.port, this.config);
|
|
27
|
+
this.server.simulator = this.simulator;
|
|
28
|
+
|
|
29
|
+
await this.server.initialize();
|
|
30
|
+
|
|
31
|
+
logger.debug('Cognito Service inicializado');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async start() {
|
|
35
|
+
if (this.isRunning) return;
|
|
36
|
+
await this.server.start();
|
|
37
|
+
this.isRunning = true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async stop() {
|
|
41
|
+
if (!this.isRunning) return;
|
|
42
|
+
await this.server.stop();
|
|
43
|
+
this.isRunning = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async reset() {
|
|
47
|
+
await this.simulator.reset();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getStatus() {
|
|
51
|
+
return {
|
|
52
|
+
running: this.isRunning,
|
|
53
|
+
port: this.port,
|
|
54
|
+
endpoint: `http://localhost:${this.port}`,
|
|
55
|
+
userPoolsCount: this.simulator?.getUserPoolsCount() || 0,
|
|
56
|
+
usersCount: this.simulator?.getTotalUsersCount() || 0,
|
|
57
|
+
identityPoolsCount: this.simulator?.getIdentityPoolsCount() || 0
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getSimulator() {
|
|
62
|
+
return this.simulator;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = CognitoService;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognito Server - Servidor HTTP para Cognito API
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const express = require('express');
|
|
6
|
+
const logger = require('../../utils/logger');
|
|
7
|
+
|
|
8
|
+
class CognitoServer {
|
|
9
|
+
constructor(port, config) {
|
|
10
|
+
this.port = port;
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.app = express();
|
|
13
|
+
this.simulator = null;
|
|
14
|
+
this.server = null;
|
|
15
|
+
this.setupMiddlewares();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
setupMiddlewares() {
|
|
19
|
+
this.app.use(express.json());
|
|
20
|
+
|
|
21
|
+
if (logger.currentLogLevel === 'verboso') {
|
|
22
|
+
this.app.use((req, res, next) => {
|
|
23
|
+
const start = Date.now();
|
|
24
|
+
res.on('finish', () => {
|
|
25
|
+
const duration = Date.now() - start;
|
|
26
|
+
logger.verboso(`Cognito: ${req.method} ${req.path} - ${duration}ms`);
|
|
27
|
+
});
|
|
28
|
+
next();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async initialize() {
|
|
34
|
+
this.setupRoutes();
|
|
35
|
+
logger.debug('Cognito Server inicializado');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
setupRoutes() {
|
|
39
|
+
// Health check
|
|
40
|
+
this.app.get('/health', (req, res) => {
|
|
41
|
+
res.json({
|
|
42
|
+
status: 'healthy',
|
|
43
|
+
service: 'cognito-simulator',
|
|
44
|
+
version: '1.0.0'
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// User Pool operations
|
|
49
|
+
this.app.post('/', async (req, res) => {
|
|
50
|
+
const target = req.headers['x-amz-target'];
|
|
51
|
+
if (!target) {
|
|
52
|
+
return res.status(400).json({ error: 'Missing X-Amz-Target header' });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const result = await this.handleRequest(target, req.body);
|
|
57
|
+
res.json(result);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
logger.error('Cognito Error:', error);
|
|
60
|
+
res.status(400).json({
|
|
61
|
+
__type: error.code || 'InternalServerError',
|
|
62
|
+
message: error.message
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Admin endpoints
|
|
68
|
+
this.setupAdminRoutes();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async handleRequest(target, params) {
|
|
72
|
+
const action = target.split('.')[2] || target.split('.')[1];
|
|
73
|
+
|
|
74
|
+
logger.verboso(`Cognito Action: ${action}`);
|
|
75
|
+
|
|
76
|
+
switch(action) {
|
|
77
|
+
// User Pool Management
|
|
78
|
+
case 'CreateUserPool':
|
|
79
|
+
return this.simulator.createUserPool(params);
|
|
80
|
+
case 'ListUserPools':
|
|
81
|
+
return this.simulator.listUserPools(params);
|
|
82
|
+
case 'DescribeUserPool':
|
|
83
|
+
return this.simulator.describeUserPool(params);
|
|
84
|
+
case 'DeleteUserPool':
|
|
85
|
+
return this.simulator.deleteUserPool(params);
|
|
86
|
+
|
|
87
|
+
// User Pool Client Management
|
|
88
|
+
case 'CreateUserPoolClient':
|
|
89
|
+
return this.simulator.createUserPoolClient(params);
|
|
90
|
+
|
|
91
|
+
// User Operations
|
|
92
|
+
case 'SignUp':
|
|
93
|
+
return this.simulator.signUp(params);
|
|
94
|
+
case 'ConfirmSignUp':
|
|
95
|
+
return this.simulator.confirmSignUp(params);
|
|
96
|
+
case 'InitiateAuth':
|
|
97
|
+
return this.simulator.initiateAuth(params);
|
|
98
|
+
case 'GetToken':
|
|
99
|
+
return this.simulator.getToken(params);
|
|
100
|
+
|
|
101
|
+
// Admin Operations
|
|
102
|
+
case 'AdminGetUser':
|
|
103
|
+
return this.simulator.adminGetUser(params);
|
|
104
|
+
case 'AdminCreateUser':
|
|
105
|
+
return this.simulator.adminCreateUser(params);
|
|
106
|
+
case 'AdminSetUserPassword':
|
|
107
|
+
return this.simulator.adminSetUserPassword(params);
|
|
108
|
+
case 'AdminDeleteUser':
|
|
109
|
+
return this.simulator.adminDeleteUser(params);
|
|
110
|
+
|
|
111
|
+
// Identity Pool Operations
|
|
112
|
+
case 'CreateIdentityPool':
|
|
113
|
+
return this.simulator.createIdentityPool(params);
|
|
114
|
+
case 'GetId':
|
|
115
|
+
return this.simulator.getId(params);
|
|
116
|
+
case 'GetCredentialsForIdentity':
|
|
117
|
+
return this.simulator.getCredentialsForIdentity(params);
|
|
118
|
+
|
|
119
|
+
default:
|
|
120
|
+
throw new Error(`Unsupported action: ${action}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
setupAdminRoutes() {
|
|
125
|
+
this.app.get('/__admin/userpools', (req, res) => {
|
|
126
|
+
res.json({
|
|
127
|
+
userPools: this.simulator.getUserPoolsCount(),
|
|
128
|
+
users: this.simulator.getTotalUsersCount(),
|
|
129
|
+
identityPools: this.simulator.getIdentityPoolsCount(),
|
|
130
|
+
activeSessions: this.simulator.getActiveSessionsCount()
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
this.app.get('/__admin/userpools/:poolId/users', (req, res) => {
|
|
135
|
+
const pool = this.simulator.userPools.get(req.params.poolId);
|
|
136
|
+
if (!pool) {
|
|
137
|
+
return res.status(404).json({ error: 'User pool not found' });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const users = [];
|
|
141
|
+
for (const userId of pool.Users) {
|
|
142
|
+
const user = this.simulator.users.get(userId);
|
|
143
|
+
if (user) {
|
|
144
|
+
users.push({
|
|
145
|
+
username: user.Username,
|
|
146
|
+
userId: user.UserId,
|
|
147
|
+
status: user.UserStatus,
|
|
148
|
+
attributes: user.Attributes,
|
|
149
|
+
created: user.CreatedDate
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
res.json(users);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
this.app.get('/__admin/validate-token', (req, res) => {
|
|
158
|
+
const authHeader = req.headers.authorization;
|
|
159
|
+
if (!authHeader) {
|
|
160
|
+
return res.status(401).json({ error: 'No token provided' });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const token = authHeader.replace('Bearer ', '');
|
|
164
|
+
const decoded = this.simulator.verifyAccessToken(token);
|
|
165
|
+
|
|
166
|
+
if (!decoded) {
|
|
167
|
+
return res.status(401).json({ error: 'Invalid token' });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
res.json({ valid: true, payload: decoded });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
this.app.post('/__admin/generate-token', (req, res) => {
|
|
174
|
+
const { username, userPoolId, clientId } = req.body;
|
|
175
|
+
|
|
176
|
+
const userPool = this.simulator.userPools.get(userPoolId);
|
|
177
|
+
if (!userPool) {
|
|
178
|
+
return res.status(404).json({ error: 'User pool not found' });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const user = this.simulator.findUserByUsername(username, null, userPoolId);
|
|
182
|
+
if (!user) {
|
|
183
|
+
return res.status(404).json({ error: 'User not found' });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const accessToken = this.simulator.generateAccessToken(user, userPool, clientId);
|
|
187
|
+
const idToken = this.simulator.generateIdToken(user, userPool, clientId);
|
|
188
|
+
|
|
189
|
+
res.json({
|
|
190
|
+
accessToken,
|
|
191
|
+
idToken,
|
|
192
|
+
expiresIn: 3600
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
start() {
|
|
198
|
+
return new Promise((resolve) => {
|
|
199
|
+
this.server = this.app.listen(this.port, () => {
|
|
200
|
+
logger.info(`🔐 Cognito rodando em http://localhost:${this.port}`);
|
|
201
|
+
resolve();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
stop() {
|
|
207
|
+
return new Promise((resolve) => {
|
|
208
|
+
if (this.server) {
|
|
209
|
+
this.server.close(() => resolve());
|
|
210
|
+
} else {
|
|
211
|
+
resolve();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
getStatus() {
|
|
217
|
+
return {
|
|
218
|
+
running: !!this.server,
|
|
219
|
+
port: this.port,
|
|
220
|
+
endpoint: `http://localhost:${this.port}`,
|
|
221
|
+
userPoolsCount: this.simulator?.getUserPoolsCount() || 0,
|
|
222
|
+
usersCount: this.simulator?.getTotalUsersCount() || 0,
|
|
223
|
+
identityPoolsCount: this.simulator?.getIdentityPoolsCount() || 0,
|
|
224
|
+
activeSessions: this.simulator?.getActiveSessionsCount() || 0
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
module.exports = CognitoServer;
|