Skip to content

Commit 77e31b8

Browse files
Sonar fixes
1 parent 75b99da commit 77e31b8

5 files changed

Lines changed: 120 additions & 122 deletions

File tree

packages/cdkConstructs/src/specifications/deployApi.ts

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,54 @@
22
import {LambdaClient, InvokeCommand} from "@aws-sdk/client-lambda"
33
import {getCFConfigValue, getCloudFormationExports} from "../config"
44

5+
export type ApiConfig = {
6+
specification: string
7+
apiName: string
8+
version: string
9+
apigeeEnvironment: string
10+
isPullRequest: boolean
11+
awsEnvironment: string
12+
stackName: string
13+
mtlsSecretName: string
14+
clientCertExportName: string
15+
clientPrivateKeyExportName: string
16+
proxygenPrivateKeyExportName: string
17+
proxygenKid: string
18+
}
19+
520
export async function deployApi(
6-
specification: string,
7-
apiName: string,
8-
version: string,
9-
apigeeEnvironment: string,
10-
isPullRequest: boolean,
11-
awsEnvironment: string,
12-
stackName: string,
13-
mtlsSecretName: string,
14-
clientCertExportName: string,
15-
clientPrivateKeyExportName: string,
16-
proxygenPrivateKeyExportName: string,
17-
proxygenKid: string,
21+
{
22+
specification,
23+
apiName,
24+
version,
25+
apigeeEnvironment,
26+
isPullRequest,
27+
awsEnvironment,
28+
stackName,
29+
mtlsSecretName,
30+
clientCertExportName,
31+
clientPrivateKeyExportName,
32+
proxygenPrivateKeyExportName,
33+
proxygenKid
34+
}: ApiConfig,
1835
dryRun: boolean
1936
): Promise<void> {
2037
const lambda = new LambdaClient({})
2138
async function invokeLambda(functionName: string, payload: unknown): Promise<void> {
22-
if (!dryRun) {
23-
const invokeResult = await lambda.send(new InvokeCommand({
24-
FunctionName: functionName,
25-
Payload: Buffer.from(JSON.stringify(payload))
26-
}))
27-
const responsePayload = Buffer.from(invokeResult.Payload!).toString()
28-
if (invokeResult.FunctionError) {
29-
throw new Error(`Error calling lambda ${functionName}: ${responsePayload}`)
30-
}
31-
console.log(`Lambda ${functionName} invoked successfully. Response:`, responsePayload)
32-
} else {
39+
if (dryRun) {
3340
console.log(`Would invoke lambda ${functionName}`)
41+
return
42+
}
43+
44+
const invokeResult = await lambda.send(new InvokeCommand({
45+
FunctionName: functionName,
46+
Payload: Buffer.from(JSON.stringify(payload))
47+
}))
48+
const responsePayload = Buffer.from(invokeResult.Payload!).toString()
49+
if (invokeResult.FunctionError) {
50+
throw new Error(`Error calling lambda ${functionName}: ${responsePayload}`)
3451
}
52+
console.log(`Lambda ${functionName} invoked successfully. Response:`, responsePayload)
3553
}
3654

3755
let instance = apiName

packages/cdkConstructs/src/specifications/writeSchemas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from "fs"
2-
import path from "path"
1+
import fs from "node:fs"
2+
import path from "node:path"
33
import {JSONSchema} from "json-schema-to-ts"
44

55
function isNotJSONSchemaArray(schema: JSONSchema | ReadonlyArray<JSONSchema>): schema is JSONSchema {
@@ -30,7 +30,7 @@ function collapseExamples(schema: JSONSchema): JSONSchema {
3030
if (schema.properties) {
3131
const properties: Record<string, JSONSchema> = {}
3232
for (const key in schema.properties) {
33-
if (Object.prototype.hasOwnProperty.call(schema.properties, key)) {
33+
if (Object.hasOwn(schema.properties, key)) {
3434
properties[key] = collapseExamples(schema.properties[key])
3535
}
3636
}
@@ -48,7 +48,7 @@ export function writeSchemas(
4848
fs.mkdirSync(outputDir, {recursive: true})
4949
}
5050
for (const name in schemas) {
51-
if (Object.prototype.hasOwnProperty.call(schemas, name)) {
51+
if (Object.hasOwn(schemas, name)) {
5252
const schema = schemas[name]
5353
const fileName = `${name}.json`
5454
const filePath = path.join(outputDir, fileName)

packages/cdkConstructs/tests/specifications/deployApi.test.ts

Lines changed: 64 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
vi
77
} from "vitest"
88
import {deployApi} from "../../src/specifications/deployApi"
9+
import type {ApiConfig} from "../../src/specifications/deployApi"
910

1011
const lambdaSendMock = vi.fn()
1112

@@ -20,8 +21,6 @@ vi.mock("@aws-sdk/client-lambda", () => {
2021
}
2122

2223
class LambdaClient {
23-
constructor() {}
24-
2524
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2625
send(command: any) {
2726
return lambdaSendMock(command)
@@ -64,39 +63,49 @@ const defaultExportsMap = {
6463
"account-resources:proxygenKey": "arn:proxygen-key"
6564
}
6665

66+
function buildConfig(overrides: Partial<ApiConfig> = {}): ApiConfig {
67+
return {
68+
specification: JSON.stringify(createSpec()),
69+
apiName: "eps",
70+
version: "1.0.0",
71+
apigeeEnvironment: "internal-dev",
72+
isPullRequest: false,
73+
awsEnvironment: "nonprod",
74+
stackName: "eps-stack-001",
75+
mtlsSecretName: "mtls/secret",
76+
clientCertExportName: "clientCert",
77+
clientPrivateKeyExportName: "clientKey",
78+
proxygenPrivateKeyExportName: "proxygenKey",
79+
proxygenKid: "kid-123",
80+
...overrides
81+
}
82+
}
83+
84+
function payloadFromCall(callIndex: number) {
85+
const command = lambdaSendMock.mock.calls[callIndex][0] as {input: {Payload: Buffer}}
86+
return JSON.parse(command.input.Payload.toString())
87+
}
88+
89+
function functionNameFromCall(callIndex: number) {
90+
const command = lambdaSendMock.mock.calls[callIndex][0] as {input: {FunctionName: string}}
91+
return command.input.FunctionName
92+
}
93+
6794
describe("deployApi", () => {
6895
beforeEach(() => {
6996
lambdaSendMock.mockReset().mockResolvedValue({Payload: Buffer.from('"ok"')})
7097
getCloudFormationExportsMock.mockReset()
7198
})
7299

73-
function payloadFromCall(callIndex: number) {
74-
const command = lambdaSendMock.mock.calls[callIndex][0] as {input: {Payload: Buffer}}
75-
return JSON.parse(command.input.Payload.toString())
76-
}
77-
78-
function functionNameFromCall(callIndex: number) {
79-
const command = lambdaSendMock.mock.calls[callIndex][0] as {input: {FunctionName: string}}
80-
return command.input.FunctionName
81-
}
82-
83100
test("stores secrets, deploys instance and publishes spec for internal-dev", async () => {
84-
const spec = JSON.stringify(createSpec())
85101
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
86102

87103
await deployApi(
88-
spec,
89-
"eps",
90-
"2.0.0",
91-
"internal-dev",
92-
false,
93-
"nonprod",
94-
"eps-stack-001",
95-
"mtls/secret",
96-
"clientCert",
97-
"clientKey",
98-
"proxygenKey",
99-
"kid-123",
104+
buildConfig({
105+
version: "2.0.0",
106+
apigeeEnvironment: "internal-dev",
107+
stackName: "eps-stack-001"
108+
}),
100109
false
101110
)
102111

@@ -135,22 +144,16 @@ describe("deployApi", () => {
135144
})
136145

137146
test("handles pull requests in sandbox without storing secrets", async () => {
138-
const spec = JSON.stringify(createSpec())
139147
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
140148

141149
await deployApi(
142-
spec,
143-
"eps",
144-
"3.1.4",
145-
"sandbox",
146-
true,
147-
"nonprod",
148-
"eps-pr-stack-456",
149-
"mtls/secret",
150-
"clientCert",
151-
"clientKey",
152-
"proxygenKey",
153-
"kid-789",
150+
buildConfig({
151+
version: "3.1.4",
152+
apigeeEnvironment: "sandbox",
153+
isPullRequest: true,
154+
stackName: "eps-pr-stack-456",
155+
proxygenKid: "kid-789"
156+
}),
154157
false
155158
)
156159

@@ -168,22 +171,16 @@ describe("deployApi", () => {
168171
})
169172

170173
test("uses prod lambdas and prod security scheme refs", async () => {
171-
const spec = JSON.stringify(createSpec())
172174
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
173175

174176
await deployApi(
175-
spec,
176-
"eps",
177-
"4.0.0",
178-
"prod",
179-
false,
180-
"prod",
181-
"eps-prod-stack",
182-
"mtls/secret",
183-
"clientCert",
184-
"clientKey",
185-
"proxygenKey",
186-
"kid-prod",
177+
buildConfig({
178+
version: "4.0.0",
179+
apigeeEnvironment: "prod",
180+
awsEnvironment: "prod",
181+
stackName: "eps-prod-stack",
182+
proxygenKid: "kid-prod"
183+
}),
187184
false
188185
)
189186

@@ -199,22 +196,15 @@ describe("deployApi", () => {
199196
})
200197

201198
test("publishes spec to prod catalogue for int environment", async () => {
202-
const spec = JSON.stringify(createSpec())
203199
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
204200

205201
await deployApi(
206-
spec,
207-
"eps",
208-
"5.0.0",
209-
"int",
210-
false,
211-
"nonprod",
212-
"eps-int-stack",
213-
"mtls/secret",
214-
"clientCert",
215-
"clientKey",
216-
"proxygenKey",
217-
"kid-int",
202+
buildConfig({
203+
version: "5.0.0",
204+
apigeeEnvironment: "int",
205+
stackName: "eps-int-stack",
206+
proxygenKid: "kid-int"
207+
}),
218208
false
219209
)
220210

@@ -227,23 +217,15 @@ describe("deployApi", () => {
227217
})
228218

229219
test("dry run only logs intended invocations", async () => {
230-
const spec = JSON.stringify(createSpec())
231220
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
232221
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined)
233222

234223
await deployApi(
235-
spec,
236-
"eps",
237-
"1.0.0",
238-
"int",
239-
false,
240-
"nonprod",
241-
"eps-int-stack",
242-
"mtls/secret",
243-
"clientCert",
244-
"clientKey",
245-
"proxygenKey",
246-
"kid-int",
224+
buildConfig({
225+
apigeeEnvironment: "int",
226+
stackName: "eps-int-stack",
227+
proxygenKid: "kid-int"
228+
}),
247229
true
248230
)
249231

@@ -255,24 +237,16 @@ describe("deployApi", () => {
255237
})
256238

257239
test("throws when lambda invocation returns a FunctionError", async () => {
258-
const spec = JSON.stringify(createSpec())
259240
getCloudFormationExportsMock.mockResolvedValue(defaultExportsMap)
260241
lambdaSendMock
261242
.mockResolvedValueOnce({FunctionError: "Handled", Payload: Buffer.from('"bad"')})
262243

263244
await expect(deployApi(
264-
spec,
265-
"eps",
266-
"1.2.3",
267-
"int",
268-
false,
269-
"nonprod",
270-
"eps-stack",
271-
"mtls/secret",
272-
"clientCert",
273-
"clientKey",
274-
"proxygenKey",
275-
"kid",
245+
buildConfig({
246+
version: "1.2.3",
247+
apigeeEnvironment: "int",
248+
stackName: "eps-stack"
249+
}),
276250
false
277251
)).rejects.toThrow("Error calling lambda lambda-resources-ProxygenProdMTLSSecretPut: \"bad\"")
278252
})

packages/cdkConstructs/tests/specifications/writeSchemas.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from "fs"
2-
import path from "path"
1+
import fs from "node:fs"
2+
import path from "node:path"
33
import {
44
describe,
55
test,

packages/cdkConstructs/tsconfig.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"outDir": "lib",
1313
"strict": true,
1414
"lib": [
15-
"es2020"
15+
"es2022"
1616
],
1717
"noImplicitAny": true,
1818
"strictNullChecks": true,
@@ -32,6 +32,12 @@
3232
"../../node_modules/@types"
3333
]
3434
},
35-
"include": ["src/**/*"],
36-
"exclude": ["node_modules", "cdk.out", "tests/**/*"]
35+
"include": [
36+
"src/**/*"
37+
],
38+
"exclude": [
39+
"node_modules",
40+
"cdk.out",
41+
"tests/**/*"
42+
]
3743
}

0 commit comments

Comments
 (0)