From 12bf31bd4c404041f868c21c4cf596820db65f4b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:51:15 +0300 Subject: [PATCH 01/21] Update auth service staging image tag to stage-b77b6fc2-1737917401 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index f5a7113538..76e009dd03 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-e12dd8a6-1737760588 + tag: stage-b77b6fc2-1737917401 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 9c244cfa12023448c0a32b29a94b18aefc18f2cb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:51:41 +0300 Subject: [PATCH 02/21] Update AirQo exceedance production image tag to prod-78dc2fc4-1737917447 --- k8s/exceedance/values-prod-airqo.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/exceedance/values-prod-airqo.yaml b/k8s/exceedance/values-prod-airqo.yaml index ec57ea5dbd..58d0e9df65 100644 --- a/k8s/exceedance/values-prod-airqo.yaml +++ b/k8s/exceedance/values-prod-airqo.yaml @@ -4,6 +4,6 @@ app: configmap: env-exceedance-production image: repository: eu.gcr.io/airqo-250220/airqo-exceedance-job - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' From 0cc96628850c72d6a64422f72d8c67961faab3b6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:51:47 +0300 Subject: [PATCH 03/21] Update KCCA exceedance production image tag to prod-78dc2fc4-1737917447 --- k8s/exceedance/values-prod-kcca.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/exceedance/values-prod-kcca.yaml b/k8s/exceedance/values-prod-kcca.yaml index 02593604f5..ad02d22f94 100644 --- a/k8s/exceedance/values-prod-kcca.yaml +++ b/k8s/exceedance/values-prod-kcca.yaml @@ -4,6 +4,6 @@ app: configmap: env-exceedance-production image: repository: eu.gcr.io/airqo-250220/kcca-exceedance-job - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' From 2d9c6701b2e3cc08b037dd34ed8764cba6926fe6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:52:16 +0300 Subject: [PATCH 04/21] Update incentives production image tag to prod-78dc2fc4-1737917447 --- k8s/incentives/values-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/incentives/values-prod.yaml b/k8s/incentives/values-prod.yaml index d4b6a729e3..645305deef 100644 --- a/k8s/incentives/values-prod.yaml +++ b/k8s/incentives/values-prod.yaml @@ -6,7 +6,7 @@ app: replicaCount: 3 image: repository: eu.gcr.io/airqo-250220/airqo-incentives-api - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' podAnnotations: {} From c9e2dc0d5ec78e07b35f5ec8fdeb4aeef9a04db6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:52:38 +0300 Subject: [PATCH 05/21] Update auth service production image tag to prod-78dc2fc4-1737917447 --- k8s/auth-service/values-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-prod.yaml b/k8s/auth-service/values-prod.yaml index a7d5202def..7cb17b3fca 100644 --- a/k8s/auth-service/values-prod.yaml +++ b/k8s/auth-service/values-prod.yaml @@ -6,7 +6,7 @@ app: replicaCount: 3 image: repository: eu.gcr.io/airqo-250220/airqo-auth-api - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 9ffdaf0ff30d474238dfca452bd1ed7ae2f3a813 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:52:45 +0300 Subject: [PATCH 06/21] Update calibrate production tags to prod-78dc2fc4-1737917447 --- k8s/calibrate/values-prod.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/k8s/calibrate/values-prod.yaml b/k8s/calibrate/values-prod.yaml index cf36fbabb5..daaf53964a 100644 --- a/k8s/calibrate/values-prod.yaml +++ b/k8s/calibrate/values-prod.yaml @@ -6,11 +6,11 @@ app: initContainer: image: repository: eu.gcr.io/airqo-250220/airqo-calibrate-pickle-file - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 replicaCount: 3 image: repository: eu.gcr.io/airqo-250220/airqo-calibrate-api - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 7a01104632a53c4a8131e8308a3cb7d41f45ec4a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:52:47 +0300 Subject: [PATCH 07/21] Update device registry production image tag to prod-78dc2fc4-1737917447 --- k8s/device-registry/values-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/device-registry/values-prod.yaml b/k8s/device-registry/values-prod.yaml index ae983017d1..5ea1d03190 100644 --- a/k8s/device-registry/values-prod.yaml +++ b/k8s/device-registry/values-prod.yaml @@ -6,7 +6,7 @@ app: replicaCount: 3 image: repository: eu.gcr.io/airqo-250220/airqo-device-registry-api - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 8b27a733680fe020989deccdf48b2f75c0e21eb6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:53:30 +0300 Subject: [PATCH 08/21] Update workflows prod image tag to prod-78dc2fc4-1737917447 --- k8s/workflows/values-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/workflows/values-prod.yaml b/k8s/workflows/values-prod.yaml index 9e2e95ec53..2c803b681d 100644 --- a/k8s/workflows/values-prod.yaml +++ b/k8s/workflows/values-prod.yaml @@ -10,7 +10,7 @@ images: initContainer: eu.gcr.io/airqo-250220/airqo-workflows-xcom redisContainer: eu.gcr.io/airqo-250220/airqo-redis containers: eu.gcr.io/airqo-250220/airqo-workflows - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 0241f44ca05de346566932d146571bd63c032cf2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:55:00 +0300 Subject: [PATCH 09/21] Update predict production image tag to prod-78dc2fc4-1737917447 --- k8s/predict/values-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/predict/values-prod.yaml b/k8s/predict/values-prod.yaml index 5098fa8e10..89a51f5649 100644 --- a/k8s/predict/values-prod.yaml +++ b/k8s/predict/values-prod.yaml @@ -7,7 +7,7 @@ images: predictJob: eu.gcr.io/airqo-250220/airqo-predict-job trainJob: eu.gcr.io/airqo-250220/airqo-train-job predictPlaces: eu.gcr.io/airqo-250220/airqo-predict-places-air-quality - tag: prod-73e08b6e-1737906940 + tag: prod-78dc2fc4-1737917447 api: name: airqo-prediction-api label: prediction-api From e0bbc811ed5797740a4b2e1b359932497f2906d3 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 00:38:21 +0300 Subject: [PATCH 10/21] just enhancing the logging for API usage --- src/auth-service/config/log4js.js | 4 +- src/auth-service/utils/token.util.js | 76 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/auth-service/config/log4js.js b/src/auth-service/config/log4js.js index 90f3766f44..ce3aabb3e9 100644 --- a/src/auth-service/config/log4js.js +++ b/src/auth-service/config/log4js.js @@ -10,6 +10,7 @@ const constants = require("./constants"); const log4js = { appenders: { console: { type: "console" }, + out: { type: "stdout" }, // Log to standard output (essential for Cloud Run/Functions) access: { type: "dateFile", filename: "log/access.log", @@ -39,9 +40,10 @@ const log4js = { }, }, categories: { - default: { appenders: ["alerts"], level: "info" }, + default: { appenders: ["alerts", "out"], level: "info" }, error: { appenders: ["alerts", "errors"], level: "error" }, http: { appenders: ["access"], level: "DEBUG" }, + "api-usage-logger": { appenders: ["out"], level: "info" }, // API Usage logs *only* to stdout, not to slack! }, }; diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index a2b74e6d25..88d3903cb6 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -25,6 +25,7 @@ const moment = require("moment-timezone"); const ObjectId = mongoose.Types.ObjectId; const log4js = require("log4js"); const logger = log4js.getLogger(`${constants.ENVIRONMENT} -- token-util`); +const apiUsageLogger = log4js.getLogger("api-usage-logger"); const async = require("async"); const { Kafka } = require("kafkajs"); const kafka = new Kafka({ @@ -415,6 +416,56 @@ const isIPBlacklistedHelper = async ( const isIPBlacklisted = (...args) => trampoline(() => isIPBlacklistedHelper(...args)); +//Create async queue for handling API Usage logs +const apiUsageQueue = async.queue(async (task, callback) => { + try { + const { + timestamp, + token, + client_id, + endpoint, + ip_address, + user_agent, + response_code, + latency, + } = task; + + const logPayload = { + timestamp, + token, + client_id, + endpoint, + ip_address, + user_agent, + response_code, + latency, + }; + + logger.info(JSON.stringify(logPayload)); // Log the structured data + callback(); + } catch (error) { + logger.error( + `🐛🐛 API Usage Queue Internal Server Error --- ${error.message}` + ); + } +}, 1); // Adjust concurrency as needed + +const logAPIUsage = async (logData) => { + try { + // Asynchronous Logging using a queue (recommended) + apiUsageQueue.push(logData); + } catch (error) { + // Handle logging errors gracefully + logger.error(`Error logging API usage: ${error.message}`); // Ensure 'logger' is defined + } +}; + +const calculateLatency = (startTime, endTime = process.hrtime.bigint()) => { + const nanoseconds = Number(endTime - startTime); + const milliseconds = nanoseconds / 1000000; + return milliseconds; +}; + const token = { verifyEmail: async (request, next) => { try { @@ -672,6 +723,7 @@ const token = { verifyToken: async (request, next) => { try { logText("I have just entered the verifyToken() function"); + const startTime = process.hrtime.bigint(); // Start measuring latency const ip = request.headers["x-client-ip"] || request.headers["x-client-original-ip"]; @@ -715,6 +767,30 @@ const token = { clientOriginalIp: ip, endpoint: endpoint ? endpoint : "unknown", }); + + const endTime = process.hrtime.bigint(); + const latency = calculateLatency(startTime, endTime); + + await AccessTokenModel("airqo").updateOne( + { token }, + { $set: { last_used_at: new Date(), last_ip_address: ip } } + ); + const logPayload = { + timestamp: new Date().toISOString(), + token: accessToken.token, + client_id: accessToken.client_id, + endpoint, + ip_address: ip, + user_agent: request.headers["user-agent"], + response_code: 200, + latency: latency, + }; + + if (constants.ENVIRONMENT === "PRODUCTION ENVIRONMENT") { + } + await logAPIUsage(logPayload); + apiUsageLogger.info(stringify(logPayload)); // Log to stdout (for BigQuery) + return createValidTokenResponse(); } } From 6a808a5afea8d497597670c4fa148fe6ac8ba66f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 00:48:42 +0300 Subject: [PATCH 11/21] Update auth service staging image tag to stage-24626bec-1737928031 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index 76e009dd03..4fecbf2d1c 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-b77b6fc2-1737917401 + tag: stage-24626bec-1737928031 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 8751753701eb1cbc1834cfe0c6e7422986ad3444 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 01:48:37 +0300 Subject: [PATCH 12/21] resolving a runtime with access token verification --- src/auth-service/models/AccessToken.js | 2 +- src/auth-service/utils/token.util.js | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/auth-service/models/AccessToken.js b/src/auth-service/models/AccessToken.js index cdcd0e90d3..2545d0c3ed 100644 --- a/src/auth-service/models/AccessToken.js +++ b/src/auth-service/models/AccessToken.js @@ -41,7 +41,7 @@ const AccessTokenSchema = new mongoose.Schema( required: [true, "token is required!"], }, last_used_at: { type: Date }, - last_ip_address: { type: Date }, + last_ip_address: { type: String }, expires_in: { type: Number }, expires: { type: Date, diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index 88d3903cb6..c7f10bb66a 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -771,10 +771,14 @@ const token = { const endTime = process.hrtime.bigint(); const latency = calculateLatency(startTime, endTime); - await AccessTokenModel("airqo").updateOne( - { token }, - { $set: { last_used_at: new Date(), last_ip_address: ip } } - ); + try { + await AccessTokenModel("airqo").updateOne( + { token }, + { $set: { last_used_at: new Date(), last_ip_address: ip } } + ); + } catch (updateError) { + logger.error(`Error updating AccessToken: ${updateError.message}`); + } const logPayload = { timestamp: new Date().toISOString(), token: accessToken.token, @@ -786,10 +790,12 @@ const token = { latency: latency, }; - if (constants.ENVIRONMENT === "PRODUCTION ENVIRONMENT") { + try { + await logAPIUsage(logPayload); + apiUsageLogger.info(stringify(logPayload)); + } catch (loggingError) { + logger.error(`Error logging API Usage: ${loggingError.message}`); } - await logAPIUsage(logPayload); - apiUsageLogger.info(stringify(logPayload)); // Log to stdout (for BigQuery) return createValidTokenResponse(); } From dd3bd0e7f1cf89be1af3b400fa150c933200d5ef Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 01:53:28 +0300 Subject: [PATCH 13/21] Update auth service staging image tag to stage-ef33d6c3-1737931922 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index 4fecbf2d1c..7902601aa5 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-24626bec-1737928031 + tag: stage-ef33d6c3-1737931922 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 40ddc5a4efdc3758be337ce81dad6559720b51d2 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 02:21:05 +0300 Subject: [PATCH 14/21] address runtime for cast error --- src/auth-service/utils/token.util.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index c7f10bb66a..c9ae8e465e 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -441,19 +441,25 @@ const apiUsageQueue = async.queue(async (task, callback) => { latency, }; - logger.info(JSON.stringify(logPayload)); // Log the structured data + apiUsageLogger.info(stringify(logPayload)); callback(); } catch (error) { logger.error( `🐛🐛 API Usage Queue Internal Server Error --- ${error.message}` ); + callback(error); } }, 1); // Adjust concurrency as needed const logAPIUsage = async (logData) => { try { - // Asynchronous Logging using a queue (recommended) - apiUsageQueue.push(logData); + // Asynchronous Logging using a queue + apiUsageQueue.push(logData, (err) => { + // Provide a callback to handle potential queue errors + if (err) { + logger.error(`Error pushing to apiUsageQueue: ${err.message}`); + } + }); } catch (error) { // Handle logging errors gracefully logger.error(`Error logging API usage: ${error.message}`); // Ensure 'logger' is defined @@ -782,7 +788,7 @@ const token = { const logPayload = { timestamp: new Date().toISOString(), token: accessToken.token, - client_id: accessToken.client_id, + client_id: accessToken.client_id.toString(), endpoint, ip_address: ip, user_agent: request.headers["user-agent"], @@ -792,7 +798,6 @@ const token = { try { await logAPIUsage(logPayload); - apiUsageLogger.info(stringify(logPayload)); } catch (loggingError) { logger.error(`Error logging API Usage: ${loggingError.message}`); } From 1187c3900c4639cee5cd62908688e58b1e8716f5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 02:26:52 +0300 Subject: [PATCH 15/21] Update auth service staging image tag to stage-0a02ff2c-1737933923 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index 7902601aa5..ca4a8d8f2f 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-ef33d6c3-1737931922 + tag: stage-0a02ff2c-1737933923 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 9c3160f610d12e22c14a0a4daeeb62810d90f731 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 02:41:57 +0300 Subject: [PATCH 16/21] runtime for processing queue of token logging --- src/auth-service/utils/token.util.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index c9ae8e465e..87028f2c67 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -451,19 +451,18 @@ const apiUsageQueue = async.queue(async (task, callback) => { } }, 1); // Adjust concurrency as needed -const logAPIUsage = async (logData) => { - try { - // Asynchronous Logging using a queue +const logAPIUsage = (logData) => { + // No need for async here + return new Promise((resolve, reject) => { apiUsageQueue.push(logData, (err) => { - // Provide a callback to handle potential queue errors if (err) { logger.error(`Error pushing to apiUsageQueue: ${err.message}`); + reject(err); // Reject the promise if there's a queue error + } else { + resolve(); // Resolve the promise if the task is pushed successfully } }); - } catch (error) { - // Handle logging errors gracefully - logger.error(`Error logging API usage: ${error.message}`); // Ensure 'logger' is defined - } + }); }; const calculateLatency = (startTime, endTime = process.hrtime.bigint()) => { From cf00561c1d54dc089ed5b2800e4cf6cbd4f4ee96 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 02:47:46 +0300 Subject: [PATCH 17/21] Update auth service staging image tag to stage-a6a0f84d-1737935177 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index ca4a8d8f2f..61cc7f2e78 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-0a02ff2c-1737933923 + tag: stage-a6a0f84d-1737935177 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 38fc5c22d91bac815f19f24872879a807add27c7 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 07:39:10 +0300 Subject: [PATCH 18/21] reverting verify token changes --- src/auth-service/utils/token.util.js | 84 ---------------------------- 1 file changed, 84 deletions(-) diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index 87028f2c67..76db83c97d 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -416,61 +416,6 @@ const isIPBlacklistedHelper = async ( const isIPBlacklisted = (...args) => trampoline(() => isIPBlacklistedHelper(...args)); -//Create async queue for handling API Usage logs -const apiUsageQueue = async.queue(async (task, callback) => { - try { - const { - timestamp, - token, - client_id, - endpoint, - ip_address, - user_agent, - response_code, - latency, - } = task; - - const logPayload = { - timestamp, - token, - client_id, - endpoint, - ip_address, - user_agent, - response_code, - latency, - }; - - apiUsageLogger.info(stringify(logPayload)); - callback(); - } catch (error) { - logger.error( - `🐛🐛 API Usage Queue Internal Server Error --- ${error.message}` - ); - callback(error); - } -}, 1); // Adjust concurrency as needed - -const logAPIUsage = (logData) => { - // No need for async here - return new Promise((resolve, reject) => { - apiUsageQueue.push(logData, (err) => { - if (err) { - logger.error(`Error pushing to apiUsageQueue: ${err.message}`); - reject(err); // Reject the promise if there's a queue error - } else { - resolve(); // Resolve the promise if the task is pushed successfully - } - }); - }); -}; - -const calculateLatency = (startTime, endTime = process.hrtime.bigint()) => { - const nanoseconds = Number(endTime - startTime); - const milliseconds = nanoseconds / 1000000; - return milliseconds; -}; - const token = { verifyEmail: async (request, next) => { try { @@ -728,7 +673,6 @@ const token = { verifyToken: async (request, next) => { try { logText("I have just entered the verifyToken() function"); - const startTime = process.hrtime.bigint(); // Start measuring latency const ip = request.headers["x-client-ip"] || request.headers["x-client-original-ip"]; @@ -773,34 +717,6 @@ const token = { endpoint: endpoint ? endpoint : "unknown", }); - const endTime = process.hrtime.bigint(); - const latency = calculateLatency(startTime, endTime); - - try { - await AccessTokenModel("airqo").updateOne( - { token }, - { $set: { last_used_at: new Date(), last_ip_address: ip } } - ); - } catch (updateError) { - logger.error(`Error updating AccessToken: ${updateError.message}`); - } - const logPayload = { - timestamp: new Date().toISOString(), - token: accessToken.token, - client_id: accessToken.client_id.toString(), - endpoint, - ip_address: ip, - user_agent: request.headers["user-agent"], - response_code: 200, - latency: latency, - }; - - try { - await logAPIUsage(logPayload); - } catch (loggingError) { - logger.error(`Error logging API Usage: ${loggingError.message}`); - } - return createValidTokenResponse(); } } From 7696bffbeb2d212e9dd0d739c93616ff934061fd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 07:49:56 +0300 Subject: [PATCH 19/21] Update auth service staging image tag to stage-880d0043-1737953310 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index 61cc7f2e78..c1d288cad4 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-a6a0f84d-1737935177 + tag: stage-880d0043-1737953310 nameOverride: '' fullnameOverride: '' podAnnotations: {} From 05837e8a3d02d9a167ea0ced35776ce4b0e85549 Mon Sep 17 00:00:00 2001 From: baalmart Date: Mon, 27 Jan 2025 07:51:23 +0300 Subject: [PATCH 20/21] remove unused imports --- src/auth-service/utils/token.util.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/auth-service/utils/token.util.js b/src/auth-service/utils/token.util.js index 76db83c97d..a2b74e6d25 100644 --- a/src/auth-service/utils/token.util.js +++ b/src/auth-service/utils/token.util.js @@ -25,7 +25,6 @@ const moment = require("moment-timezone"); const ObjectId = mongoose.Types.ObjectId; const log4js = require("log4js"); const logger = log4js.getLogger(`${constants.ENVIRONMENT} -- token-util`); -const apiUsageLogger = log4js.getLogger("api-usage-logger"); const async = require("async"); const { Kafka } = require("kafkajs"); const kafka = new Kafka({ @@ -716,7 +715,6 @@ const token = { clientOriginalIp: ip, endpoint: endpoint ? endpoint : "unknown", }); - return createValidTokenResponse(); } } From 7a591033df6639a6ca1348f8032c4da5216ab601 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 07:58:24 +0300 Subject: [PATCH 21/21] Update auth service staging image tag to stage-ae418a99-1737953821 --- k8s/auth-service/values-stage.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/auth-service/values-stage.yaml b/k8s/auth-service/values-stage.yaml index c1d288cad4..87c5c2c142 100644 --- a/k8s/auth-service/values-stage.yaml +++ b/k8s/auth-service/values-stage.yaml @@ -6,7 +6,7 @@ app: replicaCount: 2 image: repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api - tag: stage-880d0043-1737953310 + tag: stage-ae418a99-1737953821 nameOverride: '' fullnameOverride: '' podAnnotations: {}