-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-pack hook event registration validation (#81)
* add pre-pack hook event registration validation * add unit tests * fix tests and refactor code * bug fix in successful validation * safely get param values from appConfig * add validation for runtime actions associated with event registrations * add unit tests * increase test coverage * fix bug: handle config from ext packages * remove unnecessary logs * use eslint-plugin-n node14 compatible version * revert dependency change * update dependencies * fix eslint-plugin-n version and use cliEnv to fetch events url * iterate over all extensions and applications for events and runtimeManifest * add more tests * update aio-lib-events version * add missing null check * use aioLogger instead of console.log * address review comments * address review commit --------- Co-authored-by: Jesse MacFadyen <purplecabbage@gmail.com>
- Loading branch information
1 parent
dc811bf
commit 9330a19
Showing
8 changed files
with
613 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
Copyright 2023 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
const { CLI } = require('@adobe/aio-lib-ims/src/context') | ||
const { getToken } = require('@adobe/aio-lib-ims') | ||
const { createFetch } = require('@adobe/aio-lib-core-networking') | ||
const { DEFAULT_ENV, getCliEnv } = require('@adobe/aio-lib-env') | ||
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-events:hooks:pre-pack-event-reg', { level: 'info' }) | ||
|
||
const EVENTS_BASE_URL = { | ||
prod: 'https://api.adobe.io/events', | ||
stage: 'https://api-stage.adobe.io/events' | ||
} | ||
/** | ||
* @returns {Promise<object>} returns headers required to make a call to the IO Events ISV validate endpoint | ||
*/ | ||
async function getRequestHeaders () { | ||
const X_API_KEY = process.env.SERVICE_API_KEY | ||
if (!X_API_KEY) { | ||
throw new Error('Required SERVICE_API_KEY is missing from .env file') | ||
} | ||
const accessToken = await getToken(CLI) | ||
const headers = {} | ||
headers.Authorization = 'Bearer ' + accessToken | ||
headers['Content-Type'] = 'application/json' | ||
headers['x-api-key'] = X_API_KEY | ||
return headers | ||
} | ||
|
||
/** | ||
* Handle error response | ||
* @param {object} response The error response object obtained from making a call to the IO Events ISV validate API | ||
* @returns {Promise<object>} returns response body of the IO Events ISV validate API call | ||
*/ | ||
async function handleErrorResponse (response) { | ||
if (!response.ok) { | ||
return response.json() | ||
.then((responseBody) => { | ||
throw new Error(JSON.stringify(responseBody)) | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Handle request to IO Events ISV Regitration Validate API | ||
* @param {string} validationUrl IO Events base url based on the cli environment | ||
* @param {object} registrations The registrations from the App Builder ISV config file | ||
* @param {object} project The project details of the ISV app | ||
* @returns {Promise<object>} returns response object of the IO Events ISV validate API call | ||
*/ | ||
async function handleRequest (validationUrl, registrations, project) { | ||
const headers = await getRequestHeaders() | ||
const url = `${validationUrl}/${project.org.id}/${project.id}/${project.workspace.id}/isv/registrations/validate` | ||
const fetch = createFetch() | ||
return fetch(url, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(registrations) | ||
}).then((response) => handleErrorResponse(response)) | ||
.then(() => aioLogger.info('Event registrations successfully validated')) | ||
.catch((error) => { | ||
throw new Error(`Error validating event registrations ${error}`) | ||
}) | ||
} | ||
|
||
/** | ||
* Extracts list of event registrations to validate by IO events and list of runtime actions to validate against runtimeManifest | ||
* @param {object} events Event registrations from the app.config.yaml file | ||
* @returns {object} Object containing list of event registrations and list of runtime actions associated with the event registrations | ||
*/ | ||
function extractRegistrationDetails (events) { | ||
const registrationsList = [] | ||
const runtimeActionList = [] | ||
const registrationsFromConfig = events.registrations | ||
for (const registrationName in registrationsFromConfig) { | ||
const registration = { | ||
name: registrationName, | ||
...registrationsFromConfig[registrationName] | ||
} | ||
registrationsList.push(registration) | ||
if (!registrationsFromConfig[registrationName].runtime_action) { | ||
throw new Error( | ||
'Invalid event registration. All Event registrations need to be associated with a runtime action') | ||
} | ||
runtimeActionList.push(registrationsFromConfig[registrationName].runtime_action) | ||
} | ||
return { registrationsList, runtimeActionList } | ||
} | ||
|
||
/** | ||
* Extract Map of packages and actions associated with each package | ||
* @param {object} manifest runtime manifest containing packages and runtime actions | ||
* @returns {object} a map containing a mapping between the package name and list of actions in the package | ||
*/ | ||
function extractRuntimeManifestDetails (manifest) { | ||
const manifestPackageToRuntimeActionsMap = {} | ||
const runtimePackages = manifest?.full?.packages || {} | ||
for (const packageName in runtimePackages) { | ||
if (runtimePackages[packageName].actions) { | ||
manifestPackageToRuntimeActionsMap[packageName] = runtimePackages[packageName].actions | ||
} | ||
} | ||
return manifestPackageToRuntimeActionsMap | ||
} | ||
|
||
/** | ||
* Validates the runtime packages and functions associated with event registrations. | ||
* The following validations are performed: | ||
* a. runtime manifest contains the package name and action used in events registration | ||
* b. the actions associated with event registrations are non-web actions | ||
* @param {object} manifestPackageToRuntimeActionsMap a map containing a mapping between the package name and list of actions in the package | ||
* @param {string[]} registrationRuntimeActions list of actions associated with event registrations | ||
*/ | ||
function validateRuntimeActionsInEventRegistrations (manifestPackageToRuntimeActionsMap, registrationRuntimeActions) { | ||
for (const registrationRuntimeAction of registrationRuntimeActions) { | ||
const packageNameToRuntimeAction = registrationRuntimeAction.split('/') | ||
if (packageNameToRuntimeAction.length !== 2) { | ||
throw new Error(`Runtime action ${registrationRuntimeAction} is not correctly defined as part of a package`) | ||
} | ||
if (!manifestPackageToRuntimeActionsMap[packageNameToRuntimeAction[0]]) { | ||
throw new Error(`Runtime manifest does not contain package: | ||
${packageNameToRuntimeAction[0]} associated with ${registrationRuntimeAction} | ||
defined in event registrations`) | ||
} | ||
const packageActions = manifestPackageToRuntimeActionsMap[packageNameToRuntimeAction[0]] | ||
if (!packageActions[packageNameToRuntimeAction[1]]) { | ||
throw new Error(`Runtime action ${registrationRuntimeAction} associated with the event registration | ||
does not exist in the runtime manifest`) | ||
} | ||
const actionDetails = packageActions[packageNameToRuntimeAction[1]] | ||
if (actionDetails.web !== 'no') { | ||
throw new Error(`Invalid runtime action ${registrationRuntimeAction}. | ||
Only non-web action can be registered for events`) | ||
} | ||
} | ||
aioLogger.info('Validated runtime actions associated with event registrations successfully') | ||
} | ||
|
||
module.exports = async function ({ appConfig }) { | ||
const applicationDetails = appConfig?.all || {} | ||
if (!applicationDetails || Object.entries(applicationDetails).length === 0) { | ||
aioLogger.debug('No event registrations to verify, skipping pre-pack events validation hook') | ||
return | ||
} | ||
if (!appConfig?.aio?.project) { | ||
throw new Error('No project found, error in pre-pack events validation hook') | ||
} | ||
let registrationsToVerify = [] | ||
let registrationRuntimeActions = [] | ||
const manifestPackageToRuntimeActionsMap = {} | ||
Object.entries(applicationDetails).forEach(([extName, extConfig]) => { | ||
if (extConfig.events) { | ||
const { registrationsList, runtimeActionList } = extractRegistrationDetails(extConfig.events) | ||
registrationsToVerify = [...registrationsToVerify, ...registrationsList] | ||
registrationRuntimeActions = [...registrationRuntimeActions, ...runtimeActionList] | ||
} | ||
if (extConfig.manifest) { | ||
const packageToRuntimeActions = extractRuntimeManifestDetails(extConfig.manifest) | ||
for (const packageToAction in packageToRuntimeActions) { | ||
manifestPackageToRuntimeActionsMap[packageToAction] = { | ||
...manifestPackageToRuntimeActionsMap[packageToAction], | ||
...packageToRuntimeActions[packageToAction] | ||
} | ||
} | ||
} | ||
}) | ||
if (registrationsToVerify?.length === 0) { | ||
aioLogger.debug('No event registrations to verify, skipping pre-pack events validation hook') | ||
return | ||
} | ||
const env = getCliEnv() || DEFAULT_ENV | ||
const validationUrl = EVENTS_BASE_URL[env] | ||
validateRuntimeActionsInEventRegistrations(manifestPackageToRuntimeActionsMap, registrationRuntimeActions) | ||
await handleRequest(validationUrl, registrationsToVerify, appConfig.aio.project) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.