Skip to content

Commit 0863dfc

Browse files
committed
feat: added impersonation as authentication method instead of JSON key download.
1 parent f550c18 commit 0863dfc

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

application/backend/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ app.use(
4444
cors({
4545
origin: "*",
4646
methods: "GET, PUT, POST, DELETE",
47-
allowedHeaders: "Content-Type",
47+
allowedHeaders: "Content-Type, Content-Encoding, enctype, x-server-timeout"
4848
})
4949
);
5050

application/backend/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"body-parser": "^1.20.3",
2020
"compression": "^1.8.0",
2121
"cors": "^2.8.5",
22+
"dotenv": "^16.5.0",
2223
"express": "^4.21.2",
24+
"google-auth-library": "^9.15.1",
2325
"multer": "1.4.5-lts.1",
2426
"pako": "^2.1.0",
2527
"pino-http": "^10.4.0"

application/backend/server.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Load environment variables from .env file before any other imports
2+
import dotenv from "dotenv";
3+
dotenv.config({ path: "../.env" });
4+
15
/*
26
Copyright 2024 Google LLC
37
@@ -14,7 +18,8 @@ See the License for the specific language governing permissions and
1418
limitations under the License.
1519
*/
1620

17-
import { app } from "./app"
21+
// Now import other modules that might use the loaded env variables
22+
import { app } from "./app";
1823
import { log } from "./logging";
1924

2025
const port = process.env.PORT ? parseInt(process.env.PORT) : 8080;

application/backend/services/optimization.ts

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import { v1 } from "@google-cloud/routeoptimization";
1818
import { google } from "@google-cloud/routeoptimization/build/protos/protos";
19+
import { GoogleAuth } from "google-auth-library";
1920
import { CallOptions } from "google-gax";
2021

2122
import { log } from "../logging";
@@ -28,9 +29,29 @@ class FleetRoutingService {
2829
if (!process.env.PROJECT_ID) {
2930
throw Error("Missing required environment variable: PROJECT_ID");
3031
}
32+
if (!process.env.IMPERSONATED_SERVICE_ACCOUNT) {
33+
throw Error(
34+
"Missing required environment variable: IMPERSONATED_SERVICE_ACCOUNT"
35+
);
36+
}
3137
this._parent = `projects/${process.env.PROJECT_ID}`;
3238

39+
const targetPrincipal = process.env.IMPERSONATED_SERVICE_ACCOUNT;
40+
const scopes = ["https://www.googleapis.com/auth/cloud-platform"];
41+
42+
// Configure GoogleAuth for impersonation
43+
const auth = new GoogleAuth({
44+
scopes: scopes,
45+
// Specify the target service account for impersonation
46+
clientOptions: {
47+
subject: targetPrincipal,
48+
},
49+
// Ensure the project ID is used if not implicitly picked up
50+
projectId: process.env.PROJECT_ID,
51+
});
52+
3353
this._client = new v1.RouteOptimizationClient({
54+
auth: auth, // Use the configured GoogleAuth instance
3455
"grpc.keepalive_time_ms": 120000, // 2m
3556
"grpc.keepalive_timeout_ms": 10000, // 10s
3657
"grpc.http2.max_pings_without_data": 0,

docs/development.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Populate `application/.env` file with the details of your Google Cloud project a
167167
| API_ROOT | URL of the backend API (probably `http://localhost:8080/api`) | |
168168
| FRONTEND_PROXY | URL of the frontend Angular development server (probably `http://localhost:4200/`) - *FOR DEVELOPMENT USE ONLY* | |
169169
| MAP_API_KEY | API Key to load Google Maps JavaScript API in frontend (see [*Authentication*](#authentication) section) | |
170-
| GOOGLE_APPLICATION_CREDENTIALS | Path to a service account credentials JSON file to authenticate Google API requests (see [*Authentication*](#authentication) section) | *Default application credentials* |
170+
| IMPERSONATED_SERVICE_ACCOUNT | Service Account to impersonate (Used by backend for Route Optimization API calls) (see [*Authentication*](#authentication) section) | |
171171
| **Optional** | | |
172172
| LOG_FORMAT | Log format to output (`google` or `pretty`) | `google` |
173173
| LOG_LEVEL | Minimum [Pino log level](https://getpino.io/#/docs/api?id=level-string) to output | `info` |

0 commit comments

Comments
 (0)