From 5803ed5718ef045f945ceb96d054928a6bc77ada Mon Sep 17 00:00:00 2001 From: caio1985 Date: Wed, 16 Apr 2025 14:14:49 -0300 Subject: [PATCH] feat: added impersonation as authentication method instead of JSON key download. --- application/backend/app.ts | 2 +- application/backend/package.json | 2 ++ application/backend/server.ts | 7 ++++++- application/backend/services/optimization.ts | 21 ++++++++++++++++++++ docs/development.md | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/application/backend/app.ts b/application/backend/app.ts index 022f062e..f353d824 100644 --- a/application/backend/app.ts +++ b/application/backend/app.ts @@ -44,7 +44,7 @@ app.use( cors({ origin: "*", methods: "GET, PUT, POST, DELETE", - allowedHeaders: "Content-Type", + allowedHeaders: "Content-Type, Content-Encoding, enctype, x-server-timeout" }) ); diff --git a/application/backend/package.json b/application/backend/package.json index 4f805aef..020c7a00 100644 --- a/application/backend/package.json +++ b/application/backend/package.json @@ -19,7 +19,9 @@ "body-parser": "^1.20.3", "compression": "^1.8.0", "cors": "^2.8.5", + "dotenv": "^16.5.0", "express": "^4.21.2", + "google-auth-library": "^9.15.1", "multer": "1.4.5-lts.1", "pako": "^2.1.0", "pino-http": "^10.4.0" diff --git a/application/backend/server.ts b/application/backend/server.ts index 2d0c6700..a8de1d36 100644 --- a/application/backend/server.ts +++ b/application/backend/server.ts @@ -1,3 +1,7 @@ +// Load environment variables from .env file before any other imports +import dotenv from "dotenv"; +dotenv.config({ path: "../.env" }); + /* Copyright 2024 Google LLC @@ -14,7 +18,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { app } from "./app" +// Now import other modules that might use the loaded env variables +import { app } from "./app"; import { log } from "./logging"; const port = process.env.PORT ? parseInt(process.env.PORT) : 8080; diff --git a/application/backend/services/optimization.ts b/application/backend/services/optimization.ts index 349621aa..ca32beeb 100644 --- a/application/backend/services/optimization.ts +++ b/application/backend/services/optimization.ts @@ -16,6 +16,7 @@ limitations under the License. import { v1 } from "@googlemaps/routeoptimization"; import { google } from "@googlemaps/routeoptimization/build/protos/protos"; +import { GoogleAuth } from "google-auth-library"; import { CallOptions } from "google-gax"; import { log } from "../logging"; @@ -28,9 +29,29 @@ class FleetRoutingService { if (!process.env.PROJECT_ID) { throw Error("Missing required environment variable: PROJECT_ID"); } + if (!process.env.IMPERSONATED_SERVICE_ACCOUNT) { + throw Error( + "Missing required environment variable: IMPERSONATED_SERVICE_ACCOUNT" + ); + } this._parent = `projects/${process.env.PROJECT_ID}`; + const targetPrincipal = process.env.IMPERSONATED_SERVICE_ACCOUNT; + const scopes = ["https://www.googleapis.com/auth/cloud-platform"]; + + // Configure GoogleAuth for impersonation + const auth = new GoogleAuth({ + scopes: scopes, + // Specify the target service account for impersonation + clientOptions: { + subject: targetPrincipal, + }, + // Ensure the project ID is used if not implicitly picked up + projectId: process.env.PROJECT_ID, + }); + this._client = new v1.RouteOptimizationClient({ + auth: auth, // Use the configured GoogleAuth instance "grpc.keepalive_time_ms": 120000, // 2m "grpc.keepalive_timeout_ms": 10000, // 10s "grpc.http2.max_pings_without_data": 0, diff --git a/docs/development.md b/docs/development.md index 4e3403fa..8712030e 100644 --- a/docs/development.md +++ b/docs/development.md @@ -167,7 +167,7 @@ Populate `application/.env` file with the details of your Google Cloud project a | API_ROOT | URL of the backend API (probably `http://localhost:8080/api`) | | | FRONTEND_PROXY | URL of the frontend Angular development server (probably `http://localhost:4200/`) - *FOR DEVELOPMENT USE ONLY* | | | MAP_API_KEY | API Key to load Google Maps JavaScript API in frontend (see [*Authentication*](#authentication) section) | | -| GOOGLE_APPLICATION_CREDENTIALS | Path to a service account credentials JSON file to authenticate Google API requests (see [*Authentication*](#authentication) section) | *Default application credentials* | +| IMPERSONATED_SERVICE_ACCOUNT | Service Account to impersonate (Used by backend for Route Optimization API calls) (see [*Authentication*](#authentication) section) | | | **Optional** | | | | LOG_FORMAT | Log format to output (`google` or `pretty`) | `google` | | LOG_LEVEL | Minimum [Pino log level](https://getpino.io/#/docs/api?id=level-string) to output | `info` |