@nicollasfrazao/liguelead-log-service 1.0.0 → 1.1.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 +47 -13
- package/dist/configs/log.storage.external.s3.config.d.ts.map +1 -1
- package/dist/configs/log.storage.external.s3.config.js +6 -1
- package/dist/configs/log.storage.external.s3.config.js.map +1 -1
- package/dist/interfaces/log.storage.external.s3.service.config.interface.d.ts +5 -0
- package/dist/interfaces/log.storage.external.s3.service.config.interface.d.ts.map +1 -1
- package/dist/services/log.storage.external.s3.service.d.ts +17 -0
- package/dist/services/log.storage.external.s3.service.d.ts.map +1 -1
- package/dist/services/log.storage.external.s3.service.js +77 -10
- package/dist/services/log.storage.external.s3.service.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install @ligue-lead-tech/log-service-nodejs
|
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
import express from 'express';
|
|
17
|
-
import { LogService,
|
|
17
|
+
import { LogService, logMiddleware } from '@ligue-lead-tech/log-service-nodejs';
|
|
18
18
|
|
|
19
19
|
const app = express();
|
|
20
20
|
|
|
@@ -25,7 +25,7 @@ process.env.LOG_DESTINATION = 'both'; // 'console', 'storage', ou 'both'
|
|
|
25
25
|
process.env.NODE_ENV = 'development';
|
|
26
26
|
|
|
27
27
|
// Adicione o middleware de logging (obrigatório para correlation IDs)
|
|
28
|
-
app.use(
|
|
28
|
+
app.use(logMiddleware);
|
|
29
29
|
|
|
30
30
|
// Suas rotas
|
|
31
31
|
app.get('/api/users', (req, res) => {
|
|
@@ -49,6 +49,7 @@ app.listen(3000, () => {
|
|
|
49
49
|
|
|
50
50
|
### 2. Configuração com S3 via Kinesis Firehose
|
|
51
51
|
|
|
52
|
+
#### Usando Nome do Stream (método tradicional)
|
|
52
53
|
```typescript
|
|
53
54
|
// Configuração para produção com S3
|
|
54
55
|
process.env.LOG_USE_S3_STORAGE = 'true';
|
|
@@ -61,6 +62,33 @@ process.env.LOG_AWS_ACCESS_KEY_ID = 'test';
|
|
|
61
62
|
process.env.LOG_AWS_SECRET_ACCESS_KEY = 'test';
|
|
62
63
|
```
|
|
63
64
|
|
|
65
|
+
#### Usando IAM Role (recomendado)
|
|
66
|
+
```typescript
|
|
67
|
+
// Configuração com IAM Role
|
|
68
|
+
process.env.LOG_USE_S3_STORAGE = 'true';
|
|
69
|
+
process.env.LOG_AWS_ROLE_ARN = 'arn:aws:iam::0000000000:role/my-firehose-delivery-role';
|
|
70
|
+
|
|
71
|
+
// As variáveis LOG_AWS_REGION e LOG_FIREHOSE_STREAM_NAME são opcionais quando usar ARN
|
|
72
|
+
// A região e nome do stream são extraídos automaticamente do ARN
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Autenticação via IAM Role ARN (Nova Funcionalidade!)
|
|
76
|
+
```typescript
|
|
77
|
+
// Configuração para usar IAM Role com AssumeRole
|
|
78
|
+
process.env.LOG_USE_S3_STORAGE = 'true';
|
|
79
|
+
process.env.LOG_FIREHOSE_STREAM_NAME = 'my-app-logs-firehose';
|
|
80
|
+
process.env.LOG_AWS_REGION = 'us-east-1';
|
|
81
|
+
process.env.LOG_AWS_ROLE_ARN = 'arn:aws:iam::0000000000:role/my-firehose-delivery-role';
|
|
82
|
+
|
|
83
|
+
// O serviço automaticamente:
|
|
84
|
+
// ✅ Assume a role usando STS
|
|
85
|
+
// ✅ Obtém credenciais temporárias
|
|
86
|
+
// ✅ Renova automaticamente antes da expiração
|
|
87
|
+
// ✅ Cache inteligente para performance
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
📖 **Documentação Completa**: [AWS IAM Role Support](./docs/aws-iam-role-support.md)
|
|
91
|
+
|
|
64
92
|
### 3. Logging Manual
|
|
65
93
|
|
|
66
94
|
```typescript
|
|
@@ -118,6 +146,7 @@ app.post('/api/process', async (req, res) => {
|
|
|
118
146
|
- ✅ **Armazenamento via Kinesis Firehose** para S3 com buffering e compressão
|
|
119
147
|
- ✅ **Fallback para armazenamento local** em caso de falha do S3
|
|
120
148
|
- ✅ **Padrão de serviço estático** - sem necessidade de instanciação
|
|
149
|
+
- ✅ **Autenticação via IAM Role ARN** (AssumeRole automático, cache, renovação, cross-account)
|
|
121
150
|
|
|
122
151
|
## Estrutura dos Arquivos de Log
|
|
123
152
|
|
|
@@ -125,7 +154,7 @@ Os logs são organizados por ambiente e salvos via **Kinesis Firehose** para S3,
|
|
|
125
154
|
|
|
126
155
|
### Armazenamento S3 (via Kinesis Firehose)
|
|
127
156
|
```
|
|
128
|
-
|
|
157
|
+
my-app-logs/
|
|
129
158
|
├── logs/
|
|
130
159
|
│ ├── development/
|
|
131
160
|
│ │ └── 2025-10-27-combined.log.gz # Logs comprimidos pelo Firehose
|
|
@@ -183,7 +212,7 @@ Quando `LOG_USE_S3_STORAGE=true`, os logs são enviados para **Kinesis Firehose*
|
|
|
183
212
|
|
|
184
213
|
Exemplo de estrutura no S3 via Firehose:
|
|
185
214
|
```
|
|
186
|
-
|
|
215
|
+
my-app-logs/
|
|
187
216
|
├── logs/
|
|
188
217
|
│ ├── 2025/10/27/14/
|
|
189
218
|
│ │ ├── firehose_output-1-2025-10-27-14-01-01-uuid.gz
|
|
@@ -208,11 +237,16 @@ NODE_ENV=development # development|production|homologation|tes
|
|
|
208
237
|
|
|
209
238
|
# S3 via Kinesis Firehose
|
|
210
239
|
LOG_USE_S3_STORAGE=true # Ativa envio para S3 via Firehose
|
|
240
|
+
|
|
241
|
+
# Opção 1: Usar IAM Role (recomendado)
|
|
242
|
+
LOG_AWS_ROLE_ARN=arn:aws:iam::0000000000:role/my-firehose-delivery-role
|
|
243
|
+
|
|
244
|
+
# Opção 2: Usar nome e região separadamente
|
|
211
245
|
LOG_FIREHOSE_STREAM_NAME=my-app-logs # Nome do stream Kinesis Firehose
|
|
246
|
+
LOG_AWS_REGION=us-east-1 # Região AWS (não necessária se usar ARN)
|
|
212
247
|
|
|
213
248
|
# AWS (apenas para desenvolvimento/LocalStack)
|
|
214
249
|
LOG_AWS_ENDPOINT=http://localhost:4566 # Endpoint AWS (LocalStack)
|
|
215
|
-
LOG_AWS_REGION=us-east-1 # Região AWS
|
|
216
250
|
LOG_AWS_ACCESS_KEY_ID=test # Access Key (dev only)
|
|
217
251
|
LOG_AWS_SECRET_ACCESS_KEY=test # Secret Key (dev only)
|
|
218
252
|
|
|
@@ -334,7 +368,7 @@ LOG_DESTINATION=storage
|
|
|
334
368
|
LOG_USE_S3_STORAGE=true
|
|
335
369
|
|
|
336
370
|
# Nome do Kinesis Firehose stream
|
|
337
|
-
LOG_FIREHOSE_STREAM_NAME=
|
|
371
|
+
LOG_FIREHOSE_STREAM_NAME=my-app-logs-firehose
|
|
338
372
|
|
|
339
373
|
# Região AWS
|
|
340
374
|
LOG_AWS_REGION=us-east-1
|
|
@@ -371,7 +405,7 @@ LOG_USE=true
|
|
|
371
405
|
LOG_LEVEL=debug
|
|
372
406
|
LOG_DESTINATION=both
|
|
373
407
|
LOG_USE_S3_STORAGE=true
|
|
374
|
-
LOG_FIREHOSE_STREAM_NAME=
|
|
408
|
+
LOG_FIREHOSE_STREAM_NAME=my-app-logs-firehose
|
|
375
409
|
LOG_AWS_ENDPOINT=http://localhost:4566
|
|
376
410
|
LOG_AWS_ACCESS_KEY_ID=test
|
|
377
411
|
LOG_AWS_SECRET_ACCESS_KEY=test
|
|
@@ -384,7 +418,7 @@ LOG_USE=true
|
|
|
384
418
|
LOG_LEVEL=info
|
|
385
419
|
LOG_DESTINATION=storage
|
|
386
420
|
LOG_USE_S3_STORAGE=true
|
|
387
|
-
LOG_FIREHOSE_STREAM_NAME=
|
|
421
|
+
LOG_FIREHOSE_STREAM_NAME=my-app-logs-firehose-hml
|
|
388
422
|
# AWS credentials via IAM role ou environment variables
|
|
389
423
|
```
|
|
390
424
|
|
|
@@ -395,7 +429,7 @@ LOG_USE=true
|
|
|
395
429
|
LOG_LEVEL=warn
|
|
396
430
|
LOG_DESTINATION=storage
|
|
397
431
|
LOG_USE_S3_STORAGE=true
|
|
398
|
-
LOG_FIREHOSE_STREAM_NAME=
|
|
432
|
+
LOG_FIREHOSE_STREAM_NAME=my-app-logs-firehose-prod
|
|
399
433
|
LOG_INCLUDE_REQUEST_BODY=false
|
|
400
434
|
LOG_INCLUDE_RESPONSE_BODY=false
|
|
401
435
|
LOG_MAX_BODY_SIZE=1000
|
|
@@ -515,9 +549,9 @@ scripts/ # Scripts utilitários
|
|
|
515
549
|
O middleware de logging é aplicado automaticamente a todas as rotas:
|
|
516
550
|
|
|
517
551
|
```typescript
|
|
518
|
-
import {
|
|
552
|
+
import { logMiddleware } from './src/middlewares/log.middleware';
|
|
519
553
|
|
|
520
|
-
app.use(
|
|
554
|
+
app.use(logMiddleware);
|
|
521
555
|
```
|
|
522
556
|
|
|
523
557
|
### 5. Logging Manual
|
|
@@ -622,13 +656,13 @@ Para integrar o serviço em sua aplicação Express:
|
|
|
622
656
|
|
|
623
657
|
```typescript
|
|
624
658
|
import express from 'express';
|
|
625
|
-
import {
|
|
659
|
+
import { logMiddleware } from './src/middlewares/log.middleware';
|
|
626
660
|
import { LogService } from './src/services/log.service';
|
|
627
661
|
|
|
628
662
|
const app = express();
|
|
629
663
|
|
|
630
664
|
// Aplicar middleware de logging
|
|
631
|
-
app.use(
|
|
665
|
+
app.use(logMiddleware);
|
|
632
666
|
|
|
633
667
|
// Exemplo de uso manual
|
|
634
668
|
app.get('/health', (req, res) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.storage.external.s3.config.d.ts","sourceRoot":"","sources":["../../src/configs/log.storage.external.s3.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0CAA0C,EAAE,MAAM,gEAAgE,CAAC;AAE5H;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,QAAO,
|
|
1
|
+
{"version":3,"file":"log.storage.external.s3.config.d.ts","sourceRoot":"","sources":["../../src/configs/log.storage.external.s3.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0CAA0C,EAAE,MAAM,gEAAgE,CAAC;AAE5H;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,QAAO,0CA4BhD,CAAA"}
|
|
@@ -12,15 +12,20 @@ const getLogStorageExternalS3Config = () => {
|
|
|
12
12
|
region: process.env.LOG_AWS_REGION || 'us-east-1',
|
|
13
13
|
version: 'latest',
|
|
14
14
|
};
|
|
15
|
+
if (process.env.LOG_AWS_ROLE_ARN) {
|
|
16
|
+
config.roleArn = process.env.LOG_AWS_ROLE_ARN;
|
|
17
|
+
}
|
|
15
18
|
if (process.env.LOG_AWS_ACCESS_KEY_ID
|
|
16
19
|
&& process.env.LOG_AWS_SECRET_ACCESS_KEY) {
|
|
17
20
|
config = {
|
|
18
21
|
...config,
|
|
19
|
-
endpoint: process.env.LOG_AWS_ENDPOINT || 'http://localhost:4566',
|
|
20
22
|
accessKeyId: process.env.LOG_AWS_ACCESS_KEY_ID,
|
|
21
23
|
secretAccessKey: process.env.LOG_AWS_SECRET_ACCESS_KEY,
|
|
22
24
|
};
|
|
23
25
|
}
|
|
26
|
+
if (process.env.LOG_AWS_ENDPOINT) {
|
|
27
|
+
config.endpoint = process.env.LOG_AWS_ENDPOINT;
|
|
28
|
+
}
|
|
24
29
|
return config;
|
|
25
30
|
};
|
|
26
31
|
exports.getLogStorageExternalS3Config = getLogStorageExternalS3Config;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.storage.external.s3.config.js","sourceRoot":"","sources":["../../src/configs/log.storage.external.s3.config.ts"],"names":[],"mappings":";;;AAEA;;;;GAIG;AACI,MAAM,6BAA6B,GAAG,GAA+C,EAAE;IAE5F,IAAI,MAAM,GAA+C;QACvD,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,sBAAsB;QAClF,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,WAAW;QACjD,OAAO,EAAE,QAAQ;KAClB,CAAC;IAEF,IACE,OAAO,CAAC,GAAG,CAAC,qBAAqB;WAC9B,OAAO,CAAC,GAAG,CAAC,yBAAyB,EACxC,CAAC;QACD,MAAM,GAAG;YACP,GAAG,MAAM;YACT,
|
|
1
|
+
{"version":3,"file":"log.storage.external.s3.config.js","sourceRoot":"","sources":["../../src/configs/log.storage.external.s3.config.ts"],"names":[],"mappings":";;;AAEA;;;;GAIG;AACI,MAAM,6BAA6B,GAAG,GAA+C,EAAE;IAE5F,IAAI,MAAM,GAA+C;QACvD,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,sBAAsB;QAClF,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,WAAW;QACjD,OAAO,EAAE,QAAQ;KAClB,CAAC;IAEF,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;QACjC,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IAChD,CAAC;IAED,IACE,OAAO,CAAC,GAAG,CAAC,qBAAqB;WAC9B,OAAO,CAAC,GAAG,CAAC,yBAAyB,EACxC,CAAC;QACD,MAAM,GAAG;YACP,GAAG,MAAM;YACT,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB;YAC9C,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,yBAAyB;SACvD,CAAC;IACJ,CAAC;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;QACjC,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAA;AA5BY,QAAA,6BAA6B,iCA4BzC"}
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* @property {string} endpoint - Custom S3 endpoint (optional)
|
|
10
10
|
* @property {string} accessKeyId - AWS access key ID (optional)
|
|
11
11
|
* @property {string} secretAccessKey - AWS secret access key (optional)
|
|
12
|
+
* @property {string} roleArn - AWS IAM role ARN for assume role authentication (optional)
|
|
12
13
|
*/
|
|
13
14
|
export interface LogStorageExternalS3ServiceConfigInterface {
|
|
14
15
|
/**
|
|
@@ -35,5 +36,9 @@ export interface LogStorageExternalS3ServiceConfigInterface {
|
|
|
35
36
|
* @var {string}
|
|
36
37
|
*/
|
|
37
38
|
secretAccessKey?: string;
|
|
39
|
+
/**
|
|
40
|
+
* @var {string}
|
|
41
|
+
*/
|
|
42
|
+
roleArn?: string;
|
|
38
43
|
}
|
|
39
44
|
//# sourceMappingURL=log.storage.external.s3.service.config.interface.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.storage.external.s3.service.config.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/log.storage.external.s3.service.config.interface.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"log.storage.external.s3.service.config.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/log.storage.external.s3.service.config.interface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,0CAA0C;IAEzD;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,kBAAkB,EAAE,MAAM,CAAC;IAE3B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -9,6 +9,7 @@ import { LogType } from '../types/log.type';
|
|
|
9
9
|
* @property {FirehoseClient} firehose
|
|
10
10
|
* @property {LogStorageExternalS3ServiceConfigInterface} config
|
|
11
11
|
* @property {Map<string, Promise<void>>} writeQueue
|
|
12
|
+
* @property {Promise<any> | null} credentialsCache
|
|
12
13
|
*/
|
|
13
14
|
export declare class LogStorageExternalS3Service extends LogStorageExternalService {
|
|
14
15
|
/**
|
|
@@ -23,6 +24,10 @@ export declare class LogStorageExternalS3Service extends LogStorageExternalServi
|
|
|
23
24
|
* @var {Map<string, Promise<void>>}
|
|
24
25
|
*/
|
|
25
26
|
private writeQueue;
|
|
27
|
+
/**
|
|
28
|
+
* @var {Promise<any> | null}
|
|
29
|
+
*/
|
|
30
|
+
private credentialsCache;
|
|
26
31
|
/**
|
|
27
32
|
* Constructor of the LogStorageS3Service
|
|
28
33
|
*
|
|
@@ -35,6 +40,18 @@ export declare class LogStorageExternalS3Service extends LogStorageExternalServi
|
|
|
35
40
|
* @returns {boolean} True if the storage target should be used, false otherwise
|
|
36
41
|
*/
|
|
37
42
|
shouldUse(): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Gets temporary credentials using STS AssumeRole
|
|
45
|
+
*
|
|
46
|
+
* @returns {Promise<any>} Credentials object for AWS SDK
|
|
47
|
+
*/
|
|
48
|
+
private getAssumeRoleCredentials;
|
|
49
|
+
/**
|
|
50
|
+
* Refreshes temporary credentials using STS AssumeRole
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise<any>} Fresh credentials object for AWS SDK
|
|
53
|
+
*/
|
|
54
|
+
private refreshAssumeRoleCredentials;
|
|
38
55
|
/**
|
|
39
56
|
* Upload log content to Firehose (which delivers to S3)
|
|
40
57
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.storage.external.s3.service.d.ts","sourceRoot":"","sources":["../../src/services/log.storage.external.s3.service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"log.storage.external.s3.service.d.ts","sourceRoot":"","sources":["../../src/services/log.storage.external.s3.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAE3E,OAAO,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAC;AAEvF,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAI5C;;;;;;;;;GASG;AACH,qBAAa,2BAA4B,SAAQ,yBAAyB;IAExE;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAiB;IAEjC;;OAEG;IACH,OAAO,CAAC,MAAM,CAA6C;IAE3D;;OAEG;IACH,OAAO,CAAC,UAAU,CAAyC;IAE3D;;OAEG;IACH,OAAO,CAAC,gBAAgB,CAA6B;IAErD;;;;OAIG;gBACU,gBAAgB,EAAE,yBAAyB;IA6BxD;;;;OAIG;IACI,SAAS,IAAI,OAAO;IAK3B;;;;OAIG;YACW,wBAAwB;IA6BtC;;;;OAIG;YACW,4BAA4B;IAsC1C;;;;;;;OAOG;YACW,SAAS;IAmBvB;;;;;;;;OAQG;YACW,SAAS;IAgCvB;;;;;;OAMG;IACU,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAoBjD"}
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LogStorageExternalS3Service = void 0;
|
|
4
4
|
const client_firehose_1 = require("@aws-sdk/client-firehose");
|
|
5
|
+
const client_sts_1 = require("@aws-sdk/client-sts");
|
|
5
6
|
const log_storage_external_service_1 = require("./log.storage.external.service");
|
|
6
7
|
const log_service_1 = require("./log.service");
|
|
7
8
|
const log_storage_external_s3_config_1 = require("../configs/log.storage.external.s3.config");
|
|
9
|
+
const log_error_1 = require("../errors/log.error");
|
|
8
10
|
/**
|
|
9
11
|
* Class LogStorageExternalS3Service
|
|
10
12
|
*
|
|
@@ -13,6 +15,7 @@ const log_storage_external_s3_config_1 = require("../configs/log.storage.externa
|
|
|
13
15
|
* @property {FirehoseClient} firehose
|
|
14
16
|
* @property {LogStorageExternalS3ServiceConfigInterface} config
|
|
15
17
|
* @property {Map<string, Promise<void>>} writeQueue
|
|
18
|
+
* @property {Promise<any> | null} credentialsCache
|
|
16
19
|
*/
|
|
17
20
|
class LogStorageExternalS3Service extends log_storage_external_service_1.LogStorageExternalService {
|
|
18
21
|
/**
|
|
@@ -26,22 +29,27 @@ class LogStorageExternalS3Service extends log_storage_external_service_1.LogStor
|
|
|
26
29
|
* @var {Map<string, Promise<void>>}
|
|
27
30
|
*/
|
|
28
31
|
this.writeQueue = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* @var {Promise<any> | null}
|
|
34
|
+
*/
|
|
35
|
+
this.credentialsCache = null;
|
|
29
36
|
this.config = (0, log_storage_external_s3_config_1.getLogStorageExternalS3Config)();
|
|
30
|
-
|
|
37
|
+
const firehoseConfig = {
|
|
31
38
|
region: this.config.region,
|
|
32
|
-
endpoint: this.config.endpoint,
|
|
33
39
|
};
|
|
34
|
-
if (this.config.
|
|
40
|
+
if (this.config.roleArn) {
|
|
41
|
+
firehoseConfig.credentials = () => this.getAssumeRoleCredentials();
|
|
42
|
+
}
|
|
43
|
+
else if (this.config.accessKeyId
|
|
35
44
|
&& this.config.secretAccessKey) {
|
|
36
|
-
firehoseConfig = {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
credentials: {
|
|
40
|
-
accessKeyId: this.config.accessKeyId,
|
|
41
|
-
secretAccessKey: this.config.secretAccessKey,
|
|
42
|
-
},
|
|
45
|
+
firehoseConfig.credentials = {
|
|
46
|
+
accessKeyId: this.config.accessKeyId,
|
|
47
|
+
secretAccessKey: this.config.secretAccessKey,
|
|
43
48
|
};
|
|
44
49
|
}
|
|
50
|
+
if (this.config.endpoint) {
|
|
51
|
+
firehoseConfig.endpoint = this.config.endpoint;
|
|
52
|
+
}
|
|
45
53
|
this.firehose = new client_firehose_1.FirehoseClient(firehoseConfig);
|
|
46
54
|
}
|
|
47
55
|
/**
|
|
@@ -52,6 +60,65 @@ class LogStorageExternalS3Service extends log_storage_external_service_1.LogStor
|
|
|
52
60
|
shouldUse() {
|
|
53
61
|
return this.logServiceConfig.useS3Storage;
|
|
54
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Gets temporary credentials using STS AssumeRole
|
|
65
|
+
*
|
|
66
|
+
* @returns {Promise<any>} Credentials object for AWS SDK
|
|
67
|
+
*/
|
|
68
|
+
async getAssumeRoleCredentials() {
|
|
69
|
+
if (!this.config.roleArn) {
|
|
70
|
+
throw new Error('Role ARN is required for AssumeRole authentication');
|
|
71
|
+
}
|
|
72
|
+
if (this.credentialsCache) {
|
|
73
|
+
try {
|
|
74
|
+
const credentials = await this.credentialsCache;
|
|
75
|
+
const expirationTime = new Date(credentials.expiration);
|
|
76
|
+
const now = new Date();
|
|
77
|
+
const bufferTime = 5 * 60 * 1000;
|
|
78
|
+
if (expirationTime.getTime() - now.getTime() > bufferTime) {
|
|
79
|
+
return credentials;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.warn('Cached credentials failed, refreshing:', error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
this.credentialsCache = this.refreshAssumeRoleCredentials();
|
|
87
|
+
return this.credentialsCache;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Refreshes temporary credentials using STS AssumeRole
|
|
91
|
+
*
|
|
92
|
+
* @returns {Promise<any>} Fresh credentials object for AWS SDK
|
|
93
|
+
*/
|
|
94
|
+
async refreshAssumeRoleCredentials() {
|
|
95
|
+
const stsClient = new client_sts_1.STSClient({
|
|
96
|
+
region: this.config.region,
|
|
97
|
+
endpoint: this.config.endpoint
|
|
98
|
+
});
|
|
99
|
+
const command = new client_sts_1.AssumeRoleCommand({
|
|
100
|
+
RoleArn: this.config.roleArn,
|
|
101
|
+
RoleSessionName: `log-service-session-${Date.now()}`,
|
|
102
|
+
DurationSeconds: 3600, // 1 hour
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
const response = await stsClient.send(command);
|
|
106
|
+
if (!response.Credentials) {
|
|
107
|
+
throw new log_error_1.LogError('Failed to retrieve credentials from STS AssumeRole');
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
accessKeyId: response.Credentials.AccessKeyId,
|
|
111
|
+
secretAccessKey: response.Credentials.SecretAccessKey,
|
|
112
|
+
sessionToken: response.Credentials.SessionToken,
|
|
113
|
+
expiration: response.Credentials.Expiration.toISOString(),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error('Failed to assume role:', error);
|
|
118
|
+
this.credentialsCache = null;
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
55
122
|
/**
|
|
56
123
|
* Upload log content to Firehose (which delivers to S3)
|
|
57
124
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.storage.external.s3.service.js","sourceRoot":"","sources":["../../src/services/log.storage.external.s3.service.ts"],"names":[],"mappings":";;;AAAA,8DAA4E;AAC5E,iFAA2E;AAG3E,+CAA2C;AAE3C,8FAA0F;
|
|
1
|
+
{"version":3,"file":"log.storage.external.s3.service.js","sourceRoot":"","sources":["../../src/services/log.storage.external.s3.service.ts"],"names":[],"mappings":";;;AAAA,8DAA4E;AAC5E,oDAAmE;AACnE,iFAA2E;AAG3E,+CAA2C;AAE3C,8FAA0F;AAC1F,mDAA+C;AAE/C;;;;;;;;;GASG;AACH,MAAa,2BAA4B,SAAQ,wDAAyB;IAsBxE;;;;OAIG;IACH,YAAa,gBAA2C;QAEtD,KAAK,CAAE,gBAAgB,CAAC,CAAC;QAjB3B;;WAEG;QACK,eAAU,GAA+B,IAAI,GAAG,EAAE,CAAC;QAE3D;;WAEG;QACK,qBAAgB,GAAwB,IAAI,CAAC;QAWnD,IAAI,CAAC,MAAM,GAAG,IAAA,8DAA6B,GAAE,CAAC;QAE9C,MAAM,cAAc,GAAQ;YAC1B,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;SAC3B,CAAC;QAEF,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACxB,cAAc,CAAC,WAAW,GAAG,GAAG,EAAE,CA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AAC,KAAK,CAAC,IAAa;QAE9B,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,wBAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YAE5C,MAAM,gBAAgB,GAAG,IAAI,CAAC,sBAAsB,EAAE,CAAC;YAEvD,MAAM,IAAI,CAAC,SAAS,CAClB,gBAAgB,EAChB,QAAQ,CACT,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CACX,6CAA6C,EAC7C,KAAK,CACN,CAAC;YAEF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AA9OD,kEA8OC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nicollasfrazao/liguelead-log-service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A standalone logging service for Express applications with multi-destination storage (local files + S3 via Kinesis Firehose) and comprehensive request tracking using strategy pattern.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@aws-sdk/client-firehose": "^3.658.1",
|
|
51
51
|
"@aws-sdk/client-s3": "^3.658.1",
|
|
52
|
+
"@aws-sdk/client-sts": "^3.921.0",
|
|
52
53
|
"uuid": "^9.0.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|