Skip to content

Commit

Permalink
feat(cli auth check): better error handling for cli when aws creds ar…
Browse files Browse the repository at this point in the history
…e required but missing
  • Loading branch information
mdial89f committed Aug 1, 2024
1 parent e6c21a6 commit 0cf582b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 7 deletions.
7 changes: 6 additions & 1 deletion bin/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Argv } from "yargs";
import { LabeledProcessRunner, writeUiEnvFile } from "../lib/";
import {
checkIfAuthenticated,
LabeledProcessRunner,
writeUiEnvFile,
} from "../lib/";
import path from "path";
import { execSync } from "child_process";
import {
Expand All @@ -17,6 +21,7 @@ export const deploy = {
return yargs.option("stage", { type: "string", demandOption: true });
},
handler: async (options: { stage: string; stack?: string }) => {
await checkIfAuthenticated();
await runner.run_command_and_output(
"CDK Deploy",
["cdk", "deploy", "-c", `stage=${options.stage}`, "--all"],
Expand Down
4 changes: 3 additions & 1 deletion bin/cli/src/commands/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DeleteStackCommand,
waitUntilStackDeleteComplete,
} from "@aws-sdk/client-cloudformation";
import { confirmDestroyCommand } from "../lib";
import { checkIfAuthenticated, confirmDestroyCommand } from "../lib";

const waitForStackDeleteComplete = async (
client: CloudFormationClient,
Expand Down Expand Up @@ -37,6 +37,8 @@ export const destroy = {
wait: boolean;
verify: boolean;
}) => {
await checkIfAuthenticated();

const stackName = `${process.env.PROJECT}-${stage}`;

if (/prod/i.test(stage)) {
Expand Down
3 changes: 2 additions & 1 deletion bin/cli/src/commands/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Argv } from "yargs";
import { LabeledProcessRunner } from "../lib";
import { checkIfAuthenticated, LabeledProcessRunner } from "../lib";

const runner = new LabeledProcessRunner();

Expand All @@ -13,6 +13,7 @@ export const e2e = {
default: false,
}),
handler: async ({ ui }: { ui: boolean }) => {
await checkIfAuthenticated();
await runner.run_command_and_output(
"Install playwright",
["bun", "playwright", "install", "--with-deps"],
Expand Down
3 changes: 3 additions & 0 deletions bin/cli/src/commands/get-cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CostExplorerClient,
GetCostAndUsageCommand,
} from "@aws-sdk/client-cost-explorer";
import { checkIfAuthenticated } from "../lib";

export const getCost = {
command: "get-cost",
Expand All @@ -11,6 +12,8 @@ export const getCost = {
return yargs.option("stage", { type: "string", demandOption: true });
},
handler: async (options: { stage: string; stack?: string }) => {
await checkIfAuthenticated();

const tags = {
PROJECT: [process.env.PROJECT!],
STAGE: [options.stage],
Expand Down
4 changes: 3 additions & 1 deletion bin/cli/src/commands/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Argv } from "yargs";
import { LabeledProcessRunner } from "../lib/";
import { checkIfAuthenticated, LabeledProcessRunner } from "../lib/";
import simpleGit from "simple-git";
import {
ResourceGroupsTaggingAPIClient,
Expand Down Expand Up @@ -29,6 +29,8 @@ export const logs = {
demandOption: true,
}),
handler: async (options: { stage?: string; functionName: string }) => {
await checkIfAuthenticated();

let { stage, functionName } = options;
if (!stage) {
const git = simpleGit();
Expand Down
3 changes: 2 additions & 1 deletion bin/cli/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Argv } from "yargs";
import { openUrl } from "../lib";
import { checkIfAuthenticated, openUrl } from "../lib";
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";

const createOpenCommand = (
Expand All @@ -12,6 +12,7 @@ const createOpenCommand = (
builder: (yargs: Argv) =>
yargs.option("stage", { type: "string", demandOption: true }),
handler: async ({ stage }: { stage: string }) => {
await checkIfAuthenticated();
const url = JSON.parse(
(
await new SSMClient({ region: "us-east-1" }).send(
Expand Down
7 changes: 6 additions & 1 deletion bin/cli/src/commands/ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Argv } from "yargs";
import { LabeledProcessRunner, writeUiEnvFile } from "../lib";
import {
checkIfAuthenticated,
LabeledProcessRunner,
writeUiEnvFile,
} from "../lib";

const runner = new LabeledProcessRunner();

Expand All @@ -10,6 +14,7 @@ export const ui = {
return yargs.option("stage", { type: "string", demandOption: true });
},
handler: async (options: { stage: string }) => {
await checkIfAuthenticated();
await writeUiEnvFile(options.stage, true);
await runner.run_command_and_output(
`Build`,
Expand Down
1 change: 1 addition & 0 deletions bin/cli/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./open-url";
export * from "./runner";
export * from "./sts";
export * from "./utils";
export * from "./write-ui-env-file";
27 changes: 27 additions & 0 deletions bin/cli/src/lib/sts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";

export async function checkIfAuthenticated(): Promise<void> {
try {
const client = new STSClient({ region: process.env.REGION_A });
const command = new GetCallerIdentityCommand({});
await client.send(command);
} catch (error) {
if (error instanceof Error) {
if (
error.message.includes("Could not load credentials from any providers")
) {
console.error(
`\x1b[31m\x1b[1mERROR: This command requires AWS credentials available to your terminal. Please configure AWS credentials and try again.\x1b[0m`,
);
} else {
console.error(
"Error occurred while checking authentication:",
error.message,
);
}
} else {
console.error("An unknown error occurred:", error);
}
process.exit(1);
}
}
5 changes: 5 additions & 0 deletions bin/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
} from "./commands";

yargs
.fail((msg, err, _) => {
if (err) throw err;
if (msg) console.error(msg);
process.exit(1);
})
.command(deploy)
.command(destroy)
.command(docs)
Expand Down
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ fi
bun install

# build and run dev.ts
bun build:cli && node ./.build_run/src/run.js "$@"
bun build:cli > /dev/null && node ./.build_run/src/run.js "$@"

0 comments on commit 0cf582b

Please sign in to comment.