|
| 1 | + |
| 2 | +import {LambdaClient, InvokeCommand} from "@aws-sdk/client-lambda" |
| 3 | +import {getCFConfigValue, getCloudFormationExports} from "../config" |
| 4 | + |
| 5 | +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, |
| 18 | + dryRun: boolean |
| 19 | +): Promise<void> { |
| 20 | + const lambda = new LambdaClient({}) |
| 21 | + 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 { |
| 33 | + console.log(`Would invoke lambda ${functionName}`) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + let instance = apiName |
| 38 | + const spec = JSON.parse(specification) |
| 39 | + if (isPullRequest) { |
| 40 | + const pr_id = stackName.split("-").pop() |
| 41 | + instance = `${apiName}-pr-${pr_id}` |
| 42 | + spec.info.title = `[PR-${pr_id}] ${spec.info.title}` |
| 43 | + spec["x-nhsd-apim"].monitoring = false |
| 44 | + delete spec["x-nhsd-apim"].target.security.secret |
| 45 | + } else { |
| 46 | + spec["x-nhsd-apim"].target.security.secret = mtlsSecretName |
| 47 | + } |
| 48 | + spec.info.version = version |
| 49 | + spec["x-nhsd-apim"].target.url = `https://${stackName}.${awsEnvironment}.eps.national.nhs.uk` |
| 50 | + if (apigeeEnvironment === "prod") { |
| 51 | + spec.servers = [ {url: `https://api.service.nhs.uk/${instance}`} ] |
| 52 | + spec.components.securitySchemes["nhs-cis2-aal3"] = { |
| 53 | + "$ref": "https://proxygen.prod.api.platform.nhs.uk/components/securitySchemes/nhs-cis2-aal3" |
| 54 | + } |
| 55 | + } else { |
| 56 | + spec.servers = [ {url: `https://${apigeeEnvironment}.api.service.nhs.uk/${instance}`} ] |
| 57 | + spec.components.securitySchemes["nhs-cis2-aal3"] = { |
| 58 | + "$ref": "https://proxygen.ptl.api.platform.nhs.uk/components/securitySchemes/nhs-cis2-aal3" |
| 59 | + } |
| 60 | + } |
| 61 | + if (apigeeEnvironment.includes("sandbox")) { |
| 62 | + delete spec["x-nhsd-apim"]["target-attributes"] // Resolve issue with sandbox trying to look up app name |
| 63 | + } |
| 64 | + |
| 65 | + const exports = await getCloudFormationExports() |
| 66 | + const clientCertArn = getCFConfigValue(exports, `account-resources:${clientCertExportName}`) |
| 67 | + const clientPrivateKeyArn = getCFConfigValue(exports, `account-resources:${clientPrivateKeyExportName}`) |
| 68 | + const proxygenPrivateKeyArn = getCFConfigValue(exports, `account-resources:${proxygenPrivateKeyExportName}`) |
| 69 | + |
| 70 | + let put_secret_lambda = "lambda-resources-ProxygenPTLMTLSSecretPut" |
| 71 | + let instance_put_lambda = "lambda-resources-ProxygenPTLInstancePut" |
| 72 | + let spec_publish_lambda = "lambda-resources-ProxygenPTLSpecPublish" |
| 73 | + if (/^(int|sandbox|prod)$/.test(apigeeEnvironment)) { |
| 74 | + put_secret_lambda = "lambda-resources-ProxygenProdMTLSSecretPut" |
| 75 | + instance_put_lambda = "lambda-resources-ProxygenProdInstancePut" |
| 76 | + spec_publish_lambda = "lambda-resources-ProxygenProdSpecPublish" |
| 77 | + } |
| 78 | + |
| 79 | + // --- Store the secret used for mutual TLS --- |
| 80 | + if (!isPullRequest) { |
| 81 | + console.log("Store the secret used for mutual TLS to AWS using Proxygen proxy lambda") |
| 82 | + await invokeLambda(put_secret_lambda, { |
| 83 | + apiName, |
| 84 | + environment: apigeeEnvironment, |
| 85 | + secretName: mtlsSecretName, |
| 86 | + secretKeyName: clientPrivateKeyArn, |
| 87 | + secretCertName: clientCertArn, |
| 88 | + kid: proxygenKid, |
| 89 | + proxygenSecretName: proxygenPrivateKeyArn |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + // --- Deploy the API instance --- |
| 94 | + console.log("Deploy the API instance using Proxygen proxy lambda") |
| 95 | + await invokeLambda(instance_put_lambda, { |
| 96 | + apiName, |
| 97 | + environment: apigeeEnvironment, |
| 98 | + specDefinition: spec, |
| 99 | + instance, |
| 100 | + kid: proxygenKid, |
| 101 | + proxygenSecretName: proxygenPrivateKeyArn |
| 102 | + }) |
| 103 | + |
| 104 | + // --- Publish the API spec to the catalogue --- |
| 105 | + let spec_publish_env |
| 106 | + if (apigeeEnvironment === "int") { |
| 107 | + console.log("Deploy the API spec to prod catalogue as it is int environment") |
| 108 | + spec.servers = [ {url: `https://sandbox.api.service.nhs.uk/${instance}`} ] |
| 109 | + spec_publish_env = "prod" |
| 110 | + } else if (apigeeEnvironment === "internal-dev" && !isPullRequest) { |
| 111 | + console.log("Deploy the API spec to uat catalogue as it is internal-dev environment") |
| 112 | + spec.servers = [ {url: `https://internal-dev-sandbox.api.service.nhs.uk/${instance}`} ] |
| 113 | + spec_publish_env = "uat" |
| 114 | + } |
| 115 | + if (spec_publish_env) { |
| 116 | + await invokeLambda(spec_publish_lambda, { |
| 117 | + apiName, |
| 118 | + environment: spec_publish_env, |
| 119 | + specDefinition: spec, |
| 120 | + instance, |
| 121 | + kid: proxygenKid, |
| 122 | + proxygenSecretName: proxygenPrivateKeyArn |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments