From 21e83834763221fea26a0ed0b0689ea3d4512291 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Sat, 31 Aug 2024 19:49:47 -0700 Subject: [PATCH] fix: Add informative logging to targets --- apps/event-worker/src/target-scan/gke.ts | 11 +++++- apps/event-worker/src/target-scan/google.ts | 42 ++++++++++++++------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/apps/event-worker/src/target-scan/gke.ts b/apps/event-worker/src/target-scan/gke.ts index 193204b6..e198c522 100644 --- a/apps/event-worker/src/target-scan/gke.ts +++ b/apps/event-worker/src/target-scan/gke.ts @@ -27,11 +27,18 @@ export const getGkeTargets = async ( googleServiceAccountEmail, ); + if (!googleClusterClient) return []; + const clusters = ( await Promise.allSettled( config.projectIds.map(async (project) => { - const clusters = await getClusters(googleClusterClient, project); - return { project, clusters }; + try { + const clusters = await getClusters(googleClusterClient, project); + return { project, clusters }; + } catch (e) { + log.error("error getting clusters"); + return { project, clusters: [] }; + } }), ) ) diff --git a/apps/event-worker/src/target-scan/google.ts b/apps/event-worker/src/target-scan/google.ts index b386ca8e..fc85320d 100644 --- a/apps/event-worker/src/target-scan/google.ts +++ b/apps/event-worker/src/target-scan/google.ts @@ -5,26 +5,40 @@ import { KubeConfig } from "@kubernetes/client-node"; import { GoogleAuth, Impersonated } from "google-auth-library"; import { SemVer } from "semver"; +import { logger } from "@ctrlplane/logger"; + import { omitNullUndefined } from "../utils.js"; const sourceCredentials = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"], }); -export const getGoogleClusterClient = async (targetPrincipal?: string | null) => - new Container.v1.ClusterManagerClient({ - authClient: new Impersonated( - targetPrincipal != null - ? { - sourceClient: await sourceCredentials.getClient(), - targetPrincipal, - lifetime: 3600, // Token lifetime in seconds - delegates: [], - targetScopes: ["https://www.googleapis.com/auth/cloud-platform"], - } - : {}, - ), - }); +const log = logger.child({ label: "target-scan/gke" }); + +export const getGoogleClusterClient = async ( + targetPrincipal?: string | null, +) => { + try { + const sourceClient = await sourceCredentials.getClient(); + log.info("got source client"); + const impersonated = new Impersonated({ + sourceClient, + targetPrincipal: targetPrincipal ?? undefined, + lifetime: 3600, + delegates: [], + targetScopes: ["https://www.googleapis.com/auth/cloud-platform"], + }); + log.info("got impersonated"); + + const clusterClient = new Container.v1.ClusterManagerClient({ + authClient: impersonated, + }); + log.info("got cluster client"); + return clusterClient; + } catch (e) { + log.error(e); + } +}; export const getClusters = async ( clusterClient: ClusterManagerClient,