mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-15 08:31:59 +00:00
30a799030c
* initial changes for env var support
* WIP
* Update applications.js
* JSON stringify before encrypting
* use call_hook.url
* env vars working
GET /v1/AppEnv?url=[URL] to trigger options request to URL and return app-schema
POST /v1/Applications with {env_vars: [OBJECT} to create app with env vars
PUT /v1/Applications/[SID] with {env_vars: [OBJECT} to change env vars
GET returns env vars
POST and PUT will also trigger an OPTIONS request to the call_hook url to get schema and then validate the env_vars against it
* update appenv cannot finish request.
* wip
* wip
* wip
* wip
---------
Co-authored-by: Dave Horton <daveh@beachdognet.com>
Co-authored-by: Quan HL <quan.luuhoang8@gmail.com>
Co-authored-by: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com>
71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
const Ajv = require('ajv');
|
|
const assert = require('assert');
|
|
|
|
const ajv = new Ajv();
|
|
const schemaSchema = require('./appenv_schemaSchema.json');
|
|
|
|
|
|
const validateAppEnvSchema = (schema) => {
|
|
const validate = ajv.compile(schemaSchema);
|
|
return validate(schema);
|
|
};
|
|
|
|
//Currently this request is not signed with the webhook secret as it is outside an account
|
|
const fetchAppEnvSchema = async(logger, url) => {
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: 'OPTIONS',
|
|
headers: {
|
|
Accept: 'application/json'
|
|
}
|
|
});
|
|
if (!response.ok) {
|
|
logger.info(`Failure to fetch app env schema ${response.status} ${response.statusText}`);
|
|
return false;
|
|
}
|
|
const schema = await response.json();
|
|
return schema;
|
|
}
|
|
catch (e) {
|
|
logger.info(`Failure to fetch app env schema ${e}`);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const validateAppEnvData = async(schema, data) => {
|
|
const schemaKeys = Object.keys(schema);
|
|
const dataKeys = Object.keys(data);
|
|
let errorMsg = false;
|
|
// Check for required keys
|
|
schemaKeys.forEach((k) => {
|
|
if (schema[k].required) {
|
|
if (!dataKeys.includes(k)) {
|
|
errorMsg = `Missing required value env_vars.${k}`;
|
|
console.log(errorMsg);
|
|
}
|
|
}
|
|
});
|
|
//Validate the values
|
|
dataKeys.forEach((k) => {
|
|
if (schemaKeys.includes(k)) {
|
|
try {
|
|
// Check value is correct type
|
|
assert(typeof data[k] == schema[k].type);
|
|
// if enum check value is valid
|
|
if (schema[k].enum) {
|
|
assert(schema[k].enum.includes(data[k]));
|
|
}
|
|
} catch (error) {
|
|
errorMsg = `Invalid value/type for env_vars.${k}`;
|
|
}
|
|
}
|
|
});
|
|
return errorMsg;
|
|
};
|
|
|
|
module.exports = {
|
|
validateAppEnvSchema,
|
|
fetchAppEnvSchema,
|
|
validateAppEnvData
|
|
};
|