azify-logger 1.0.50 → 1.0.51-test

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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/register.js +20 -7
  3. package/server.js +48 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.50",
3
+ "version": "1.0.51-test",
4
4
  "description": "Azify Logger Client - Centralized logging for OpenSearch",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -60,8 +60,8 @@
60
60
  "fastify-plugin": "^5.0.0",
61
61
  "import-in-the-middle": "^3.0.0",
62
62
  "js-yaml": "^4.1.0",
63
+ "openid-client": "^5.7.1",
63
64
  "passport": "^0.7.0",
64
- "passport-azure-ad": "^4.3.5",
65
65
  "require-in-the-middle": "^7.4.0",
66
66
  "uuid": "^9.0.1"
67
67
  },
package/register.js CHANGED
@@ -39,6 +39,17 @@ try {
39
39
  const loggerUrlString = loggerEndpoint.toString()
40
40
  let transport = null
41
41
  const MAX_PENDING_OUTBOUND = 2000
42
+ let requestResponseDirectHttpFailureAnnounced = false
43
+
44
+ function announceRequestResponseDirectHttpFailure(line) {
45
+ if (requestResponseDirectHttpFailureAnnounced) {
46
+ return
47
+ }
48
+ requestResponseDirectHttpFailureAnnounced = true
49
+ try {
50
+ process.stderr.write(line)
51
+ } catch (_) {}
52
+ }
42
53
 
43
54
  function sendRequestResponseDirectHttp(payload) {
44
55
  try {
@@ -57,17 +68,19 @@ try {
57
68
  const debug = process.env.AZIFY_LOGGER_DEBUG === '1'
58
69
  const req = lib.request(opts, function (res) {
59
70
  res.resume()
60
- if (res.statusCode < 200 || res.statusCode >= 300) {
61
- try {
62
- process.stderr.write(`[azify-logger] REQUEST/RESPONSE log POST failed: server responded ${res.statusCode} (check log server at ${loggerUrlString})\n`)
63
- } catch (_) {}
71
+ if (res.statusCode >= 200 && res.statusCode < 300) {
72
+ requestResponseDirectHttpFailureAnnounced = false
73
+ } else if (res.statusCode < 200 || res.statusCode >= 300) {
74
+ announceRequestResponseDirectHttpFailure(
75
+ `[azify-logger] REQUEST/RESPONSE log POST failed: server responded ${res.statusCode} (check log server at ${loggerUrlString})\n`,
76
+ )
64
77
  if (debug) try { process.stderr.write(`[azify-logger] direct HTTP log server responded ${res.statusCode}\n`) } catch (_) {}
65
78
  }
66
79
  })
67
80
  req.on('error', function (err) {
68
- try {
69
- process.stderr.write(`[azify-logger] REQUEST/RESPONSE log POST failed: ${err && err.message} (is log server running at ${loggerUrlString}?)\n`)
70
- } catch (_) {}
81
+ announceRequestResponseDirectHttpFailure(
82
+ `[azify-logger] REQUEST/RESPONSE log POST failed: ${err && err.message} (is log server running at ${loggerUrlString}?)\n`,
83
+ )
71
84
  if (debug) try { process.stderr.write(`[azify-logger] direct HTTP to log server failed: ${err && err.message}\n`) } catch (_) {}
72
85
  })
73
86
  req.setTimeout(5000, function () { req.destroy() })
package/server.js CHANGED
@@ -67,14 +67,13 @@ if (authEnabled) {
67
67
  console.error('[auth] ⚠️ Azure AD habilitado mas variáveis faltando. Defina AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET e AZURE_REDIRECT_URL em env/app.env')
68
68
  console.error('[auth] Variáveis atuais: AZURE_TENANT_ID=' + (tenantId ? '***' : 'VAZIO') + ', AZURE_CLIENT_ID=' + (clientId ? '***' : 'VAZIO') + ', AZURE_CLIENT_SECRET=' + (clientSecret ? '***' : 'VAZIO') + ', AZURE_REDIRECT_URL=' + (redirectUrl ? '***' : 'VAZIO'))
69
69
  } else {
70
- const { setupAuth, setupAuthRoutes, ensureAuthenticated: _ensureAuth, ensureAdmin: _ensureAdmin } = require('./auth')
70
+ const { setupAuth, ensureAuthenticated: _ensureAuth, ensureAdmin: _ensureAdmin } = require('./auth')
71
71
  setupAuth(app)
72
- setupAuthRoutes(app)
73
72
  ensureAuthenticated = _ensureAuth
74
73
  ensureAdmin = _ensureAdmin
75
74
  }
76
75
  }
77
- console.log('[startup] auth configured')
76
+ console.log('[startup] auth session configured (OIDC routes registradas após init)')
78
77
 
79
78
  const IS_LOCAL = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'dev' || !process.env.NODE_ENV
80
79
 
@@ -2564,28 +2563,53 @@ app.use((err, req, res, next) => {
2564
2563
 
2565
2564
  const port = process.env.PORT || 3001
2566
2565
 
2567
- app.listen(port, () => {
2568
- console.log('[startup] server listening on port', port, '- /health ready')
2569
- setTimeout(() => writeOtelCollectorConfigIfEnabled().catch(() => {}), 3000)
2570
- setTimeout(() => {
2571
- fetchGrafanaOrgs()
2572
- .then((orgs) => {
2573
- const toSetup = orgs.filter((o) => o.id !== 1 && o.name && o.name.toLowerCase() !== 'main org')
2574
- if (setupGrafanaForApp._cache) setupGrafanaForApp._cache.clear()
2575
- function runNext(i) {
2576
- if (i >= toSetup.length) {
2577
- if (toSetup.length) writeOtelCollectorConfigIfEnabled().catch(() => {})
2578
- return
2566
+ async function startServer() {
2567
+ if (authEnabled) {
2568
+ const tenantId = process.env.AZURE_TENANT_ID
2569
+ const clientId = process.env.AZURE_CLIENT_ID
2570
+ const clientSecret = process.env.AZURE_CLIENT_SECRET
2571
+ const redirectUrl = process.env.AZURE_REDIRECT_URL
2572
+ if (tenantId && clientId && clientSecret && redirectUrl) {
2573
+ try {
2574
+ const auth = require('./auth')
2575
+ await auth.initializeAzureAuth()
2576
+ auth.setupAuthRoutes(app)
2577
+ console.log('[startup] Azure AD OIDC (openid-client) rotas /auth prontas')
2578
+ } catch (e) {
2579
+ console.error('[auth] Falha ao inicializar OIDC:', e?.message || e)
2580
+ process.exit(1)
2581
+ }
2582
+ }
2583
+ }
2584
+
2585
+ app.listen(port, () => {
2586
+ console.log('[startup] server listening on port', port, '- /health ready')
2587
+ setTimeout(() => writeOtelCollectorConfigIfEnabled().catch(() => {}), 3000)
2588
+ setTimeout(() => {
2589
+ fetchGrafanaOrgs()
2590
+ .then((orgs) => {
2591
+ const toSetup = orgs.filter((o) => o.id !== 1 && o.name && o.name.toLowerCase() !== 'main org')
2592
+ if (setupGrafanaForApp._cache) setupGrafanaForApp._cache.clear()
2593
+ function runNext(i) {
2594
+ if (i >= toSetup.length) {
2595
+ if (toSetup.length) writeOtelCollectorConfigIfEnabled().catch(() => {})
2596
+ return
2597
+ }
2598
+ setImmediate(() => {
2599
+ setupGrafanaForApp(toSetup[i].name).catch((e) => console.warn('[setupGrafana] Reaplicar Tempo para org', toSetup[i].name, ':', e?.message || e))
2600
+ .finally(() => setTimeout(() => runNext(i + 1), 1000))
2601
+ })
2579
2602
  }
2580
- setImmediate(() => {
2581
- setupGrafanaForApp(toSetup[i].name).catch((e) => console.warn('[setupGrafana] Reaplicar Tempo para org', toSetup[i].name, ':', e?.message || e))
2582
- .finally(() => setTimeout(() => runNext(i + 1), 1000))
2583
- })
2584
- }
2585
- runNext(0)
2586
- })
2587
- .catch((e) => console.warn('[setupGrafana] Reaplicar Tempo para todas as orgs:', e?.message || e))
2588
- }, 60000)
2603
+ runNext(0)
2604
+ })
2605
+ .catch((e) => console.warn('[setupGrafana] Reaplicar Tempo para todas as orgs:', e?.message || e))
2606
+ }, 60000)
2607
+ })
2608
+ }
2609
+
2610
+ startServer().catch((e) => {
2611
+ console.error('[startup] erro fatal:', e)
2612
+ process.exit(1)
2589
2613
  })
2590
2614
 
2591
2615
  process.on('SIGTERM', () => {