redwax-cli 0.0.0-stage → 1.0.0
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 +90 -3
- package/dashboard/apple-touch-icon.png +0 -0
- package/dashboard/assets/data-DnUM1Mpn.js +17 -0
- package/dashboard/assets/index-Cb3ZdSPZ.js +191 -0
- package/dashboard/assets/index-GSeTo6vS.css +1 -0
- package/dashboard/assets/radix-Cu8wEyce.js +41 -0
- package/dashboard/assets/react-Cnjii2my.js +59 -0
- package/dashboard/favicon.svg +12 -0
- package/dashboard/index.html +37 -0
- package/package.json +68 -6
- package/scripts/bundle.cjs +56 -0
- package/server/package.json +5 -0
- package/server/src/app.js +141 -0
- package/server/src/config/env.js +102 -0
- package/server/src/config/keys.js +81 -0
- package/server/src/config/metrics.js +161 -0
- package/server/src/config/openapi.js +507 -0
- package/server/src/controllers/auth.controller.js +115 -0
- package/server/src/controllers/project.controller.js +154 -0
- package/server/src/controllers/secret.controller.js +138 -0
- package/server/src/domain.js +49 -0
- package/server/src/middleware/auth.middleware.js +26 -0
- package/server/src/middleware/rateLimit.middleware.js +49 -0
- package/server/src/middleware/rbac.middleware.js +47 -0
- package/server/src/middleware/validate.middleware.js +35 -0
- package/server/src/models/AuditLog.model.js +32 -0
- package/server/src/models/Project.model.js +33 -0
- package/server/src/models/Secret.model.js +30 -0
- package/server/src/models/SecretVersion.model.js +25 -0
- package/server/src/models/User.model.js +31 -0
- package/server/src/routes/auth.routes.js +15 -0
- package/server/src/routes/project.routes.js +45 -0
- package/server/src/routes/secret.routes.js +44 -0
- package/server/src/services/audit.service.js +65 -0
- package/server/src/services/auth.service.js +130 -0
- package/server/src/services/crypto.service.js +73 -0
- package/server/src/services/project.service.js +178 -0
- package/server/src/services/secret.service.js +198 -0
- package/server/src/start.js +49 -0
- package/server/src/store/index.js +60 -0
- package/server/src/store/mongo.js +336 -0
- package/server/src/store/sqlite.js +538 -0
- package/server/src/store/sqliteRuntime.js +64 -0
- package/server/src/utils/apiError.js +10 -0
- package/server/src/utils/asyncHandler.js +8 -0
- package/server/src/utils/logger.js +20 -0
- package/server/src/utils/requestContext.js +19 -0
- package/src/commands/diff.js +64 -0
- package/src/commands/history.js +56 -0
- package/src/commands/init.js +59 -0
- package/src/commands/login.js +53 -0
- package/src/commands/logout.js +30 -0
- package/src/commands/pull.js +82 -0
- package/src/commands/push.js +76 -0
- package/src/commands/server.js +313 -0
- package/src/commands/whoami.js +26 -0
- package/src/index.js +52 -0
- package/src/lib/api.js +137 -0
- package/src/lib/config.js +36 -0
- package/src/lib/credentials.js +43 -0
- package/src/lib/dotenv.js +88 -0
- package/src/lib/prompt.js +50 -0
- package/src/lib/runtime.js +88 -0
- package/src/lib/ui.js +68 -0
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
const { version } = require('../../package.json');
|
|
2
|
+
const { ROLE_VALUES, AUDIT_ACTIONS } = require('@redwax/shared');
|
|
3
|
+
|
|
4
|
+
const ref = (name) => ({ $ref: `#/components/schemas/${name}` });
|
|
5
|
+
const json = (schema) => ({ content: { 'application/json': { schema } } });
|
|
6
|
+
|
|
7
|
+
const projectIdParam = {
|
|
8
|
+
name: 'id',
|
|
9
|
+
in: 'path',
|
|
10
|
+
required: true,
|
|
11
|
+
description: 'Project id',
|
|
12
|
+
schema: { type: 'string', example: '65b1f0c2a3d4e5f601234567' },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const keyParam = {
|
|
16
|
+
name: 'key',
|
|
17
|
+
in: 'path',
|
|
18
|
+
required: true,
|
|
19
|
+
description: 'Secret key',
|
|
20
|
+
schema: { type: 'string', example: 'DATABASE_URL' },
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const environmentParam = {
|
|
24
|
+
name: 'environment',
|
|
25
|
+
in: 'query',
|
|
26
|
+
required: true,
|
|
27
|
+
description: 'Target environment',
|
|
28
|
+
schema: { type: 'string', example: 'production' },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const errors = {
|
|
32
|
+
400: { description: 'Validation failed', ...json(ref('Error')) },
|
|
33
|
+
401: { description: 'Missing or invalid access token', ...json(ref('Error')) },
|
|
34
|
+
403: { description: 'Role lacks the required permission', ...json(ref('Error')) },
|
|
35
|
+
404: { description: 'Not found', ...json(ref('Error')) },
|
|
36
|
+
409: { description: 'Conflict', ...json(ref('Error')) },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
module.exports = {
|
|
40
|
+
openapi: '3.0.3',
|
|
41
|
+
info: {
|
|
42
|
+
title: 'Redwax API',
|
|
43
|
+
version,
|
|
44
|
+
description:
|
|
45
|
+
'Self-hostable secret management. Values are encrypted with AES-256-GCM before they reach the database — ' +
|
|
46
|
+
'plaintext is only ever produced on an explicit, audited read.',
|
|
47
|
+
license: { name: 'MIT' },
|
|
48
|
+
},
|
|
49
|
+
servers: [{ url: '/api/v1', description: 'Versioned API root' }],
|
|
50
|
+
tags: [
|
|
51
|
+
{ name: 'Auth', description: 'Registration, login and token rotation' },
|
|
52
|
+
{ name: 'Projects', description: 'Projects, environments and membership' },
|
|
53
|
+
{ name: 'Secrets', description: 'Encrypted key-value storage per environment' },
|
|
54
|
+
{ name: 'Audit', description: 'Append-only access trail' },
|
|
55
|
+
],
|
|
56
|
+
security: [{ bearerAuth: [] }],
|
|
57
|
+
|
|
58
|
+
components: {
|
|
59
|
+
securitySchemes: {
|
|
60
|
+
bearerAuth: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT (RS256)' },
|
|
61
|
+
},
|
|
62
|
+
schemas: {
|
|
63
|
+
Error: {
|
|
64
|
+
type: 'object',
|
|
65
|
+
properties: { error: { type: 'string', example: 'Secret not found' } },
|
|
66
|
+
},
|
|
67
|
+
User: {
|
|
68
|
+
type: 'object',
|
|
69
|
+
properties: {
|
|
70
|
+
id: { type: 'string' },
|
|
71
|
+
name: { type: 'string' },
|
|
72
|
+
email: { type: 'string', format: 'email' },
|
|
73
|
+
createdAt: { type: 'string', format: 'date-time' },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
AuthResponse: {
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
user: ref('User'),
|
|
80
|
+
accessToken: { type: 'string', description: 'RS256 JWT, 15 minute lifetime' },
|
|
81
|
+
expiresIn: { type: 'string', example: '15m' },
|
|
82
|
+
refreshToken: {
|
|
83
|
+
type: 'string',
|
|
84
|
+
description: 'Returned only when the request carries `X-Redwax-Client: cli`; browsers receive an HTTP-only cookie instead.',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
Member: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
id: { type: 'string' },
|
|
92
|
+
name: { type: 'string' },
|
|
93
|
+
email: { type: 'string', format: 'email' },
|
|
94
|
+
role: { type: 'string', enum: [...ROLE_VALUES] },
|
|
95
|
+
addedAt: { type: 'string', format: 'date-time' },
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
Project: {
|
|
99
|
+
type: 'object',
|
|
100
|
+
properties: {
|
|
101
|
+
id: { type: 'string' },
|
|
102
|
+
name: { type: 'string' },
|
|
103
|
+
slug: { type: 'string' },
|
|
104
|
+
description: { type: 'string' },
|
|
105
|
+
environments: { type: 'array', items: { type: 'string' } },
|
|
106
|
+
role: { type: 'string', enum: [...ROLE_VALUES], description: "The caller's own role" },
|
|
107
|
+
members: { type: 'array', items: ref('Member') },
|
|
108
|
+
createdAt: { type: 'string', format: 'date-time' },
|
|
109
|
+
updatedAt: { type: 'string', format: 'date-time' },
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
SecretMeta: {
|
|
113
|
+
type: 'object',
|
|
114
|
+
description: 'Secret metadata. The value stays encrypted until explicitly revealed.',
|
|
115
|
+
properties: {
|
|
116
|
+
id: { type: 'string' },
|
|
117
|
+
key: { type: 'string', example: 'DATABASE_URL' },
|
|
118
|
+
environment: { type: 'string' },
|
|
119
|
+
version: { type: 'integer', example: 3 },
|
|
120
|
+
sensitive: { type: 'boolean' },
|
|
121
|
+
updatedBy: ref('User'),
|
|
122
|
+
updatedAt: { type: 'string', format: 'date-time' },
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
SecretValue: {
|
|
126
|
+
allOf: [
|
|
127
|
+
ref('SecretMeta'),
|
|
128
|
+
{ type: 'object', properties: { value: { type: 'string' } } },
|
|
129
|
+
],
|
|
130
|
+
},
|
|
131
|
+
SecretVersion: {
|
|
132
|
+
type: 'object',
|
|
133
|
+
properties: {
|
|
134
|
+
version: { type: 'integer' },
|
|
135
|
+
action: { type: 'string', enum: ['created', 'updated', 'deleted', 'restored'] },
|
|
136
|
+
value: { type: 'string', nullable: true, description: 'null for delete events' },
|
|
137
|
+
changedAt: { type: 'string', format: 'date-time' },
|
|
138
|
+
changedBy: ref('User'),
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
AuditEntry: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
properties: {
|
|
144
|
+
action: { type: 'string', enum: [...AUDIT_ACTIONS] },
|
|
145
|
+
user: ref('User'),
|
|
146
|
+
secretKey: { type: 'string', nullable: true },
|
|
147
|
+
environment: { type: 'string', nullable: true },
|
|
148
|
+
ip: { type: 'string' },
|
|
149
|
+
userAgent: { type: 'string' },
|
|
150
|
+
timestamp: { type: 'string', format: 'date-time' },
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
paths: {
|
|
157
|
+
'/auth/register': {
|
|
158
|
+
post: {
|
|
159
|
+
tags: ['Auth'], summary: 'Register a new user', security: [],
|
|
160
|
+
requestBody: {
|
|
161
|
+
required: true,
|
|
162
|
+
...json({
|
|
163
|
+
type: 'object',
|
|
164
|
+
required: ['name', 'email', 'password'],
|
|
165
|
+
properties: {
|
|
166
|
+
name: { type: 'string', minLength: 2 },
|
|
167
|
+
email: { type: 'string', format: 'email' },
|
|
168
|
+
password: { type: 'string', minLength: 8 },
|
|
169
|
+
},
|
|
170
|
+
}),
|
|
171
|
+
},
|
|
172
|
+
responses: {
|
|
173
|
+
201: { description: 'Registered', ...json(ref('AuthResponse')) },
|
|
174
|
+
400: errors[400],
|
|
175
|
+
409: { description: 'Email already registered', ...json(ref('Error')) },
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
'/auth/login': {
|
|
180
|
+
post: {
|
|
181
|
+
tags: ['Auth'], summary: 'Log in', security: [],
|
|
182
|
+
requestBody: {
|
|
183
|
+
required: true,
|
|
184
|
+
...json({
|
|
185
|
+
type: 'object',
|
|
186
|
+
required: ['email', 'password'],
|
|
187
|
+
properties: {
|
|
188
|
+
email: { type: 'string', format: 'email' },
|
|
189
|
+
password: { type: 'string' },
|
|
190
|
+
},
|
|
191
|
+
}),
|
|
192
|
+
},
|
|
193
|
+
responses: {
|
|
194
|
+
200: { description: 'Logged in', ...json(ref('AuthResponse')) },
|
|
195
|
+
401: { description: 'Invalid credentials', ...json(ref('Error')) },
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
'/auth/refresh': {
|
|
200
|
+
post: {
|
|
201
|
+
tags: ['Auth'], security: [],
|
|
202
|
+
summary: 'Rotate the refresh token and issue a new access token',
|
|
203
|
+
description: 'Reads the refresh token from the HTTP-only cookie, or from `refreshToken` in the body for CLI clients. The presented token is invalidated.',
|
|
204
|
+
requestBody: json({ type: 'object', properties: { refreshToken: { type: 'string' } } }),
|
|
205
|
+
responses: {
|
|
206
|
+
200: { description: 'Rotated', ...json(ref('AuthResponse')) },
|
|
207
|
+
401: { description: 'Invalid or expired refresh token', ...json(ref('Error')) },
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
'/auth/logout': {
|
|
212
|
+
post: {
|
|
213
|
+
tags: ['Auth'], summary: 'Invalidate the refresh token', security: [],
|
|
214
|
+
responses: { 200: { description: 'Logged out' } },
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
'/auth/me': {
|
|
218
|
+
get: {
|
|
219
|
+
tags: ['Auth'], summary: 'Current user',
|
|
220
|
+
responses: {
|
|
221
|
+
200: { description: 'Current user', ...json({ type: 'object', properties: { user: ref('User') } }) },
|
|
222
|
+
401: errors[401],
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
'/projects': {
|
|
228
|
+
get: {
|
|
229
|
+
tags: ['Projects'], summary: 'List projects the caller belongs to',
|
|
230
|
+
responses: {
|
|
231
|
+
200: { description: 'Projects', ...json({ type: 'object', properties: { projects: { type: 'array', items: ref('Project') } } }) },
|
|
232
|
+
401: errors[401],
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
post: {
|
|
236
|
+
tags: ['Projects'], summary: 'Create a project',
|
|
237
|
+
description: 'The creator becomes the owner. The slug is derived from the name when omitted.',
|
|
238
|
+
requestBody: {
|
|
239
|
+
required: true,
|
|
240
|
+
...json({
|
|
241
|
+
type: 'object',
|
|
242
|
+
required: ['name'],
|
|
243
|
+
properties: {
|
|
244
|
+
name: { type: 'string' },
|
|
245
|
+
slug: { type: 'string' },
|
|
246
|
+
description: { type: 'string' },
|
|
247
|
+
environments: { type: 'array', items: { type: 'string' } },
|
|
248
|
+
},
|
|
249
|
+
}),
|
|
250
|
+
},
|
|
251
|
+
responses: {
|
|
252
|
+
201: { description: 'Created', ...json({ type: 'object', properties: { project: ref('Project') } }) },
|
|
253
|
+
400: errors[400], 409: errors[409],
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
'/projects/{id}': {
|
|
258
|
+
parameters: [projectIdParam],
|
|
259
|
+
get: {
|
|
260
|
+
tags: ['Projects'], summary: 'Project detail',
|
|
261
|
+
responses: { 200: { description: 'Project', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 404: errors[404] },
|
|
262
|
+
},
|
|
263
|
+
patch: {
|
|
264
|
+
tags: ['Projects'], summary: 'Rename a project or add/remove an environment',
|
|
265
|
+
requestBody: {
|
|
266
|
+
required: true,
|
|
267
|
+
...json({
|
|
268
|
+
type: 'object',
|
|
269
|
+
properties: {
|
|
270
|
+
name: { type: 'string' },
|
|
271
|
+
description: { type: 'string' },
|
|
272
|
+
addEnvironment: { type: 'string' },
|
|
273
|
+
removeEnvironment: { type: 'string', description: 'Only allowed when the environment holds no live secrets' },
|
|
274
|
+
},
|
|
275
|
+
}),
|
|
276
|
+
},
|
|
277
|
+
responses: { 200: { description: 'Updated', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 403: errors[403], 409: errors[409] },
|
|
278
|
+
},
|
|
279
|
+
delete: {
|
|
280
|
+
tags: ['Projects'], summary: 'Delete a project (owner only)',
|
|
281
|
+
description: 'Cascades to every secret, version and audit record.',
|
|
282
|
+
responses: { 200: { description: 'Deleted' }, 403: errors[403] },
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
'/projects/{id}/members': {
|
|
286
|
+
parameters: [projectIdParam],
|
|
287
|
+
post: {
|
|
288
|
+
tags: ['Projects'], summary: 'Add a member by email',
|
|
289
|
+
description: 'The invitee must already have a Redwax account — v1 sends no email.',
|
|
290
|
+
requestBody: {
|
|
291
|
+
required: true,
|
|
292
|
+
...json({
|
|
293
|
+
type: 'object',
|
|
294
|
+
required: ['email'],
|
|
295
|
+
properties: {
|
|
296
|
+
email: { type: 'string', format: 'email' },
|
|
297
|
+
role: { type: 'string', enum: ['admin', 'developer', 'readonly'] },
|
|
298
|
+
},
|
|
299
|
+
}),
|
|
300
|
+
},
|
|
301
|
+
responses: { 201: { description: 'Added', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 403: errors[403], 404: errors[404], 409: errors[409] },
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
'/projects/{id}/members/{uid}': {
|
|
305
|
+
parameters: [projectIdParam, { name: 'uid', in: 'path', required: true, schema: { type: 'string' } }],
|
|
306
|
+
patch: {
|
|
307
|
+
tags: ['Projects'], summary: "Change a member's role",
|
|
308
|
+
requestBody: { required: true, ...json({ type: 'object', required: ['role'], properties: { role: { type: 'string', enum: ['admin', 'developer', 'readonly'] } } }) },
|
|
309
|
+
responses: { 200: { description: 'Updated', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 403: errors[403], 404: errors[404] },
|
|
310
|
+
},
|
|
311
|
+
delete: {
|
|
312
|
+
tags: ['Projects'], summary: 'Remove a member',
|
|
313
|
+
responses: { 200: { description: 'Removed', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 400: errors[400], 403: errors[403] },
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
'/projects/{id}/transfer': {
|
|
317
|
+
parameters: [projectIdParam],
|
|
318
|
+
post: {
|
|
319
|
+
tags: ['Projects'], summary: 'Transfer ownership (owner only)',
|
|
320
|
+
description: 'The new owner must already be a member. The previous owner is demoted to admin.',
|
|
321
|
+
requestBody: { required: true, ...json({ type: 'object', required: ['userId'], properties: { userId: { type: 'string' } } }) },
|
|
322
|
+
responses: { 200: { description: 'Transferred', ...json({ type: 'object', properties: { project: ref('Project') } }) }, 403: errors[403], 404: errors[404] },
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
'/projects/{id}/secrets': {
|
|
327
|
+
parameters: [projectIdParam],
|
|
328
|
+
get: {
|
|
329
|
+
tags: ['Secrets'], summary: 'List secrets in an environment (values masked)',
|
|
330
|
+
parameters: [environmentParam],
|
|
331
|
+
responses: {
|
|
332
|
+
200: {
|
|
333
|
+
description: 'Secret metadata',
|
|
334
|
+
...json({ type: 'object', properties: { environment: { type: 'string' }, secrets: { type: 'array', items: ref('SecretMeta') } } }),
|
|
335
|
+
},
|
|
336
|
+
403: errors[403], 404: errors[404],
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
post: {
|
|
340
|
+
tags: ['Secrets'], summary: 'Create or overwrite a secret',
|
|
341
|
+
description: 'Encrypts server-side with a fresh IV and appends a version.',
|
|
342
|
+
requestBody: {
|
|
343
|
+
required: true,
|
|
344
|
+
...json({
|
|
345
|
+
type: 'object',
|
|
346
|
+
required: ['key', 'value', 'environment'],
|
|
347
|
+
properties: {
|
|
348
|
+
key: { type: 'string', example: 'DATABASE_URL' },
|
|
349
|
+
value: { type: 'string' },
|
|
350
|
+
environment: { type: 'string' },
|
|
351
|
+
sensitive: { type: 'boolean', default: false },
|
|
352
|
+
},
|
|
353
|
+
}),
|
|
354
|
+
},
|
|
355
|
+
responses: { 201: { description: 'Created', ...json({ type: 'object', properties: { secret: ref('SecretMeta') } }) }, 200: { description: 'Updated', ...json({ type: 'object', properties: { secret: ref('SecretMeta') } }) }, 400: errors[400], 403: errors[403] },
|
|
356
|
+
},
|
|
357
|
+
},
|
|
358
|
+
'/projects/{id}/secrets/bulk': {
|
|
359
|
+
parameters: [projectIdParam],
|
|
360
|
+
post: {
|
|
361
|
+
tags: ['Secrets'], summary: 'Bulk import (CLI push / .env upload)',
|
|
362
|
+
description: 'Unchanged values are skipped so repeated pushes do not churn version history. `prune` additionally requires delete rights.',
|
|
363
|
+
requestBody: {
|
|
364
|
+
required: true,
|
|
365
|
+
...json({
|
|
366
|
+
type: 'object',
|
|
367
|
+
required: ['environment', 'secrets'],
|
|
368
|
+
properties: {
|
|
369
|
+
environment: { type: 'string' },
|
|
370
|
+
prune: { type: 'boolean', default: false, description: 'Soft-delete remote keys absent from the payload' },
|
|
371
|
+
secrets: {
|
|
372
|
+
type: 'array',
|
|
373
|
+
items: {
|
|
374
|
+
type: 'object',
|
|
375
|
+
required: ['key', 'value'],
|
|
376
|
+
properties: { key: { type: 'string' }, value: { type: 'string' }, sensitive: { type: 'boolean' } },
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
}),
|
|
381
|
+
},
|
|
382
|
+
responses: {
|
|
383
|
+
200: {
|
|
384
|
+
description: 'Import summary',
|
|
385
|
+
...json({
|
|
386
|
+
type: 'object',
|
|
387
|
+
properties: {
|
|
388
|
+
environment: { type: 'string' },
|
|
389
|
+
created: { type: 'integer' }, updated: { type: 'integer' },
|
|
390
|
+
restored: { type: 'integer' }, deleted: { type: 'integer' }, unchanged: { type: 'integer' },
|
|
391
|
+
},
|
|
392
|
+
}),
|
|
393
|
+
},
|
|
394
|
+
400: errors[400], 403: errors[403],
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
'/projects/{id}/secrets/export': {
|
|
399
|
+
parameters: [projectIdParam],
|
|
400
|
+
get: {
|
|
401
|
+
tags: ['Secrets'], summary: 'Export a whole environment (decrypted)',
|
|
402
|
+
description: 'Writes an audit entry on every call. Serve over HTTPS only.',
|
|
403
|
+
parameters: [
|
|
404
|
+
environmentParam,
|
|
405
|
+
{ name: 'format', in: 'query', schema: { type: 'string', enum: ['env', 'json'], default: 'env' } },
|
|
406
|
+
],
|
|
407
|
+
responses: {
|
|
408
|
+
200: {
|
|
409
|
+
description: '.env file or JSON map',
|
|
410
|
+
content: {
|
|
411
|
+
'text/plain': { schema: { type: 'string', example: 'DATABASE_URL=postgres://...\n' } },
|
|
412
|
+
'application/json': { schema: { type: 'object', properties: { environment: { type: 'string' }, secrets: { type: 'object', additionalProperties: { type: 'string' } } } } },
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
403: errors[403],
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
'/projects/{id}/secrets/{key}': {
|
|
420
|
+
parameters: [projectIdParam, keyParam],
|
|
421
|
+
get: {
|
|
422
|
+
tags: ['Secrets'], summary: 'Reveal one decrypted value',
|
|
423
|
+
description: 'Always recorded in the audit log.',
|
|
424
|
+
parameters: [environmentParam],
|
|
425
|
+
responses: { 200: { description: 'Decrypted secret', ...json({ type: 'object', properties: { secret: ref('SecretValue') } }) }, 403: errors[403], 404: errors[404] },
|
|
426
|
+
},
|
|
427
|
+
patch: {
|
|
428
|
+
tags: ['Secrets'], summary: 'Update a value (creates a new version)',
|
|
429
|
+
description: 'Sending only `sensitive` toggles masking without creating a version.',
|
|
430
|
+
requestBody: {
|
|
431
|
+
required: true,
|
|
432
|
+
...json({
|
|
433
|
+
type: 'object',
|
|
434
|
+
required: ['environment'],
|
|
435
|
+
properties: { environment: { type: 'string' }, value: { type: 'string' }, sensitive: { type: 'boolean' } },
|
|
436
|
+
}),
|
|
437
|
+
},
|
|
438
|
+
responses: { 200: { description: 'Updated', ...json({ type: 'object', properties: { secret: ref('SecretMeta') } }) }, 400: errors[400], 403: errors[403], 404: errors[404] },
|
|
439
|
+
},
|
|
440
|
+
delete: {
|
|
441
|
+
tags: ['Secrets'], summary: 'Soft-delete a secret',
|
|
442
|
+
description: 'The value is retained in version history and can be restored by writing the key again.',
|
|
443
|
+
parameters: [environmentParam],
|
|
444
|
+
responses: { 200: { description: 'Deleted' }, 403: errors[403], 404: errors[404] },
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
'/projects/{id}/secrets/{key}/history': {
|
|
448
|
+
parameters: [projectIdParam, keyParam],
|
|
449
|
+
get: {
|
|
450
|
+
tags: ['Secrets'], summary: 'Version history for a key',
|
|
451
|
+
description: 'Up to 50 retained versions, newest first, decrypted for diffing.',
|
|
452
|
+
parameters: [environmentParam],
|
|
453
|
+
responses: {
|
|
454
|
+
200: {
|
|
455
|
+
description: 'History',
|
|
456
|
+
...json({
|
|
457
|
+
type: 'object',
|
|
458
|
+
properties: {
|
|
459
|
+
key: { type: 'string' }, environment: { type: 'string' },
|
|
460
|
+
currentVersion: { type: 'integer' }, deleted: { type: 'boolean' },
|
|
461
|
+
versions: { type: 'array', items: ref('SecretVersion') },
|
|
462
|
+
},
|
|
463
|
+
}),
|
|
464
|
+
},
|
|
465
|
+
403: errors[403], 404: errors[404],
|
|
466
|
+
},
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
'/projects/{id}/audit': {
|
|
471
|
+
parameters: [projectIdParam],
|
|
472
|
+
get: {
|
|
473
|
+
tags: ['Audit'], summary: 'Paginated audit trail (owner and admin only)',
|
|
474
|
+
parameters: [
|
|
475
|
+
{ name: 'page', in: 'query', schema: { type: 'integer', default: 1 } },
|
|
476
|
+
{ name: 'limit', in: 'query', schema: { type: 'integer', default: 50, maximum: 200 } },
|
|
477
|
+
{ name: 'user', in: 'query', schema: { type: 'string' }, description: 'Filter by user id' },
|
|
478
|
+
{ name: 'action', in: 'query', schema: { type: 'string', enum: [...AUDIT_ACTIONS] } },
|
|
479
|
+
{ name: 'from', in: 'query', schema: { type: 'string', format: 'date-time' } },
|
|
480
|
+
{ name: 'to', in: 'query', schema: { type: 'string', format: 'date-time' } },
|
|
481
|
+
{ name: 'format', in: 'query', schema: { type: 'string', enum: ['json', 'csv'], default: 'json' } },
|
|
482
|
+
],
|
|
483
|
+
responses: {
|
|
484
|
+
200: {
|
|
485
|
+
description: 'Audit entries',
|
|
486
|
+
content: {
|
|
487
|
+
'application/json': {
|
|
488
|
+
schema: {
|
|
489
|
+
type: 'object',
|
|
490
|
+
properties: {
|
|
491
|
+
entries: { type: 'array', items: ref('AuditEntry') },
|
|
492
|
+
pagination: {
|
|
493
|
+
type: 'object',
|
|
494
|
+
properties: { page: { type: 'integer' }, limit: { type: 'integer' }, total: { type: 'integer' }, pages: { type: 'integer' } },
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
'text/csv': { schema: { type: 'string' } },
|
|
500
|
+
},
|
|
501
|
+
},
|
|
502
|
+
403: errors[403],
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const store = require('../store');
|
|
2
|
+
const { toPublicUser } = require('../domain');
|
|
3
|
+
const ApiError = require('../utils/apiError');
|
|
4
|
+
const asyncHandler = require('../utils/asyncHandler');
|
|
5
|
+
const { isCliClient } = require('../utils/requestContext');
|
|
6
|
+
const { JWT_EXPIRES_IN, ALLOW_REGISTRATION } = require('../config/env');
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
hashPassword,
|
|
10
|
+
verifyPassword,
|
|
11
|
+
burnPasswordCheck,
|
|
12
|
+
refreshTokenExpiry,
|
|
13
|
+
signAccessToken,
|
|
14
|
+
generateRefreshToken,
|
|
15
|
+
parseRefreshToken,
|
|
16
|
+
refreshTokenMatches,
|
|
17
|
+
refreshCookieOptions,
|
|
18
|
+
REFRESH_COOKIE,
|
|
19
|
+
} = require('../services/auth.service');
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Issues a fresh access token and rotates the refresh token.
|
|
23
|
+
* Browsers get the refresh token as an HTTP-only cookie; the CLI gets it in
|
|
24
|
+
* the body because it has nowhere to keep a cookie.
|
|
25
|
+
*/
|
|
26
|
+
async function issueTokens(req, res, user, status = 200) {
|
|
27
|
+
const accessToken = signAccessToken(user);
|
|
28
|
+
const { token, hash } = generateRefreshToken(user.id);
|
|
29
|
+
|
|
30
|
+
await store.users.setRefreshToken(user.id, hash, refreshTokenExpiry());
|
|
31
|
+
|
|
32
|
+
const body = { user: toPublicUser(user), accessToken, expiresIn: JWT_EXPIRES_IN };
|
|
33
|
+
|
|
34
|
+
if (isCliClient(req)) {
|
|
35
|
+
body.refreshToken = token;
|
|
36
|
+
} else {
|
|
37
|
+
res.cookie(REFRESH_COOKIE, token, refreshCookieOptions(req));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
res.status(status).json(body);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const register = asyncHandler(async (req, res) => {
|
|
44
|
+
if (!ALLOW_REGISTRATION) {
|
|
45
|
+
throw new ApiError(403, 'Registration is disabled on this server — ask an administrator for an account');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { name, email, password } = req.body;
|
|
49
|
+
|
|
50
|
+
if (await store.users.existsByEmail(email)) {
|
|
51
|
+
throw new ApiError(409, 'That email is already registered');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const user = await store.users.create({ name, email, passwordHash: await hashPassword(password) });
|
|
55
|
+
await issueTokens(req, res, user, 201);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const login = asyncHandler(async (req, res) => {
|
|
59
|
+
const { email, password } = req.body;
|
|
60
|
+
|
|
61
|
+
const user = await store.users.findByEmail(email);
|
|
62
|
+
// Same message and same bcrypt cost either way — do not leak which emails
|
|
63
|
+
// are registered, through the response or through its timing.
|
|
64
|
+
const valid = user
|
|
65
|
+
? await verifyPassword(password, user.passwordHash)
|
|
66
|
+
: await burnPasswordCheck(password);
|
|
67
|
+
if (!user || !valid) {
|
|
68
|
+
throw new ApiError(401, 'Invalid email or password');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
await issueTokens(req, res, user);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const refresh = asyncHandler(async (req, res) => {
|
|
75
|
+
const presented = req.body?.refreshToken || req.cookies?.[REFRESH_COOKIE];
|
|
76
|
+
if (!presented) throw new ApiError(401, 'No refresh token provided');
|
|
77
|
+
|
|
78
|
+
const parsed = parseRefreshToken(presented);
|
|
79
|
+
if (!parsed || !store.isValidId(parsed.userId)) {
|
|
80
|
+
throw new ApiError(401, 'Malformed refresh token');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const user = await store.users.findById(parsed.userId);
|
|
84
|
+
if (!user || !refreshTokenMatches(parsed.secret, user.refreshTokenHash, user.refreshTokenExpiresAt ?? null)) {
|
|
85
|
+
throw new ApiError(401, 'Invalid or expired refresh token');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Rotation: issueTokens overwrites refreshTokenHash, invalidating this one.
|
|
89
|
+
await issueTokens(req, res, user);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const logout = asyncHandler(async (req, res) => {
|
|
93
|
+
const presented = req.body?.refreshToken || req.cookies?.[REFRESH_COOKIE];
|
|
94
|
+
const parsed = presented ? parseRefreshToken(presented) : null;
|
|
95
|
+
|
|
96
|
+
// Only revoke when the caller actually holds the current token. Acting on
|
|
97
|
+
// the user id alone would let anyone sign any user out by naming their id.
|
|
98
|
+
if (parsed && store.isValidId(parsed.userId)) {
|
|
99
|
+
const user = await store.users.findById(parsed.userId);
|
|
100
|
+
if (user && refreshTokenMatches(parsed.secret, user.refreshTokenHash)) {
|
|
101
|
+
await store.users.clearRefreshToken(user.id, user.refreshTokenHash);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
res.clearCookie(REFRESH_COOKIE, { ...refreshCookieOptions(req), maxAge: undefined });
|
|
106
|
+
res.json({ message: 'Logged out' });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const me = asyncHandler(async (req, res) => {
|
|
110
|
+
const user = await store.users.findById(req.user.id);
|
|
111
|
+
if (!user) throw new ApiError(404, 'User not found');
|
|
112
|
+
res.json({ user: toPublicUser(user) });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
module.exports = { register, login, refresh, logout, me };
|