diff --git a/USER_GUIDE.md b/USER_GUIDE.md index aead95e..9557ce0 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -28,6 +28,7 @@ Option | Default Value | Valid Options | Environment Variable | Description -n, --filename | opensearch-report-timestamp | - | OPENSEARCH_FILENAME | file name of the report -a, --auth | none | basic, saml, cognito | - | authentication type for the report -t, --tenant | private | - | - | tenants in opensearch dashboards +--multitenancy | true | true, false | - | enable or disable multi-tenancy -c, --credentials | - | - | OPENSEARCH_USERNAME and OPENSEARCH_PASSWORD | login credentials in the format of username:password for connecting to url -s, --from | - | - | OPENSEARCH_FROM | email address of the sender -r, --to | - | - | OPENSEARCH_TO | email address of the recipient @@ -39,6 +40,7 @@ Option | Default Value | Valid Options | Environment Variable | Description --smtpsecure | - | - | OPENSEARCH_SMTP_SECURE | if true the connection will use TLS when connecting to server. --subject | This is an email containing your dashboard report | - | OPENSEARCH_SUBJECT | subject for the email --note | Hi,\nHere is the latest report! | string or path to text file | OPENSEARCH_EMAIL_NOTE | The email body +--selfsignedcerts | false | true, false | - | enable or disable self-signed certicates for smtp transport | - | - | - | CHROMIUM_PATH | path to chromium directory You can also find this information using help command. diff --git a/src/arguments.js b/src/arguments.js index 5630e5e..49ea7c0 100644 --- a/src/arguments.js +++ b/src/arguments.js @@ -8,7 +8,7 @@ const { program, Option } = require('commander'); const { exit } = require('process'); const fs = require('fs'); const ora = require('ora'); -const { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT, DEFAULT_EMAIL_NOTE, DEFAULT_MULTI_TENANCY } = require('./constants.js'); +const { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT, DEFAULT_EMAIL_NOTE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES } = require('./constants.js'); const dotenv = require("dotenv"); dotenv.config(); const spinner = ora(''); @@ -30,8 +30,8 @@ async function getCommandArguments() { .addOption(new Option('-t, --tenant ', 'tenants in opensearch dashboards') .default(DEFAULT_TENANT)) .addOption(new Option('--multitenancy ', 'enable or disable multi-tenancy') - .default(DEFAULT_MULTI_TENANCY) - .choices(['true', 'false'])) + .default(DEFAULT_MULTI_TENANCY) + .choices(['true', 'false'])) .addOption(new Option('-f, --format ', 'file format for the report') .default(DEFAULT_FORMAT) .choices([FORMAT.PDF, FORMAT.PNG, FORMAT.CSV])) @@ -65,6 +65,9 @@ async function getCommandArguments() { .addOption(new Option('--note ', 'email body (string or path to text file)') .default(DEFAULT_EMAIL_NOTE) .env(ENV_VAR.EMAIL_NOTE)) + .addOption(new Option('--selfsignedcerts ', 'enable or disable self-signed certicates for smtp transport') + .default(DEFAULT_SELF_SIGNED_CERTIFICATES) + .choices(['true', 'false'])); program.addHelpText('after', ` Note: The tenant in the url has the higher priority than tenant value provided as command option.`); @@ -94,6 +97,8 @@ async function getEventArguments(event) { event['note'] = DEFAULT_EMAIL_NOTE; if (event.multitenancy === undefined) event['multitenancy'] = DEFAULT_MULTI_TENANCY; + if (event.selfsignedcerts === undefined) + event['selfsignedcerts'] = DEFAULT_SELF_SIGNED_CERTIFICATES; return getOptions(event); } @@ -121,7 +126,8 @@ function getOptions(options) { subject: null, time: null, note: null, - emailbody: null + emailbody: null, + selfsignedcerts: null } // Set url. @@ -213,6 +219,9 @@ function getOptions(options) { } commandOptions.note = getHtml(commandOptions.note); + // Set self signed certificate flag. + commandOptions.selfsignedcerts = options.selfsignedcerts; + spinner.succeed('Fetched argument values'); return commandOptions; } diff --git a/src/constants.js b/src/constants.js index 37a44b9..c427678 100644 --- a/src/constants.js +++ b/src/constants.js @@ -13,6 +13,7 @@ const DEFAULT_FILENAME = 'opensearch-report'; const DEFAULT_EMAIL_SUBJECT = 'This is an email containing your opensearch dashboard report'; const DEFAULT_EMAIL_NOTE = 'Hi,\nHere is the latest report!'; const DEFAULT_MULTI_TENANCY = true; +const DEFAULT_SELF_SIGNED_CERTIFICATES = false; const REPORT_TYPE = { DASHBOARD: 'Dashboard', @@ -73,5 +74,5 @@ const TRANSPORT_TYPE = { module.exports = { CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_TENANT, DEFAULT_FORMAT, DEFAULT_WIDTH, DEFAULT_MIN_HEIGHT, DEFAULT_FILENAME, DEFAULT_EMAIL_SUBJECT, - DEFAULT_EMAIL_NOTE, REPORT_TYPE, SELECTOR, FORMAT, AUTH, URL_SOURCE, ENV_VAR, TRANSPORT_TYPE, DEFAULT_MULTI_TENANCY + DEFAULT_EMAIL_NOTE, REPORT_TYPE, SELECTOR, FORMAT, AUTH, URL_SOURCE, ENV_VAR, TRANSPORT_TYPE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES }; \ No newline at end of file diff --git a/src/email-helpers.js b/src/email-helpers.js index b3e631e..05b80cf 100644 --- a/src/email-helpers.js +++ b/src/email-helpers.js @@ -20,7 +20,7 @@ try { // Do not set AWS_SDK_LOAD_CONFIG if aws config file is missing. } -module.exports = async function sendEmail(filename, url, sender, recipient, transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, subject, note, emailbody) { +module.exports = async function sendEmail(filename, url, sender, recipient, transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, subject, note, emailbody, selfsignedcerts) { if (transport !== undefined && (transport === 'smtp' || ses !== undefined) && sender !== undefined && recipient !== undefined) { spinner.start('Sending email...'); } else { @@ -41,7 +41,7 @@ module.exports = async function sendEmail(filename, url, sender, recipient, tran let mailOptions = getmailOptions(url, sender, recipient, filename, subject, note, emailbody); - let transporter = getTransporter(transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword); + let transporter = getTransporter(transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, selfsignedcerts); transporter.use("compile", hbs({ viewEngine: { @@ -54,21 +54,22 @@ module.exports = async function sendEmail(filename, url, sender, recipient, tran // send email return new Promise((success, fail) => { - transporter.sendMail(mailOptions, function (err, info) { - if (err) { - spinner.fail('Error sending email' + err); - fail(err); - exit(1); - } else { - spinner.succeed('Email sent successfully'); - deleteTemporaryImage(emailbody); - success(info); - } - }); + transporter.sendMail(mailOptions, function (err, info) { + if (err) { + spinner.fail('Error sending email' + err); + fail(err); + exit(1); + } else { + spinner.succeed('Email sent successfully'); + deleteTemporaryImage(emailbody); + success(info); + } }); + }); } -const getTransporter = (transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, transporter) => { +const getTransporter = (transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, selfsignedcerts, transporter) => { + var rejectSelfSignedCerts = selfsignedcerts === 'true' ? false : true; if (transport === 'ses') { transporter = nodemailer.createTransport({ SES: ses @@ -81,7 +82,10 @@ const getTransporter = (transport, smtphost, smtpport, smtpsecure, smtpusername, auth: { user: smtpusername, pass: smtppassword, - } + }, + tls: { + rejectUnauthorized: rejectSelfSignedCerts, + }, }); } return transporter; diff --git a/src/run.js b/src/run.js index e6a9c65..e6dc78c 100755 --- a/src/run.js +++ b/src/run.js @@ -43,6 +43,7 @@ module.exports = async function run(args) { options.smtppassword, options.subject, options.note, - options.emailbody + options.emailbody, + options.selfsignedcerts ); } diff --git a/test/help.test.js b/test/help.test.js index a7002e2..419cae7 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -33,6 +33,7 @@ Options: --smtppassword smtp password (env: OPENSEARCH_SMTP_PASSWORD) --subject email subject (default: "This is an email containing your opensearch dashboard report", env: OPENSEARCH_EMAIL_SUBJECT) --note email body (string or path to text file) (default: "Hi,\\nHere is the latest report!", env: OPENSEARCH_EMAIL_NOTE) + --selfsignedcerts enable or disable self-signed certicates for smtp transport (choices: "true", "false", default: false) -h, --help display help for command Note: The tenant in the url has the higher priority than tenant value provided as command option.