From c66b65538c8ae7cbf79cfe0a2b705ec19764f3c0 Mon Sep 17 00:00:00 2001 From: fabgo Date: Wed, 30 Oct 2024 15:21:02 -0500 Subject: [PATCH] ENG-2960 Read ssoProvider from config host, not p0-prod (#135) Reads the ssoProvider for login from the host specified in the config, instead of defaulting to p0-prod. --- src/commands/login.ts | 14 +++++++------- src/drivers/config.ts | 15 +++++++++++++-- src/drivers/firestore.ts | 12 +++++++----- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/commands/login.ts b/src/commands/login.ts index 3f2db6f..4fb20dc 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -14,8 +14,8 @@ import { IDENTITY_FILE_PATH, } from "../drivers/auth"; import { saveConfig } from "../drivers/config"; -import { bootstrapConfig } from "../drivers/env"; -import { fsShutdownGuard, publicDoc } from "../drivers/firestore"; +import { fsShutdownGuard, initializeFirebase } from "../drivers/firestore"; +import { doc } from "../drivers/firestore"; import { print2 } from "../drivers/stdio"; import { pluginLoginMap } from "../plugins/login"; import { TokenResponse } from "../types/oidc"; @@ -34,13 +34,13 @@ export const login = async ( args: { org: string }, options?: { skipAuthenticate?: boolean } ) => { - const orgDoc = await getDoc( - publicDoc(`orgs/${args.org}`) - ); + await saveConfig(args.org); + await initializeFirebase(); + + const orgDoc = await getDoc(doc(`orgs/${args.org}`)); const orgData = orgDoc.data(); - if (!orgData) throw "Could not find organization"; - await saveConfig(orgData.config ?? bootstrapConfig); + if (!orgData) throw "Could not find organization"; const orgWithSlug: OrgData = { ...orgData, slug: args.org }; diff --git a/src/drivers/config.ts b/src/drivers/config.ts index 0cf5525..d02d048 100644 --- a/src/drivers/config.ts +++ b/src/drivers/config.ts @@ -8,9 +8,12 @@ This file is part of @p0security/cli You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see . **/ -import { Config } from "../types/org"; +import { Config, RawOrgData } from "../types/org"; import { P0_PATH } from "../util"; +import { bootstrapConfig } from "./env"; +import { bootstrapDoc } from "./firestore"; import { print2 } from "./stdio"; +import { getDoc } from "firebase/firestore"; import fs from "fs/promises"; import path from "path"; @@ -22,11 +25,19 @@ export function getTenantConfig(): Config { return tenantConfig; } -export async function saveConfig(config: Config) { +export async function saveConfig(orgId: string) { + const orgDoc = await getDoc( + bootstrapDoc(`orgs/${orgId}`) + ); + const orgData = orgDoc.data(); + const config = orgData?.config ?? bootstrapConfig; + print2(`Saving config to ${CONFIG_FILE_PATH}.`); + const dir = path.dirname(CONFIG_FILE_PATH); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(CONFIG_FILE_PATH, JSON.stringify(config), { mode: "600" }); + tenantConfig = config; } diff --git a/src/drivers/firestore.ts b/src/drivers/firestore.ts index 0b89ece..afad27e 100644 --- a/src/drivers/firestore.ts +++ b/src/drivers/firestore.ts @@ -35,10 +35,12 @@ const bootstrapFirestore = getFirestore(bootstrapApp); let app: FirebaseApp; let firestore: Firestore; -async function initializeFirebase() { - const tenantConfig = await loadConfig(); - app = initializeApp(tenantConfig.fs, "authFirebase"); - firestore = getFirestore(app); +export async function initializeFirebase() { + if (!firestore) { + const tenantConfig = await loadConfig(); + app = initializeApp(tenantConfig.fs, "authFirebase"); + firestore = getFirestore(app); + } } export async function authenticateToFirebase( @@ -85,7 +87,7 @@ export const doc = (path: string) => { return fsDoc(firestore, path) as DocumentReference; }; -export const publicDoc = (path: string) => { +export const bootstrapDoc = (path: string) => { return fsDoc(bootstrapFirestore, path) as DocumentReference; };