serverless-ssm-publish
Version:
Serverless Framework plugin to publish data to AWS SSM Parameter Store
123 lines • 5.81 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareParams = exports.validateParams = exports.evaluateEnabled = void 0;
var chalk_1 = __importDefault(require("chalk"));
var js_yaml_1 = __importDefault(require("js-yaml"));
/**
* Determines whether this plugin should be enabled.
*
* This method reads the ssmPublish property "enabled" to see if this plugin should be enabled.
* If the property's value is undefined, a default value of true is assumed (for backwards compatibility).
* If the property's value is provided, this should be boolean, otherwise an exception is thrown.
*/
var evaluateEnabled = function (serviceCustomBlock, throwFunction) {
if (!serviceCustomBlock || !serviceCustomBlock.ssmPublish) {
throwFunction('Plugin configuration is missing.');
}
var enabled = serviceCustomBlock.ssmPublish.enabled;
// Enabled by default
if (enabled === undefined) {
return true;
}
switch (typeof enabled) {
case 'boolean':
return enabled;
case 'string':
if (enabled === 'true')
return true;
if (enabled === 'false') {
return false;
}
throwFunction("Ambiguous value for \"enabled\": '".concat(enabled, "'"));
return false;
default:
throwFunction("Ambiguous value for \"enabled\": '".concat(enabled, "'"));
return false;
}
};
exports.evaluateEnabled = evaluateEnabled;
/**
* Validates params passed in serverless.yaml
* Throws error if no params or incorrect syntax.
*/
var isParam = function (param) { return !!param; };
var validateParams = function (params, throwFunction, logFunction) {
if (!params || !params.length) {
throwFunction('No params defined');
throw new Error('only here for typescript'); // typescript doesn't recognise that throwFunction above throws
}
var validateParam = function (param) {
var maxNameLength = 1011; // needs to account for ARN stuff being added
var maxDescriptionLength = 1024;
var maxDepth = 15;
var paramKeys = Object.keys(param);
if (!paramKeys.includes('path') ||
['value', 'source'].every(function (requiredKey) { return paramKeys.includes(requiredKey); }) ||
!['value', 'source'].some(function (requiredKey) { return paramKeys.includes(requiredKey); }))
throwFunction('path and either value or source are required fields for params');
if (param.secure && typeof param.secure !== 'boolean') { // tslint:disable-line:strict-type-predicates
logFunction(chalk_1.default.redBright("Param at path ".concat(param.path, " should pass secure as boolean value")));
}
if (param.path.match(/^(aws|ssm)/gi) || // check if name begins with illegal patterns (aws/ssm)
param.path.match(/[^a-zA-Z0-9_.\-\/]/g) || // check if name contains illegal characters
(param.path.match(/\//g) || []).length > maxDepth ||
param.path.length > maxNameLength)
throwFunction("Param ".concat(param.path, " name doesn't match AWS constraints"));
if (param.description && param.description.length > maxDescriptionLength)
throwFunction("Param ".concat(param.path, " description is too long"));
if (param.secure && typeof param.secure !== 'boolean') { // tslint:disable-line:strict-type-predicates
logFunction(chalk_1.default.redBright("Param at path ".concat(param.path, " should pass \"secure\" as boolean value")));
}
if (paramKeys.includes('enabled') && param.enabled === false)
return undefined;
return __assign(__assign({}, param), { secure: param.secure !== false });
};
return params.map(validateParam).filter(isParam);
};
exports.validateParams = validateParams;
/**
* Helper function to compare values in sls.yaml and remote SSM
*/
var compareParams = function (localParams, remoteParams) { return localParams.reduce(function (acc, curr) {
var existingParam = remoteParams === null || remoteParams === void 0 ? void 0 : remoteParams.find(function (param) { return param.Name === curr.path; });
if (!existingParam) {
acc.nonExistingParams.push(curr);
return acc;
}
// Compare unchanged
if (typeof curr.value === 'string') {
// Simple string comparison
if (existingParam.Value === curr.value) {
acc.existingUnchangedParams.push(curr);
return acc;
}
}
else {
// Can't rely on serialized YAML string to be the same as the original definition.
// We therefore parse it & do quick-n-dirty deep object comparison via JSON.stringify
var parsedExisting = js_yaml_1.default.load(existingParam.Value ? existingParam.Value : '');
if (JSON.stringify(parsedExisting) === JSON.stringify(curr.value)) {
acc.existingUnchangedParams.push(curr);
return acc;
}
}
// Compare changed
acc.existingChangedParams.push(curr);
return acc;
}, { nonExistingParams: [], existingChangedParams: [], existingUnchangedParams: [] }); };
exports.compareParams = compareParams;
//# sourceMappingURL=util.js.map