@futdevpro/nts-dynamo 1.15.19 → 1.15.21
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/_specifications/BACKLOG.md +4 -4
- package/build/_modules/admin-auth/_models/admin-api-key-config.interface.d.ts +32 -0
- package/build/_modules/admin-auth/_models/admin-api-key-config.interface.d.ts.map +1 -0
- package/build/_modules/admin-auth/_models/admin-api-key-config.interface.js +3 -0
- package/build/_modules/admin-auth/_models/admin-api-key-config.interface.js.map +1 -0
- package/build/_modules/admin-auth/admin-api-key.auth-service.d.ts +90 -0
- package/build/_modules/admin-auth/admin-api-key.auth-service.d.ts.map +1 -0
- package/build/_modules/admin-auth/admin-api-key.auth-service.js +195 -0
- package/build/_modules/admin-auth/admin-api-key.auth-service.js.map +1 -0
- package/build/_modules/admin-auth/index.d.ts +3 -0
- package/build/_modules/admin-auth/index.d.ts.map +1 -0
- package/build/_modules/admin-auth/index.js +6 -0
- package/build/_modules/admin-auth/index.js.map +1 -0
- package/build/_modules/logs/_models/file-log-entry.interface.d.ts +14 -0
- package/build/_modules/logs/_models/file-log-entry.interface.d.ts.map +1 -0
- package/build/_modules/logs/_models/file-log-entry.interface.js +3 -0
- package/build/_modules/logs/_models/file-log-entry.interface.js.map +1 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.d.ts +36 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.d.ts.map +1 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.js +3 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.js.map +1 -0
- package/build/_modules/logs/file-log.service.d.ts +46 -0
- package/build/_modules/logs/file-log.service.d.ts.map +1 -1
- package/build/_modules/logs/file-log.service.js +178 -0
- package/build/_modules/logs/file-log.service.js.map +1 -1
- package/build/_modules/logs/file-logs.controller.d.ts +41 -0
- package/build/_modules/logs/file-logs.controller.d.ts.map +1 -0
- package/build/_modules/logs/file-logs.controller.js +139 -0
- package/build/_modules/logs/file-logs.controller.js.map +1 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.d.ts +32 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.d.ts.map +1 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.js +38 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.js.map +1 -0
- package/build/_modules/logs/index.d.ts +4 -0
- package/build/_modules/logs/index.d.ts.map +1 -1
- package/build/_modules/logs/index.js +5 -1
- package/build/_modules/logs/index.js.map +1 -1
- package/package.json +1 -1
- package/src/_modules/admin-auth/_models/admin-api-key-config.interface.ts +33 -0
- package/src/_modules/admin-auth/admin-api-key.auth-service.spec.ts +200 -0
- package/src/_modules/admin-auth/admin-api-key.auth-service.ts +220 -0
- package/src/_modules/admin-auth/index.ts +2 -0
- package/src/_modules/logs/_models/file-log-entry.interface.ts +13 -0
- package/src/_modules/logs/_models/file-log-read-result.interface.ts +37 -0
- package/src/_modules/logs/file-log.service.spec.ts +139 -0
- package/src/_modules/logs/file-log.service.ts +183 -0
- package/src/_modules/logs/file-logs.controller.spec.ts +245 -0
- package/src/_modules/logs/file-logs.controller.ts +165 -0
- package/src/_modules/logs/get-file-logs-routing-module.util.ts +51 -0
- package/src/_modules/logs/index.ts +7 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
|
|
3
|
+
import { DyFM_Error } from '@futdevpro/fsm-dynamo';
|
|
4
|
+
|
|
5
|
+
import { DyNTS_AdminApiKey_AuthService } from './admin-api-key.auth-service';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const TEST_KEY: string = 'super-secret-admin-key-1234567890';
|
|
9
|
+
const TEST_ENV_VAR: string = 'DYNTS_ADMIN_API_KEY';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/** Test-only — minimal Request mock-ot ad vissza adott headers-szel. */
|
|
13
|
+
const mockReq = (headers: Record<string, string | undefined> = {}): Request => {
|
|
14
|
+
return { headers: headers } as unknown as Request;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Test-only — minimal Response stub (verify nem irja, csak typing miatt kell). */
|
|
18
|
+
const mockRes = (): Response => {
|
|
19
|
+
return {} as unknown as Response;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
describe('| DyNTS_AdminApiKey_AuthService', (): void => {
|
|
24
|
+
let svc: DyNTS_AdminApiKey_AuthService;
|
|
25
|
+
let originalEnv: string | undefined;
|
|
26
|
+
|
|
27
|
+
beforeEach((): void => {
|
|
28
|
+
svc = DyNTS_AdminApiKey_AuthService.getInstance();
|
|
29
|
+
svc._resetForTesting();
|
|
30
|
+
originalEnv = process.env[TEST_ENV_VAR];
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach((): void => {
|
|
34
|
+
svc._resetForTesting();
|
|
35
|
+
if (originalEnv === undefined) {
|
|
36
|
+
delete process.env[TEST_ENV_VAR];
|
|
37
|
+
} else {
|
|
38
|
+
process.env[TEST_ENV_VAR] = originalEnv;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
describe('| verify() — config errors', (): void => {
|
|
44
|
+
it('| 500 ha az env var nincs beallitva', async (): Promise<void> => {
|
|
45
|
+
delete process.env[TEST_ENV_VAR];
|
|
46
|
+
let thrown: any = null;
|
|
47
|
+
try { await svc.verify(mockReq({}), mockRes()); } catch (e) { thrown = e; }
|
|
48
|
+
expect(thrown).not.toBeNull();
|
|
49
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(500);
|
|
50
|
+
expect(DyFM_Error.getErrorCode(thrown)).toContain('DyNTS-AAK-CONFIG');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('| 500 ha az env var ures string', async (): Promise<void> => {
|
|
54
|
+
process.env[TEST_ENV_VAR] = '';
|
|
55
|
+
let thrown: any = null;
|
|
56
|
+
try { await svc.verify(mockReq({}), mockRes()); } catch (e) { thrown = e; }
|
|
57
|
+
expect(thrown).not.toBeNull();
|
|
58
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(500);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('| verify() — auth errors', (): void => {
|
|
63
|
+
beforeEach((): void => { process.env[TEST_ENV_VAR] = TEST_KEY; });
|
|
64
|
+
|
|
65
|
+
it('| 401 ha a header teljesen hianyzik', async (): Promise<void> => {
|
|
66
|
+
let thrown: any = null;
|
|
67
|
+
try { await svc.verify(mockReq({}), mockRes()); } catch (e) { thrown = e; }
|
|
68
|
+
expect(thrown).not.toBeNull();
|
|
69
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(401);
|
|
70
|
+
expect(DyFM_Error.getErrorCode(thrown)).toContain('DyNTS-AAK-MISSING');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('| 401 ha a header rossz erteku', async (): Promise<void> => {
|
|
74
|
+
let thrown: any = null;
|
|
75
|
+
try {
|
|
76
|
+
await svc.verify(mockReq({ 'x-admin-api-key': 'wrong-key' }), mockRes());
|
|
77
|
+
} catch (e) { thrown = e; }
|
|
78
|
+
expect(thrown).not.toBeNull();
|
|
79
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(401);
|
|
80
|
+
expect(DyFM_Error.getErrorCode(thrown)).toContain('DyNTS-AAK-INVALID');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('| 401 length-mismatch eseten is (timing-safe path)', async (): Promise<void> => {
|
|
84
|
+
let thrown: any = null;
|
|
85
|
+
try {
|
|
86
|
+
await svc.verify(mockReq({ 'x-admin-api-key': 'short' }), mockRes());
|
|
87
|
+
} catch (e) { thrown = e; }
|
|
88
|
+
expect(thrown).not.toBeNull();
|
|
89
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(401);
|
|
90
|
+
expect(DyFM_Error.getErrorCode(thrown)).toContain('DyNTS-AAK-INVALID');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('| verify() — happy paths', (): void => {
|
|
95
|
+
beforeEach((): void => { process.env[TEST_ENV_VAR] = TEST_KEY; });
|
|
96
|
+
|
|
97
|
+
it('| silent pass ha az x-admin-api-key header egyezik', async (): Promise<void> => {
|
|
98
|
+
await expectAsync(
|
|
99
|
+
svc.verify(mockReq({ 'x-admin-api-key': TEST_KEY }), mockRes()),
|
|
100
|
+
).toBeResolved();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('| silent pass Authorization Bearer fallback-bol (default engedelyezve)', async (): Promise<void> => {
|
|
104
|
+
await expectAsync(
|
|
105
|
+
svc.verify(mockReq({ authorization: `Bearer ${TEST_KEY}` }), mockRes()),
|
|
106
|
+
).toBeResolved();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('| silent pass Authorization Bearer kis-nagybetu case-insensitive', async (): Promise<void> => {
|
|
110
|
+
await expectAsync(
|
|
111
|
+
svc.verify(mockReq({ authorization: `bearer ${TEST_KEY}` }), mockRes()),
|
|
112
|
+
).toBeResolved();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('| configure() — overrides', (): void => {
|
|
117
|
+
it('| custom envVarName-bol olvas', async (): Promise<void> => {
|
|
118
|
+
svc.configure({ envVarName: 'MY_CUSTOM_KEY' });
|
|
119
|
+
process.env['MY_CUSTOM_KEY'] = TEST_KEY;
|
|
120
|
+
|
|
121
|
+
await expectAsync(
|
|
122
|
+
svc.verify(mockReq({ 'x-admin-api-key': TEST_KEY }), mockRes()),
|
|
123
|
+
).toBeResolved();
|
|
124
|
+
|
|
125
|
+
delete process.env['MY_CUSTOM_KEY'];
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('| custom headerName-rol olvas', async (): Promise<void> => {
|
|
129
|
+
process.env[TEST_ENV_VAR] = TEST_KEY;
|
|
130
|
+
svc.configure({ headerName: 'x-my-admin' });
|
|
131
|
+
|
|
132
|
+
// Default x-admin-api-key MOSTANTOL NEM mukodik
|
|
133
|
+
let thrown: any = null;
|
|
134
|
+
try {
|
|
135
|
+
await svc.verify(mockReq({ 'x-admin-api-key': TEST_KEY }), mockRes());
|
|
136
|
+
} catch (e) { thrown = e; }
|
|
137
|
+
expect(thrown).not.toBeNull();
|
|
138
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(401);
|
|
139
|
+
|
|
140
|
+
// Az uj header viszont igen
|
|
141
|
+
await expectAsync(
|
|
142
|
+
svc.verify(mockReq({ 'x-my-admin': TEST_KEY }), mockRes()),
|
|
143
|
+
).toBeResolved();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('| allowAuthorizationBearer=false letiltja a Bearer fallback-et', async (): Promise<void> => {
|
|
147
|
+
process.env[TEST_ENV_VAR] = TEST_KEY;
|
|
148
|
+
svc.configure({ allowAuthorizationBearer: false });
|
|
149
|
+
|
|
150
|
+
let thrown: any = null;
|
|
151
|
+
try {
|
|
152
|
+
await svc.verify(mockReq({ authorization: `Bearer ${TEST_KEY}` }), mockRes());
|
|
153
|
+
} catch (e) { thrown = e; }
|
|
154
|
+
expect(thrown).not.toBeNull();
|
|
155
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(401);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('| configure() partial override — a tobbi mezo default marad', (): void => {
|
|
159
|
+
svc.configure({ headerName: 'x-only-header' });
|
|
160
|
+
const config = svc.getConfig();
|
|
161
|
+
expect(config.headerName).toBe('x-only-header');
|
|
162
|
+
expect(config.envVarName).toBe('DYNTS_ADMIN_API_KEY');
|
|
163
|
+
expect(config.allowAuthorizationBearer).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('| configure() headerName lowercase-re normalizal', (): void => {
|
|
167
|
+
svc.configure({ headerName: 'X-Mixed-Case-Header' });
|
|
168
|
+
expect(svc.getConfig().headerName).toBe('x-mixed-case-header');
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('| env-read-on-each-call (dynamic)', (): void => {
|
|
173
|
+
it('| env var update kozben hat: install-utani env-modositas ervenyes', async (): Promise<void> => {
|
|
174
|
+
delete process.env[TEST_ENV_VAR];
|
|
175
|
+
// Elso hivas: env hianyzik → 500
|
|
176
|
+
let thrown: any = null;
|
|
177
|
+
try { await svc.verify(mockReq({}), mockRes()); } catch (e) { thrown = e; }
|
|
178
|
+
expect(DyFM_Error.getErrorStatus(thrown)).toBe(500);
|
|
179
|
+
|
|
180
|
+
// Most allitsuk be az env-et
|
|
181
|
+
process.env[TEST_ENV_VAR] = TEST_KEY;
|
|
182
|
+
|
|
183
|
+
// Masodik hivas helyes header-rel → pass
|
|
184
|
+
await expectAsync(
|
|
185
|
+
svc.verify(mockReq({ 'x-admin-api-key': TEST_KEY }), mockRes()),
|
|
186
|
+
).toBeResolved();
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('| .verify binding', (): void => {
|
|
191
|
+
it('| verify atadhato preProcesses-be detached method-kent (this-binding megtarttva)', async (): Promise<void> => {
|
|
192
|
+
process.env[TEST_ENV_VAR] = TEST_KEY;
|
|
193
|
+
const detached: (req: Request, res: Response) => Promise<void> = svc.verify;
|
|
194
|
+
// NEM call-ban svc.verify(...) hanem szabad fuggveny-kent — bindelt this-re kell hagyatkozni
|
|
195
|
+
await expectAsync(
|
|
196
|
+
detached(mockReq({ 'x-admin-api-key': TEST_KEY }), mockRes()),
|
|
197
|
+
).toBeResolved();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import * as crypto from 'crypto';
|
|
3
|
+
|
|
4
|
+
import { DyFM_Error } from '@futdevpro/fsm-dynamo';
|
|
5
|
+
|
|
6
|
+
import { DyNTS_SingletonServiceBase } from '../../_services/base/singleton.service-base';
|
|
7
|
+
import { DyNTS_global_settings } from '../../_collections/global-settings.const';
|
|
8
|
+
|
|
9
|
+
import { DyNTS_AdminApiKey_Config } from './_models/admin-api-key-config.interface';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/** Default env var nev az admin API key-hez. */
|
|
13
|
+
const DEFAULT_ENV_VAR_NAME: string = 'DYNTS_ADMIN_API_KEY';
|
|
14
|
+
|
|
15
|
+
/** Default header nev (Express lowercase-re normalizalja az osszes header-t). */
|
|
16
|
+
const DEFAULT_HEADER_NAME: string = 'x-admin-api-key';
|
|
17
|
+
|
|
18
|
+
/** Default Bearer fallback engedelyezve van. */
|
|
19
|
+
const DEFAULT_ALLOW_BEARER: boolean = true;
|
|
20
|
+
|
|
21
|
+
/** Service-nev az error-okhoz. */
|
|
22
|
+
const SERVICE_NAME: string = 'DyNTS_AdminApiKey_AuthService';
|
|
23
|
+
|
|
24
|
+
/** ErrorCode prefix — system shortcode + saját kod. */
|
|
25
|
+
const buildErrorCode = (subcode: string): string => {
|
|
26
|
+
const sys: string = DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
|
|
27
|
+
return `${sys}|DyNTS-AAK-${subcode}`;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Admin API key auth service — opt-in HTTP guard a meglevo `DyNTS_Endpoint_Params.preProcesses`
|
|
33
|
+
* mechanizmushoz. Egy env var-ban tarolt fix kulccsal valid-alja a bejovo kerest.
|
|
34
|
+
*
|
|
35
|
+
* **Hasznalat (host app):**
|
|
36
|
+
* ```ts
|
|
37
|
+
* const adminAuth = DyNTS_AdminApiKey_AuthService.getInstance();
|
|
38
|
+
* // opcionalis konfig:
|
|
39
|
+
* // adminAuth.configure({ envVarName: 'MY_KEY', headerName: 'x-my-key' });
|
|
40
|
+
*
|
|
41
|
+
* new DyNTS_Endpoint_Params({
|
|
42
|
+
* ...,
|
|
43
|
+
* preProcesses: [adminAuth.verify, ...other],
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // vagy a logs routing module-on at
|
|
47
|
+
* DyNTS_getLogsRoutingModule({ authPreProcess: adminAuth.verify });
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* **Viselkedes:**
|
|
51
|
+
* - env var beallitva ES helyes header → silent pass
|
|
52
|
+
* - env var beallitva, header hianyzik / rossz → 401 DyFM_Error
|
|
53
|
+
* - env var NINCS beallitva → 500 DyFM_Error (fail-closed; NEM silent allow)
|
|
54
|
+
*
|
|
55
|
+
* **Header lookup:**
|
|
56
|
+
* 1. `x-admin-api-key` (default canonical header)
|
|
57
|
+
* 2. `Authorization: Bearer <key>` (fallback ha `allowAuthorizationBearer === true`)
|
|
58
|
+
*
|
|
59
|
+
* **Timing-safe:** `crypto.timingSafeEqual` Buffer-konvertalassal. Length-mismatch
|
|
60
|
+
* eseten dummy compare-rel azonos idő, hogy a kulcs-hossz ne szivarogjon ki.
|
|
61
|
+
*
|
|
62
|
+
* **Env var read-on-each-call:** a `verify()` minden hivasnal olvassa az env-et,
|
|
63
|
+
* nem cache-eli. Igy a host az env-et utolagosan is allithatja (pl. config
|
|
64
|
+
* loader az auth.service.install() utan).
|
|
65
|
+
*
|
|
66
|
+
* **Singleton:** `getInstance()`-szel hivd. A `.verify` mezo binding-elve van
|
|
67
|
+
* `this`-re, igy direkt atadhato `preProcesses`-be ujracsomagolas nelkul.
|
|
68
|
+
*/
|
|
69
|
+
export class DyNTS_AdminApiKey_AuthService extends DyNTS_SingletonServiceBase {
|
|
70
|
+
|
|
71
|
+
static getInstance(): DyNTS_AdminApiKey_AuthService {
|
|
72
|
+
return DyNTS_AdminApiKey_AuthService.getSingletonInstance() as DyNTS_AdminApiKey_AuthService;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private envVarName: string = DEFAULT_ENV_VAR_NAME;
|
|
76
|
+
private headerName: string = DEFAULT_HEADER_NAME;
|
|
77
|
+
private allowAuthorizationBearer: boolean = DEFAULT_ALLOW_BEARER;
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Konfig override. Hianyzo mezok a default-okat orzik.
|
|
82
|
+
* Hivhato barmikor — a `verify()` a friss config-ot olvassa.
|
|
83
|
+
*/
|
|
84
|
+
configure(config: DyNTS_AdminApiKey_Config): void {
|
|
85
|
+
if (config.envVarName !== undefined) {
|
|
86
|
+
this.envVarName = config.envVarName;
|
|
87
|
+
}
|
|
88
|
+
if (config.headerName !== undefined) {
|
|
89
|
+
// Express lowercase-re normalizal — itt is lowercase-eljuk a konzisztenciaert
|
|
90
|
+
this.headerName = config.headerName.toLowerCase();
|
|
91
|
+
}
|
|
92
|
+
if (config.allowAuthorizationBearer !== undefined) {
|
|
93
|
+
this.allowAuthorizationBearer = config.allowAuthorizationBearer;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Aktualis konfig olvasasa (test/diagnosztika celokra).
|
|
99
|
+
*/
|
|
100
|
+
getConfig(): Required<DyNTS_AdminApiKey_Config> {
|
|
101
|
+
return {
|
|
102
|
+
envVarName: this.envVarName,
|
|
103
|
+
headerName: this.headerName,
|
|
104
|
+
allowAuthorizationBearer: this.allowAuthorizationBearer,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Pre-process function — atadhato `DyNTS_Endpoint_Params.preProcesses`-be,
|
|
110
|
+
* vagy `DyNTS_getLogsRoutingModule({ authPreProcess: ... })`-be.
|
|
111
|
+
*
|
|
112
|
+
* Throws:
|
|
113
|
+
* - 500 ha az env var nincs beallitva (vagy ures string)
|
|
114
|
+
* - 401 ha a header hianyzik vagy nem egyezik
|
|
115
|
+
*
|
|
116
|
+
* A `req`/`res` parametereket NEM modositja (a kerest tovabb engedi a tovabbi
|
|
117
|
+
* preProcess-eknek; csak hiba eseten throw-ol).
|
|
118
|
+
*/
|
|
119
|
+
readonly verify = async (req: Request, _res: Response): Promise<void> => {
|
|
120
|
+
const expectedKey: string = process.env[this.envVarName] ?? '';
|
|
121
|
+
if (expectedKey.length === 0) {
|
|
122
|
+
throw new DyFM_Error({
|
|
123
|
+
status: 500,
|
|
124
|
+
errorCode: buildErrorCode('CONFIG'),
|
|
125
|
+
addECToUserMsg: true,
|
|
126
|
+
message: `Admin API key not configured: env var ${this.envVarName} is not set or empty`,
|
|
127
|
+
userMessage: 'Server configuration error',
|
|
128
|
+
issuerService: SERVICE_NAME,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const providedKey: string | null = this.extractKeyFromRequest(req);
|
|
133
|
+
if (providedKey === null) {
|
|
134
|
+
throw new DyFM_Error({
|
|
135
|
+
status: 401,
|
|
136
|
+
errorCode: buildErrorCode('MISSING'),
|
|
137
|
+
addECToUserMsg: true,
|
|
138
|
+
message: `Admin API key required (expected header: ${this.headerName})`,
|
|
139
|
+
userMessage: 'Admin API key required',
|
|
140
|
+
issuerService: SERVICE_NAME,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!this.timingSafeEquals(providedKey, expectedKey)) {
|
|
145
|
+
throw new DyFM_Error({
|
|
146
|
+
status: 401,
|
|
147
|
+
errorCode: buildErrorCode('INVALID'),
|
|
148
|
+
addECToUserMsg: true,
|
|
149
|
+
message: 'Admin API key invalid',
|
|
150
|
+
userMessage: 'Admin API key invalid',
|
|
151
|
+
issuerService: SERVICE_NAME,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Silent pass — return resolved promise
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Header lookup — elobb a primer header, aztan opcionalisan az Authorization Bearer.
|
|
161
|
+
* Az ures string is "hianyzo"-nak szamit (a Buffer.from('') es timingSafeEqual
|
|
162
|
+
* konzisztencia miatt).
|
|
163
|
+
*/
|
|
164
|
+
private extractKeyFromRequest(req: Request): string | null {
|
|
165
|
+
// Primer header
|
|
166
|
+
const primary: unknown = req.headers[this.headerName];
|
|
167
|
+
const primaryStr: string = Array.isArray(primary) ? primary[0] ?? '' : (typeof primary === 'string' ? primary : '');
|
|
168
|
+
if (primaryStr.length > 0) {
|
|
169
|
+
return primaryStr;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Authorization Bearer fallback
|
|
173
|
+
if (this.allowAuthorizationBearer) {
|
|
174
|
+
const authHeader: unknown = req.headers['authorization'];
|
|
175
|
+
const authStr: string = Array.isArray(authHeader) ? authHeader[0] ?? '' : (typeof authHeader === 'string' ? authHeader : '');
|
|
176
|
+
if (authStr.toLowerCase().startsWith('bearer ')) {
|
|
177
|
+
const token: string = authStr.substring(7).trim();
|
|
178
|
+
if (token.length > 0) {
|
|
179
|
+
return token;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Timing-safe compare ket string kozott. Length-mismatch eseten egy dummy
|
|
189
|
+
* compare-rel azonos idot kenyszeritunk (a kulcs-hossz nem szivaroghat ki
|
|
190
|
+
* timing-attackal).
|
|
191
|
+
*
|
|
192
|
+
* crypto.timingSafeEqual KOTELEZOEN azonos Buffer-hosszt var — kulonbozo
|
|
193
|
+
* hosszra throw-ol, ezert vizsgaljuk elobb a length-et es csak utana
|
|
194
|
+
* compare-elunk.
|
|
195
|
+
*/
|
|
196
|
+
private timingSafeEquals(a: string, b: string): boolean {
|
|
197
|
+
const aBuf: Buffer = Buffer.from(a, 'utf-8');
|
|
198
|
+
const bBuf: Buffer = Buffer.from(b, 'utf-8');
|
|
199
|
+
|
|
200
|
+
if (aBuf.length !== bBuf.length) {
|
|
201
|
+
// Dummy compare ugyanazzal a string-gel: konstans ideju mukodest biztosit
|
|
202
|
+
// mielott visszaternenk false-szal — igy a length-mismatch nem szivaroghat ki.
|
|
203
|
+
crypto.timingSafeEqual(bBuf, bBuf);
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return crypto.timingSafeEqual(aBuf, bBuf);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Test-only: visszaallitja a default config-ot, hogy a specfajlok ne szivarogjak
|
|
213
|
+
* at egymas state-jet. Production code NE hivja.
|
|
214
|
+
*/
|
|
215
|
+
_resetForTesting(): void {
|
|
216
|
+
this.envVarName = DEFAULT_ENV_VAR_NAME;
|
|
217
|
+
this.headerName = DEFAULT_HEADER_NAME;
|
|
218
|
+
this.allowAuthorizationBearer = DEFAULT_ALLOW_BEARER;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Egy log fajl meta-adata a listazasban.
|
|
3
|
+
*/
|
|
4
|
+
export interface DyNTS_FileLog_Entry {
|
|
5
|
+
/** Fajlnev (csak basename — NEM teljes path). */
|
|
6
|
+
name: string;
|
|
7
|
+
/** Fajlmeret byte-ban. */
|
|
8
|
+
sizeBytes: number;
|
|
9
|
+
/** Modositasi ido ms-ban (mtimeMs). */
|
|
10
|
+
mtimeMs: number;
|
|
11
|
+
/** Igaz, ha ez az aktualis aktiv session fajlja. */
|
|
12
|
+
isCurrent: boolean;
|
|
13
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `readLogFile()` valaszobjektum.
|
|
3
|
+
*/
|
|
4
|
+
export interface DyNTS_FileLog_ReadResult {
|
|
5
|
+
/** Fajlnev (csak basename). */
|
|
6
|
+
name: string;
|
|
7
|
+
/** Teljes fajlmeret byte-ban. */
|
|
8
|
+
sizeBytes: number;
|
|
9
|
+
/** A fajlban talalhato osszes sor szama. */
|
|
10
|
+
totalLines: number;
|
|
11
|
+
/** A visszaadott sorok. */
|
|
12
|
+
lines: string[];
|
|
13
|
+
/** Mod amiben olvastunk: 'tail' | 'head' | 'range'. */
|
|
14
|
+
mode: 'tail' | 'head' | 'range';
|
|
15
|
+
/** Visszaadott tartomany 1-based start (range/tail/head normalizalva). */
|
|
16
|
+
rangeStart: number;
|
|
17
|
+
/** Visszaadott tartomany 1-based end (inclusive). */
|
|
18
|
+
rangeEnd: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `readLogFile(name, options)` opciok.
|
|
24
|
+
*
|
|
25
|
+
* Egyetlen mod aktiv egyszerre. Prioritas: range > head > tail.
|
|
26
|
+
* Egyik sem megadott → default `tail: 200`.
|
|
27
|
+
*/
|
|
28
|
+
export interface DyNTS_FileLog_ReadOptions {
|
|
29
|
+
/** Utolso N sor (default mod ha semmi mas nincs). */
|
|
30
|
+
tail?: number;
|
|
31
|
+
/** Elso N sor. */
|
|
32
|
+
head?: number;
|
|
33
|
+
/** 1-based line range start (inclusive). `rangeEnd` is kell. */
|
|
34
|
+
rangeStart?: number;
|
|
35
|
+
/** 1-based line range end (inclusive). */
|
|
36
|
+
rangeEnd?: number;
|
|
37
|
+
}
|
|
@@ -199,4 +199,143 @@ describe('| DyNTS_FileLog_Service', (): void => {
|
|
|
199
199
|
expect(DyNTS_FileLog_Service.getInstance().isInstalled()).toBe(false);
|
|
200
200
|
});
|
|
201
201
|
});
|
|
202
|
+
|
|
203
|
+
describe('| listLogFiles()', (): void => {
|
|
204
|
+
it('| ures lista ha nincs installalva', (): void => {
|
|
205
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
206
|
+
// NEM hivunk install-t
|
|
207
|
+
expect(svc.listLogFiles()).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('| listazza az osszes prefix-elt log fajlt + jelzi az isCurrent flag-et', (): void => {
|
|
211
|
+
// 3 fajl letrehozasa kezzel kulonbozo prefix-szel
|
|
212
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-01_aaa.log'), 'a');
|
|
213
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-02_bbb.log'), 'bb');
|
|
214
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-03_ccc.log'), 'ccc');
|
|
215
|
+
fs.writeFileSync(path.join(tempDir, 'other-2026-01-04_zzz.log'), 'should-be-ignored');
|
|
216
|
+
|
|
217
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
218
|
+
enabled: true,
|
|
219
|
+
logDir: tempDir,
|
|
220
|
+
filenamePrefix: 'spec-',
|
|
221
|
+
};
|
|
222
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
223
|
+
svc.install();
|
|
224
|
+
|
|
225
|
+
const list = svc.listLogFiles();
|
|
226
|
+
// 3 spec- fajl + 1 aktualis session fajl = 4
|
|
227
|
+
expect(list.length).toBe(4);
|
|
228
|
+
const names: string[] = list.map((e) => e.name);
|
|
229
|
+
expect(names.find((n) => n.startsWith('other-'))).toBeUndefined();
|
|
230
|
+
expect(list.filter((e) => e.isCurrent).length).toBe(1);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe('| readLogFile() — happy paths', (): void => {
|
|
235
|
+
let svc: DyNTS_FileLog_Service;
|
|
236
|
+
let testFile: string;
|
|
237
|
+
|
|
238
|
+
beforeEach((): void => {
|
|
239
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
240
|
+
enabled: true,
|
|
241
|
+
logDir: tempDir,
|
|
242
|
+
filenamePrefix: 'spec-',
|
|
243
|
+
};
|
|
244
|
+
svc = DyNTS_FileLog_Service.getInstance();
|
|
245
|
+
svc.install();
|
|
246
|
+
// Kulon teszt-fajl
|
|
247
|
+
testFile = `spec-test-${Date.now()}.log`;
|
|
248
|
+
const lines: string[] = [];
|
|
249
|
+
for (let i: number = 1; i <= 50; i++) {
|
|
250
|
+
lines.push(`line-${i}`);
|
|
251
|
+
}
|
|
252
|
+
fs.writeFileSync(path.join(tempDir, testFile), lines.join('\n') + '\n');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('| tail mod: utolso N sor', (): void => {
|
|
256
|
+
const result = svc.readLogFile(testFile, { tail: 5 });
|
|
257
|
+
expect(result.mode).toBe('tail');
|
|
258
|
+
expect(result.lines).toEqual(['line-46', 'line-47', 'line-48', 'line-49', 'line-50']);
|
|
259
|
+
expect(result.totalLines).toBe(50);
|
|
260
|
+
expect(result.rangeStart).toBe(46);
|
|
261
|
+
expect(result.rangeEnd).toBe(50);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('| head mod: elso N sor', (): void => {
|
|
265
|
+
const result = svc.readLogFile(testFile, { head: 3 });
|
|
266
|
+
expect(result.mode).toBe('head');
|
|
267
|
+
expect(result.lines).toEqual(['line-1', 'line-2', 'line-3']);
|
|
268
|
+
expect(result.rangeStart).toBe(1);
|
|
269
|
+
expect(result.rangeEnd).toBe(3);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('| range mod: explicit 10-15 tartomany', (): void => {
|
|
273
|
+
const result = svc.readLogFile(testFile, { rangeStart: 10, rangeEnd: 15 });
|
|
274
|
+
expect(result.mode).toBe('range');
|
|
275
|
+
expect(result.lines).toEqual(['line-10', 'line-11', 'line-12', 'line-13', 'line-14', 'line-15']);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('| default tail 200 ha semmi mod nincs megadva', (): void => {
|
|
279
|
+
const result = svc.readLogFile(testFile, {});
|
|
280
|
+
expect(result.mode).toBe('tail');
|
|
281
|
+
// A teszt fajl 50 sor → mind visszajon
|
|
282
|
+
expect(result.lines.length).toBe(50);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe('| readLogFile() — error paths', (): void => {
|
|
287
|
+
let svc: DyNTS_FileLog_Service;
|
|
288
|
+
|
|
289
|
+
beforeEach((): void => {
|
|
290
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
291
|
+
enabled: true,
|
|
292
|
+
logDir: tempDir,
|
|
293
|
+
filenamePrefix: 'spec-',
|
|
294
|
+
};
|
|
295
|
+
svc = DyNTS_FileLog_Service.getInstance();
|
|
296
|
+
svc.install();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('| throw ha invalid filename (prefix nem matchel)', (): void => {
|
|
300
|
+
expect(() => svc.readLogFile('other-2026.log')).toThrowError('invalid filename');
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('| throw ha path traversal kiserlet (`../`)', (): void => {
|
|
304
|
+
expect(() => svc.readLogFile('../etc/passwd')).toThrowError('invalid filename');
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('| throw ha path separator (`/`) van a nevben', (): void => {
|
|
308
|
+
expect(() => svc.readLogFile('spec-foo/bar.log')).toThrowError('invalid filename');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('| throw ha file nem letezik', (): void => {
|
|
312
|
+
expect(() => svc.readLogFile('spec-nonexistent.log')).toThrowError('file not found');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('| NEM installalt service → throw `not installed`', (): void => {
|
|
316
|
+
svc._teardownForTesting();
|
|
317
|
+
expect(() => svc.readLogFile('spec-foo.log')).toThrowError(/not installed/);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe('| getCurrentLogFilename()', (): void => {
|
|
322
|
+
it('| ures string ha nincs installalva', (): void => {
|
|
323
|
+
expect(DyNTS_FileLog_Service.getInstance().getCurrentLogFilename()).toBe('');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('| basename-t ad vissza ha installalva van', (): void => {
|
|
327
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
328
|
+
enabled: true,
|
|
329
|
+
logDir: tempDir,
|
|
330
|
+
filenamePrefix: 'spec-',
|
|
331
|
+
};
|
|
332
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
333
|
+
svc.install();
|
|
334
|
+
const basename: string = svc.getCurrentLogFilename();
|
|
335
|
+
expect(basename.startsWith('spec-')).toBe(true);
|
|
336
|
+
expect(basename.endsWith('.log')).toBe(true);
|
|
337
|
+
expect(basename.includes('/')).toBe(false);
|
|
338
|
+
expect(basename.includes('\\')).toBe(false);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
202
341
|
});
|