From c95191c33c4c52705af082f1b97d2be008a3af17 Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Wed, 25 Sep 2024 14:22:19 +0200 Subject: [PATCH] feat: deploy proxy function automatically --- package-lock.json | 29 ++++++++++++- package.json | 1 + ...-function-proxy.js => aws-deploy-proxy.js} | 18 +++----- test/aws.deployer.test.js | 43 +++++++++++++++++++ 4 files changed, 78 insertions(+), 13 deletions(-) rename src/template/{aws-function-proxy.js => aws-deploy-proxy.js} (76%) diff --git a/package-lock.json b/package-lock.json index a0e5f1ff..f00f36e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@adobe/helix-deploy", - "version": "12.1.5", + "version": "12.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@adobe/helix-deploy", - "version": "12.1.5", + "version": "12.2.0", "license": "Apache-2.0", "dependencies": { "@adobe/fetch": "4.1.9", @@ -48,6 +48,7 @@ "mocha-multi-reporters": "1.5.1", "nock": "13.5.5", "semantic-release": "24.1.1", + "xml2js": "0.6.2", "yauzl": "3.1.3" }, "engines": { @@ -13656,6 +13657,30 @@ "dev": true, "license": "MIT" }, + "node_modules/xml2js": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz", + "integrity": "sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==", + "dev": true, + "license": "MIT", + "dependencies": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4.0" + } + }, "node_modules/xtend": { "version": "4.0.2", "dev": true, diff --git a/package.json b/package.json index 600552e0..9319ec94 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "mocha-multi-reporters": "1.5.1", "nock": "13.5.5", "semantic-release": "24.1.1", + "xml2js": "0.6.2", "yauzl": "3.1.3" }, "engines": { diff --git a/src/template/aws-function-proxy.js b/src/template/aws-deploy-proxy.js similarity index 76% rename from src/template/aws-function-proxy.js rename to src/template/aws-deploy-proxy.js index 21a8342e..1fe56462 100644 --- a/src/template/aws-function-proxy.js +++ b/src/template/aws-deploy-proxy.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ /* eslint-disable no-console */ -// eslint-disable-next-line import/no-unresolved import { LambdaClient, InvokeCommand, InvocationType } from '@aws-sdk/client-lambda'; /** @@ -21,13 +20,11 @@ import { LambdaClient, InvokeCommand, InvocationType } from '@aws-sdk/client-lam * ANY /helix-services/{action}/{version} * ANY /helix-services/{action}/{version}/{path+} * - * ANY /helix-observation/{action}/{version} - * ANY /helix-observation/{action}/{version}/{path+} - * - * Note that the package cannot be a path parameter, as it would clash with the pages proxy + * ANY /helix3/{action}/{version} + * ANY /helix3/{action}/{version}/{path+} * * @param event - * @returns {Promise<{body, statusCode}|any>} + * @returns {Promise<{body, headers, statusCode}|any>} */ export const handler = async (event) => { const { action, version } = event.pathParameters; @@ -43,14 +40,13 @@ export const handler = async (event) => { Payload: JSON.stringify(event), }), ); - const data = JSON.parse(new TextDecoder('utf8').decode(ret.Payload)); - console.log(`statusCode: ${data.statusCode}`); - return data; + return JSON.parse(new TextDecoder('utf8').decode(ret.Payload)); } catch (err) { console.error(err); return { - statusCode: err.statusCode, - body: err.message, + statusCode: err.$metadata?.httpStatusCode ?? err.statusCode, + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ message: err.message }), }; } }; diff --git a/test/aws.deployer.test.js b/test/aws.deployer.test.js index e1527493..abac8b9d 100644 --- a/test/aws.deployer.test.js +++ b/test/aws.deployer.test.js @@ -13,12 +13,25 @@ /* eslint-env mocha */ import assert from 'assert'; import nock from 'nock'; +import xml2js from 'xml2js'; import BaseConfig from '../src/BaseConfig.js'; import AWSConfig from '../src/deploy/AWSConfig.js'; import AWSDeployer from '../src/deploy/AWSDeployer.js'; import ActionBuilder from '../src/ActionBuilder.js'; describe('AWS Deployer Test', () => { + beforeEach(() => { + process.env.AWS_ACCESS_KEY_ID = 'fake'; + process.env.AWS_SECRET_ACCESS_KEY = 'fake'; + process.env.AWS_REGION = 'us-east-1'; + }); + + afterEach(() => { + delete process.env.AWS_ACCESS_KEY_ID; + delete process.env.AWS_SECRET_ACCESS_KEY; + delete process.env.AWS_REGION; + }); + it('sets the default lambda name', async () => { const cfg = new BaseConfig() .withName('/helix-services/static@4.3.1'); @@ -57,6 +70,16 @@ describe('AWS Deployer Test', () => { }); it('sets the default deploy bucket', async () => { + nock('https://sts.us-east-1.amazonaws.com/') + .post('/') + .reply(() => [200, new xml2js.Builder().buildObject({ + GetCallerIdentityResponse: { + GetCallerIdentityResult: { + Account: '118435662149', + }, + }, + })]); + const cfg = new BaseConfig() .withVersion('1.18.2') // eslint-disable-next-line no-template-curly-in-string @@ -72,6 +95,16 @@ describe('AWS Deployer Test', () => { }); it('sets the default deploy bucket with region', async () => { + nock('https://sts.eu-central-1.amazonaws.com/') + .post('/') + .reply(() => [200, new xml2js.Builder().buildObject({ + GetCallerIdentityResponse: { + GetCallerIdentityResult: { + Account: '118435662149', + }, + }, + })]); + const cfg = new BaseConfig() .withVersion('1.18.2') // eslint-disable-next-line no-template-curly-in-string @@ -86,6 +119,16 @@ describe('AWS Deployer Test', () => { }); it('sets the custom deploy bucket', async () => { + nock('https://sts.eu-central-1.amazonaws.com/') + .post('/') + .reply(() => [200, new xml2js.Builder().buildObject({ + GetCallerIdentityResponse: { + GetCallerIdentityResult: { + Account: '118435662149', + }, + }, + })]); + const cfg = new BaseConfig() .withVersion('1.18.2') // eslint-disable-next-line no-template-curly-in-string