From 4dda1465cdf7ac3d63e95fdfc55a3e938952cffe Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Mon, 13 Jul 2020 11:36:58 +0800 Subject: [PATCH 1/6] chore: reformat deploy-support.js --- src/lib/deploy/deploy-support.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/lib/deploy/deploy-support.js b/src/lib/deploy/deploy-support.js index de43149bd..65b497d00 100644 --- a/src/lib/deploy/deploy-support.js +++ b/src/lib/deploy/deploy-support.js @@ -6,9 +6,9 @@ const debug = require('debug')('fun:deploy'); const promiseRetry = require('../retry'); const getProfile = require('../profile').getProfile; -const { green, red } = require('colors'); -const { processApiParameters } = require('./deploy-support-api'); -const { getCloudApiClient, getSlsClient, getMnsClient } = require('../client'); +const {green, red} = require('colors'); +const {processApiParameters} = require('./deploy-support-api'); +const {getCloudApiClient, getSlsClient, getMnsClient} = require('../client'); const { getOtsClient, @@ -75,7 +75,9 @@ async function makeLogstore({ console.log(red(`\t\tretry ${times} times`)); retry(ex); - } else { exists = false; } + } else { + exists = false; + } } }); @@ -153,7 +155,9 @@ async function slsProjectExist(slsClient, projectName) { console.log(red(`\tretry ${times} times`)); retry(ex); - } else { projectExist = false; } + } else { + projectExist = false; + } } }); return projectExist; @@ -373,9 +377,11 @@ async function makeApi(group, { 'requestPath': requestPath }, requestConfig); - const { apiRequestParameters, + const { + apiRequestParameters, apiServiceParameters, - apiServiceParametersMap } = processApiParameters(requestParameters, serviceParameters, serviceParametersMap); + apiServiceParametersMap + } = processApiParameters(requestParameters, serviceParameters, serviceParametersMap); const profile = await getProfile(); @@ -676,4 +682,4 @@ module.exports = { makeApiTrigger, makeSlsProject, makeOtsInstance, makeCustomDomain, makeLogstoreIndex, makeSlsAuto, listCustomDomains -}; \ No newline at end of file +}; From 09b6e3fb994744d2ef5421cb53a0de289de1adc2 Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Mon, 13 Jul 2020 11:42:12 +0800 Subject: [PATCH 2/6] feat: validate rsa private key and certificate content format --- src/lib/deploy/deploy-support.js | 51 ++++++++++++++++++++++++++++---- test/fs-extra.test.js | 17 +++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 test/fs-extra.test.js diff --git a/src/lib/deploy/deploy-support.js b/src/lib/deploy/deploy-support.js index 65b497d00..866051903 100644 --- a/src/lib/deploy/deploy-support.js +++ b/src/lib/deploy/deploy-support.js @@ -1,6 +1,7 @@ 'use strict'; const fs = require('fs-extra'); +const path = require('path'); const ram = require('../ram'); const debug = require('debug')('fun:deploy'); const promiseRetry = require('../retry'); @@ -254,12 +255,50 @@ async function makeCustomDomain({ let privateKey = certConfig.PrivateKey; let certificate = certConfig.Certificate; - if (privateKey && privateKey.endsWith('.pem')) { - certConfig.PrivateKey = await fs.readFile(privateKey, 'utf-8'); - } - if (certificate && certificate.endsWith('.pem')) { - certConfig.Certificate = await fs.readFile(certificate, 'utf-8'); - } + if (privateKey) { + //region resolve RSA private key content + let p = path.resolve(__dirname, privateKey); + // private key is provided by local file + if (fs.pathExistsSync(p)) { + certConfig.PrivateKey = fs.readFileSync(p, 'utf-8'); + } // or it is hardcoded + //endregion + + //region validate RSA private key content + let expectedPrefix = '-----BEGIN RSA PRIVATE KEY-----', expectedSuffix = '-----END RSA PRIVATE KEY-----'; + if (!certConfig.PrivateKey.startsWith(expectedPrefix) || !certConfig.PrivateKey.endsWith(expectedSuffix)) { + throw new Error(red(` + Please provide a valid PEM encoded RSA private key for ${domainName}. + It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}". + + See: + http://fileformats.archiveteam.org/wiki/PEM_encoded_RSA_private_key`)); + } + //endregion + } // private key is not provided + + if (certificate) { + //region resolve certificate content + let p = path.resolve(__dirname, certificate); + // certificate is provided by local file + if (fs.pathExistsSync(p)) { + certConfig.Certificate = fs.readFileSync(p, 'utf-8'); + } // or it is hardcoded + //endregion + + //region validate certificate content + let expectedPrefix = '-----BEGIN CERTIFICATE-----', expectedSuffix = '-----END CERTIFICATE-----'; + if (!certConfig.Certificate.startsWith(expectedPrefix) || !certConfig.Certificate.endsWith(expectedSuffix)) { + throw new Error(red(` + Please provide a valid PEM encoded certificate for ${domainName}. + It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}". + + See: + http://fileformats.archiveteam.org/wiki/PEM_encoded_certificate`)); + } + //endregion + } // certificate is not provided + Object.assign(options, { certConfig }); diff --git a/test/fs-extra.test.js b/test/fs-extra.test.js new file mode 100644 index 000000000..39caba884 --- /dev/null +++ b/test/fs-extra.test.js @@ -0,0 +1,17 @@ +let fs = require('fs-extra'); +let path = require('path'); +const expect = require('expect.js'); + +describe('fs-extra module Tests', function () { + + it('should exists', function () { + let p = path.resolve(__dirname, '../LICENSE'); + expect(fs.pathExistsSync(p)).to.be(true); + console.log(fs.readFileSync(p, 'utf-8')); + }); + + it('should not exists', function () { + let p = path.resolve(__dirname, '-----BEGIN RSA PRIVATE KEY-----'); + expect(fs.pathExistsSync(p)).to.be(false); + }); +}); From 0523b4cd05ec811c914436b77754e9ed72d8220f Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Tue, 14 Jul 2020 10:55:24 +0800 Subject: [PATCH 3/6] refactor: replace fs.readFileSync() with await fs.readFile() --- src/lib/deploy/deploy-support.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/deploy/deploy-support.js b/src/lib/deploy/deploy-support.js index 866051903..3c18ddbfd 100644 --- a/src/lib/deploy/deploy-support.js +++ b/src/lib/deploy/deploy-support.js @@ -260,7 +260,7 @@ async function makeCustomDomain({ let p = path.resolve(__dirname, privateKey); // private key is provided by local file if (fs.pathExistsSync(p)) { - certConfig.PrivateKey = fs.readFileSync(p, 'utf-8'); + certConfig.PrivateKey = await fs.readFile(p, 'utf-8'); } // or it is hardcoded //endregion @@ -282,7 +282,7 @@ async function makeCustomDomain({ let p = path.resolve(__dirname, certificate); // certificate is provided by local file if (fs.pathExistsSync(p)) { - certConfig.Certificate = fs.readFileSync(p, 'utf-8'); + certConfig.Certificate = await fs.readFile(p, 'utf-8'); } // or it is hardcoded //endregion From f11f463f1c576b0c7d96bdc469c38039c72e1d45 Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Tue, 14 Jul 2020 11:10:29 +0800 Subject: [PATCH 4/6] refactor: extract constant value --- src/lib/deploy/deploy-support.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/deploy/deploy-support.js b/src/lib/deploy/deploy-support.js index 3c18ddbfd..80108240c 100644 --- a/src/lib/deploy/deploy-support.js +++ b/src/lib/deploy/deploy-support.js @@ -11,6 +11,11 @@ const {green, red} = require('colors'); const {processApiParameters} = require('./deploy-support-api'); const {getCloudApiClient, getSlsClient, getMnsClient} = require('../client'); +const EXPECTED_RSA_PRIVATE_KEY_PREFIX = '-----BEGIN RSA PRIVATE KEY-----'; +const EXPECTED_RSA_PRIVATE_KEY_SUFFIX = '-----END RSA PRIVATE KEY-----'; +const EXPECTED_CERTIFICATE_PREFIX = '-----BEGIN CERTIFICATE-----'; +const EXPECTED_CERTIFICATE_SUFFIX = '-----END CERTIFICATE-----'; + const { getOtsClient, getOtsPopClient, @@ -265,11 +270,10 @@ async function makeCustomDomain({ //endregion //region validate RSA private key content - let expectedPrefix = '-----BEGIN RSA PRIVATE KEY-----', expectedSuffix = '-----END RSA PRIVATE KEY-----'; - if (!certConfig.PrivateKey.startsWith(expectedPrefix) || !certConfig.PrivateKey.endsWith(expectedSuffix)) { + if (!certConfig.PrivateKey.startsWith(EXPECTED_RSA_PRIVATE_KEY_PREFIX) || !certConfig.PrivateKey.endsWith(EXPECTED_RSA_PRIVATE_KEY_SUFFIX)) { throw new Error(red(` Please provide a valid PEM encoded RSA private key for ${domainName}. - It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}". + It's content MUST start with "${EXPECTED_RSA_PRIVATE_KEY_PREFIX}" AND end with "${EXPECTED_RSA_PRIVATE_KEY_SUFFIX}". See: http://fileformats.archiveteam.org/wiki/PEM_encoded_RSA_private_key`)); @@ -287,11 +291,10 @@ async function makeCustomDomain({ //endregion //region validate certificate content - let expectedPrefix = '-----BEGIN CERTIFICATE-----', expectedSuffix = '-----END CERTIFICATE-----'; - if (!certConfig.Certificate.startsWith(expectedPrefix) || !certConfig.Certificate.endsWith(expectedSuffix)) { + if (!certConfig.Certificate.startsWith(EXPECTED_CERTIFICATE_PREFIX) || !certConfig.Certificate.endsWith(EXPECTED_CERTIFICATE_SUFFIX)) { throw new Error(red(` Please provide a valid PEM encoded certificate for ${domainName}. - It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}". + It's content MUST start with "${EXPECTED_CERTIFICATE_PREFIX}" AND end with "${EXPECTED_CERTIFICATE_SUFFIX}". See: http://fileformats.archiveteam.org/wiki/PEM_encoded_certificate`)); From 42f70e87a1fab5a85682e31d6d3b5d09913d6caa Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Tue, 14 Jul 2020 11:36:52 +0800 Subject: [PATCH 5/6] refactor: remove redundant test case --- test/fs-extra.test.js | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 test/fs-extra.test.js diff --git a/test/fs-extra.test.js b/test/fs-extra.test.js deleted file mode 100644 index 39caba884..000000000 --- a/test/fs-extra.test.js +++ /dev/null @@ -1,17 +0,0 @@ -let fs = require('fs-extra'); -let path = require('path'); -const expect = require('expect.js'); - -describe('fs-extra module Tests', function () { - - it('should exists', function () { - let p = path.resolve(__dirname, '../LICENSE'); - expect(fs.pathExistsSync(p)).to.be(true); - console.log(fs.readFileSync(p, 'utf-8')); - }); - - it('should not exists', function () { - let p = path.resolve(__dirname, '-----BEGIN RSA PRIVATE KEY-----'); - expect(fs.pathExistsSync(p)).to.be(false); - }); -}); From 5caf5055307d0aaaa1da0e6634800f9829e91018 Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Tue, 14 Jul 2020 11:38:21 +0800 Subject: [PATCH 6/6] revert: "chore: reformat deploy-support.js" This reverts commit 4dda1465 --- src/lib/deploy/deploy-support.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/lib/deploy/deploy-support.js b/src/lib/deploy/deploy-support.js index 80108240c..c12a6505e 100644 --- a/src/lib/deploy/deploy-support.js +++ b/src/lib/deploy/deploy-support.js @@ -7,9 +7,9 @@ const debug = require('debug')('fun:deploy'); const promiseRetry = require('../retry'); const getProfile = require('../profile').getProfile; -const {green, red} = require('colors'); -const {processApiParameters} = require('./deploy-support-api'); -const {getCloudApiClient, getSlsClient, getMnsClient} = require('../client'); +const { green, red } = require('colors'); +const { processApiParameters } = require('./deploy-support-api'); +const { getCloudApiClient, getSlsClient, getMnsClient } = require('../client'); const EXPECTED_RSA_PRIVATE_KEY_PREFIX = '-----BEGIN RSA PRIVATE KEY-----'; const EXPECTED_RSA_PRIVATE_KEY_SUFFIX = '-----END RSA PRIVATE KEY-----'; @@ -81,9 +81,7 @@ async function makeLogstore({ console.log(red(`\t\tretry ${times} times`)); retry(ex); - } else { - exists = false; - } + } else { exists = false; } } }); @@ -161,9 +159,7 @@ async function slsProjectExist(slsClient, projectName) { console.log(red(`\tretry ${times} times`)); retry(ex); - } else { - projectExist = false; - } + } else { projectExist = false; } } }); return projectExist; @@ -419,11 +415,9 @@ async function makeApi(group, { 'requestPath': requestPath }, requestConfig); - const { - apiRequestParameters, + const { apiRequestParameters, apiServiceParameters, - apiServiceParametersMap - } = processApiParameters(requestParameters, serviceParameters, serviceParametersMap); + apiServiceParametersMap } = processApiParameters(requestParameters, serviceParameters, serviceParametersMap); const profile = await getProfile(); @@ -724,4 +718,4 @@ module.exports = { makeApiTrigger, makeSlsProject, makeOtsInstance, makeCustomDomain, makeLogstoreIndex, makeSlsAuto, listCustomDomains -}; +}; \ No newline at end of file