Skip to content

Commit

Permalink
Merge pull request #25 from p0-security/nathan/ssh-guard
Browse files Browse the repository at this point in the history
ssh: Show friendly error message if not installed
  • Loading branch information
nbrahms authored Feb 22, 2024
2 parents 77e650d + 6871828 commit 7f5e7f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/commands/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { doc, guard } from "../drivers/firestore";
import { print2 } from "../drivers/stdio";
import { ssm } from "../plugins/aws/ssm";
import { AwsSsh } from "../plugins/aws/types";
import { SshConfig } from "../plugins/ssh/types";
import { Authn } from "../types/identity";
import {
DENIED_STATUSES,
Expand All @@ -12,7 +13,7 @@ import {
Request,
} from "../types/request";
import { request } from "./request";
import { onSnapshot } from "firebase/firestore";
import { getDoc, onSnapshot } from "firebase/firestore";
import { pick } from "lodash";
import yargs from "yargs";

Expand All @@ -33,6 +34,20 @@ export const sshCommand = (yargs: yargs.Argv) =>
guard(ssh)
);

const validateSshInstall = async (authn: Authn) => {
const configDoc = await getDoc<SshConfig, object>(
doc(`o/${authn.identity.org.tenantId}/integrations/ssh`)
);
const items = configDoc
.data()
?.workflows?.items.filter(
(i) => i.state === "installed" && i.type === "aws"
);
if (!items?.length) {
throw "This organization is not configured for SSH access via the P0 CLI";
}
};

// TODO: Move this to a shared utility
/** Waits until P0 grants access for a request */
const waitForProvisioning = async <P extends PluginRequest>(
Expand Down Expand Up @@ -83,6 +98,7 @@ const waitForProvisioning = async <P extends PluginRequest>(
const ssh = async (args: yargs.ArgumentsCamelCase<{ instance: string }>) => {
// Prefix is required because the backend uses it to determine that this is an AWS request
const authn = await authenticate();
await validateSshInstall(authn);
const response = await request(
{
...pick(args, "$0", "_"),
Expand Down
12 changes: 12 additions & 0 deletions src/plugins/ssh/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type SshItemConfig = {
alias?: string;
identifier: string;
state: string;
type: "aws" | "gcloud";
};

export type SshConfig = {
workflows?: {
items: SshItemConfig[];
};
};

0 comments on commit 7f5e7f3

Please sign in to comment.