in functions/source/lambda-self-signed-certificate/index.js [102:165]
async function createCertificate(resourceProperties, serverNamePrefix, newServerNameIdArray) {
var selfsigned = require('selfsigned');
const ssgenerate = util.promisify(selfsigned.generate);
const _shortNames = {
CN: 'commonName',
C: 'countryName',
L: 'localityName',
ST: 'stateOrProvinceName',
O: 'organizationName',
OU: 'organizationalUnitName',
E: 'emailAddress'
};
try {
// Cast integer properties from passed string type back to integer
var attributes = toCamel(resourceProperties.Attributes)
attributes.keySize = parseInt(attributes.keySize) || 1024
// Parse the # of days the certificate should exists, defaulting to 365 if not provided
attributes.days = parseInt(attributes.days) || 365
// If an explicit 'ExpiresOn' YYYY-MM-DD is provided, validate that. Must be:
// 1. Non-empty string
// 2. In the form YYYY-MM-DD where MM is 01-12, DD is 01-31. Does not check for leap year
// 3. Must be at least 1 day in the future.
//
if (resourceProperties.hasOwnProperty("ExpiresOn")) {
attributes.days = 365
if (resourceProperties["ExpiresOn"] !== "") {
if (resourceProperties["ExpiresOn"].match(/^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/gm) == null) {
throw new Error("Invalid 'ExpiresOn' date format. Expected 'YYYY-MM-DD'");
}
// Calc number of days between new expiration and today (12:00midnight)
var curDate = new Date()
var expDate = new Date(resourceProperties["ExpiresOn"])
attributes.days = Math.round((expDate.setUTCHours(0,0,0)-curDate.setUTCHours(0,0,0)) / (1000*60*60*24))
}
}
if (attributes.days <= 0) {
throw new Error("'ExpiresOn' or 'Attributes.Days' must result in an expiration date at least one day in the future.");
}
// Iterate over provided options list with 'short names' and substitute back 'long names' removing
// leading/trailing whitespace for the name and value
var options = []
for (var o of resourceProperties.Options.split(';')) {
o = o.split('=')
o[0] = o[0].trim()
if (_shortNames[o[0]] != null) {
o[0] = _shortNames[o[0]]
}
options.push({
'name': o[0].toCamelCase(),
'value': o[1].trim()
})
}
return await ssgenerate(options, attributes);
} catch (err) {
throw new Error("createCertificate() error: " + err.message);
};
}