forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.controller.ts
More file actions
258 lines (244 loc) · 8.48 KB
/
Copy pathadmin.controller.ts
File metadata and controls
258 lines (244 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import {
Controller,
Post,
Get,
Patch,
Delete,
Body,
Param,
Query,
UseGuards,
Req,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { IsEnum, IsOptional, IsString } from 'class-validator';
import { Request } from 'express';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { AdminRoleGuard } from './guards/admin-role.guard';
import { AdminService } from './admin.service';
import { AuditService } from './audit.service';
import { ReindexDto } from './dto/reindex.dto';
import { AuditQueryDto } from './dto/audit-query.dto';
import { FeatureFlagDto } from './dto/feature-flag.dto';
import { SetRateLimitDto, EnableOverrideDto } from './dto/rate-limit.dto';
import { PrivacyService, PrivacyRequestType } from '../maintenance/privacy.service';
import { RateLimitService } from '../rate-limit/rate-limit.service';
import { QueueMonitorService } from '../queues/queue-monitor.service';
class PrivacyRequestDto {
@IsString() subjectWalletAddress!: string;
@IsEnum(['ANONYMIZE', 'DELETE']) requestType!: PrivacyRequestType;
@IsOptional() @IsString() notes?: string;
}
type AdminRequest = Request & {
user?: {
walletAddress?: string;
};
};
@ApiTags('admin')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, AdminRoleGuard)
@Controller('admin')
export class AdminController {
constructor(
private readonly adminService: AdminService,
private readonly auditService: AuditService,
private readonly privacyService: PrivacyService,
private readonly rateLimitService: RateLimitService,
private readonly queueMonitor: QueueMonitorService,
) {}
/**
* POST /admin/reindex
*
* Enqueues an async reindex job starting from the given ledger sequence.
* Returns a jobId so operators can track progress via the queue dashboard.
*
* Requires: admin role + valid JWT.
* Writes an immutable audit row with actor and full payload.
*/
@Post('reindex')
@HttpCode(HttpStatus.ACCEPTED)
@ApiOperation({ summary: 'Enqueue a ledger reindex job from a given ledger' })
async reindex(@Body() dto: ReindexDto, @Req() req: AdminRequest) {
const actor = req.user?.walletAddress ?? 'unknown';
const network =
dto.network ?? this.configService.get<string>('STELLAR_NETWORK', 'testnet');
const jobId = await this.adminService.enqueueReindex(dto.fromLedger, network);
await this.auditService.write({
actor,
action: 'reindex',
payload: { fromLedger: dto.fromLedger, network, jobId },
ipAddress: req.ip,
});
return { jobId, fromLedger: dto.fromLedger, network, status: 'queued' };
}
/**
* GET /admin/audits
*
* Paginated read of the immutable admin audit log.
* Requires: admin role + valid JWT.
*/
@Get('audits')
@ApiOperation({ summary: 'Paginated admin audit log' })
async getAudits(@Query() query: AuditQueryDto) {
return this.auditService.findAll(query.page, query.limit, query.action);
}
/**
* GET /admin/feature-flags
*
* Lists all feature flags and their current state.
*/
@Get('feature-flags')
@ApiOperation({ summary: 'List all feature flags' })
async listFeatureFlags() {
return this.adminService.getFeatureFlags();
}
/**
* PATCH /admin/feature-flags/:key
*
* Toggles a feature flag on or off.
* Writes an immutable audit row with actor and full payload.
*
* Legal note: disabling flags that gate user-facing activity (e.g. claim
* filing, policy creation) constitutes a staff-initiated pause of user
* operations. Such actions must be authorised by a designated compliance
* officer and are subject to applicable insurance-regulation obligations.
* The audit row created here serves as the immutable record of that action.
*/
/** POST /admin/privacy/requests — execute anonymization or deletion for a subject. */
@Post('privacy/requests')
@HttpCode(HttpStatus.ACCEPTED)
@ApiOperation({ summary: 'Submit a privacy request (anonymize or delete off-chain data)' })
async submitPrivacyRequest(@Body() dto: PrivacyRequestDto, @Req() req: Request) {
const actor = (req.user as { walletAddress?: string })?.walletAddress ?? 'unknown';
return this.privacyService.handleRequest({
subjectWalletAddress: dto.subjectWalletAddress,
requestType: dto.requestType,
requestedBy: actor,
ipAddress: req.ip,
notes: dto.notes,
});
}
/** GET /admin/privacy/requests — list all privacy requests. */
@Get('privacy/requests')
@ApiOperation({ summary: 'List privacy requests' })
async listPrivacyRequests(@Query('page') page = 1, @Query('limit') limit = 20) {
return this.privacyService.listRequests(Number(page), Number(limit));
}
@Patch('feature-flags/:key')
@ApiOperation({ summary: 'Set a feature flag value' })
async setFeatureFlag(
@Param('key') key: string,
@Body() dto: FeatureFlagDto,
@Req() req: AdminRequest,
) {
const actor = req.user?.walletAddress ?? 'unknown';
const flag = await this.adminService.setFeatureFlag(key, dto.enabled, dto.description, actor);
await this.auditService.write({
actor,
action: 'feature_flag_update',
payload: { key, enabled: dto.enabled, description: dto.description },
ipAddress: req.ip,
});
return flag;
}
/**
* POST /admin/rate-limits/:policyId
*
* Set custom rate limit for a policy.
* Writes an immutable audit row with actor and full payload.
*/
@Post('rate-limits/:policyId')
@ApiOperation({ summary: 'Set custom rate limit for a policy' })
async setRateLimit(
@Param('policyId') policyId: string,
@Body() dto: SetRateLimitDto,
@Req() req: AdminRequest,
) {
const actor = req.user?.walletAddress ?? 'unknown';
await this.rateLimitService.setLimit(policyId, dto.limit, actor);
await this.auditService.write({
actor,
action: 'rate_limit_set',
payload: { policyId, limit: dto.limit },
ipAddress: req.ip,
});
return { policyId, limit: dto.limit, status: 'updated' };
}
/**
* GET /admin/rate-limits/:policyId
*
* Get rate limit status for a policy.
*/
@Get('rate-limits/:policyId')
@ApiOperation({ summary: 'Get rate limit status for a policy' })
async getRateLimitStatus(@Param('policyId') policyId: string) {
return this.rateLimitService.getCounterState(policyId);
}
/**
* POST /admin/rate-limits/:policyId/override
*
* Enable manual override for a policy during catastrophic events.
* Writes an immutable audit row with actor and full payload.
*/
@Post('rate-limits/:policyId/override')
@ApiOperation({ summary: 'Enable manual override for a policy' })
async enableOverride(
@Param('policyId') policyId: string,
@Body() dto: EnableOverrideDto,
@Req() req: AdminRequest,
) {
const actor = req.user?.walletAddress ?? 'unknown';
await this.rateLimitService.enableOverride(policyId, actor, dto.reason);
await this.auditService.write({
actor,
action: 'rate_limit_override_enabled',
payload: { policyId, reason: dto.reason },
ipAddress: req.ip,
});
return { policyId, overrideActive: true };
}
/**
* DELETE /admin/rate-limits/:policyId/override
*
* Disable manual override for a policy.
* Writes an immutable audit row with actor and full payload.
*/
@Delete('rate-limits/:policyId/override')
@ApiOperation({ summary: 'Disable manual override for a policy' })
async disableOverride(
@Param('policyId') policyId: string,
@Req() req: AdminRequest,
) {
const actor = req.user?.walletAddress ?? 'unknown';
await this.rateLimitService.disableOverride(policyId, actor);
await this.auditService.write({
actor,
action: 'rate_limit_override_disabled',
payload: { policyId },
ipAddress: req.ip,
});
return { policyId, overrideActive: false };
}
/** POST /admin/queues/:queue/jobs/:jobId/retry — replay a DLQ job */
@Post('queues/:queue/jobs/:jobId/retry')
@HttpCode(HttpStatus.ACCEPTED)
@ApiOperation({ summary: 'Replay a failed (DLQ) job by id' })
async retryDlqJob(
@Param('queue') queue: string,
@Param('jobId') jobId: string,
@Req() req: AdminRequest,
) {
const actor = req.user?.walletAddress ?? 'unknown';
await this.queueMonitor.replayJob(queue, jobId);
await this.auditService.write({
actor,
action: 'dlq_job_replayed',
payload: { queue, jobId },
ipAddress: req.ip,
});
return { queue, jobId, status: 'retried' };
}
}