-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENG-2884 Add multi-host support #132
Changes from 3 commits
31a6994
6b3de7b
7ae96f6
d5481a8
52043b7
039f839
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,15 @@ You should have received a copy of the GNU General Public License along with @p0 | |
import { | ||
IDENTITY_CACHE_PATH, | ||
IDENTITY_FILE_PATH, | ||
authenticate, | ||
loadCredentials, | ||
} from "../drivers/auth"; | ||
import { doc, guard } from "../drivers/firestore"; | ||
import { saveConfig } from "../drivers/config"; | ||
import { bootstrapConfig } from "../drivers/env"; | ||
import { | ||
authenticateToFirebase, | ||
fsShutdownGuard, | ||
publicDoc, | ||
} from "../drivers/firestore"; | ||
import { print2 } from "../drivers/stdio"; | ||
import { pluginLoginMap } from "../plugins/login"; | ||
import { TokenResponse } from "../types/oidc"; | ||
|
@@ -32,10 +38,14 @@ export const login = async ( | |
args: { org: string }, | ||
options?: { skipAuthenticate?: boolean } | ||
) => { | ||
const orgDoc = await getDoc<RawOrgData, object>(doc(`orgs/${args.org}`)); | ||
const orgDoc = await getDoc<RawOrgData, object>( | ||
publicDoc(`orgs/${args.org}`) | ||
); | ||
const orgData = orgDoc.data(); | ||
if (!orgData) throw "Could not find organization"; | ||
|
||
await saveConfig(orgData.config ?? bootstrapConfig); | ||
|
||
const orgWithSlug: OrgData = { ...orgData, slug: args.org }; | ||
|
||
const plugin = orgWithSlug?.ssoProvider; | ||
|
@@ -49,7 +59,10 @@ export const login = async ( | |
await writeIdentity(orgWithSlug, tokenResponse); | ||
|
||
// validate auth | ||
if (!options?.skipAuthenticate) await authenticate({ noRefresh: true }); | ||
if (!options?.skipAuthenticate) { | ||
const identity = await loadCredentials({ noRefresh: true }); | ||
await authenticateToFirebase(identity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It used to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because authenticate now saves the config & initializes firebase. I would like to rename the authenticate method now, but couldn't think of a better name. And there used to be a circular call chain, which I am trying to break: authenticate -> loadCredentials -> login -> authenticate |
||
} | ||
|
||
print2(`You are now logged in, and can use the p0 CLI.`); | ||
}; | ||
|
@@ -95,5 +108,5 @@ export const loginCommand = (yargs: yargs.Argv) => | |
type: "string", | ||
describe: "Your P0 organization ID", | ||
}), | ||
guard(login) | ||
fsShutdownGuard(login) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** Copyright © 2024-present P0 Security | ||
|
||
This file is part of @p0security/cli | ||
|
||
@p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. | ||
|
||
@p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>. | ||
**/ | ||
import { Config } from "../types/org"; | ||
import { P0_PATH } from "../util"; | ||
import { print2 } from "./stdio"; | ||
import fs from "fs/promises"; | ||
import path from "path"; | ||
|
||
export const CONFIG_FILE_PATH = path.join(P0_PATH, "config.json"); | ||
|
||
let tenantConfig: Config; | ||
|
||
export function getTenantConfig(): Config { | ||
return tenantConfig; | ||
} | ||
|
||
export async function saveConfig(config: Config) { | ||
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" }); | ||
} | ||
|
||
export async function loadConfig() { | ||
const buffer = await fs.readFile(CONFIG_FILE_PATH); | ||
tenantConfig = JSON.parse(buffer.toString()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is won't pass on other computers because it has the home folder in it. I think jest has some way of asserting on a pattern instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed