Skip to content

Commit

Permalink
Merge branch 'main' into wf-val
Browse files Browse the repository at this point in the history
  • Loading branch information
mdial89f committed Aug 1, 2024
2 parents 9758746 + 0cf582b commit 55a740b
Show file tree
Hide file tree
Showing 39 changed files with 857 additions and 774 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
17 changes: 2 additions & 15 deletions lib/lambda/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,8 @@ import {
completeIntake,
removeAppkChild,
} from "./package-actions";
import { setupWriteService } from "./package-actions/setup-write-service";

const checkIfActionType = (
potentialActionType: unknown,
): potentialActionType is Action => {
if (potentialActionType) {
return true;
}
return false;
};

export const handler = async (event: APIGatewayEvent) => {
if (typeof globalThis.packageActionWriteService === "undefined") {
global.packageActionWriteService = await setupWriteService();
}
if (!event.pathParameters || !event.pathParameters.actionType) {
return response({
statusCode: 400,
Expand Down Expand Up @@ -94,10 +81,10 @@ export const handler = async (event: APIGatewayEvent) => {
await respondToRai(body, result._source);
break;
case Action.ENABLE_RAI_WITHDRAW:
await toggleRaiResponseWithdraw(body, true);
await toggleRaiResponseWithdraw({ toggle: true, ...body });
break;
case Action.DISABLE_RAI_WITHDRAW:
await toggleRaiResponseWithdraw(body, false);
await toggleRaiResponseWithdraw(body);
break;
case Action.WITHDRAW_RAI:
await withdrawRai(body, result._source);
Expand Down
9 changes: 3 additions & 6 deletions lib/lambda/package-actions/complete-intake/complete-intake.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { completeIntakeSchema, Action } from "shared-types";
import { response } from "../../../libs/handler-lib";
import { TOPIC_NAME } from "../consts";
import { type PackageActionWriteService } from "../services/package-action-write-service";
import { completeIntakeAction } from "../services/package-action-write-service";

export async function completeIntake(
body: any,
packageActionWriteService: PackageActionWriteService = globalThis.packageActionWriteService,
) {
export async function completeIntake(body: any) {
console.log("CMS performing intake for a record.");

const result = completeIntakeSchema.safeParse(body);
Expand All @@ -27,7 +24,7 @@ export async function completeIntake(

const now = new Date().getTime();

await packageActionWriteService.completeIntake({
await completeIntakeAction({
timestamp: now,
action: Action.COMPLETE_INTAKE,
cpoc: result.data.cpoc,
Expand Down
9 changes: 3 additions & 6 deletions lib/lambda/package-actions/issue-rai/issue-rai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { RaiIssue, raiIssueSchema, Action, SEATOOL_STATUS } from "shared-types";
import { seaToolFriendlyTimestamp } from "shared-utils";
import { response } from "../../../libs/handler-lib";
import { TOPIC_NAME } from "../consts";
import { type PackageActionWriteService } from "../services/package-action-write-service";
import { issueRaiAction } from "../services/package-action-write-service";

export async function issueRai(
body: RaiIssue,
packageActionWriteService: PackageActionWriteService = globalThis.packageActionWriteService,
) {
export async function issueRai(body: RaiIssue) {
console.log("CMS issuing a new RAI");
const now = new Date().getTime();
const today = seaToolFriendlyTimestamp();
Expand All @@ -27,7 +24,7 @@ export async function issueRai(
});
}

await packageActionWriteService.issueRai({
await issueRaiAction({
...result.data,
action: Action.ISSUE_RAI,
id: result.data.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { seaToolFriendlyTimestamp } from "shared-utils";
import { response } from "../../../libs/handler-lib";
import { TOPIC_NAME } from "../consts";
import { removeAppkChildAction } from "../services/package-action-write-service";
export async function removeAppkChild(doc: opensearch.main.Document) {
const result = removeAppkChildSchema.safeParse(doc);

Expand All @@ -22,7 +23,7 @@ export async function removeAppkChild(doc: opensearch.main.Document) {
const now = new Date().getTime();
const today = seaToolFriendlyTimestamp();

await packageActionWriteService.removeAppkChild({
await removeAppkChildAction({
...result.data,
action: Action.REMOVE_APPK_CHILD,
id: result.data.id,
Expand Down
60 changes: 27 additions & 33 deletions lib/lambda/package-actions/respond-to-rai/respond-to-rai.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { respondToRai } from "./respond-to-rai";
import { vi, describe, it, expect, beforeEach } from "vitest";
import { MockPackageActionWriteService } from "../services/package-action-write-service";
import { Action, raiResponseSchema } from "shared-types";
import { raiResponseSchema } from "shared-types";
import { generateMock } from "@anatine/zod-mock";
import { ExtendedItemResult } from "../../../libs/api/package";
const mockPackageWrite = new MockPackageActionWriteService();
import * as packageActionWriteService from "../services/package-action-write-service";

vi.mock("../services/package-action-write-service", () => {
return {
respondToRaiAction: vi.fn(),
};
});

describe("respondToRai", async () => {
beforeEach(() => {
Expand All @@ -19,52 +23,42 @@ describe("respondToRai", async () => {
raiReceivedDate: "999",
raiWithdrawnDate: "999",
},
mockPackageWrite,
);
expect(response.statusCode).toBe(400);
});

it("should return a 400 when no requested date is sent", async () => {
const mockData = generateMock(raiResponseSchema);
const response = await respondToRai(
mockData,
{
raiRequestedDate: null,
raiReceivedDate: null,
raiWithdrawnDate: null,
},
mockPackageWrite,
);
const response = await respondToRai(mockData, {
raiRequestedDate: null,
raiReceivedDate: null,
raiWithdrawnDate: null,
});
expect(response.statusCode).toBe(400);
});

it("should return a 400 when a bad requestDate is sent", async () => {
const mockData = generateMock(raiResponseSchema);
const response = await respondToRai(
mockData,
{
raiRequestedDate: "123456789", // should be an isoString
raiReceivedDate: null,
raiWithdrawnDate: null,
},
mockPackageWrite,
);
const response = await respondToRai(mockData, {
raiRequestedDate: "123456789", // should be an isoString
raiReceivedDate: null,
raiWithdrawnDate: null,
});
expect(response.statusCode).toBe(400);
expect(JSON.parse(response.body).message).toBe("Event validation error");
});

it("should return a 200 when a good payload is sent", async () => {
const packageWriteSpy = vi.spyOn(mockPackageWrite, "respondToRai");
const mockData = generateMock(raiResponseSchema);
const response = await respondToRai(
mockData,
{
raiRequestedDate: new Date().toISOString(),
raiReceivedDate: null,
raiWithdrawnDate: null,
},
mockPackageWrite,
const packageWriteSpy = vi.spyOn(
packageActionWriteService,
"respondToRaiAction",
);
const mockData = generateMock(raiResponseSchema);
const response = await respondToRai(mockData, {
raiRequestedDate: new Date().toISOString(),
raiReceivedDate: null,
raiWithdrawnDate: null,
});
expect(packageWriteSpy).toHaveBeenCalledOnce();
expect(response.statusCode).toBe(200);
});
Expand Down
5 changes: 2 additions & 3 deletions lib/lambda/package-actions/respond-to-rai/respond-to-rai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { seaToolFriendlyTimestamp } from "shared-utils";
import { response } from "../../../libs/handler-lib";
import { TOPIC_NAME } from "../consts";
import { ExtendedItemResult } from "../../../libs/api/package";
import { PackageWriteClass } from "../services/package-action-write-service";
import { respondToRaiAction } from "../services/package-action-write-service";

export async function respondToRai(
body: any,
document: Pick<
ExtendedItemResult["_source"],
"raiReceivedDate" | "raiRequestedDate" | "raiWithdrawnDate"
>,
packageActionWriteService: PackageWriteClass = globalThis.packageActionWriteService,
) {
console.log("State responding to RAI");
if (!document.raiRequestedDate) {
Expand Down Expand Up @@ -45,7 +44,7 @@ export async function respondToRai(
});
}
try {
await packageActionWriteService.respondToRai({
await respondToRaiAction({
...result.data,
action: Action.RESPOND_TO_RAI,
id: result.data.id,
Expand Down
Loading

0 comments on commit 55a740b

Please sign in to comment.